Mercurial Hosting > traffic-intelligence
comparison python/sensors.py @ 863:a8ca72dc1564
work on user detectors
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 08 Nov 2016 17:59:40 -0500 |
parents | |
children | 933670761a57 |
comparison
equal
deleted
inserted
replaced
862:2d6249fe905a | 863:a8ca72dc1564 |
---|---|
1 #! /usr/bin/env python | |
2 '''Libraries for detecting, counting, etc., road users''' | |
3 | |
4 from numpy import mean, isnan | |
5 | |
6 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 xrange(len(sensors)-1): | |
62 result = result and (detectionInstants[i] <= detectionInstants[i+1]) | |
63 if not result: | |
64 return result | |
65 return result |