Question about a print()

Discussion related to "under the hood" OpenMV topics.
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Question about a print()

Postby gabriel_colon32 » Wed Jan 13, 2021 9:55 pm

Hi! I was getting 0.00% on the print that I highlighted and I was wondering why was that happening? For context, this is the call script of the RPC module.

Code: Select all

def get_frame_buffer_call_back(pixformat_str, framesize_str, cutthrough, silent):

    if not silent:
        print("Getting Remote Frame...")

    result = interface.call("jpeg_image_snapshot", "%s,%s" %
                            (pixformat_str, framesize_str))
    if result is not None:

        size = struct.unpack("<I", result)[0]

        img = bytearray(size)

        print(1)

        if cutthrough:

            # Fast cutthrough data transfer with no error checking.

            # Before starting the cut through data transfer we need to sync both the master and the

            # slave device. On return both devices are in sync.

            result = interface.call("jpeg_image_read")

            if result is not None:

                # GET BYTES NEEDS TO EXECUTE NEXT IMMEDIATELY WITH LITTLE DELAY NEXT.

                # Read all the image data in one very large transfer.

                interface.get_bytes(img, 5000)  # timeout

        else:

            # Slower data transfer with error checking.

            # Transfer 32 KB chunks.

            chunk_size = (1 << 15)

            if not silent:
                print("Reading %d bytes..." % size)

            for i in range(0, size, chunk_size):

                ok = False

                for j in range(3):  # Try up to 3 times.

                    result = interface.call(
                        "jpeg_image_read", struct.pack("<II", i, chunk_size))

                    if result is not None:

                        img[i:i+chunk_size] = result  # Write the image data.

                        if not silent:
                           
print("%.2f%%" % ((i * 100) / size))

Code: Select all


                        ok = True

                        break

                    if not silent:
                        print("Retrying... %d/2" % (j + 1))

                if not ok:

                    if not silent:
                        print("Error!")
                    return None
        return img
    else:
        if not silent:
            print("Failed to get Remote Frame!")

    return None
    
User avatar
kwagyeman
Posts: 4857
Joined: Sun May 24, 2015 2:10 pm

Re: Question about a print()

Postby kwagyeman » Wed Jan 13, 2021 10:20 pm

Can you paste the log?
Nyamekye,
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Wed Jan 13, 2021 10:27 pm

Sure!

Available Ports:

/dev/ttyACM0 : OpenMV Virtual Comm Port in FS Mode [USB VID:PID=1209:ABD1 SER=3839344B3137 LOCATION=1-1.1.2:1.0]
/dev/ttyAMA0 : ttyAMA0 [3f201000.serial]

Please enter a port name: /dev/ttyACM0

Getting Remote Frame...
Reading 21883 bytes...
0.00%
Getting Remote Frame...
Reading 24796 bytes...
0.00%
Getting Remote Frame...
Reading 27326 bytes...
0.00%
Getting Remote Frame...
Reading 28057 bytes...
0.00%
Getting Remote Frame...
Reading 29174 bytes...
0.00%
[INFO] stitching images...
[INFO] image stitching failed (1)
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Wed Jan 13, 2021 11:28 pm

I tried viewing the image but it's just pitch black
User avatar
kwagyeman
Posts: 4857
Joined: Sun May 24, 2015 2:10 pm

Re: Question about a print()

Postby kwagyeman » Wed Jan 13, 2021 11:40 pm

It’s failing to grab the frame. What’s the OpenMV Cam running? Is it running the script that is designed to pair with that script?
Nyamekye,
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Wed Jan 13, 2021 11:51 pm

Yes, it is running the callback script.

https://github.com/openmv/openmv/blob/m ... omputer.py
User avatar
kwagyeman
Posts: 4857
Joined: Sun May 24, 2015 2:10 pm

Re: Question about a print()

Postby kwagyeman » Thu Jan 14, 2021 12:00 am

So, this script is on the PC: https://github.com/openmv/openmv/blob/m ... _device.py

