mirror of
https://git.datalinker.icu/kijai/ComfyUI-KJNodes.git
synced 2026-07-08 18:37:13 +08:00
fix webcam index selection
This commit is contained in:
parent
bc4890f798
commit
6e3af6e7b9
@ -490,24 +490,27 @@ 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:
|
||||||
raise Exception("Failed to capture image from webcam")
|
raise Exception("Failed to capture image from webcam")
|
||||||
|
|
||||||
# Crop the frame to the specified bbox
|
# Crop the frame to the specified bbox
|
||||||
frame = frame[y:y+height, x:x+width]
|
frame = frame[y:y+height, x:x+width]
|
||||||
img_torch = torch.from_numpy(frame[..., [2, 1, 0]]).float() / 255.0
|
img_torch = torch.from_numpy(frame[..., [2, 1, 0]]).float() / 255.0
|
||||||
|
|
||||||
if release:
|
if release:
|
||||||
self.cap.release()
|
self.cap.release()
|
||||||
self.cap = None
|
self.cap = None
|
||||||
|
|
||||||
return (img_torch.unsqueeze(0),)
|
return (img_torch.unsqueeze(0),)
|
||||||
|
|
||||||
class AddLabel:
|
class AddLabel:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user