|
| 1 | +# Import Libraries |
| 2 | +import cv2 |
| 3 | +import numpy as np |
| 4 | + |
| 5 | + |
| 6 | +# The gender model architecture |
| 7 | +# https://drive.google.com/open?id=1W_moLzMlGiELyPxWiYQJ9KFaXroQ_NFQ |
| 8 | +GENDER_MODEL = 'weights/deploy_gender.prototxt' |
| 9 | +# The gender model pre-trained weights |
| 10 | +# https://drive.google.com/open?id=1AW3WduLk1haTVAxHOkVS_BEzel1WXQHP |
| 11 | +GENDER_PROTO = 'weights/gender_net.caffemodel' |
| 12 | +# Each Caffe Model impose the shape of the input image also image preprocessing is required like mean |
| 13 | +# substraction to eliminate the effect of illunination changes |
| 14 | +MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746) |
| 15 | +# Represent the gender classes |
| 16 | +GENDER_LIST = ['Male', 'Female'] |
| 17 | +# https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt |
| 18 | +FACE_PROTO = "weights/deploy.prototxt.txt" |
| 19 | +# https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel |
| 20 | +FACE_MODEL = "weights/res10_300x300_ssd_iter_140000_fp16.caffemodel" |
| 21 | + |
| 22 | +# load face Caffe model |
| 23 | +face_net = cv2.dnn.readNetFromCaffe(FACE_PROTO, FACE_MODEL) |
| 24 | +# Load gender prediction model |
| 25 | +gender_net = cv2.dnn.readNetFromCaffe(GENDER_MODEL, GENDER_PROTO) |
| 26 | + |
| 27 | +# Initialize frame size |
| 28 | +frame_width = 1280 |
| 29 | +frame_height = 720 |
| 30 | + |
| 31 | + |
| 32 | +def get_faces(frame, confidence_threshold=0.5): |
| 33 | + # convert the frame into a blob to be ready for NN input |
| 34 | + blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), (104, 177.0, 123.0)) |
| 35 | + # set the image as input to the NN |
| 36 | + face_net.setInput(blob) |
| 37 | + # perform inference and get predictions |
| 38 | + output = np.squeeze(face_net.forward()) |
| 39 | + # initialize the result list |
| 40 | + faces = [] |
| 41 | + # Loop over the faces detected |
| 42 | + for i in range(output.shape[0]): |
| 43 | + confidence = output[i, 2] |
| 44 | + if confidence > confidence_threshold: |
| 45 | + box = output[i, 3:7] * \ |
| 46 | + np.array([frame.shape[1], frame.shape[0], |
| 47 | + frame.shape[1], frame.shape[0]]) |
| 48 | + # convert to integers |
| 49 | + start_x, start_y, end_x, end_y = box.astype(np.int) |
| 50 | + # widen the box a little |
| 51 | + start_x, start_y, end_x, end_y = start_x - \ |
| 52 | + 10, start_y - 10, end_x + 10, end_y + 10 |
| 53 | + start_x = 0 if start_x < 0 else start_x |
| 54 | + start_y = 0 if start_y < 0 else start_y |
| 55 | + end_x = 0 if end_x < 0 else end_x |
| 56 | + end_y = 0 if end_y < 0 else end_y |
| 57 | + # append to our list |
| 58 | + faces.append((start_x, start_y, end_x, end_y)) |
| 59 | + return faces |
| 60 | + |
| 61 | + |
| 62 | +def get_optimal_font_scale(text, width): |
| 63 | + """Determine the optimal font scale based on the hosting frame width""" |
| 64 | + for scale in reversed(range(0, 60, 1)): |
| 65 | + textSize = cv2.getTextSize(text, fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=scale/10, thickness=1) |
| 66 | + new_width = textSize[0][0] |
| 67 | + if (new_width <= width): |
| 68 | + return scale/10 |
| 69 | + return 1 |
| 70 | + |
| 71 | +# from: https://stackoverflow.com/questions/44650888/resize-an-image-without-distortion-opencv |
| 72 | +def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA): |
| 73 | + # initialize the dimensions of the image to be resized and |
| 74 | + # grab the image size |
| 75 | + dim = None |
| 76 | + (h, w) = image.shape[:2] |
| 77 | + # if both the width and height are None, then return the |
| 78 | + # original image |
| 79 | + if width is None and height is None: |
| 80 | + return image |
| 81 | + # check to see if the width is None |
| 82 | + if width is None: |
| 83 | + # calculate the ratio of the height and construct the |
| 84 | + # dimensions |
| 85 | + r = height / float(h) |
| 86 | + dim = (int(w * r), height) |
| 87 | + # otherwise, the height is None |
| 88 | + else: |
| 89 | + # calculate the ratio of the width and construct the |
| 90 | + # dimensions |
| 91 | + r = width / float(w) |
| 92 | + dim = (width, int(h * r)) |
| 93 | + # resize the image |
| 94 | + return cv2.resize(image, dim, interpolation = inter) |
| 95 | + |
| 96 | + |
| 97 | +def predict_gender(): |
| 98 | + """Predict the gender of the faces showing in the image""" |
| 99 | + # create a new cam object |
| 100 | + cap = cv2.VideoCapture(0) |
| 101 | + |
| 102 | + while True: |
| 103 | + _, img = cap.read() |
| 104 | + # resize the image, uncomment if you want to resize the image |
| 105 | + # img = cv2.resize(img, (frame_width, frame_height)) |
| 106 | + # Take a copy of the initial image and resize it |
| 107 | + frame = img.copy() |
| 108 | + if frame.shape[1] > frame_width: |
| 109 | + frame = image_resize(frame, width=frame_width) |
| 110 | + # predict the faces |
| 111 | + faces = get_faces(frame) |
| 112 | + # Loop over the faces detected |
| 113 | + # for idx, face in enumerate(faces): |
| 114 | + for i, (start_x, start_y, end_x, end_y) in enumerate(faces): |
| 115 | + face_img = frame[start_y: end_y, start_x: end_x] |
| 116 | + # image --> Input image to preprocess before passing it through our dnn for classification. |
| 117 | + # scale factor = After performing mean substraction we can optionally scale the image by some factor. (if 1 -> no scaling) |
| 118 | + # size = The spatial size that the CNN expects. Options are = (224*224, 227*227 or 299*299) |
| 119 | + # mean = mean substraction values to be substracted from every channel of the image. |
| 120 | + # swapRB=OpenCV assumes images in BGR whereas the mean is supplied in RGB. To resolve this we set swapRB to True. |
| 121 | + blob = cv2.dnn.blobFromImage(image=face_img, scalefactor=1.0, size=( |
| 122 | + 227, 227), mean=MODEL_MEAN_VALUES, swapRB=False, crop=False) |
| 123 | + # Predict Gender |
| 124 | + gender_net.setInput(blob) |
| 125 | + gender_preds = gender_net.forward() |
| 126 | + i = gender_preds[0].argmax() |
| 127 | + gender = GENDER_LIST[i] |
| 128 | + gender_confidence_score = gender_preds[0][i] |
| 129 | + # Draw the box |
| 130 | + label = "{}-{:.2f}%".format(gender, gender_confidence_score*100) |
| 131 | + print(label) |
| 132 | + yPos = start_y - 15 |
| 133 | + while yPos < 15: |
| 134 | + yPos += 15 |
| 135 | + # get the font scale for this image size |
| 136 | + optimal_font_scale = get_optimal_font_scale(label,((end_x-start_x)+25)) |
| 137 | + box_color = (255, 0, 0) if gender == "Male" else (147, 20, 255) |
| 138 | + cv2.rectangle(frame, (start_x, start_y), (end_x, end_y), box_color, 2) |
| 139 | + # Label processed image |
| 140 | + cv2.putText(frame, label, (start_x, yPos), |
| 141 | + cv2.FONT_HERSHEY_SIMPLEX, optimal_font_scale, box_color, 2) |
| 142 | + |
| 143 | + # Display processed image |
| 144 | + |
| 145 | + # frame = cv2.resize(frame, (frame_height, frame_width)) |
| 146 | + cv2.imshow("Gender Estimator", frame) |
| 147 | + if cv2.waitKey(1) == ord("q"): |
| 148 | + break |
| 149 | + # uncomment if you want to save the image |
| 150 | + # cv2.imwrite("output.jpg", frame) |
| 151 | + # Cleanup |
| 152 | + cv2.destroyAllWindows() |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == '__main__': |
| 157 | + predict_gender() |
0 commit comments