forked from BenGrainger/LabCamera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_scriptMKII.py
153 lines (119 loc) · 5.04 KB
/
stream_scriptMKII.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
144
145
146
147
148
149
150
151
152
153
import vimba
from vimba import *
import sys
from typing import Optional
import cv2
import time
import datetime
import json
import pathlib
import subprocess
from time import sleep
import numpy as np
import threading
def get_camera(camera_id: Optional[str]) -> Camera:
with Vimba.get_instance() as vimba:
if camera_id:
try:
return vimba.get_camera_by_id(camera_id)
except VimbaCameraError:
print('Failed to access Camera \'{}\'. Abort.'.format(camera_id))
else:
cams = vimba.get_all_cameras()
if not cams:
print('No Cameras accessible. Abort.')
return cams[0]
def setup_camera(cam: Camera):
with cam:
settings_file = r'C:\Users\BMLab21\Desktop\camera\fourth.xml'
cam.load_settings(settings_file, PersistType.All)
# Query available, open_cv compatible pixel formats
# prefer color formats over monochrome formats
cv_fmts = intersect_pixel_formats(cam.get_pixel_formats(), OPENCV_PIXEL_FORMATS)
color_fmts = intersect_pixel_formats(cv_fmts, COLOR_PIXEL_FORMATS)
if color_fmts:
cam.set_pixel_format(color_fmts[0])
else:
mono_fmts = intersect_pixel_formats(cv_fmts, MONO_PIXEL_FORMATS)
if mono_fmts:
cam.set_pixel_format(mono_fmts[0])
else:
abort('Camera does not support a OpenCV compatible format natively. Abort.')
class Handler:
def __init__(self):
self.shutdown_event = threading.Event()
self.frame_number = 0
def __call__(self, cam: Camera, frame: Frame):
ENTER_KEY_CODE = 13
key = cv2.waitKey(1)
time = get_hour()
if key == ENTER_KEY_CODE:
self.shutdown_event.set()
return
elif frame.get_status() == FrameStatus.Complete:
time = get_hour()
if stream_begin < time < stream_end:
msg = 'Stream from \'{}\'. Press <Enter> to stop stream.'
cv2.imshow(msg.format(cam.get_name()), frame.as_opencv_image())
self.frame_number +=1
img = frame.as_opencv_image()
result.write(img)
else:
print('shut down camera')
self.shutdown_event.set()
return
cam.queue_frame(frame)
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())
stream_begin = 8
stream_end = 17
hr = get_hour()
if stream_begin < hr < stream_end: # between the hours of 8 and 23
start = str(datetime.datetime.now().time())
with Vimba.get_instance() as vimba:
cams = vimba.get_all_cameras()
print('Cameras found: {}'.format(len(cams)))
for cam in cams:
cameraID = cam.get_id()
with get_camera(cameraID) as cam:
single_frame = cam.get_frame().as_numpy_ndarray()
size = (single_frame.shape[0], single_frame.shape[1])
video_file_path = r'C:\Users\BMLab21\Documents\CrabStreams\{}.avi'.format(datestr)
# Start Streaming, wait for five seconds, stop streaming
setup_camera(cam)
cam.AcquisitionFrameRateAbs = 25
fps=cam.AcquisitionFrameRateAbs
result = cv2.VideoWriter(video_file_path, cv2.VideoWriter_fourcc(*'MJPG'), fps, size[::-1])
handler = Handler()
try:
# Start Streaming with a custom a buffer of 10 Frames (defaults to 5)
cam.start_streaming(handler=handler, buffer_count=10)
handler.shutdown_event.wait()
end = str(datetime.datetime.now().time())
finally:
cam.stop_streaming()
print('Streaming has stopped')
result.release()
print('video released')
cv2.destroyAllWindows()
# save accompanying Json
json_file_path = r'C:\Users\BMLab21\Documents\CrabStreams\{}_Meta.JSON'.format(datestr)
metaData = {'date': datestr, 'start': start, 'end': end}
pathlib.Path(json_file_path).write_text(json.dumps(metaData))
# compression
video_ouput_to_server = r'C:\Users\BMLab21\Documents\CrabStreams\{}_x264.avi'.format(datestr)
command = 'ffmpeg -i {} -c:v libx264 -crf 26 {}'.format(video_file_path, video_ouput_to_server)
try:
result = subprocess.run(command)
print('video compressed')
except CompressionError:
print('compression failed')
os.remove(video_file_path)