Zdravim.
Snazim sa zrychlit moj program ktory detekuje objekty. Pouzivam Python 3.7 , opencv. Chcel som vytvorit novy thread pre nacitanie videa skrz vstup a druhy ktory by ho spracovaval. V povodnom by bezalo GUI. Neviem ci to robim dobre ale cele je to o dost pomalsie (bez threadingu = 0.03 s 0.7). Co robim zle? Pripadne ako to zrychlit?
#Camera Theard
class VideoWriterWidget(object):
def __init__(self, video_file_name, src=0):
# Create a VideoCapture object
self.frame_name = str(src)
self.video_file = video_file_name
self.video_file_name = video_file_name + '.avi'
self.cap = cv2.VideoCapture(src)
# Default resolutions of the frame are obtained (system dependent)
self.frame_width = int(self.cap.get(3))
self.frame_height = int(self.cap.get(4))
# Set up codec and output video settings
self.codec = cv2.VideoWriter_fourcc('M','J','P','G')
self.output_video = cv2.VideoWriter(self.video_file_name, self.codec, 30, (self.frame_width, self.frame_height))
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
# Start another thread to show/save frames
self.start_recording()
print('initialized {}'.format(self.video_file))
time.sleep(0.2)
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.cap.isOpened():
(self.status, self.frame) = self.cap.read()
def show_frame(self):
# Display frames in main program
globals().update(global_frame_for_process = self.frame)
def save_frame(self):
if self.status:
# Save obtained frame into video output file
#Tu volam druhu funckiu ktora riesi processing
self.output_video.write(self.frame)
def start_recording(self):
# Create another thread to show/save frames
def start_recording_thread():
while True:
try:
self.show_frame()
self.save_frame()
except AttributeError:
pass
self.recording_thread = Thread(target=start_recording_thread, args=())
self.recording_thread.daemon = True
self.recording_thread.start()
cele to potom volam s GUI video_writer_widget1 = VideoWriterWidget('Camera 1 start', src1)
Nie je mi jasne ci sa GUI spusta v tom istom threade alebo sa vytvara nove? Tiez ten thread vyzera akoby bezal "davkovo" teda spracuje 20 framov a na milisenku sa sekne a znova...
Ako to spravit tak aby to bezalo rychlejsie? Idem na to dobre alebo cele zle?