-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognize.py
More file actions
52 lines (42 loc) · 1.87 KB
/
recognize.py
File metadata and controls
52 lines (42 loc) · 1.87 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
import cv2
def draw_boundary(img, classifier, scaleFactor, minNeighbors, color, text, clf):
# Converting image to gray-scale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# detecting features in gray-scale image, returns coordinates, width and height of features
features = classifier.detectMultiScale(gray_img, scaleFactor, minNeighbors)
coords = []
# drawing rectangle around the feature and labeling it
for (x, y, w, h) in features:
cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
# Predicting the id of the user
id, _ = clf.predict(gray_img[y:y+h, x:x+w])
# Check for id of user and label the rectangle accordingly
if id==1:
cv2.putText(img, "Ali", (x, y-4), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 1, cv2.LINE_AA)
coords = [x, y, w, h]
return coords
# Method to recognize the person
def recognize(img, clf, faceCascade):
color = {"blue": (255, 0, 0), "red": (0, 0, 255), "green": (0, 255, 0), "white": (255, 255, 255)}
coords = draw_boundary(img, faceCascade, 1.1, 10, color["white"], "Face", clf)
return img
# Loading classifier
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Loading custom classifier to recognize
clf = cv2.face.LBPHFaceRecognizer_create()
clf.read("classifier.yml")
# Capturing real time video stream. 0 for built-in web-cams, 0 or -1 for external web-cams
video_capture = cv2.VideoCapture(-1)
while True:
# Reading image from video stream
_, img = video_capture.read()
# Call method we defined above
img = recognize(img, clf, faceCascade)
# Writing processed image in a new window
cv2.imshow("face detection", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# releasing web-cam
video_capture.release()
# Destroying output window
cv2.destroyAllWindows()