optional processing or threadding

This commit is contained in:
Askill 2022-08-16 23:28:56 +02:00
parent 2a3de97c22
commit d4f4dbf993
1 changed files with 16 additions and 6 deletions

View File

@ -1,5 +1,6 @@
import multiprocessing import multiprocessing
import os import os
import queue
import threading import threading
import cv2 import cv2
@ -10,7 +11,7 @@ class VideoReader:
w = None w = None
h = None h = None
def __init__(self, config, setOfFrames=None): def __init__(self, config, setOfFrames=None, multiprocess=False):
videoPath = config["inputPath"] videoPath = config["inputPath"]
if videoPath is None: if videoPath is None:
raise Exception("ERROR: Video reader needs a videoPath!") raise Exception("ERROR: Video reader needs a videoPath!")
@ -18,8 +19,11 @@ class VideoReader:
self.lastFrame = 0 self.lastFrame = 0
# buffer data struct: # buffer data struct:
# buffer = Queue([(frameNumber, frame), ]) # buffer = Queue([(frameNumber, frame), ])
self.buffer = multiprocessing.Queue(config["videoBufferLength"]) self.multiprocess = multiprocess
# self.vc = cv2.VideoCapture(videoPath) if multiprocess:
self.buffer = multiprocessing.Queue(config["videoBufferLength"])
else:
self.buffer = queue.Queue(config["videoBufferLength"])
self.stopped = False self.stopped = False
self.getWH() self.getWH()
self.calcFPS() self.calcFPS()
@ -49,10 +53,16 @@ class VideoReader:
if listOfFrames is not None: if listOfFrames is not None:
self.listOfFrames = listOfFrames self.listOfFrames = listOfFrames
if self.listOfFrames is not None: if self.multiprocess:
self.thread = multiprocessing.Process(target=self.readFramesByList, args=()) if self.listOfFrames is not None:
self.thread = multiprocessing.Process(target=self.readFramesByList, args=())
else:
self.thread = multiprocessing.Process(target=self.readFrames, args=())
else: else:
self.thread = multiprocessing.Process(target=self.readFrames, args=()) if self.listOfFrames is not None:
self.thread = threading.Thread(target=self.readFramesByList, args=())
else:
self.thread = threading.Thread(target=self.readFrames, args=())
self.thread.start() self.thread.start()
def readFrames(self): def readFrames(self):