forked from nirek13/HaarCascadeClasifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImager.py
93 lines (72 loc) · 3.77 KB
/
Imager.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Importing Libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt
import math
# Importing Cascades
face_cascade = cv2.CascadeClassifier('Cascade_Files/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('Cascade_Files/haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('Cascade_Files/haarcascade_smile.xml')
sign_cascade = cv2.CascadeClassifier('Cascade_Files/sign_cascade.xml')
traffic_light_cascade = cv2.CascadeClassifier('Cascade_Files/traffic_light.xml')
hand_cascade = cv2.CascadeClassifier('Cascade_Files/hand_cascde.xml')
palm_cascade = cv2.CascadeClassifier('Cascade_Files/rpalm_cascade.xml')
yield_sign_cascade = cv2.CascadeClassifier('Cascade_Files/yield_sign_cascade.xml')
traffic_light_cascade = cv2.CascadeClassifier('Cascade_Files/traffic_light_cascade.xml')
# Making function to detect
def detect(gray, frame):
signs = sign_cascade.detectMultiScale(gray, 1.3, 5)
hands = hand_cascade.detectMultiScale(gray, 1.3, 5)
traffic_lights = traffic_light_cascade.detectMultiScale(gray, 1.3, 5)
palm = palm_cascade.detectMultiScale(gray, 1.3, 5)
yield_sign = yield_sign_cascade.detectMultiScale(gray, 1.3, 5)
faces = face_cascade.detectMultiScale(gray, 1.3, 7)
traffic_light = traffic_light_cascade.detectMultiScale(gray, 1.3, 7)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = frame[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
smiles = smile_cascade.detectMultiScale(roi_gray, 1.2, 12)
for (ex, ey, ew, eh) in eyes:
cv2.putText(roi_color, 'Eyes', (ex, ey), cv2.FONT_ITALIC, 1, (255, 255, 255), 2)
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
for (sx, sy, sw, sh) in smiles:
cv2.putText(roi_color, 'Smile', (sx, sy), cv2.FONT_ITALIC, 1, (255, 255, 255), 2)
cv2.rectangle(roi_color, (sx, sy), (sx + sw, sy + sh), (0, 0, 255), 2)
for (x, y, w, h) in signs:
cv2.putText(frame, 'Signs', (x, y), cv2.FONT_ITALIC, 1, (255, 255, 255), 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 100, 100), 2)
for (x, y, w, h) in traffic_lights:
cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 100, 100), 2)
cv2.putText(frame, 'Traffic_Light', (x, y), cv2.FONT_ITALIC, 1, (255, 255, 255), 2)
for (x, y, w, h) in hands:
cv2.putText(frame, 'Hand', (x, y), cv2.FONT_ITALIC, 1, (255, 255, 255), 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 100, 100), 2)
for (x, y, w, h) in palm:
cv2.putText(frame, 'Palm', (x, y), cv2.FONT_ITALIC, 1, (255, 255, 255), 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 100, 100), 2)
for (x, y, w, h) in yield_sign:
cv2.putText(frame, 'Yield_Sign', (x, y), cv2.FONT_ITALIC, 1, (255, 255, 255), 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 100, 100), 2)
for (x, y, w, h) in traffic_light:
cv2.putText(frame, 'Traffic_Light', (x, y), cv2.FONT_ITALIC, 1, (255, 255, 255), 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 100, 100), 2)
return frame
# Stating Camera
video_capture = cv2.VideoCapture(0) # 0, if you have internal camera, 1 if you have externel camera
while True:
_, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Also An Edge Detector in another frame
lower = 115
upper = 235
canvas = cv2.Canny(gray, lower, upper)
cv2.imshow('Canny Edge Detector', canvas)
detected = detect(gray, frame)
cv2.imshow('Video', detected)
# Press q to break the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()