1919from vivefacialtracker .vivetracker import ViveTracker
2020from vivefacialtracker .camera_controller import FTCameraController
2121
22+ from mjpeg_streamer import MJPEGVideoCapture
23+
2224WAIT_TIME = 0.1
2325BUFFER_SIZE = 32768
2426MAX_RESOLUTION : int = 600
@@ -61,6 +63,7 @@ def __init__(
6163 self .current_capture_source = config .capture_source
6264 self .cv2_camera : "cv2.VideoCapture" = None
6365 self .vft_camera : FTCameraController = None
66+ self .http : bool = None
6467
6568 self .serial_connection = None
6669 self .last_frame_time = time .time ()
@@ -156,6 +159,8 @@ def run(self):
156159 if self .cancellation_event .wait (WAIT_TIME ):
157160 return
158161 if self .config .capture_source not in self .camera_list :
162+ if "http://" in str (self .config .capture_source ): self .http = True
163+ else : self .http = False
159164 self .current_capture_source = self .config .capture_source
160165 else :
161166 self .current_capture_source = get_camera_index_by_name (self .config .capture_source )
@@ -165,23 +170,27 @@ def run(self):
165170 self .current_capture_source , cv2 .CAP_FFMPEG
166171 )
167172 else :
168- self .cv2_camera = cv2 .VideoCapture ()
169- self .cv2_camera .open (self .current_capture_source )
170-
171- if not self .settings .gui_cam_resolution_x == 0 :
172- self .cv2_camera .set (
173- cv2 .CAP_PROP_FRAME_WIDTH ,
174- self .settings .gui_cam_resolution_x ,
175- )
176- if not self .settings .gui_cam_resolution_y == 0 :
177- self .cv2_camera .set (
178- cv2 .CAP_PROP_FRAME_HEIGHT ,
179- self .settings .gui_cam_resolution_y ,
180- )
181- if not self .settings .gui_cam_framerate == 0 :
182- self .cv2_camera .set (
183- cv2 .CAP_PROP_FPS , self .settings .gui_cam_framerate
184- )
173+ if not self .http :
174+ self .cv2_camera = cv2 .VideoCapture ()
175+ self .cv2_camera .open (self .current_capture_source )
176+ else :
177+ self .cv2_camera = MJPEGVideoCapture (self .current_capture_source )
178+ self .cv2_camera .open ()
179+ if not self .http :
180+ if not self .settings .gui_cam_resolution_x == 0 :
181+ self .cv2_camera .set (
182+ cv2 .CAP_PROP_FRAME_WIDTH ,
183+ self .settings .gui_cam_resolution_x ,
184+ )
185+ if not self .settings .gui_cam_resolution_y == 0 :
186+ self .cv2_camera .set (
187+ cv2 .CAP_PROP_FRAME_HEIGHT ,
188+ self .settings .gui_cam_resolution_y ,
189+ )
190+ if not self .settings .gui_cam_framerate == 0 :
191+ self .cv2_camera .set (
192+ cv2 .CAP_PROP_FPS , self .settings .gui_cam_framerate
193+ )
185194 should_push = False
186195 else :
187196 # We don't have a capture source to try yet, wait for one to show up in the GUI.
@@ -216,22 +225,24 @@ def get_camera_picture(self, should_push):
216225 self .frame_number = self .frame_number + 1
217226 elif self .cv2_camera is not None and self .cv2_camera .isOpened ():
218227 ret , image = self .cv2_camera .read () # MJPEG Stream reconnects are currently limited by the hard coded 30 second timeout time on VideoCapture.read(). We can get around this by recompiling OpenCV or using a custom MJPEG stream imp.
219- if not ret :
220- self .cv2_camera .set (cv2 .CAP_PROP_POS_FRAMES , 0 )
221- raise RuntimeError (lang ._instance .get_string ("error.frame" ))
222- self .frame_number = self .cv2_camera .get (cv2 .CAP_PROP_POS_FRAMES ) + 1
223- else :
224- # Switching from a Vive Facial Tracker to a CV2 camera
225- return
226- self .FRAME_SIZE = image .shape
227- # Calculate FPS
228- current_frame_time = time .time () # Should be using "time.perf_counter()", not worth ~3x cycles?
229- delta_time = current_frame_time - self .last_frame_time
230- self .last_frame_time = current_frame_time
231- current_fps = 1 / delta_time if delta_time > 0 else 0
232- # Exponential moving average (EMA). ~1100ns savings, delicious..
233- self .fps = 0.02 * current_fps + 0.98 * self .fps
234- self .bps = image .nbytes * self .fps
228+ if ret and image is not None :
229+ if not ret :
230+ if not self .http :
231+ self .cv2_camera .set (cv2 .CAP_PROP_POS_FRAMES , 0 )
232+ raise RuntimeError (lang ._instance .get_string ("error.frame" ))
233+ self .frame_number = self .cv2_camera .get (cv2 .CAP_PROP_POS_FRAMES ) + 1
234+ else :
235+ # Switching from a Vive Facial Tracker to a CV2 camera
236+ return
237+ self .FRAME_SIZE = image .shape
238+ # Calculate FPS
239+ current_frame_time = time .time () # Should be using "time.perf_counter()", not worth ~3x cycles?
240+ delta_time = current_frame_time - self .last_frame_time
241+ self .last_frame_time = current_frame_time
242+ current_fps = 1 / delta_time if delta_time > 0 else 0
243+ # Exponential moving average (EMA). ~1100ns savings, delicious..
244+ self .fps = 0.02 * current_fps + 0.98 * self .fps
245+ self .bps = image .nbytes * self .fps
235246
236247 if should_push :
237248 self .push_image_to_queue (image , self .frame_number , self .fps )
0 commit comments