Hello,
I’ve a problem with imshow. The situation is as following.
I have a camera and write a class for grabbing images and saving them to a video. The Class also has a ring-buffer, so that the newest image can be gotten. I started a thread to update this class by calling procced method and get the latest image to main thread by calling get_latest method. But why doesn’t the window update automatically. So the codes worked well if I dont use multi-threading. Here are the codes.
class ResourceTest:
def __init__(self) -> None:
self.cam = Camera(1)
self.gen = self.cam.generate_image_all()
self.gas = GrabberAndSaver(self.gen,10,1,(1080,1440,3),cv2.VideoWriter('test.mp4',cv2.VideoWriter_fourcc(*'mp4v'),18,(1440,1080)))
cv2.namedWindow('resource test')
self.th = Thread(target=self.gas.proceed)
self.th.start()
while True:
im = self.gas.get_latest()[0,...,::-1]
#im = next(self.gen)[0,...,::-1]
cv2.imshow('resource test',im)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
class GrabberAndSaver:
cache = None
ptr_latest = 0
ptr_earliest = 0
def __init__(self,gen: Generator,cache_len:int,cam_num:int,im_shape:Union[list,tuple,np.ndarray],video_writer:Union[list,tuple]) -> None:
self.gen = gen
self.cache = np.zeros((cache_len,cam_num,*im_shape),dtype = np.uint8)
self.saver = Saver(video_writer)
def proceed(self):
c = 0
while True:
im = next(self.gen)
#self.saver.write(im[...,::-1])
c += 1
self._push(im)
if c == 150:
break
def _push(self,value:Any):
for i in range(len(value)):
self.cache[self.ptr_latest,i] = value[i]
if self.ptr_latest == len(self.cache) - 1:
self.ptr_latest = 0
else:
self.ptr_latest += 1
def get_latest(self):
return self.cache[self.ptr_latest-1]
Thanks for helping
Best wishes