basler / pypylon

The official python wrapper for the pylon Camera Software Suite
http://www.baslerweb.com
BSD 3-Clause "New" or "Revised" License
550 stars 207 forks source link

Read operation on device '2676:ba02:2:4:44' failed: 'The requested operation timed out.' : TimeoutException thrown (file 'PylonUsbDevice.cpp', line 778) #422

Open pratiksparwatwar opened 2 years ago

pratiksparwatwar commented 2 years ago

I am using two basler acA1300-200uc camera and two Flir cameras. Running one script for Basler camera in sequence and other script for Flir. I am using Powered USB Hub in between Computer and Cameras.

In two days time, Consistently I am getting the following error. Read operation on device '2676:ba02:2:4:44' failed: 'The requested operation timed out.' : TimeoutException thrown (file 'PylonUsbDevice.cpp', line 778) in one of the two basler cameras

Following is the script I am using for frame capturing

`from tendo import singleton me = singleton.SingleInstance()

import cv2 import time import os import threading import logging from pypylon import pylon import traceback import multiprocessing as mp

for recording time limit

import queue import shutil import datetime import mysql.connector from mysql.connector import Error

grab_state=True mv_logger = None

import pymysql

DB credentials

db_user = 'root' db_pass = 'insightzz123' db_host = 'localhost'

processID = os.getpid() print("This process has the PID", processID)

def update_cam_status(cam_pos, status): try: db_update = pymysql.connect(host=db_host, # your host, usually localhost user=db_user, # your username passwd=db_pass, # your password db="ASTA_DB") cur = db_update.cursor() query = "UPDATE CAM_HEALTH_TABLE set ACTIVE = '"+ status + "' where CAM_POSITION = '" + cam_pos +"'" cur.execute(query) db_update.commit() cur.close()

print(data_set)

except Exception as e:
    print('Exception : ',e)
    cur.close()

class NumpyMySQLConverter(mysql.connector.conversion.MySQLConverter): """ A mysql.connector Converter that handles Numpy types """

def _float32_to_mysql(self, value):
    return float(value)

def _float64_to_mysql(self, value):
    return float(value)

def _int32_to_mysql(self, value):
    return int(value)

def _int64_to_mysql(self, value):
    return int(value)

config = { 'user': 'root', 'host': 'localhost', 'password': 'insightzz123', 'database': 'ASTA_DB' }

def updateProcessId(processId): try: mySQLconnection = mysql.connector.connect(**config) mySQLconnection.set_converter_class(NumpyMySQLConverter) sql_update_query = "UPDATE PROCESS_ID_TABLE set PROCESS_ID = " + \ str(processId) + " where PROCESS_NAME = 'ALL_CAM'" cursor = mySQLconnection.cursor() result = cursor.execute(sql_update_query)

print("Update Result is ", result)

    mySQLconnection.commit()
except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    # closing database connection.
    if(mySQLconnection .is_connected()):
        mySQLconnection.close()

class mvrecordingObj: global grab_state,mv_logger logging.basicConfig(filename="MV_RECORDACT.log",filemode='a',format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") mv_logger=logging.getLogger("MV_RECORDACT") mv_logger.setLevel(logging.DEBUG) mv_logger.debug("CODE STARTED")
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
        all_cam_list_dict = {}
        all_cam_list = []
        for i in pylon.TlFactory.GetInstance().EnumerateDevices():
            if i.GetSerialNumber() == "23571608":
                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))
            elif i.GetSerialNumber() == "23541722":
                try:
                    bottom_cam_post = i
                except Exception as e:
                    print("bottom post error i : "+str(e))
                    mv_logger.debug("bottom 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)
        mv_logger.debug("main() Exception : "+str(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:
        # 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=25
                camera_top_cam_post.ExposureTime=300                
            except Exception as e:
                print("top post error : "+str(e))
                mv_logger.debug("top post error : "+str(e))

        if bottom_cam_post is not None:
            try:
                camera_bottom_cam_post = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateDevice(bottom_cam_post))
                camera_bottom_cam_post.Open()
                camera_bottom_cam_post.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
                camera_bottom_cam_post.AcquisitionFrameRateEnable=True
                camera_bottom_cam_post.AcquisitionFrameRate=25
                camera_bottom_cam_post.ExposureTime=280
            except Exception as e:
                print("bottom post error : "+str(e))
                mv_logger.debug("bottom post error : "+str(e))

        converter = pylon.ImageFormatConverter()
        converter.OutputPixelFormat = pylon.PixelType_BGR8packed
        converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned

        post_top_once1 = True
        post_top_once2 = True

        post_bottom_once1 = True
        post_bottom_once2 = True

        while True:
            ############# Top Post ###################
            try:
                t1 = int(time.time()*1000) 
                if camera_top_cam_post.IsGrabbing():
                    grabResult = camera_top_cam_post.RetrieveResult(1000, 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.imshow("img", img)
                    #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_"+".jpg",img)
                    shutil.move("FRAMES_TOP_POST/IMG_"+".jpg", "FRAMES_TOP_POST/TMP/IMG_"+".jpg")                
                if(top_cam_post is not None):
                    grabResult.Release()
                #print("time for camera_top_cam_post frame : ", int(time.time()*1000) - t1) 
                post_top_once1 = True
                if post_top_once2:
                    update_cam_status("TOP_POST","YES")
                    post_top_once2 = False
            except Exception as e:
                if post_top_once1:
                    post_top_once2 = True
                    print("Exception in top post is ", e)
                    mv_logger.debug("Exception in top post is "+str(e))  
                    update_cam_status("TOP_POST","NO")
                    post_top_once1 = False

            ############# Bottom Post ###################
            try:
                t1 = int(time.time()*1000) 
                if camera_bottom_cam_post.IsGrabbing():
                    grabResult = camera_bottom_cam_post.RetrieveResult(1000, 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.imshow("img", img)
                    #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_BOTTOM_POST/IMG_"+".jpg",img)
                    shutil.move("FRAMES_BOTTOM_POST/IMG_"+".jpg", "FRAMES_BOTTOM_POST/TMP/IMG_"+".jpg")                
                if(bottom_cam_post is not None):
                    grabResult.Release()
                #print("time for camera_top_cam_post frame : ", int(time.time()*1000) - t1) 
                post_bottom_once1 = True
                if post_bottom_once2:
                    update_cam_status("BOTTOM_POST","YES")
                    post_bottom_once2 = False
            except Exception as e:
                if post_bottom_once1:
                    post_bottom_once2 = True
                    print("Exception in bottom post is ", e)
                    mv_logger.debug("Exception in bottom post is "+str(e))  
                    update_cam_status("BOTTOM_POST","NO")
                    post_bottom_once1 = False

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        if(camera_top_cam_post is not None):
            camera_top_cam_post.StopGrabbing()
            camera_top_cam_post.close()            
        if(camera_bottom_cam_post is not None):
            camera_bottom_cam_post.StopGrabbing()
            camera_bottom_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")      

def run_once(f): def wrapper(*args, *kwargs): if not wrapper.has_run: wrapper.has_run = True return f(args, **kwargs) wrapper.has_run = False return wrapper

def call_error(cam_pos): print("Error in function my function "+str(cam_pos))

if name=="main": updateProcessId(processID)
obj1=mvrecordingObj(os.getcwd(), "FNL_TST", 60) obj1.run_module()`

SMA2016a commented 2 years ago

what action is needed to get the system in a good shape after issue have occurred?

1) Restarting the app enough? 2) camera power cycle needed? 3) PC Reboot?