-
Notifications
You must be signed in to change notification settings - Fork 149
/
label.py
43 lines (31 loc) · 1.35 KB
/
label.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
import cv2
import label_image
size = 4
#Load the xml file
classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
webcam = cv2.VideoCapture(0) #Using default WebCam connected to PC.
while True:
(rval, im) = webcam.read()
im=cv2.flip(im,1,0) #Flip to act as a mirror
# Resize the image to speed up detection
mini = cv2.resize(im, (int(im.shape[1]/size), int(im.shape[0]/size)))
# detect MultiScale / faces
faces = classifier.detectMultiScale(mini)
# Draw rectangles around each face
for f in faces:
(x, y, w, h) = [v * size for v in f] #Scale the shapesize backup
cv2.rectangle(im, (x,y), (x+w,y+h), (0,255,0), 4)
#Save just the rectangle faces in SubRecFaces
sub_face = im[y:y+h, x:x+w]
FaceFileName = "test.jpg" #Saving the current image from the webcam for testing.
cv2.imwrite(FaceFileName, sub_face)
text = label_image.main(FaceFileName)# Getting the Result from the label_image file, i.e., Classification Result.
text = text.title()# Title Case looks Stunning.
font = cv2.FONT_HERSHEY_TRIPLEX
cv2.putText(im, text,(x+w,y), font, 1, (0,0,255), 2)
# Show the image
cv2.imshow('Capture', im)
key = cv2.waitKey(10)
# if Esc key is press then break out of the loop
if key == 27: #The Esc key
break