# import face_recognition import cv2 from skimage.feature import hog from skimage import data, exposure import matplotlib.pyplot as plt import numpy as np import dlib import time imagefile = "Marjo.jpg" prototxt = "dnn/face_detector/opencv_face_detector.pbtxt" prototxt = "dnn/face_detector/deploy.prototxt" model = "dnn/face_detector/res10_300x300_ssd_iter_140000_fp16.caffemodel" confidence_threshold = .0 image = cv2.imread(imagefile) # rows = open(args["labels"]).read().strip().split("\n") # classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows] (h, w) = image.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0)) print("[INFO] loding model...") net = cv2.dnn.readNetFromCaffe(prototxt, model) print("Loaded") net.setInput(blob) start = time.time() detections = net.forward() end = time.time() print("[INFO] classification took {:.5} seconds".format(end-start)) idxs = np.argsort(detections[0])[::-1][:5] for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with the # prediction confidence = detections[0, 0, i, 2] # compute the (x, y)-coordinates of the bounding box for the # object box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # we always draw # First we crop the sub-rect from the image sub_img = image[startY:endY, startX:endX] rect_img = sub_img.copy() width = 2 cv2.rectangle(rect_img, (0, 0), (sub_img.shape[1]-int(width/2), sub_img.shape[0]-int(width/2)), (0, 0, 255), width) # white_rect = np.ones(sub_img.shape, dtype=np.uint8) * 255 # At least 10% opacity alpha = max(.1, confidence) res = cv2.addWeighted(sub_img, 1-alpha, rect_img, alpha, 1.0) # Putting the image back to its position image[startY:endY, startX:endX] = res # filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if confidence > confidence_threshold: # draw the bounding box of the face along with the associated # probability text = "{:.2f}%".format(confidence * 100) y = startY - 10 if startY - 10 > 10 else startY + 10 # cv2.rectangle(image, (startX, startY), (endX, endY), # (0, 0, 255), 2) cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) # show the output image cv2.imshow("Output", image) cv2.waitKey(0)