genicam / harvesters

Image Acquisition Library for GenICam-based Machine Vision System
Apache License 2.0
521 stars 90 forks source link

Image Acquisition fails after error in Python code #144

Closed neelsbrink closed 4 years ago

neelsbrink commented 4 years ago

Good day I am accessing a JAI camera using Python 3.6 and Harvesters. Every first time I access the camera, I get images, but as soon there is an error in my code ia = h.create_image_acquirer(0) gives an error and I can't access the camera again until I exit everything. I then have to access the camera using its own control toolbox. When I start my program again after that, I can access the camera again. What could be the cause?

thank you

kazunarikudo commented 4 years ago

@neelsbrink Hi. Thank you for trying out Harvester. Could you give me some more information, please? Which version of Harvester are you using? In addition, I would appreciate if you could give describe the detail with a simplified code snippet. /Kazunari

neelsbrink commented 4 years ago

Hi, I think it is V1.1.1. Initially, I can capture and display images with: ia = h.create_image_acquirer(0) If I make an error in the code, fix it and re-run my program I get [] as a result after: h.update_device_info_list() h.device_info_list print(h.device_info_list) followed by the rest of the error message:

NameError: name 'ia' is not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "J:/CameraControl/jai.py", line 37, in ia = h.create_image_acquirer(0) #camera nr 0 File "C:\ProgramData\Anaconda3\lib\site-packages\harvesters\core.py", line 2649, in create_image_acquirer device = self.device_info_list[list_index].create_device() IndexError: list index out of range thanks

kazunarikudo commented 4 years ago

If I make an error in the code

What is the error? That's what I would like to know.

neelsbrink commented 4 years ago

It can really be any error, like a typing error

kazunarikudo commented 4 years ago

Typing error?

neelsbrink commented 4 years ago

variable name misspelled, missing parenthesis, wrong image dimension specified in my output file

kazunarikudo commented 4 years ago

Are you talking about a use case where you run Python code interactively on IPython or something else? If so, it would be better to show me all lines from the beginning to the line where you notice the result turns something wrong to you. I mean, it should be better to do something as if you are demonstrating the instruction.

neelsbrink commented 4 years ago

I run the code in PyCharm: from harvesters.core import Harvester import matplotlib.pyplot as plt from numpy import * import numpy as np import cv2 from skimage import data, color from skimage.transform import rescale, resize, downscale_local_mean

h = Harvester()

output

outputavi='test.avi' fourcc = cv2.VideoWriter_fourcc(*'divx'); video = None

h.add_cti_file(r'C:/Program Files/MATRIX VISION/mvIMPACT Acquire/bin/x64/mvGenTLProducer.cti') print(h.cti_files) h.update_device_info_list() h.device_info_list print(h.device_info_list)

Specify an image acquirer if it doesn't exist:

try: ia except NameError: ia = h.create_image_acquirer(0) #camera nr 0 nrow=2058 ncol=2456 else: nrow = 2058 ncol = 2456

So here I get the message that "ia is not defined"

kazunarikudo commented 4 years ago

So here I get the message that "ia is not defined"

Because you have not defined ia; I guess you have a reason though. Why do you place the try-except block? Why don't you assign the returned object to ia?

neelsbrink commented 4 years ago

I use the 'try' because if ia already exists I get an error message with ia = h.create_image_acquirer(0)

kazunarikudo commented 4 years ago

Does your program reach out to a place where you do not need the ia? If so and if reuse the ia as an identifier, call ia.destroy(). The method releases all external resources such as a control privilege of the camera.

neelsbrink commented 4 years ago

I am using destroy now and it seems to work. But after my latest run the camera is not visible.: Traceback (most recent call last): File "J:/CameraControl/jai.py", line 28, in ia = h.create_image_acquirer(0) #camera nr 0 File "C:\ProgramData\Anaconda3\lib\site-packages\harvesters\core.py", line 2649, in create_image_acquirer device = self.device_info_list[list_index].create_device() IndexError: list index out of range

I have to connect to the camera via the JAI control toolkit first before I have comms with the camera in PyCharm and then it works fine.

kazunarikudo commented 4 years ago

As far as I hear your description, you seem to repeat running a specific set of code programmatically but on the other hand, you say you can make "mistakes" such as a typo or a syntax error as if you are running a program typing line by line. Excuse me, I have not fully understood how you are running the code and what the code looks like. Yes, you should (1) all lines that you run and (2) should describe how you run the code. You talked about PyCharm a bit but how do you use that? Are you running the code as a Python script file and run it from PyCharm? You said you can make a typo so do you line the code from PyCharm's Python Console not involving any Python script file? So far, I guess the reality should not be complicated but the information that you have provided is not enough.

