started using pygrabber to get input cameras with names; fixed issue with webcam preview not stopping when the preview window is closed

pull/329/head
Aleksandr Spiridonov 2024-08-15 00:42:53 -04:00
parent 4ddcd60c49
commit eb733ad8c5
2 changed files with 17 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import customtkinter as ctk
from typing import Callable, Tuple from typing import Callable, Tuple
import cv2 import cv2
from PIL import Image, ImageOps from PIL import Image, ImageOps
from pygrabber.dshow_graph import FilterGraph
# Import OS-specific modules only when necessary # Import OS-specific modules only when necessary
if platform.system() == 'Darwin': # macOS if platform.system() == 'Darwin': # macOS
@ -310,6 +311,10 @@ def webcam_preview(camera_name: str):
global preview_label, PREVIEW global preview_label, PREVIEW
WIDTH = 960
HEIGHT = 540
FPS = 60
# Select the camera by its name # Select the camera by its name
selected_camera = select_camera(camera_name) selected_camera = select_camera(camera_name)
if selected_camera is None: if selected_camera is None:
@ -325,12 +330,12 @@ def webcam_preview(camera_name: str):
update_status(f"Error: Could not open camera {camera_name}") update_status(f"Error: Could not open camera {camera_name}")
return return
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 960) cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 540) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
cap.set(cv2.CAP_PROP_FPS, 60) cap.set(cv2.CAP_PROP_FPS, FPS)
PREVIEW_MAX_WIDTH = 960 PREVIEW_MAX_WIDTH = WIDTH
PREVIEW_MAX_HEIGHT = 540 PREVIEW_MAX_HEIGHT = HEIGHT
preview_label.configure(image=None) preview_label.configure(image=None)
PREVIEW.deiconify() PREVIEW.deiconify()
@ -355,6 +360,9 @@ def webcam_preview(camera_name: str):
preview_label.configure(image=image) preview_label.configure(image=image)
ROOT.update() ROOT.update()
if PREVIEW.state() == 'withdrawn':
break
cap.release() cap.release()
PREVIEW.withdraw() PREVIEW.withdraw()
@ -389,12 +397,7 @@ def get_available_cameras():
print(f"Skipping Continuity Camera: {device.localizedName()}") print(f"Skipping Continuity Camera: {device.localizedName()}")
elif platform.system() == 'Windows' or platform.system() == 'Linux': elif platform.system() == 'Windows' or platform.system() == 'Linux':
# Use OpenCV to detect camera indexes # Use OpenCV to detect camera indexes
index = 0 devices = FilterGraph().get_input_devices()
while True:
cap = cv2.VideoCapture(index) available_cameras = devices
if not cap.isOpened():
break
available_cameras.append(f"Camera {index}")
cap.release()
index += 1
return available_cameras return available_cameras

View File

@ -22,3 +22,4 @@ protobuf==4.23.2
tqdm==4.66.4 tqdm==4.66.4
gfpgan==1.3.8 gfpgan==1.3.8
pyobjc==9.1; sys_platform == 'darwin' pyobjc==9.1; sys_platform == 'darwin'
pygrabber==0.2