Computer Vision News - April 2022
15 Facial Recognition with Open CV2 print(Preprocess_face.shape) Preprocess_face_input = Preprocess_face.reshape(1, 224, 224, 3) print(Preprocess_face_input.shape) Run Prediction and convert prediction into names # perform prediction yhat = model.predict(Preprocess_face_input) # convert prediction into names results = decode_predictions(yhat) # display most likely results for result in results[0]: print('%s: %.3f%%' % (result[0], result[1]*100)) Draw a rectangle on the original image with the prediction label def draw_rectangle_with_label(image, label): faces_coord = detector.detectMultiScale(image, scaleFactor=scale_factor, minNeighbors=min_neighbors, minSize=min_size, flags=flags) draw_rectangle(image,faces_coord) cv2.putText(image, name, (faces_coord[0][0], faces_coord[0][1]), cv2.FONT_HERSHEY_SIMPLEX , 1, (66, 53, 243), 3) plt.imshow(image) plt.show() for result in results[0]: print('%s: %.3f%%' % (result[0], result[1]*100)) draw_rectangle_with_label(Sample, results) Step 2.5 Understand the hidden layer in VGG or CNN Check the MaxPooling Layer # load the model again model = VGGFace(model='resnet50') # redefine model to output right after the third hidden layer model = Model(inputs=model.inputs, outputs=model.layers[4].output) model.summary() # load the image with the required shape img = extract_face # convert the image to an array img = img_to_array(img) # expand dimensions so that it represents a single 'sample' img = expand_dims(img, axis=0) # prepare the image (e.g. scale pixel values for the vgg) img = preprocess_input(img) # get feature map for first hidden layer feature_maps = model.predict(img) # plot all 4 maps in an 4x4 squares
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=