forked from gregtinkers/carspeed.py
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalibrate.py
42 lines (34 loc) · 1.12 KB
/
calibrate.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
"""
Return the estimated distance based on a real event and the actual mph
Usage:
calibrate.py <eventfile> --mph=<mph>
Options:
-h --help Show this screen.
"""
import logging
import json
import docopt
import math
import pathlib
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(message)s'
)
# Load arguments
args = docopt.docopt(__doc__)
# Get the actual mph
mph = int(args['--mph'])
# Load the event data
event_data = None
with open(pathlib.Path(args['<eventfile>']), 'r') as json_file:
event_data = json.load(json_file)
# Figure out the speed
total_distance = 0
direction = None
for frame, event in enumerate(event_data):
direction = event['dir']
# MATH!
distance = ((((mph / 0.681818) * event['secs']) / event['delta']) * float(event['image_width'])) / (2 * (math.tan(math.radians(event['fov'] * 0.5))))
total_distance += distance
logging.info('Frame {:2.0f}: {:1.2f}sec {:4.2f}px {:2.0f}mph == {:4.2f} distance'.format(frame, event['secs'], event['delta'], event['mph'], distance))
logging.info('Updated distance for {}: {:4.2f}'.format(direction, total_distance / len(event_data)))