'cv2 Stitcher and Thread Behaving Strange
I am trying to stitch 16 images using Stitcher from cv2 on RPi 4 8GB RAM, when I use it along with Thread it is not working Below is python script :
import cv2
import numpy as np
import os
import time
from threading import Thread
cv2.ocl.setUseOpenCL(True)
global images
images = []
imgreg_path = "/home/pi/shared/"
class t1:
def __init__(self):
self._running = True
def terminate(self):
self._running = False
def run(self):
global images
global stitcher
stitcher = cv2.Stitcher_create(mode=1)
print("Thread 1 running")
print(len(images))
(status, stitched) = stitcher.stitch(images)
if status == 0:
cv2.imwrite('/home/pi/Desktop/stitched.jpg', stitched)
print('Thread 1 Stitched')
images = []
else:
print('Thread 1 Not Stitched')
images = []
for i in range(2,18):
curImg = cv2.imread(imgreg_path+"image"+str(i)+".jpg")
images.append(curImg)
print(len(images))
task1 = t1()
thrd1 = Thread(target=task1.run)
thrd1.start()
while True:
time.sleep(5)
and OUTPUT
>>> %Run testsew.py
16
Thread 1 running
16
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/home/pi/Desktop/testsew.py", line 24, in run
(status, stitched) = stitcher.stitch(images)
cv2.error: OpenCV(4.1.0) /home/pi/opencv-python/opencv/modules/core/src/umatrix.cpp:454: error: (-215:Assertion failed) u != 0 in function 'create'
However it works perfectly without threading, below is python script :
import cv2
import numpy as np
import os
import time
from threading import Thread
cv2.ocl.setUseOpenCL(True)
imgreg_path = "/home/pi/shared/"
stitcher = cv2.Stitcher_create(mode=1)
images = []
print("Running")
for i in range(2,18):
curImg = cv2.imread(imgreg_path+"image"+str(i)+".jpg")
images.append(curImg)
print(len(images))
(status, stitched) = stitcher.stitch(images)
if status == 0:
cv2.imwrite('/home/pi/Desktop/stitched.jpg', stitched)
print('Stitched')
images = []
else:
print('Not Stitched')
images = []
and OUTPUT
>>> %Run testsew_read.py
Running
16
Stitched
Any help is appreciated to achieve stitching along with threading. Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|