face_detection_opencv_haar.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import argparse
  2. import os
  3. import time
  4. import cv2
  5. def detectFaceOpenCVHaar(faceCascade, frame, inHeight=300, inWidth=0):
  6. frameOpenCVHaar = frame.copy()
  7. frameHeight = frameOpenCVHaar.shape[0]
  8. frameWidth = frameOpenCVHaar.shape[1]
  9. if not inWidth:
  10. inWidth = int((frameWidth / frameHeight) * inHeight)
  11. scaleHeight = frameHeight / inHeight
  12. scaleWidth = frameWidth / inWidth
  13. frameOpenCVHaarSmall = cv2.resize(frameOpenCVHaar, (inWidth, inHeight))
  14. frameGray = cv2.cvtColor(frameOpenCVHaarSmall, cv2.COLOR_BGR2GRAY)
  15. faces = faceCascade.detectMultiScale(frameGray)
  16. bboxes = []
  17. for (x, y, w, h) in faces:
  18. x1 = x
  19. y1 = y
  20. x2 = x + w
  21. y2 = y + h
  22. cvRect = [
  23. int(x1 * scaleWidth),
  24. int(y1 * scaleHeight),
  25. int(x2 * scaleWidth),
  26. int(y2 * scaleHeight),
  27. ]
  28. bboxes.append(cvRect)
  29. cv2.rectangle(
  30. frameOpenCVHaar,
  31. (cvRect[0], cvRect[1]),
  32. (cvRect[2], cvRect[3]),
  33. (0, 255, 0),
  34. int(round(frameHeight / 150)),
  35. 4,
  36. )
  37. return frameOpenCVHaar, bboxes
  38. if __name__ == "__main__":
  39. parser = argparse.ArgumentParser(description="Face detection")
  40. parser.add_argument("--video", type=str, default="", help="Path to video file")
  41. args = parser.parse_args()
  42. source = args.video
  43. faceCascade = cv2.CascadeClassifier("models/haarcascade_frontalface_default.xml")
  44. outputFolder = "output-haar-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. 25,
  58. (frame.shape[1], frame.shape[0]),
  59. )
  60. frame_count = 0
  61. tt_opencvHaar = 0
  62. while True:
  63. hasFrame, frame = cap.read()
  64. if not hasFrame:
  65. break
  66. frame_count += 1
  67. t = time.time()
  68. outOpencvHaar, bboxes = detectFaceOpenCVHaar(faceCascade, frame)
  69. tt_opencvHaar += time.time() - t
  70. fpsOpencvHaar = frame_count / tt_opencvHaar
  71. label = "OpenCV Haar; FPS : {:.2f}".format(fpsOpencvHaar)
  72. cv2.putText(
  73. outOpencvHaar,
  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", outOpencvHaar)
  83. vid_writer.write(outOpencvHaar)
  84. if frame_count == 1:
  85. tt_opencvHaar = 0
  86. key = cv2.waitKey(5)
  87. if key == 27:
  88. break
  89. cv2.destroyAllWindows()
  90. vid_writer.release()