neelsbrink commented 4 years ago

Dear Kazunari

Sorry for the delayed response, we were celebrating Easter. Also, apologies for the lack of proper info, I am absolutely new at Python. Thank you for your patience. I run the code below in Pycharm as a whole as a script, not in console. If I do not communicate with the camera first using its own control toolbox, then h.update_device_info_list() does not detect the camera. As I said, if there is any mistake in the code, the command also does not detect the camera after I have corrected the mistake and I have to exit Pycharm. regards Neels

from harvesters.core import Harvester import matplotlib.pyplot as plt from numpy import * import numpy as np import cv2 from skimage import data, color from skimage.transform import rescale, resize, downscale_local_mean

h = Harvester()

output

outputavi='test.avi' fourcc = cv2.VideoWriter_fourcc(*'divx'); #divx,mp4v en avi werk video = None h.add_cti_file(r'C:/Program Files/MATRIX VISION/mvIMPACT Acquire/bin/x64/mvGenTLProducer.cti') print(h.cti_files) h.update_device_info_list() h.device_info_list print(h.device_info_list)

ia = h.create_image_acquirer(0) #camera nr 0

nrow=2058 ncol=2456 nrow_out=int(nrow/2) ncol_out=int(ncol/2) video = cv2.VideoWriter(outputavi, fourcc, 15, (ncol_out,nrow_out), False)

Start acquisition

ia.start_image_acquisition() k=0 cv2.namedWindow('win1',cv2.WINDOW_FREERATIO) cv2.namedWindow('win2',cv2.WINDOW_FREERATIO)

cv2.namedWindow('win2')

cv2.resizeWindow('win1',420,420) cv2.resizeWindow('win2',420,420) cv2.moveWindow('win1',10,500) cv2.moveWindow('win2',500,500) while True: with ia.fetch_buffer() as buffer: component = buffer.payload.components[0] image = component.data.reshape(nrow, ncol) if(k<50):

        II = (255 * resize(image, (nrow_out,ncol_out),

mode='reflect')).astype(uint8) video.write(II) k=k+1 cv2.imshow('win1',image) cv2.imshow('win2', II)

    if k==1:
        cv2.selectROI('win2',II)

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

ia.destroy() cv2.destroyAllWindows() ia.stop_image_acquisition() h.reset() video.release()

On Thu, Apr 9, 2020 at 5:38 PM Kazunari Kudo notifications@github.com wrote:

As far as I hear your description, you seem to repeat running a specific set of code programmatically but on the other hand, you say you can make "mistakes" such as a typo or a syntax error as if you are running a program typing line by line. Excuse me, I have not fully understood how you are running the code and what the code looks like. Yes, you should (1) all lines that you run and (2) should describe how you run the code. You talked about PyCharm a bit but how do you use that? Are you running the code as a Python script file and run it from PyCharm? You said you can make a typo so do you line the code from PyCharm's Python Console not involving any Python script file? So far, I guess the reality should not be complicated but the information that you have provided is not enough.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/genicam/harvesters/issues/144#issuecomment-611596485, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKBNQCNEMS5JAUKRBZLGMDRLXTXRANCNFSM4METKXEA .

kazunarikudo commented 4 years ago

@neelsbrink Hi.

If I do not communicate with the camera first using its own control toolbox, then h.update_device_info_list() does not detect the camera.

So do you mean if you use your control toolbox then the device_info_list is filled up with camera information? In addition, could you tell me what your control toolbox is, please? /Kazunari

neelsbrink commented 4 years ago

Hi

After I have connected to the camera via the JAI control tool and closed the tool, and I go back to PyCharm and run my script, then it is filled with camera information. I am using the Jai Camera Control Tool (Ver 3.0.7.29451) with a BM 500 GE Jai camera. Neels

On Thu, Apr 16, 2020 at 2:30 AM Kazunari Kudo notifications@github.com wrote:

@neelsbrink https://github.com/neelsbrink Hi.

If I do not communicate with the camera first using its own control toolbox, then h.update_device_info_list() does not detect the camera.

So do you mean if you use your control toolbox then the device_info_list is filled up with camera information? In addition, could you tell me what your control toolbox is, please? /Kazunari

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/genicam/harvesters/issues/144#issuecomment-614346314, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKBNQB7JYZUST5RGUNGP5TRMZGSRANCNFSM4METKXEA .

