-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
147 lines (111 loc) · 5.42 KB
/
main.py
File metadata and controls
147 lines (111 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import cv2
#print(cv2.__version__)
#----------------------------------------------------------------------------------------------------------------
'''
# Milestone 1 : Setting Up Environment
# Ok now we will accesss the default camera of the pc
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read() # we will be capturing frame by frame the image
cv2.imshow('frame', frame) # Display the frame
if cv2.waitKey(1) & 0xFF == ord('q'): break
cap.release() # Release the capture
cv2.destroyAllWindows() # destroy the window
'''
#----------------------------------------------------------------------------------------------------------------
'''
# Milestone 2 : Face Detection
# Load the face detection model
facecascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Capture video from webcam (index 0)
cap = cv2.VideoCapture(0)
while True:
# Read a frame from the webcam
ret, frame = cap.read()
# Check if frame is read successfully
if not ret:
print("Error: Failed to capture frame from webcam")
break
# Convert frame to grayscale (better for face detection)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = facecascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# Draw a green rectangle around each detected face
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the frame with detected faces
cv2.imshow('frame', frame)
# Exit if 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture object
cap.release()
# Close all OpenCV windows
cv2.destroyAllWindows()
'''
#----------------------------------------------------------------------------------------------------------------
''''
# Milestone 3 : Face Recognition with a Basic Classifier
import cv2
# Load the pre-trained Haar Cascade classifiers for face and animal face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cat_cascade = cv2.CascadeClassifier('haarcascade_frontalcatface.xml')
dog_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
# Access the default camera (usually the first camera connected)
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read() # Capture frame-by-frame
# Convert frame to grayscale (required for face detection)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
cats = cat_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
dogs = dog_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# Draw rectangles around detected faces and animals
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # BGR color format
for (x, y, w, h) in cats:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # BGR color format
for (x, y, w, h) in dogs:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) # BGR color format
# Display the frame with detected faces and animals
cv2.imshow('REDAs Face Detection Software', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release() # Release the capture
cv2.destroyAllWindows() # Close all OpenCV windows
'''
#----------------------------------------------------------------------------------------------------------------
# Milestone 4 : adding labels
import cv2
# Load the pre-trained Haar Cascade classifiers for face and animal face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cat_cascade = cv2.CascadeClassifier('haarcascade_frontalcatface.xml')
dog_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
# Access the default camera (usually the first camera connected)
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read() # Capture frame-by-frame
# Convert frame to grayscale (required for face detection)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
cats = cat_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
dogs = dog_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# Label detected faces as human or animal
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # BGR color format
cv2.putText(frame, 'Human', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
for (x, y, w, h) in cats:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # BGR color format
cv2.putText(frame, 'Cat', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
for (x, y, w, h) in dogs:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) # BGR color format
cv2.putText(frame, 'Dog', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# Display the frame with detected faces and animals
cv2.imshow('REDAs Face Detection Software', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and close OpenCV windows
cap.release()
cv2.destroyAllWindows()