diff --git a/face-try.py b/face-try.py index ba08c99..1b20bf2 100644 --- a/face-try.py +++ b/face-try.py @@ -24,8 +24,28 @@ print(f"[ERROR] Failed to load cascade from {cascade_path}") sys.exit(1) + + +#checking camera +def check_camera(idx): + cap = cv2.VideoCapture(idx) + if not cap.isOpened(): + return None + + ok, _ = cap.read() + if not ok: + cap.release() + return None + + return cap + # Start video capture -cap = cv2.VideoCapture(0) +cap = check_camera(0) + +if cap is None: + print("[WARNING] Built-In camera failed.Trying external camera...") + cap = check_camera(1) + if not cap.isOpened(): print("[ERROR] Cannot access webcam.") @@ -61,5 +81,6 @@ print("\n[INFO] Exiting on Ctrl+C") finally: - cap.release() - cv2.destroyAllWindows() + if cap: + cap.release() + cv2.destroyAllWindows() \ No newline at end of file