This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_tracking.py
80 lines (52 loc) · 1.61 KB
/
simple_tracking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sensor
import image
import time
import json
from simple_tracker import Tracker
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
green_threshold = [(44, 88, -50, 18, 9, 66)]
tracker = Tracker(max_distance=50)
record = True
tracker_file = 'tracker.txt'
data_file = 'data.txt'
def main():
if record:
data = {'tracker': {'max_distance': tracker.max_distance,
'timeout': tracker.timeout}}
write_file(tracker_file, data)
write_file(data_file, '')
while(True):
clock.tick()
img = sensor.snapshot()
find_blobs(img)
print(clock.fps())
def find_blobs(img):
blobs = img.find_blobs(green_threshold, pixels_threshold=500)
centroids = [(blob.cx(), blob.cy()) for blob in blobs]
lost_tracking = tracker.update(centroids)
for blob in blobs:
img.draw_rectangle(blob.rect())
for id, point in tracker.points.items():
img.draw_string(point[0], point[1], str(id), scale=2)
if record:
# ujson isn't the best... so record like this
data = {
'frame': tracker.frame,
'test_data': centroids,
'assertations': tracker.points,
'counts': lost_tracking
}
append_file(data_file, data)
def write_file(path, data):
with open(path, 'w') as file:
file.write(str(data))
def append_file(path, data):
with open(path, 'a') as file:
file.write(str(data) + ',\n')
main()