0

代码是来自 6 个不同相机的简单图像抓取 python 代码。代码在开始时完美运行,但在某个时间后随机在其中一个摄像头抓取超时异常被抛出。该错误与任何特定相机无关。

#MV record module
import cv2
import time
import os
import threading
from pypylon import pylon
#for recording time limit
import queue
import shutil
import datetime
            
class mvrecordingObj:
    def __init__(self,vid_save_loc,vid_fl_prefx,vid_duration):
        global grab_state,mv_logger
        grab_state=True
        self.clear_raw_frames()
        
    def init_cam(self):
        try:
            # Pypylon get camera by serial number
            top_cam_post = None
            bottom_cam_post = None
            top_cam_pre = None
            bottom_cam_pre = None
            left_cam_pre = None
            right_cam_pre = None
            for i in pylon.TlFactory.GetInstance().EnumerateDevices():
                if i.GetSerialNumber() == "23504114":
                    try:
                        top_cam_post = i
                    except Exception as e:
                        print("top post error i : "+str(e))
                        mv_logger.debug("top post error i : "+str(e))
                     ....
                     ....
            self.startframegrabing(top_cam_post,bottom_cam_post,top_cam_pre,bottom_cam_pre,left_cam_pre,right_cam_pre)
        except Exception as e:
            print("main() Exception : ", e)
    
    def startframegrabing(self,top_cam_post,bottom_cam_post,top_cam_pre,bottom_cam_pre,left_cam_pre,right_cam_pre):
        global FRAME_LOCATION
        try:
            image_no_counter = 1
            # VERY IMPORTANT STEP! To use Basler PyPylon OpenCV viewer you have to call .Open() method on you camera
            if top_cam_post is not None:
                try:
                    camera_top_cam_post = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateDevice(top_cam_post))
                    camera_top_cam_post.Open()
                    camera_top_cam_post.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
                    camera_top_cam_post.AcquisitionFrameRateEnable=True
                    camera_top_cam_post.AcquisitionFrameRate=50
                    camera_top_cam_post.ExposureTime=500                
                except Exception as e:
                    print("top post error : "+str(e))
            
                    ....
                    ....
            converter = pylon.ImageFormatConverter()
            converter.OutputPixelFormat = pylon.PixelType_BGR8packed
            converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
            while True:
                ############# Top Post ###################
                try:
                    t1 = int(time.time()*1000) 
                    if camera_top_cam_post.IsGrabbing():
                        grabResult = camera_top_cam_post.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
                    if grabResult.GrabSucceeded():
                        # Access the image data
                        image = converter.Convert(grabResult)
                        img = image.GetArray()
                        img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
                        #cv2.putText(img,str(time.time())[8:-5], (20, 80),
                                    #cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 2, cv2.LINE_AA)
                        cv2.imwrite("FRAMES_TOP_POST/IMG_"+str(image_no_counter)+".jpg",img)
                        if image_no_counter == 1:
                            shutil.move("FRAMES_TOP_POST/IMG_"+str(image_no_counter)+".jpg", "FRAMES_TOP_POST/TMP/IMG_"+str(image_no_counter)+".jpg")                
                    if(top_cam_post is not None):
                        grabResult.Release()
                    print("time for camera_top_cam_post frame : ", int(time.time()*1000) - t1) 
                except Exception as e:
                    print("Exception in top post is ", e)
                    mv_logger.debug("Exception in top post is "+str(e))                    
                    ....
                    ....

            if(camera_top_cam_post is not None):
                camera_top_cam_post.StopGrabbing()
                camera_top_cam_post.close()            
            ....
            ....
        except Exception as e:
            print("after while  Exception : ", e)
            mv_logger.debug("after while  Exception : "+str(e))
            threading.Timer(10.0,self.init_cam()).start()
            
    def clear_raw_frames(self):
        pass
            
        
    def run_module(self):
        #mv_logger.debug("MV Record Process Started")
        self.init_cam()
        #mv_logger.debug("Process ended")      

if __name__=="__main__":
    obj1=mvrecordingObj(os.getcwd(), "FNL_TST", 60)
    obj1.run_module()

预期输出 抓取和保存来自 6 个不同相机的图像

一段时间后随机出现以下错误 Grab timed out. : TimeoutException thrown (file 'InstantCameraImpl.h', line 1064)

任何人都可以帮助解决这个问题

4

1 回答 1

0

如果您按如下方式设置此参数,则会发生这种情况

camera.AcquisitionMode.SetValue('SingleFrame')

将其更改为

camera.AcquisitionMode.SetValue('Continuous')
于 2021-07-22T07:44:05.950 回答