-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageRecog.py
65 lines (47 loc) · 2.56 KB
/
ImageRecog.py
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
import cv2
#python3 ~/Desktop/ImageRecog1/ImageRecog.py
def detect_objects():
# Load pre-trained face, eye, and profile face detection models (Haar cascades)
face_cascade = cv2.CascadeClassifier('/Users/jung/Desktop/ImageRecog1/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/Users/jung/Desktop/ImageRecog1/haarcascade_eye.xml')
profile_cascade = cv2.CascadeClassifier('/Users/jung/Desktop/ImageRecog1/haarcascade_profileface.xml')
# Set up other necessary configurations
min_face_size = (30, 30)
min_eye_size = (10, 10)
min_profile_size = (30, 30)
scale_factor = 1.3
# Start video capture
cap = cv2.VideoCapture(0) # 0 for default camera
while True:
# Read the current frame from the video feed
ret, frame = cap.read()
# Convert frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Perform face detection using the face cascade model
faces = face_cascade.detectMultiScale(gray, scaleFactor=scale_factor, minSize=min_face_size)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame, "Face", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Extract the region of interest (ROI) within the face region
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
# Perform eye detection within the face region
eyes = eye_cascade.detectMultiScale(roi_gray, scaleFactor=scale_factor, minSize=min_eye_size)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (255, 0, 0), 2)
cv2.putText(roi_color, "Eye", (ex, ey-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
# Perform profile face detection within the face region
profiles = profile_cascade.detectMultiScale(roi_gray, scaleFactor=scale_factor, minSize=min_profile_size)
for (px, py, pw, ph) in profiles:
cv2.rectangle(roi_color, (px, py), (px+pw, py+ph), (0, 0, 255), 2)
cv2.putText(roi_color, "Profile", (px, py-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
# Display the resulting frame
cv2.imshow('Object Recognition Robot', frame)
# Exit loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release video capture and close windows
cap.release()
cv2.destroyAllWindows()
# Call the function to start object detection
detect_objects()