We’ll use our previously trained model to make predictions from a webcam feed. For this we’ll use OpenCV library to get images from webcam. The basic idea is that we’ll detect faces and for each of the faces, we’ll ask the model to classify. The code is pretty straightforward. Predictions made by the model

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
import cv2
 
import numpy as np
import keras
model = keras.models.load_model('gender.model')
 
# capture video from webcam
cap = cv2.VideoCapture(0)
# initialize face detection
cascPath = 'haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascPath)
 
while(True):
    # read a frame. frame is in BGR( Blue, Green, Red) format
    ret, frame = cap.read()
 
    # convert the image to grayscale for performing face detection
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )
 
    # create a rgb frame since our model expects its input image
    # to be in RGB format
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
   
 
    cropped_faces = []
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        # increase the width, height of face region by some pixels.
        # we want a bit more pixels rather than just the face
        extra_pad = 40
        x = max(1, x - extra_pad)
        y = max(1, y - extra_pad)
        width = min(frame.shape[1], x + w + extra_pad*2)
        height = min(frame.shape[0], y + h + extra_pad*2)
        cv2.rectangle(rgb_frame, (x, y), (width, height), (0, 255, 255), 2)
 
        # crop the face
        cropped = rgb_frame[y: height, x: width].astype("float32")
        cropped = cv2.resize(cropped, (198, 198))
       
        # need to rescale the values from 0 to 1
        cropped = cropped /255.0
        cropped = np.clip(cropped, 0, 1)
       
        cropped_faces.append(cropped)
 
 
    # predict the faces
    cv2.putText(rgb_frame, "Total faces = {}".format(len(faces)), (10, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (30, 255, 200), 2)
    if len(faces) > 0 and 1 == 1:
        predictions = model.predict(np.array(cropped_faces))
        predictions = np.where(predictions.flatten() < 0.5, "female", "male")
        for i, prediction in enumerate(predictions):
            print(prediction)
            text = prediction
 
            x, y, _, _ = faces[i]
            cv2.putText(rgb_frame, text, (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
   
    # convert our RGB frame back to BGR format before displaying
    cv2.imshow('frame', cv2.cvtColor(rgb_frame, cv2.COLOR_RGB2BGR))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
 
cap.release()
cv2.destroyAllWindows()

Here is a short video of me and some celebrities getting classified.

Comments