-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarSpeedConfig.py
91 lines (83 loc) · 3.33 KB
/
CarSpeedConfig.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import json
import pathlib
import math
from typing import Union
class MonitorArea(object):
def __init__(self,data=None):
self.class_name = self.__class__.__name__
self.upper_left_x:int = 0
self.upper_left_y:int = 0
self.lower_right_x:int = 0
self.lower_right_y:int = 0
if data:
self.__dict__ = data
class CarSpeedConfig(object):
DEF_CONFIG_FILE = "CarSpeed.json"
M_TO_FT = 3.28084
FT_TO_M = 0.30490
def __init__(self,data=None):
self.class_name = self.__class__.__name__
self.id:int = 0
self.name:str = ''
self.l2r_distance:float = 14 # in m
self.r2l_distance:float = 11 # in m
self.min_speed_image:int = 0
self.min_speed_save:int = 10
self.max_speed_save:int = 80
self.field_of_view:float = 75 # 75 is standard pi camera module 3, 120 for wide angle
self.h_flip = False
self.v_flip = False
self.monitor_area = MonitorArea()
if data:
self.__dict__ = data
def getL2RFrameWidthFt(self)->float:
l2r_distance_ft = self.l2r_distance*CarSpeedConfig.M_TO_FT
frame_width_ft = 2*(math.tan(math.radians(self.field_of_view*0.5))*l2r_distance_ft)
return frame_width_ft
def getR2LFrameWidthFt(self)->float:
r2l_distance_ft = self.r2l_distance*CarSpeedConfig.M_TO_FT
frame_width_ft = 2*(math.tan(math.radians(self.field_of_view*0.5))*r2l_distance_ft)
return frame_width_ft
def toJson(self)->str:
return json.dumps(self, default=lambda o: o.__dict__, indent=4)
@staticmethod
def fromJsonFile(file:str):
with open(file,"r") as f:
return json.load(f, object_hook=CarSpeedConfig._objectHook)
@staticmethod
def fromJsonStr(str:str):
return json.loads(str, object_hook=CarSpeedConfig._objectHook)
@staticmethod
def fromDefJsonFile():
config = None
configPath = pathlib.Path(CarSpeedConfig.DEF_CONFIG_FILE)
if ( configPath.exists()):
print(f"Loading config from [{CarSpeedConfig.DEF_CONFIG_FILE}]")
config = CarSpeedConfig.fromJsonFile(CarSpeedConfig.DEF_CONFIG_FILE)
return config
@staticmethod
def _objectHook(dict):
if ( 'class_name' in dict):
class_name = dict['class_name']
if (class_name=='CarSpeedConfig'):
return CarSpeedConfig(dict)
elif (class_name=='MonitorArea'):
return MonitorArea(dict)
def shortDict(self):
config={'l2r_distance': self.l2r_distance,\
'r2l_distance': self.r2l_distance,\
'min_speed_image': self.min_speed_image,\
'min_speed_save': self.min_speed_save,\
'max_speed_save': self.max_speed_save,\
'field_of_view': self.field_of_view,\
'h_flip': self.h_flip,\
'v_flip': self.v_flip}
return config
def isMonitorAreaDefined(self)->bool:
if not self.monitor_area:
return False
elif self.monitor_area.upper_left_x==0 and self.monitor_area.upper_left_y==0\
and self.monitor_area.lower_right_x==0 and self.monitor_area.lower_right_y==0:
return False
else:
return True