How to control a while loop iteration in a class from a while loop outside it

你说的曾经没有我的故事 提交于 2021-01-07 00:03:04

问题


I am trying to read frames of a video leveraging a multithreading class. So, there is a while loop inside the class, which keeps reading the video. And there is a while loop in the main, which accesses the frames from the variables of that class, but what I found is, the outer while has no control over the inner while. It is just grabbing the current running frame from the inner while.

To experiment, I put a sleep and saw the loss of frames. I do not want that, I want the outer loop to get each and every frame (zero frame drop) whatever time the outer loop takes to do other operations in each iteration, while doing so, I still want to leverage multithreading.

The code:

from threading import Thread
import cv2, time
import numpy as np


class VideoStreamWidget(object):
    def __init__(self, src=0):
        self.capture = cv2.VideoCapture(src)
        # Start the thread to read frames from the video stream
        self.status = ''
        self.frame = ''
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()
        # self.count = 0


    def update(self):
        # Read the next frame from the stream in a different thread
        while True:
            if self.capture.isOpened():
                self.status, self.image = self.capture.read()
                self.frame = cv2.resize(self.image, (640, 480))
                # self.count += 1
            time.sleep(.01)

if __name__ == '__main__':

    path2save_results = r'D:\test_frames\multithreaded_lower\multithreaded_lower_'
    # With Multithreading enabled
    video_stream_widget1 = VideoStreamWidget(src=r'C:\Users\aa\Documents\Videos\ch6.asf')
    video_stream_widget2 = VideoStreamWidget(src=r'C:\Users\aa\Documents\Videos\ch4.asf')
    c1 = 0
    c2 = 0
    while True:
        # try:
        # start_time = time.time()

        # Added this sleep to clearly make out that interim frames are dropping out
        time.sleep(10)
        vflag, image_np = video_stream_widget1.status, video_stream_widget1.image
        if vflag:
            # new_image = cv2.resize(image_np, (640, 480))
            new_image = cv2.resize(image_np, (64, 48))
            cv2.imwrite(path2save_results + str(c1) + '.jpg', new_image)
            c1 += 1
            cv2.imshow('Lower', new_image)
            key = cv2.waitKey(1)
        print('C1 is: ', c1)

How to go about it?

Note: I have multiple (2) videos to run in parallel, as of now I'm doing on static videos stored in hard drive, but in the longer run they'd be fed in from two cameras located separately to capture the same object from different angles. I'll be processing one's feed, based on some object detection through the other one's feed, so one shouldn't go faster than the other.

来源:https://stackoverflow.com/questions/65121094/how-to-control-a-while-loop-iteration-in-a-class-from-a-while-loop-outside-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!