Last active
April 26, 2020 11:58
-
-
Save KevinPatel04/5f4052ebc0523dc03a83100ec43f4f6e to your computer and use it in GitHub Desktop.
Real time Face Recognition Blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# capture the video from default camera | |
# video_stream = cv2.VideoCapture(0) | |
# read video from video file | |
video_file_path = 'test/face-demographics-walking.mp4' | |
video_stream = cv2.VideoCapture(video_file_path) | |
# initialize the number of frame needed to be skipped | |
skip = 0 | |
while True: | |
#Read the frame | |
for i in range(skip): | |
video_stream.grab() | |
# get current frame | |
stat, frame = video_stream.read() | |
# check that frame is left to read | |
if stat == False: | |
print('Video has ended') | |
break | |
# reduce size of image to 50% | |
scaled_frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5) | |
# find all face locations using face_locations() function | |
# model can be "cnn" or "hog" | |
# number_of_times_to_upsample = 1 higher and detect more faces | |
all_face_locations = face_recognition.face_locations(scaled_frame, model="cnn") | |
for index,face_location in enumerate(all_face_locations): | |
# split the tuple | |
top_pos, right_pos, bottom_pos, left_pos = face_location | |
# scale up the coordinates by 2 times | |
top_pos = top_pos * 2 | |
right_pos = right_pos * 2 | |
bottom_pos = bottom_pos * 2 | |
left_pos = left_pos * 2 | |
#--------------------------------------------------------------------------- | |
# Face Expression Detection | |
#--------------------------------------------------------------------------- | |
# Slice frame image array by positions face_locations | |
face_image = frame[top_pos:bottom_pos, left_pos:right_pos] | |
# preprocessing input, convert it to an image like as the data in dataset | |
# convert face image to grayscale | |
face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2GRAY) | |
# resize to 48X48 px size | |
face_image = cv2.resize(face_image, (48, 48)) | |
# convert the PIL image into a 3D numpy array | |
img_pixels = image.img_to_array(face_image) | |
# expand the shape of an array into single row multiple columns | |
img_pixels = np.expand_dims(img_pixels, axis = 0) | |
# pixels are in range of [0, 255]. Normalize all pixels in scale of [0, 1] | |
img_pixels /= 255 | |
# do prediction using model, get the prediction values for all 7 expressions | |
exp_predictions = face_exp_model.predict(img_pixels) | |
# find max indexed prediction value (0 till 7) | |
max_index = np.argmax(exp_predictions[0]) | |
# get the corresponding label from emotions_label list | |
emotion_label = emotions_label[max_index] | |
# draw rectangle around each face | |
cv2.rectangle(frame, (left_pos,top_pos), (right_pos,bottom_pos), (0,0,255), 2) | |
# disply the name of emotion as text along the face | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
cv2.rectangle(frame, (left_pos-1, bottom_pos + 18), (right_pos+1, bottom_pos), (0, 0, 255), -1) | |
cv2.putText(frame, emotion_label, (left_pos+2,bottom_pos+11), font, 0.5, (255,255,255), 1) | |
#--------------------------------------------------------------------------- | |
# display frame | |
cv2.imshow("Video",frame) | |
# for only google colab | |
# cv2_imshow(frame) | |
# Press 'q' on the keyboard to break the while loop | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# Release the video stream resource | |
video_stream.release() | |
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# importing required libraries | |
import cv2 | |
import numpy as np | |
from keras.preprocessing import image | |
from keras.models import model_from_json | |
import face_recognition | |
# only for google colab | |
# from google.colab.patches import cv2_imshow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# face expression model initialization | |
face_exp_model = model_from_json(open("models/facial_expression_model_structure.json","r").read()) | |
# load weights into model | |
face_exp_model.load_weights("models/facial_expression_model_weights.h5") | |
# list of emotions labels | |
emotions_label = ('Angry','Digust','Fear','Happy','Sad','Surprise','Neutral') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# importing required libraries | |
import cv2 | |
import numpy as np | |
from keras.preprocessing import image | |
from keras.models import model_from_json | |
import face_recognition | |
# only for google colab | |
# from google.colab.patches import cv2_imshow | |
# face expression model initialization | |
face_exp_model = model_from_json(open("models/facial_expression_model_structure.json","r").read()) | |
# load weights into model | |
face_exp_model.load_weights("models/facial_expression_model_weights.h5") | |
# list of emotions labels | |
emotions_label = ('Angry','Digust','Fear','Happy','Sad','Surprise','Neutral') | |
# capture the video from default camera | |
# video_stream = cv2.VideoCapture(0) | |
# read video from video file | |
video_file_path = 'test/face-demographics-walking.mp4' | |
video_stream = cv2.VideoCapture(video_file_path) | |
# initialize the number of frame needed to be skipped | |
skip = 0 | |
while True: | |
#Read the frame | |
for i in range(skip): | |
video_stream.grab() | |
# get current frame | |
stat, frame = video_stream.read() | |
# check that frame is left to read | |
if stat == False: | |
print('Video has ended') | |
break | |
# reduce size of image to 50% | |
scaled_frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5) | |
# find all face locations using face_locations() function | |
# model can be "cnn" or "hog" | |
# number_of_times_to_upsample = 1 higher and detect more faces | |
all_face_locations = face_recognition.face_locations(scaled_frame, model="cnn") | |
for index,face_location in enumerate(all_face_locations): | |
# split the tuple | |
top_pos, right_pos, bottom_pos, left_pos = face_location | |
# scale up the coordinates by 2 times | |
top_pos = top_pos * 2 | |
right_pos = right_pos * 2 | |
bottom_pos = bottom_pos * 2 | |
left_pos = left_pos * 2 | |
#--------------------------------------------------------------------------- | |
# Face Expression Detection | |
#--------------------------------------------------------------------------- | |
# Slice frame image array by positions face_locations | |
face_image = frame[top_pos:bottom_pos, left_pos:right_pos] | |
# preprocessing input, convert it to an image like as the data in dataset | |
# convert face image to grayscale | |
face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2GRAY) | |
# resize to 48X48 px size | |
face_image = cv2.resize(face_image, (48, 48)) | |
# convert the PIL image into a 3D numpy array | |
img_pixels = image.img_to_array(face_image) | |
# expand the shape of an array into single row multiple columns | |
img_pixels = np.expand_dims(img_pixels, axis = 0) | |
# pixels are in range of [0, 255]. Normalize all pixels in scale of [0, 1] | |
img_pixels /= 255 | |
# do prediction using model, get the prediction values for all 7 expressions | |
exp_predictions = face_exp_model.predict(img_pixels) | |
# find max indexed prediction value (0 till 7) | |
max_index = np.argmax(exp_predictions[0]) | |
# get the corresponding label from emotions_label list | |
emotion_label = emotions_label[max_index] | |
# draw rectangle around each face | |
cv2.rectangle(frame, (left_pos,top_pos), (right_pos,bottom_pos), (0,0,255), 2) | |
# disply the name of emotion as text along the face | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
cv2.rectangle(frame, (left_pos-1, bottom_pos + 18), (right_pos+1, bottom_pos), (0, 0, 255), -1) | |
cv2.putText(frame, emotion_label, (left_pos+2,bottom_pos+11), font, 0.5, (255,255,255), 1) | |
#--------------------------------------------------------------------------- | |
# display frame | |
cv2.imshow("Face Expressions Video",frame) | |
# for only google colab | |
# cv2_imshow(frame) | |
# Press 'q' on the keyboard to break the while loop | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# Release the video stream resource | |
video_stream.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment