99 lines
3.0 KiB
Python
99 lines
3.0 KiB
Python
# 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 logging
|
|
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
|
|
|
|
logger = logging.getLogger('dnn')
|
|
|
|
image = cv2.imread(imagefile)
|
|
# rows = open(args["labels"]).read().strip().split("\n")
|
|
# classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows]
|
|
|
|
|
|
logger.info("[INFO] loding model...")
|
|
net = cv2.dnn.readNetFromCaffe(prototxt, model)
|
|
logger.info("Loaded")
|
|
|
|
video_capture = cv2.VideoCapture(2)
|
|
|
|
while True:
|
|
# Grab a single frame of video
|
|
ret, image = video_capture.read()
|
|
|
|
(h, w) = image.shape[:2]
|
|
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
|
|
(300, 300), (104.0, 177.0, 123.0))
|
|
|
|
net.setInput(blob)
|
|
start = time.time()
|
|
detections = net.forward()
|
|
end = time.time()
|
|
logger.debug(f"classification took {end-start:.5} seconds")
|
|
# 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)
|
|
|
|
# Hit 'q' on the keyboard to quit!
|
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
|
break
|
|
|
|
|
|
# Release handle to the webcam
|
|
video_capture.release()
|
|
cv2.destroyAllWindows() |