Hi Stackoverflow Users,
I'm currently trying to perform a multithreaded frame extraction using OpenCV. I've made such multithreaded operations before on other tasks, but for some reason it doesnt seem to work on the frame extraction. This is my Code:
import cv2
import os
import face_recognition
from PIL import Image
import multiprocessing
try:
if not os.path.exists('frames'):
os.makedirs('frames')
except OSError:
print('Error: Creating directory of frames')
try:
if not os.path.exists('faces'):
os.makedirs('faces')
except OSError:
print('Error: Creating directory of faces')
def frame_extract_1():
currentFrame_extract = 1
video_capture = cv2.VideoCapture("DOLFACE_1.mp4")
while(True):
ret1, frame = video_capture.read()
if ret1 == False:
break
name = 'frames/frame_' + str(currentFrame_extract) + '.jpg'
print(f"Processor 1 extracted Frame {currentFrame_extract}, saving it as Frame_{currentFrame_extract}.jpg")
cv2.imwrite(name, frame)
currentFrame_extract += 4
video_capture.release()
cv2.destroyAllWindows()
def frame_extract_2():
currentFrame_extract = 2
video_capture = cv2.VideoCapture("DOLFACE_1.mp4")
while(True):
ret2, frame = video_capture.read()
if ret2 == False:
break
name = 'frames/frame_' + str(currentFrame_extract) + '.jpg'
print(f"Processor 2 extracted Frame {currentFrame_extract}, saving it as Frame_{currentFrame_extract}.jpg")
cv2.imwrite(name, frame)
currentFrame_extract += 4
video_capture.release()
cv2.destroyAllWindows()
def frame_extract_3():
currentFrame_extract = 3
video_capture = cv2.VideoCapture("DOLFACE_1.mp4")
while(True):
ret3, frame = video_capture.read()
if ret3 == False:
break
name = 'frames/frame_' + str(currentFrame_extract) + '.jpg'
print(f"Processor 3 extracted Frame {currentFrame_extract}, saving it as Frame_{currentFrame_extract}.jpg")
cv2.imwrite(name, frame)
currentFrame_extract += 4
video_capture.release()
cv2.destroyAllWindows()
def frame_extract_4():
currentFrame_extract = 4
video_capture = cv2.VideoCapture("DOLFACE_1.mp4")
while(True):
ret4, frame = video_capture.read()
if ret4 == False:
break
name = 'frames/frame_' + str(currentFrame_extract) + '.jpg'
print(f"Processor 4 extracted Frame {currentFrame_extract}, saving it as Frame_{currentFrame_extract}.jpg")
cv2.imwrite(name, frame)
currentFrame_extract += 4
video_capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
video_file_path = "DOLFACE_1.mp4"
frame_extractor_1 = multiprocessing.Process(target=frame_extract_1)
frame_extractor_2 = multiprocessing.Process(target=frame_extract_2)
frame_extractor_3 = multiprocessing.Process(target=frame_extract_3)
frame_extractor_4 = multiprocessing.Process(target=frame_extract_4)
frame_extractor_1.start()
frame_extractor_2.start()
frame_extractor_3.start()
frame_extractor_4.start()
frame_extractor_1.join()
frame_extractor_2.join()
frame_extractor_3.join()
frame_extractor_4.join()
Do you know what I am doing wrong? Every "Processor" creates the full Video, without skipping 4 Frames and letting the other Processors do the remaining 3.