kazunarikudo commented 4 years ago

@neelsbrink

and I go back to PyCharm and run my script, then it is filled with camera information.

Do you mean the list is filled up at the moment? And would you face an issue after that? If so, could you show me the traceback message here? You seemingly would face an issue and then you see nothing in the camera info list, don't you?

This should not be the answer but I will show you a part of pseudo-code as below:

with Harvester() as h:
    #output
    outputavi='test.avi'
    fourcc = cv2.VideoWriter_fourcc(*'divx'); #divx,mp4v en avi werk
    video = None

    #
    h.add_file(r'C:/Program Files/MATRIX VISION/mvIMPACT
    Acquire/bin/x64/mvGenTLProducer.cti')
    print(h.cti_files)

    #
    h.update()
    print(h.device_info_list)

    with h.create_image_acquirer(0) as ia:  #camera nr 0
        #
        nrow=2058
        ncol=2456
        nrow_out=int(nrow/2)
        ncol_out=int(ncol/2)
        video = cv2.VideoWriter(outputavi, fourcc, 15, (ncol_out,nrow_out), False)

        #Start acquisition
        ia.start_acquisition()

        k=0
        cv2.namedWindow('win1',cv2.WINDOW_FREERATIO)
        cv2.namedWindow('win2',cv2.WINDOW_FREERATIO)
        #cv2.namedWindow('win2')
        cv2.resizeWindow('win1',420,420)
        cv2.resizeWindow('win2',420,420)
        cv2.moveWindow('win1',10,500)
        cv2.moveWindow('win2',500,500)

        while True:
            with ia.fetch_buffer() as buffer:
                component = buffer.payload.components[0]
                image = component.data.reshape(nrow, ncol)
                if(k<50):

                    II = (255 * resize(image, (nrow_out,ncol_out),
        mode='reflect')).astype(uint8)
                    video.write(II)
                k=k+1
                cv2.imshow('win1',image)
                cv2.imshow('win2', II)

                if k==1:
                    cv2.selectROI('win2',II)

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

        # Stop image acquisition:
        ia.stop_acquisition()
        cv2.destroyAllWindows()
        video.release()

Note that you are not preparing a case where you'd get an exception by accident. In addition, you were calling ia.destroy() before calling ia.stop_acquisition(). It is totally fine to do so because the destroymethod should call the stop_acquisition by itself but note that the code you write implies a wrong intention to the readers. PS: Try to update Harvester to the latest version first even though it does not relate to the issue you are facing.

neelsbrink commented 4 years ago

Thanks, somehow now I don't get an empty list anymore upon

h.update_device_info_list()

even if I made an error in the code. It is now only empty the very first time I try to connect in PyCharm. I still have to use the Jai Control tool first, close it and then run my script. Only then can I connect to the camera. Maybe it is a Jai thing

regards

On Fri, Apr 17, 2020 at 2:11 AM Kazunari Kudo notifications@github.com wrote:

@neelsbrink https://github.com/neelsbrink

and I go back to PyCharm and run my script, then it is filled with camera information.

Do you mean the list is filled up at the moment? And would you face an issue after that? If so, could you show me the traceback message here? You seemingly would face an issue and then you see nothing in the camera info list, don't you?

This should not be the answer but I will show you a part of pseudo-code as below:

with Harvester() as h:

output

outputavi='test.avi'
fourcc = cv2.VideoWriter_fourcc(*'divx'); #divx,mp4v en avi werk
video = None

#
h.add_file(r'C:/Program Files/MATRIX VISION/mvIMPACT
Acquire/bin/x64/mvGenTLProducer.cti')
print(h.cti_files)

#
h.update()
h.device_info_list
print(h.device_info_list)

with h.create_image_acquirer(0) as ia:  #camera nr 0
    #
    nrow=2058
    ncol=2456
    nrow_out=int(nrow/2)
    ncol_out=int(ncol/2)
    video = cv2.VideoWriter(outputavi, fourcc, 15, (ncol_out,nrow_out), False)

    #Start acquisition
    ia.start_acquisition()

    k=0
    cv2.namedWindow('win1',cv2.WINDOW_FREERATIO)
    cv2.namedWindow('win2',cv2.WINDOW_FREERATIO)
    #cv2.namedWindow('win2')
    cv2.resizeWindow('win1',420,420)
    cv2.resizeWindow('win2',420,420)
    cv2.moveWindow('win1',10,500)
    cv2.moveWindow('win2',500,500)

    while True:
        with ia.fetch_buffer() as buffer:
            component = buffer.payload.components[0]
            image = component.data.reshape(nrow, ncol)
            if(k<50):

                II = (255 * resize(image, (nrow_out,ncol_out),
    mode='reflect')).astype(uint8)
                video.write(II)
            k=k+1
            cv2.imshow('win1',image)
            cv2.imshow('win2', II)

            if k==1:
                cv2.selectROI('win2',II)

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

    # Stop image acquisition:
    ia.stop_acquisition()
    cv2.destroyAllWindows()
    video.release()

Note that you are not preparing a case where you'd get an exception by accident. In addition, you were calling ia.destroy() before calling ia.stop_acquisition(). It is totally fine to do so because the destroymethod should call the stop_acquisition by itself but note that the code you write implies a wrong intention to the readers.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/genicam/harvesters/issues/144#issuecomment-614962444, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKBNQD377XXMM2YPDZ7FP3RM6NB3ANCNFSM4METKXEA .

kazunarikudo commented 4 years ago

@neelsbrink Congratulations. Maybe you should know that there is an idea that is widely used by GenICam compliant machine vision standards. That is "control privilege". There are several types of privileges but in most cases, an application exclusively controls a device. While the device is exclusively controlled, it can't answer to other applications. I guess you were in the situation. Anyway, congratulations again and I hope you enjoy to keep working with Harvester. /Kazunari

neelsbrink commented 4 years ago

Thanks for your replies and advice

On Fri, 17 Apr 2020, 16:42 Kazunari Kudo notifications@github.com wrote:

@neelsbrink https://github.com/neelsbrink Congratulations. Maybe you should know that there is an idea that is widely used by GenICam compliant machine vision standards. That is "control privilege". There are several types of privileges but in most cases, an application exclusively controls a device. While the device is exclusively controlled, it can't answer to other applications. I guess you were in the situation. Anyway, congratulations again and I hope you enjoy to keep working with Harvester. /Kazunari

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/genicam/harvesters/issues/144#issuecomment-615283837, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKBNQC77TGTKKYFXPF6HCLRNBTFZANCNFSM4METKXEA .

rishabh-maker commented 1 month ago

Hello kazunarikudo,

from the above reference , Please help me for harvester coding in python. I have Omron GigE camera (STC-MCS202POE) to acquire image in python file . So far from the harvester document , have connect the .cti file to code , but still no image buffer recieved yet , also frame per second is 0.0. In the Omron SDK the image acquisition is clearly visible.

Here is my code


import sys from PyQt5.QtWidgets import QApplication import cv2 from harvesters.core import Harvester from harvesters.core import ImageAcquirer from genicam.gentl import TimeoutException from harvesters.util.pfnc import mono_location_formats h = Harvester()

h.add_file('C:\Program Files (x86)\Common Files\OMRON_SENTECH\GenTL\v1_5\StGenTL_MD_VC141_v1_5_x64.cti')

h.add_file('D:\Extra\iui\StGenTL_MD_VC141_v1_5_x64.cti') h.files h.update() print(h.device_info_list) ia=h.create(0)

buffer1=ia.try_fetch(timeout=3) if buffer1:

ia.start() buffer = ia.fetch() buffer.queue() img = buffer.payload.components[0].data img = img.reshape(buffer.payload.components[0].height, buffer.payload.components[0].width) img_copy = img.copy() img_copy = cv2.cvtColor(img, cv2.COLOR_BayerRG2RGB) cv2.namedWindow("window", cv2.WINDOW_KEEPRATIO | cv2.WINDOW_NORMAL) cv2.imshow("window", img_copy) fps = ia.statistics.fps print("FPS: ", fps)

if cv2.waitKey(0) == ord('q'): cv2.destroyAllWindows() else: print("No buffer found..!!")


All I got the message in console is-

[{'access_status': 1, 'displayname': 'STC-MCS202POE(23K7797)', 'id': 'D4:7C:44:31:0C:68', 'model': 'STC-MCS202POE', 'parent': <genicam.gentl.Interface; proxy of <Swig Object of type 'std::shared_ptr< GenTLCpp::TLInterface > *' at 0x000001ABCA6B8CC0> >, 'serial_number': '23K7797', 'thisown': True, 'tl_type': 'GEV', 'user_defined_name': '', 'vendor': 'OMRON SENTECH', 'version': '22.60.2(22.60)'}] No buffer found..!!