comparison trafficintelligence/sensors.py @ 1028:cc5cb04b04b0

major update using the trafficintelligence package name and install through pip
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 15 Jun 2018 11:19:10 -0400
parents python/sensors.py@933670761a57
children
comparison
equal deleted inserted replaced
1027:6129296848d3 1028:cc5cb04b04b0
1 #! /usr/bin/env python
2 '''Libraries for detecting, counting, etc., road users'''
3
4 from numpy import mean, isnan
5
6 from trafficintelligence import moving
7
8 # TODO graphical user interface for creation
9
10 class Sensor:
11 def detect(self, o):
12 print("Detect method not implemented")
13 return False
14
15 def detectInstants(self, o):
16 print("DetectInstants method not implemented")
17 return []
18
19 class BoxSensor(Sensor):
20 def __init__(self, polygon, minNPointsInBox = 1):
21 self.polygon = polygon # check 2xN?
22 self.minNPointsInBox = minNPointsInBox
23
24 def detectInstants(self, obj):
25 indices = obj.getPositions().getInstantsInPolygon(self.polygon)
26 firstInstant = obj.getFirstInstant()
27 return [i+firstInstant for i in indices]
28
29 def detect(self, obj):
30 instants = self.detectInstants(obj)
31 return len(instants) >= self.minNPointsInBox
32
33 def detectAnd(sensors, obj):
34 'Returns True if all sensors detect the object'
35 result = True
36 for s in sensors:
37 result = result and s.detect(obj)
38 if not result:
39 return result
40 return result
41
42 def detectOr(sensors, obj):
43 'Returns True if any sensor detects the object'
44 result = False
45 for s in sensors:
46 result = result or s.detect(obj)
47 if result:
48 return result
49 return result
50
51 def detectAndOrder(sensors, obj):
52 'Returns True if all sensors are detected and in their order'
53 detectionInstants = []
54 for s in sensors:
55 instants = s.detectInstants(obj)
56 if len(instants) == 0:
57 return False
58 else:
59 detectionInstants.append(mean(instants))
60 result = True
61 for i in range(len(sensors)-1):
62 result = result and (detectionInstants[i] <= detectionInstants[i+1])
63 if not result:
64 return result
65 return result