0

Code is simple image grabing python code from 6 different camera. Code runs perfectly at start but after sometime randomly in one of the camera grabtimee out exception is thrown. The error is not related to any specific camera.

#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()

Expected output grabing and saving images from 6 different camera

The following Error is coming randomly after some time Grab timed out. : TimeoutException thrown (file 'InstantCameraImpl.h', line 1064)

Can anyone help with this problems solution

jfb
  • 45
  • 4

1 Answers1

0

Well this happens if you have set this parameter as follows

camera.AcquisitionMode.SetValue('SingleFrame')

Change it to

camera.AcquisitionMode.SetValue('Continuous')
Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
sundar
  • 1
  • 1