changeset 863:a8ca72dc1564

work on user detectors
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 08 Nov 2016 17:59:40 -0500
parents 2d6249fe905a
children 8f28b4fcf129
files python/moving.py python/sensors.py
diffstat 2 files changed, 76 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/python/moving.py	Mon Nov 07 11:11:10 2016 -0500
+++ b/python/moving.py	Tue Nov 08 17:59:40 2016 -0500
@@ -946,6 +946,15 @@
                            self.positions[1][::step]])
 
     if shapelyAvailable:
+        def getInstantsInPolygon(self, polygon):
+            '''Returns the list of instants at which the trajectory is in the polygon'''
+            instants = []
+            n = self.length()
+            for t, x, y in zip(range(n), self.positions[0], self.positions[1]):
+                if polygon.contains(shapelyPoint(x, y)):
+                    instants.append(t)
+            return instants
+
         def getTrajectoryInPolygon(self, polygon, t2 = None):
             '''Returns the trajectory built with the set of points inside the (shapely) polygon
             The polygon could be a prepared polygon (faster) from prepared.prep
@@ -966,9 +975,9 @@
             return traj, traj2
 
         def proportionInPolygon(self, polygon, minProportion = 0.5):
-            inPolygon = [polygon.contains(shapelyPoint(x, y)) for x, y in zip(self.positions[0], self.positions[1])]
+            instants = self.getInstantsInPolygon(polygon)
             lengthThreshold = float(self.length())*minProportion
-            return sum(inPolygon) >= lengthThreshold
+            return len(instants) >= lengthThreshold
     else:
         def getTrajectoryInPolygon(self, polygon, t2 = None):
             '''Returns the trajectory built with the set of points inside the polygon
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/sensors.py	Tue Nov 08 17:59:40 2016 -0500
@@ -0,0 +1,65 @@
+#! /usr/bin/env python
+'''Libraries for detecting, counting, etc., road users'''
+
+from numpy import mean, isnan
+
+import moving
+
+# TODO graphical user interface for creation
+
+class Sensor:
+    def detect(self, o):
+        print("Detect method not implemented")
+        return False
+    
+    def detectInstants(self, o):
+        print("DetectInstants method not implemented")
+        return []
+
+class BoxSensor(Sensor):
+    def __init__(self, polygon, minNPointsInBox = 1):
+        self.polygon = polygon # check 2xN?
+        self.minNPointsInBox = minNPointsInBox
+    
+    def detectInstants(self, obj):
+        indices = obj.getPositions().getInstantsInPolygon(self.polygon)
+        firstInstant = obj.getFirstInstant()
+        return [i+firstInstant for i in indices]
+
+    def detect(self, obj):
+        instants = self.detectInstants(obj)
+        return len(instants) >= self.minNPointsInBox
+
+def detectAnd(sensors, obj):
+    'Returns True if all sensors detect the object'
+    result = True
+    for s in sensors:
+        result = result and s.detect(obj)
+        if not result:
+            return result
+    return result
+
+def detectOr(sensors, obj):
+    'Returns True if any sensor detects the object'
+    result = False
+    for s in sensors:
+        result = result or s.detect(obj)
+        if result:
+            return result
+    return result
+
+def detectAndOrder(sensors, obj):
+    'Returns True if all sensors are detected and in their order'
+    detectionInstants = []
+    for s in sensors:
+        instants = s.detectInstants(obj)
+        if len(instants) == 0:
+            return False
+        else:
+            detectionInstants.append(mean(instants))
+    result = True
+    for i in xrange(len(sensors)-1):
+        result = result and (detectionInstants[i] <= detectionInstants[i+1])
+        if not result:
+            return result
+    return result