face_detection_dlib_mmod.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import argparse
  2. import os
  3. import time
  4. import cv2
  5. import dlib
  6. def detectFaceDlibMMOD(detector, frame, inHeight=300, inWidth=0):
  7. frameDlibMMOD = frame.copy()
  8. frameHeight = frameDlibMMOD.shape[0]
  9. frameWidth = frameDlibMMOD.shape[1]
  10. if not inWidth:
  11. inWidth = int((frameWidth / frameHeight) * inHeight)
  12. scaleHeight = frameHeight / inHeight
  13. scaleWidth = frameWidth / inWidth
  14. frameDlibMMODSmall = cv2.resize(frameDlibMMOD, (inWidth, inHeight))
  15. frameDlibMMODSmall = cv2.cvtColor(frameDlibMMODSmall, cv2.COLOR_BGR2RGB)
  16. faceRects = detector(frameDlibMMODSmall, 0)
  17. print(frameWidth, frameHeight, inWidth, inHeight)
  18. bboxes = []
  19. for faceRect in faceRects:
  20. cvRect = [
  21. int(faceRect.rect.left() * scaleWidth),
  22. int(faceRect.rect.top() * scaleHeight),
  23. int(faceRect.rect.right() * scaleWidth),
  24. int(faceRect.rect.bottom() * scaleHeight),
  25. ]
  26. bboxes.append(cvRect)
  27. cv2.rectangle(
  28. frameDlibMMOD,
  29. (cvRect[0], cvRect[1]),
  30. (cvRect[2], cvRect[3]),
  31. (0, 255, 0),
  32. int(round(frameHeight / 150)),
  33. 4,
  34. )
  35. return frameDlibMMOD, 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. mmodFaceDetector = dlib.cnn_face_detection_model_v1(
  42. "models/mmod_human_face_detector.dat",
  43. )
  44. outputFolder = "output-mmod-videos"
  45. if not os.path.exists(outputFolder):
  46. os.makedirs(outputFolder)
  47. if source:
  48. cap = cv2.VideoCapture(source)
  49. outputFile = os.path.basename(source)[:-4] + ".avi"
  50. else:
  51. cap = cv2.VideoCapture(0, cv2.CAP_V4L)
  52. outputFile = "grabbed_from_camera.avi"
  53. hasFrame, frame = cap.read()
  54. vid_writer = cv2.VideoWriter(
  55. os.path.join(outputFolder, outputFile),
  56. cv2.VideoWriter_fourcc("M", "J", "P", "G"),
  57. 15,
  58. (frame.shape[1], frame.shape[0]),
  59. )
  60. frame_count = 0
  61. tt_dlibMmod = 0
  62. while True:
  63. hasFrame, frame = cap.read()
  64. if not hasFrame:
  65. break
  66. frame_count += 1
  67. t = time.time()
  68. outDlibMMOD, bboxes = detectFaceDlibMMOD(mmodFaceDetector, frame)
  69. tt_dlibMmod += time.time() - t
  70. fpsDlibMmod = frame_count / tt_dlibMmod
  71. label = "DLIB MMOD; FPS : {:.2f}".format(fpsDlibMmod)
  72. cv2.putText(
  73. outDlibMMOD,
  74. label,
  75. (10, 50),
  76. cv2.FONT_HERSHEY_SIMPLEX,
  77. 1.3,
  78. (0, 0, 255),
  79. 3,
  80. cv2.LINE_AA,
  81. )
  82. cv2.imshow("Face Detection Comparison", outDlibMMOD)
  83. vid_writer.write(outDlibMMOD)
  84. if frame_count == 1:
  85. tt_dlibMmod = 0
  86. k = cv2.waitKey(5)
  87. if k == 27:
  88. break
  89. cv2.destroyAllWindows()
  90. vid_writer.release()