face_detection_dlib_hog.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import argparse
  2. import os
  3. import time
  4. import cv2
  5. import dlib
  6. def detectFaceDlibHog(detector, frame, inHeight=300, inWidth=0):
  7. frameDlibHog = frame.copy()
  8. frameHeight = frameDlibHog.shape[0]
  9. frameWidth = frameDlibHog.shape[1]
  10. if not inWidth:
  11. inWidth = int((frameWidth / frameHeight) * inHeight)
  12. scaleHeight = frameHeight / inHeight
  13. scaleWidth = frameWidth / inWidth
  14. frameDlibHogSmall = cv2.resize(frameDlibHog, (inWidth, inHeight))
  15. frameDlibHogSmall = cv2.cvtColor(frameDlibHogSmall, cv2.COLOR_BGR2RGB)
  16. faceRects = detector(frameDlibHogSmall, 0)
  17. print(frameWidth, frameHeight, inWidth, inHeight)
  18. bboxes = []
  19. for faceRect in faceRects:
  20. cvRect = [
  21. int(faceRect.left() * scaleWidth),
  22. int(faceRect.top() * scaleHeight),
  23. int(faceRect.right() * scaleWidth),
  24. int(faceRect.bottom() * scaleHeight),
  25. ]
  26. bboxes.append(cvRect)
  27. cv2.rectangle(
  28. frameDlibHog,
  29. (cvRect[0], cvRect[1]),
  30. (cvRect[2], cvRect[3]),
  31. (0, 255, 0),
  32. int(round(frameHeight / 150)),
  33. 4,
  34. )
  35. return frameDlibHog, bboxes
  36. if __name__ == "__main__":
  37. parser = argparse.ArgumentParser(description="Face detection")
  38. parser.add_argument("--video", type=str, default="", help="Path to video file")
  39. args = parser.parse_args()
  40. source = args.video
  41. hogFaceDetector = dlib.get_frontal_face_detector()
  42. outputFolder = "output-hog-videos"
  43. if not os.path.exists(outputFolder):
  44. os.makedirs(outputFolder)
  45. if source:
  46. cap = cv2.VideoCapture(source)
  47. outputFile = os.path.basename(source)[:-4] + ".avi"
  48. else:
  49. cap = cv2.VideoCapture(0, cv2.CAP_V4L)
  50. outputFile = "grabbed_from_camera.avi"
  51. hasFrame, frame = cap.read()
  52. vid_writer = cv2.VideoWriter(
  53. os.path.join(outputFolder, outputFile),
  54. cv2.VideoWriter_fourcc("M", "J", "P", "G"),
  55. 15,
  56. (frame.shape[1], frame.shape[0]),
  57. )
  58. frame_count = 0
  59. tt_dlibHog = 0
  60. while True:
  61. hasFrame, frame = cap.read()
  62. if not hasFrame:
  63. break
  64. frame_count += 1
  65. t = time.time()
  66. outDlibHog, bboxes = detectFaceDlibHog(hogFaceDetector, frame)
  67. tt_dlibHog += time.time() - t
  68. fpsDlibHog = frame_count / tt_dlibHog
  69. label = "DLIB HoG; FPS : {:.2f}".format(fpsDlibHog)
  70. cv2.putText(
  71. outDlibHog,
  72. label,
  73. (10, 50),
  74. cv2.FONT_HERSHEY_SIMPLEX,
  75. 1.3,
  76. (0, 0, 255),
  77. 3,
  78. cv2.LINE_AA,
  79. )
  80. cv2.imshow("Face Detection Comparison", outDlibHog)
  81. vid_writer.write(outDlibHog)
  82. if frame_count == 1:
  83. tt_dlibHog = 0
  84. k = cv2.waitKey(5)
  85. if k == 27:
  86. break
  87. cv2.destroyAllWindows()
  88. vid_writer.release()