-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrecognition_webcam.py
53 lines (46 loc) · 1.97 KB
/
recognition_webcam.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
#Coded by:- Kushal Bhavsra
#From:- Techmicra IT solution
import time
import cv2
import label_image
import os,random
import subprocess
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
size = 4
# We load the xml file
classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
global text
webcam = cv2.VideoCapture(0) # Using default WebCam connected to the 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
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
if text == 'Angry':
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 25, 255), 7)
cv2.putText(im, text, (x + h, y), font, 1, (0, 25,255), 2)
if text == 'Smile':
cv2.rectangle(im, (x, y), (x + w, y + h), (0,260,0), 7)
cv2.putText(im, text, (x + h, y), font, 1, (0,260,0), 2)
if text == 'Fear':
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 255), 7)
cv2.putText(im, text, (x + h, y), font, 1, (0, 255, 255), 2)
if text == 'Sad':
cv2.rectangle(im, (x, y), (x + w, y + h), (0,191,255), 7)
cv2.putText(im, text, (x + h, y), font, 1, (0,191,255), 2)
# Show the image/
cv2.imshow('Emotion recognition', im)
key = cv2.waitKey(30)& 0xff
if key == 27: # The Esc key
break