Skip to content

Commit 3b2ba21

Browse files
authored
Update main.py
1 parent 771cd58 commit 3b2ba21

File tree

1 file changed

+24
-9
lines changed
  • Python_Begginer_Projects/Intermediate/Face Dention Program

1 file changed

+24
-9
lines changed
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,45 @@
11
import cv2
2+
import os
23

3-
# Load the pre-trained Haar Cascade classifier for face detection
4-
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
4+
# Check if the XML file exists in the specified path
5+
if not os.path.isfile(r'C:\Users\dell\OneDrive\Documents\Programming\Python\Python_Begginer_Projects\Intermediate\Face Dention Program\haarcascade_frontalface_default.xml'):
6+
print("XML file not found!")
7+
else:
8+
print("XML file found!")
59

6-
# Start video capture from the webcam
10+
# Load the pre-trained Haar cascade for face detection
11+
face_cascade = cv2.CascadeClassifier(r'C:\Users\dell\OneDrive\Documents\Programming\Python\Python_Begginer_Projects\Intermediate\Face Dention Program\haarcascade_frontalface_default.xml')
12+
13+
# Check if the cascade is loaded properly
14+
if face_cascade.empty():
15+
raise IOError("Unable to load the cascade classifier xml file")
16+
17+
# Initialize video capture from webcam (camera 0)
718
video_capture = cv2.VideoCapture(0)
819

920
while True:
10-
# Capture frame-by-frame
1121
ret, frame = video_capture.read()
12-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert to grayscale
22+
if not ret:
23+
print("Failed to capture frame")
24+
break
25+
26+
# Convert the frame to grayscale
27+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
1328

1429
# Detect faces in the image
1530
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
1631

17-
# Draw rectangles around detected faces
32+
# Draw rectangles around the faces
1833
for (x, y, w, h) in faces:
19-
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
34+
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
2035

2136
# Display the resulting frame
2237
cv2.imshow('Video', frame)
2338

24-
# Break the loop on 'q' key press
39+
# Break the loop when the 'q' key is pressed
2540
if cv2.waitKey(1) & 0xFF == ord('q'):
2641
break
2742

28-
# Release the capture and close windows
43+
# Release the video capture object and close any OpenCV windows
2944
video_capture.release()
3045
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)