You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

preprocessing.py 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import cv2
  2. import numpy as np
  3. faceCascade = cv2.CascadeClassifier("haarcascades/haarcascade_frontalface_alt0.xml")
  4. # Read in and simultaneously preprocess video
  5. def read_video(path):
  6. cap = cv2.VideoCapture(path)
  7. fps = int(cap.get(cv2.CAP_PROP_FPS))
  8. video_frames = []
  9. face_rects = ()
  10. while cap.isOpened():
  11. ret, img = cap.read()
  12. if not ret:
  13. break
  14. gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
  15. roi_frame = img
  16. # Detect face
  17. if len(video_frames) == 0:
  18. face_rects = faceCascade.detectMultiScale(gray, 1.3, 5)
  19. # Select ROI
  20. if len(face_rects) > 0:
  21. for (x, y, w, h) in face_rects:
  22. roi_frame = img[y:y + h, x:x + w]
  23. if roi_frame.size != img.size:
  24. roi_frame = cv2.resize(roi_frame, (500, 500))
  25. frame = np.ndarray(shape=roi_frame.shape, dtype="float")
  26. frame[:] = roi_frame * (1. / 255)
  27. video_frames.append(frame)
  28. frame_ct = len(video_frames)
  29. cap.release()
  30. return video_frames, frame_ct, fps