And this is on the camera: https://github.com/openmv/openmv/blob/m ... omputer.py

...

What operating system are you on? I can confirm it works fine on windows. The timeouts in the code are likely too short. Another forum user had an issue with this.
Nyamekye,
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Thu Jan 14, 2021 12:26 am

I'm running it on Raspbian on my Raspberry Pi 3B+. I also tried it on Windows and it worked fine. Seems weird to me.
User avatar
kwagyeman
Posts: 4857
Joined: Sun May 24, 2015 2:10 pm

Re: Question about a print()

Postby kwagyeman » Thu Jan 14, 2021 12:48 am

Ah...

Mmm, I'll have to debug that using a USB packet analyzer. Most likely the timing is off causing the library to not work. It's really hard to debug via USB.

If you can switch to the UART master? That should work.

The issue is probably related to how pyserial buffers data on the pi. I'll have to debug this manually and possibly make the library act different on the Pi.

...

To be clear, try editing this class:

https://github.com/openmv/openmv/blob/m ... pc.py#L370

The get_bytes method. You can add a print there. If you see that method timing out then just making the timeouts bigger should fix the issue.
Nyamekye,
User avatar
kwagyeman
Posts: 4857
Joined: Sun May 24, 2015 2:10 pm

Re: Question about a print()

Postby kwagyeman » Thu Jan 14, 2021 12:50 am

Nyamekye,
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Thu Jan 14, 2021 12:56 am

Got it! How big tho?
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Thu Jan 14, 2021 1:04 am

I tried making it bigger but it still did not work. I'll try changing to UART, but wasn't it unavailable yet for the Pi?
User avatar
kwagyeman
Posts: 4857
Joined: Sun May 24, 2015 2:10 pm

Re: Question about a print()

Postby kwagyeman » Thu Jan 14, 2021 1:12 am

The timeout is per 1KB of data. So, making it 0.02 is 20 ms to move 1KB of data.

Mmm...

Okay, copy this whole script to the SD card, but, then change the timeout lines here: https://github.com/openmv/openmv/blob/m ... pc.py#L542

timeout=timeout_ms -> timeout=timeout_ms*10

Verify the library is saved and then restart the camera. This will make the camera VCP timeout 10X longer. This will negatively affect performance for short data transfers but not affect longer ones.
Nyamekye,
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Thu Jan 14, 2021 1:34 am

Sadly, I got the same result :(
User avatar
kwagyeman
Posts: 4857
Joined: Sun May 24, 2015 2:10 pm

Re: Question about a print()

Postby kwagyeman » Thu Jan 14, 2021 10:45 am

Please make a bug on github. I'll debug this. Sorry this doesn't work.
Nyamekye,
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Thu Jan 14, 2021 3:25 pm

I will! No problem!

Is UART available for the Pi?
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Thu Jan 14, 2021 3:31 pm

I just realized; this happened when I started running it on python 3.

Is the rpc library supposed to be ran on Python2?
User avatar
kwagyeman
Posts: 4857
Joined: Sun May 24, 2015 2:10 pm

Re: Question about a print()

Postby kwagyeman » Thu Jan 14, 2021 4:32 pm

It should work on both. The only variable is the PySerial library. That is what actually does the USB stuff. My python code is rather generic.
Nyamekye,
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Thu Jan 14, 2021 6:40 pm

Understood!
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Thu Jan 14, 2021 10:02 pm

This is interesting... I ran the test scripts for the RPC in python2 and it ran perfectly. It seems there is a problem with running it on python3. Maybe what you mentioned about the serial connection.

I'll install it with pip3
gabriel_colon32
Posts: 36
Joined: Mon Oct 05, 2020 12:19 pm

Re: Question about a print()

Postby gabriel_colon32 » Thu Jan 14, 2021 10:51 pm

Still nothing :c
User avatar
kwagyeman
Posts: 4857
Joined: Sun May 24, 2015 2:10 pm

Re: Question about a print()

Postby kwagyeman » Fri Jan 15, 2021 2:16 pm

I'll test on my Pi within a week.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest