-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream_script.py
73 lines (53 loc) · 2.37 KB
/
stream_script.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
import numpy as np
import cv2
import time
import datetime
import json
import pathlib
import subprocess
def get_hour():
"""
returns the current hour only
"""
current = datetime.datetime.now().time() #creates a datetime now object
hr = int(current.hour) # collects and integerizes the hour object
return hr
while(True): # forever loop - planning to always running
x = datetime.datetime.now()
datestr = str(x.date())
start = str(x.time())
hr = get_hour()
if 8 < hr < 23: # between the hours of 8 and 23
# cv2 to create camera object
video = cv2.VideoCapture(0)
cap = cv2.VideoCapture(0)
size = (int(video.get(3)), int(video.get(4)))
video_file_path = r'C:\Users\BMLab21\Documents\CrabStreams\{}.avi'.format(datestr)
result = cv2.VideoWriter(video_file_path,
cv2.VideoWriter_fourcc(*'MJPG'), # fourcc is how openCV find # MJPG is the codec
30, size)
frame_n = 0 # frame counter
while(8 < hr < 23): # during the hours of 8-23 write a video to video_file_path
hr = get_hour()
frame_n += 1
ret, frame = cap.read()
result.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# stop writing and release after 23
cap.release()
result.release()
cv2.destroyAllWindows()
end = str(datetime.datetime.now().time())
# save accompanying Json
json_file_path = r'C:\Users\BMLab21\Documents\CrabStreams\{}_Meta.JSON'.format(datestr)
metaData = {'date': datestr, 'start': start, 'end': end, 'frames': frame_n}
pathlib.Path(json_file_path).write_text(json.dumps(metaData))
# compress and output to server
video_ouput_to_server = r'C:\Users\BMLab21\Documents\CrabStreams\{}.avi'.format(datestr)
json_server_file_path = r'C:\Users\BMLab21\Documents\CrabStreams\{}_Meta.JSON'.format(datestr)
command = 'ffmpeg -i {} -b x265 {}'.format(video_ouput_to_server, json_server_file_path)
result = subprocess.run(command)
pathlib.Path(json_server_file_path).write_text(json.dumps(metaData))
# back to start