pipeline.py 1.2 KB

12345678910111213141516171819202122232425262728
  1. import cv2
  2. from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
  3. import numpy as np
  4. def slice_clips(segments, root, fps=2):
  5. for path, classes in segments.items():
  6. for cls, ts in classes.items():
  7. for i, (t1, t2) in enumerate(ts):
  8. set_ = np.random.choice(['train', 'val'], p=[2 / 3, 1 / 3])
  9. # get all the still frames
  10. file_name, ext = path.split('.')
  11. target = f"{root}{file_name}_{cls}_{i + 1}.{ext}"
  12. print(f'target: {target}')
  13. ffmpeg_extract_subclip(f'{root}{path}', t1, t2, targetname=target)
  14. vidcap = cv2.VideoCapture(target)
  15. vidcap.set(cv2.CAP_PROP_FPS, fps)
  16. print(cv2.CAP_PROP_FPS)
  17. success, image = vidcap.read()
  18. count = 0
  19. while success:
  20. frame_path = f'{root}casino/{set_}/{cls}/{file_name}_{i}_{count + 1}.jpg'
  21. # print(frame_path)
  22. cv2.imwrite(frame_path, image) # save frame as JPEG file
  23. success, image = vidcap.read()
  24. # print('Read a new frame: ', success)
  25. count += 1