Mercurial Hosting > traffic-intelligence
annotate trafficintelligence/sensors.py @ 1040:20799ac9524e
integrating code of learn-motion-patterns in storage.py
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 04 Jul 2018 15:37:04 -0400 |
parents | cc5cb04b04b0 |
children |
rev | line source |
---|---|
863
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
2 '''Libraries for detecting, counting, etc., road users''' |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
3 |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
4 from numpy import mean, isnan |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
5 |
1028
cc5cb04b04b0
major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
998
diff
changeset
|
6 from trafficintelligence import moving |
863
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
7 |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
8 # TODO graphical user interface for creation |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
9 |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
10 class Sensor: |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
11 def detect(self, o): |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
12 print("Detect method not implemented") |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
13 return False |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
14 |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
15 def detectInstants(self, o): |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
16 print("DetectInstants method not implemented") |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
17 return [] |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
18 |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
19 class BoxSensor(Sensor): |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
20 def __init__(self, polygon, minNPointsInBox = 1): |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
21 self.polygon = polygon # check 2xN? |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
22 self.minNPointsInBox = minNPointsInBox |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
23 |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
24 def detectInstants(self, obj): |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
25 indices = obj.getPositions().getInstantsInPolygon(self.polygon) |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
26 firstInstant = obj.getFirstInstant() |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
27 return [i+firstInstant for i in indices] |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
28 |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
29 def detect(self, obj): |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
30 instants = self.detectInstants(obj) |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
31 return len(instants) >= self.minNPointsInBox |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
32 |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
33 def detectAnd(sensors, obj): |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
34 'Returns True if all sensors detect the object' |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
35 result = True |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
36 for s in sensors: |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
37 result = result and s.detect(obj) |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
38 if not result: |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
39 return result |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
40 return result |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
41 |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
42 def detectOr(sensors, obj): |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
43 'Returns True if any sensor detects the object' |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
44 result = False |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
45 for s in sensors: |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
46 result = result or s.detect(obj) |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
47 if result: |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
48 return result |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
49 return result |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
50 |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
51 def detectAndOrder(sensors, obj): |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
52 'Returns True if all sensors are detected and in their order' |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
53 detectionInstants = [] |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
54 for s in sensors: |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
55 instants = s.detectInstants(obj) |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
56 if len(instants) == 0: |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
57 return False |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
58 else: |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
59 detectionInstants.append(mean(instants)) |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
60 result = True |
998
933670761a57
updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
863
diff
changeset
|
61 for i in range(len(sensors)-1): |
863
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
62 result = result and (detectionInstants[i] <= detectionInstants[i+1]) |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
63 if not result: |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
64 return result |
a8ca72dc1564
work on user detectors
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
65 return result |