Skip to content

Commit f733b12

Browse files
authored
Merge pull request Project-Babble#92 from RamesTheGeneric/main
Fix: Make MJPEG stream sources actually connect.
2 parents 5cfab32 + c038f2f commit f733b12

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

BabbleApp/camera.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from enum import Enum
1111
import serial.tools.list_ports
1212
from lang_manager import LocaleStringManager as lang
13-
1413
from colorama import Fore
1514
from config import BabbleConfig, BabbleSettingsConfig
1615
from utils.misc_utils import get_camera_index_by_name, list_camera_names, is_nt
@@ -95,6 +94,7 @@ def run(self):
9594
self.config.capture_source is not None
9695
and self.config.capture_source != ""
9796
):
97+
self.current_capture_source = self.config.capture_source
9898
isSerial = any(x in str(self.config.capture_source) for x in PORTS)
9999

100100
if isSerial:
@@ -103,7 +103,7 @@ def run(self):
103103
self.cv2_camera = None
104104
if self.vft_camera is not None:
105105
self.vft_camera.close()
106-
self.device_is_vft = False;
106+
self.device_is_vft = False
107107
if (
108108
self.serial_connection is None
109109
or self.camera_status == CameraState.DISCONNECTED
@@ -116,7 +116,7 @@ def run(self):
116116
if self.cv2_camera is not None:
117117
self.cv2_camera.release()
118118
self.cv2_camera = None
119-
self.device_is_vft = True;
119+
self.device_is_vft = True
120120

121121
if self.vft_camera is None:
122122
print(self.error_message.format(self.config.capture_source))
@@ -141,18 +141,18 @@ def run(self):
141141
self.cv2_camera is None
142142
or not self.cv2_camera.isOpened()
143143
or self.camera_status == CameraState.DISCONNECTED
144-
or get_camera_index_by_name(self.config.capture_source) != self.current_capture_source
144+
#or get_camera_index_by_name(self.config.capture_source) != self.current_capture_source
145+
or self.config.capture_source != self.current_capture_source
145146
):
146147
if self.vft_camera is not None:
147148
self.vft_camera.close()
148-
self.device_is_vft = False;
149+
self.device_is_vft = False
149150

150151
print(self.error_message.format(self.config.capture_source))
151152
# This requires a wait, otherwise we can error and possible screw up the camera
152153
# firmware. Fickle things.
153154
if self.cancellation_event.wait(WAIT_TIME):
154155
return
155-
156156
if self.config.capture_source not in self.camera_list:
157157
self.current_capture_source = self.config.capture_source
158158
else:
@@ -163,9 +163,8 @@ def run(self):
163163
self.current_capture_source, cv2.CAP_FFMPEG
164164
)
165165
else:
166-
self.cv2_camera = cv2.VideoCapture(
167-
self.current_capture_source
168-
)
166+
self.cv2_camera = cv2.VideoCapture()
167+
self.cv2_camera.open(self.current_capture_source)
169168

170169
if not self.settings.gui_cam_resolution_x == 0:
171170
self.cv2_camera.set(
@@ -181,7 +180,6 @@ def run(self):
181180
self.cv2_camera.set(
182181
cv2.CAP_PROP_FPS, self.settings.gui_cam_framerate
183182
)
184-
185183
should_push = False
186184
else:
187185
# We don't have a capture source to try yet, wait for one to show up in the GUI.
@@ -215,15 +213,14 @@ def get_camera_picture(self, should_push):
215213
return
216214
self.frame_number = self.frame_number + 1
217215
elif self.cv2_camera is not None and self.cv2_camera.isOpened():
218-
ret, image = self.cv2_camera.read()
216+
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.
219217
if not ret:
220218
self.cv2_camera.set(cv2.CAP_PROP_POS_FRAMES, 0)
221219
raise RuntimeError(lang._instance.get_string("error.frame"))
222220
self.frame_number = self.cv2_camera.get(cv2.CAP_PROP_POS_FRAMES) + 1
223221
else:
224222
# Switching from a Vive Facial Tracker to a CV2 camera
225223
return
226-
227224
self.FRAME_SIZE = image.shape
228225
# Calculate FPS
229226
current_frame_time = time.time() # Should be using "time.perf_counter()", not worth ~3x cycles?
@@ -361,4 +358,4 @@ def push_image_to_queue(self, image, frame_number, fps):
361358
f'{Fore.YELLOW}[{lang._instance.get_string("log.warn")}] {lang._instance.get_string("warn.backpressure1")} {qsize}. {lang._instance.get_string("warn.backpressure2")}{Fore.RESET}'
362359
)
363360
self.camera_output_outgoing.put((self.clamp_max_res(image), frame_number, fps))
364-
self.capture_event.clear()
361+
self.capture_event.clear()

0 commit comments

Comments
 (0)