Skip to content

Commit 7d78650

Browse files
author
HappySaxena
committed
fixing issues in face-try.py file
1 parent b024099 commit 7d78650

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

face-try.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,53 @@
55
@author: Lenovo
66
"""
77
import cv2
8+
import sys
89

9-
face_cascade = cv2.CascadeClassifier('E:\\project\\main\\models\\haarcascade_frontalface_default.xml')
10+
# Load Haar cascade
11+
cascade_path = '/home/happy/gssoc/driver-drowsiness-detection-system/models/haarcascade_frontalface_default.xml'
12+
face_cascade = cv2.CascadeClassifier(cascade_path)
1013

14+
# Check if cascade loaded successfully
15+
if face_cascade.empty():
16+
print(f"[ERROR] Failed to load cascade from {cascade_path}")
17+
sys.exit(1)
18+
19+
# Start video capture
1120
cap = cv2.VideoCapture(0)
1221

13-
while cap.isOpened():
14-
_, img = cap.read()
22+
if not cap.isOpened():
23+
print("[ERROR] Cannot access webcam.")
24+
sys.exit(1)
25+
26+
try:
27+
while True:
28+
ret, img = cap.read()
29+
30+
# Check if frame was captured
31+
if not ret or img is None:
32+
print("[WARNING] Failed to read frame from webcam.")
33+
continue
34+
35+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
36+
37+
try:
38+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4)
39+
except cv2.error as e:
40+
print(f"[ERROR] detectMultiScale failed: {e}")
41+
continue
42+
43+
for (x, y, w, h) in faces:
44+
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3)
1545

16-
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
17-
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
46+
cv2.imshow('Face Detection', img)
1847

19-
for (x, y , w ,h) in faces:
20-
cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0 , 0), 3)
48+
if cv2.waitKey(1) & 0xFF == ord('q'):
49+
print("[INFO] Exiting on user request.")
50+
break
2151

22-
cv2.imshow('Face Detection', img)
23-
if cv2.waitKey(1) & 0xFF == ord('q'):
24-
break
52+
except KeyboardInterrupt:
53+
print("\n[INFO] Exiting on Ctrl+C")
2554

26-
cap.release()
27-
cv2.destroyAllWindows()
55+
finally:
56+
cap.release()
57+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)