Face Detection

The more accurate OpenCV face detector is deep learning based, and in particular, utilizes the Single Shot Detector (SSD) framework with ResNet as the base network.

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. from imutils.video import VideoStream
  4. import numpy as np
  5. import argparse
  6. import cv2
  7. import imutils
  8. import time
  9. def detected_draw(args, img, detections):
  10. (h, w) = img.shape[:2]
  11. # loop over the detections
  12. for i in range(0, detections.shape[2]):
  13. # extract the confidence (i.e., probability) associated with the prediction
  14. confidence = detections[0, 0, i, 2]
  15. # filter out weak detections by ensuring the `confidence` is greater than the minimum confidence
  16. if confidence > args["confidence"]:
  17. # compute the (x, y)-coordinates of the bounding box for the object
  18. box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
  19. (startX, startY, endX, endY) = box.astype("int")
  20. # draw the bounding box of the face along with the associated probability
  21. text = "{:.2f}%".format(confidence * 100)
  22. y = startY - 10 if startY - 10 > 10 else startY + 10
  23. cv2.rectangle(img, (startX, startY), (endX, endY), (255, 255, 255), 2)
  24. cv2.putText(img, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (255, 255, 255), 2)
  25. pass
  26. pass
  27. def img_detect(args):
  28. # load our serialized model from disk
  29. print("[INFO] loading model...")
  30. net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
  31. # load the input image and construct an input blob for the image
  32. # by resizing to a fixed 300x300 pixels and then normalizing it
  33. image = cv2.imread(args["image"])
  34. blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
  35. # pass the blob through the network and obtain the detections and predictions
  36. print("[INFO] computing object detections...")
  37. net.setInput(blob)
  38. detections = net.forward()
  39. detected_draw(args, image, detections)
  40. # show the output image
  41. cv2.imshow("Output", image)
  42. cv2.waitKey(0)
  43. pass
  44. def cam_detect(args):
  45. # load our serialized model from disk
  46. print("[INFO] loading model...")
  47. net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
  48. # initialize the video stream and allow the camera sensor to warm up
  49. print("[INFO] starting video stream...")
  50. vs = VideoStream(src=0).start()
  51. #for Raspberry Pi camera
  52. #vs = VideoStream(usePiCamera=True).start()
  53. time.sleep(2.0)
  54. # loop over the frames from the video stream
  55. while True:
  56. # grab the frame from the threaded video stream and resize it
  57. # to have a maximum width of 400 pixels
  58. frame = vs.read()
  59. frame = imutils.resize(frame, width=800)
  60. # grab the frame dimensions and convert it to a blob
  61. blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
  62. # pass the blob through the network and obtain the detections and predictions
  63. net.setInput(blob)
  64. detections = net.forward()
  65. detected_draw(args, frame, detections)
  66. # show the output frame
  67. cv2.imshow("Frame", frame)
  68. key = cv2.waitKey(1) & 0xFF
  69. # if the `q` key was pressed, break from the loop
  70. if key == ord("q"):
  71. break
  72. pass
  73. # do a bit of cleanup
  74. cv2.destroyAllWindows()
  75. vs.stop()
  76. pass
  77. if __name__ == '__main__':
  78. # construct the argument parse and parse the arguments
  79. ap = argparse.ArgumentParser()
  80. ap.add_argument("-i", "--image", required=False, default='', help="path to input image")
  81. ap.add_argument("-p", "--prototxt", required=False, default='deploy.prototxt', help="path to Caffe 'deploy' prototxt file")
  82. ap.add_argument("-m", "--model", required=False, default='res10_300x300_ssd_iter_140000_fp16.caffemodel', help="path to Caffe pre-trained model")
  83. ap.add_argument("-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections")
  84. args = vars(ap.parse_args())
  85. if len(args['image']) == 0:
  86. cam_detect(args)
  87. else:
  88. img_detect(args)
  89. pass

Output_screenshot_27.11.2020.png

refs:
the Caffe prototxt file: deploy.prototxt
the Caffe weight file: res10_300x300_ssd_iter_140000_fp16.caffemodel