Multithreading issue for two separate sources

I am trying to run two functions at the same time. One function is supposed to detect scenes from local video and the other should detect scenes from my webcam. Then I make some processing with these scenes. Here is the code snippet:

scenes_1 = []
scenes_2 = []

scene_manager1 = SceneManager()
scene_manager1.add_detector(ContentDetector(threshold=30.0))

scene_manager2 = SceneManager()
scene_manager2.add_detector(ContentDetector(threshold=30.0))

def callback_1(image, frame_num):
  global scenes_1
  print("callback_1: Found a scene on video 1.")
  scenes_1 += [(image, frame_num)]

def callback_2(image, frame_num):
  global scenes_2
  print("callback_2: Found a scene on video 2.")
  scenes_2 += [(image, frame_num)]

def function_1():
    video_capture_0 = BrowserVideoCapture(src=0)

    while True:
        ret0,frame0=video_capture_0.read()
        if(ret0):
            scene_manager1.detect_scenes(frame0, callback=callback_1)
        else:
            break
    video_capture_0.release()

def function_2():

    video_capture_1 = cv2.VideoCapture("Our Story.mp4")

    while True:
        ret1,frame1=video_capture_1.read()
        if(ret1):
            scene_manager2.detect_scenes(frame1, callback=callback_2)
            if cv2.waitKey(34) & 0xFF == ord('q'):
                break
        else:
            break
    video_capture_1.release()
    cv2.destroyAllWindows()

t1=threading.Thread(target=function_1)
t2=threading.Thread(target=function_2)

t1.start()
t2.start()
t1.join()
t2.join()

while True:
  for im, frame_num in scenes_2:
    tar_image_example = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    tar_image_example = tar_image_example.astype(np.uint8)
    print("Found scene at frame %d in video 2." % frame_num)

  for im, frame_num in scenes_1:
    src_image_example = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    src_image_example = src_image_example.astype(np.uint8)
    print("Found scene at frame %d in video 1." % frame_num)

But I get the following error:

Exception in thread Thread-29:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "<ipython-input-25-1d65acb7c14c>", line 71, in function_2
    scene_manager2.detect_scenes(frame1, callback=callback_2)
  File "/usr/local/lib/python3.7/dist-packages/scenedetect/scene_manager.py", line 626, in detect_scenes
    timecode=0, fps=frame_source.get(cv2.CAP_PROP_FPS))
AttributeError: 'numpy.ndarray' object has no attribute 'get'

Exception in thread Thread-28:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "<ipython-input-25-1d65acb7c14c>", line 54, in function_1
    scene_manager1.detect_scenes(frame0, callback=callback_1)
  File "/usr/local/lib/python3.7/dist-packages/scenedetect/scene_manager.py", line 626, in detect_scenes
    timecode=0, fps=frame_source.get(cv2.CAP_PROP_FPS))
AttributeError: 'numpy.ndarray' object has no attribute 'get'

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-25-1d65acb7c14c> in <module>()
    112     tar_image_example = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    113     tar_image_example = tar_image_example.astype(np.uint8)
--> 114     print("Found scene at frame %d in video 2." % frame_num)
    115   for im, frame_num in scenes_1:

KeyboardInterrupt:

What am I doing wrong, please?

error is from your SceneManager class, which we cannot see.

What am I doing wrong, please?

you’re overcomplicating it

1 Like

also not (yet) a multithreading issue, but basic programming.

experienced programmers for very good reason avoid needless abstraction/complication. if newbies should learn anything, it’s that heuristic but not going “yes I need this shiny thing” and then justifying it by saying “architecture”.

my usual recommendation: