fix webcam index selection

This commit is contained in:
kijai 2024-07-24 21:59:02 +03:00
parent bc4890f798
commit 6e3af6e7b9

View File

@ -490,11 +490,14 @@ Can be used for realtime diffusion with autoqueue.
} }
def capture(self, x, y, cam_index, width, height, release): def capture(self, x, y, cam_index, width, height, release):
# Check if the camera index has changed or the capture object doesn't exist
if not hasattr(self, "cap") or self.cap is None: if not hasattr(self, "cap") or self.cap is None or self.current_cam_index != cam_index:
if hasattr(self, "cap") and self.cap is not None:
self.cap.release()
self.current_cam_index = cam_index
self.cap = cv2.VideoCapture(cam_index) self.cap = cv2.VideoCapture(cam_index)
if not self.cap.isOpened(): if not self.cap.isOpened():
raise Exception("Could not open webcam") raise Exception("Could not open webcam")
ret, frame = self.cap.read() ret, frame = self.cap.read()
if not ret: if not ret: