-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdashcam.py
143 lines (122 loc) · 3.64 KB
/
dashcam.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from karmen import Karmen
import threading
import queue
import logging
import time
import os
import datetime
from common import isCI
if isCI():
import mockcamera as picamera
else:
import picamera
##get_new_filename##
camera = None
def getserial():
# Extract serial from cpuinfo file
cpuserial = "0000000000000000"
try:
f = open("/proc/cpuinfo", "r")
for line in f:
if line[0:6] == "Serial":
cpuserial = line[10:26]
f.close()
except:
cpuserial = "ERROR000000000"
return cpuserial
def get_new_filename():
return (
"travel__"
+ datetime.datetime.now().strftime("%Y--%m--%d__%H--%M--%S")
+ "__"
+ str(getserial())
+ ".h264"
)
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s.%(msecs)d:LINE %(lineno)d:TID %(thread)d:%(levelname)s - %(message)s",
datefmt="%d-%b-%y %H:%M:%S",
)
def start_preview(params, result):
logging.info("Starting the preview...")
try:
global camera
# Cheat and place the preview inside a window that the GUI will have a black box around
camera.start_preview(fullscreen=False, window=(100, 100, 400, 600))
# camera.start_preview()
# The preview alpha has to be set after the preview is already active
camera.preview.alpha = 128
except Exception as e:
logging.error(e)
result.code = 500
result.code = 200
def stop_preview(params, result):
logging.info("Stopping the preview...")
try:
global camera
camera.stop_preview()
except Exception as e:
logging.error(e)
result.code = 500
return
result.code = 200
def start_recording(params, result):
logging.info(f"params for start_recording are: {params}")
HRES = int(params.get("hres", 1280))
VRES = int(params.get("vres", 720))
ROT = int(params.get("rot", 0))
FRAMERATE = int(params.get("framerate", 10))
logging.info("Starting the recording...")
# try:
global camera
# Do all the camera setup
camera = picamera.PiCamera() # the camera object
camera.resolution = (HRES, VRES)
# annotations
camera.annotate_foreground = picamera.Color("white")
camera.annotate_background = picamera.Color("black")
camera.annotate_frame_num = True
camera.annotate_text_size = 48
camera.annotate_text = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
# set the framerate
camera.framerate = FRAMERATE
# set the rotation
camera.rotation = ROT
try:
camera.start_recording(f"/recordings/{get_new_filename()}", sps_timing=True)
except Exception as e:
logging.error(e)
result.code = 500
return
# spawn a thread that handles updating the time/frame counter
threading.Thread(target=update_annotations).start()
result.code = 200
def stop_recording(params, result):
logging.info("Stopping the recording...")
try:
global camera
camera.stop_recording()
camera.close()
except Exception as e:
logging.error(e)
result.code = 500
return
result.code = 200
def update_annotations():
global camera
while True:
try:
camera.annotate_text = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
except Exception as e:
logging.error(e)
break
time.sleep(0.2)
###MAIN###
k = Karmen(hostname="karmen")
k.addAction(start_recording, "start_recording")
k.addAction(stop_recording, "stop_recording")
k.addAction(start_preview, "start_preview")
k.addAction(stop_preview, "stop_preview")
k.register()
while True:
time.sleep(10)