Send image via serialport

Hi. I’ve checked this topic for clues and found this GitHub example.

I’d like to ask for a tutorial how it should work. I’m a years long programmer, but haven’t touched python yet. All packages are installed, camera connected. The script (on PC) should get a captured frame from the camera.

Should be the camera connected via USB-microUSB cable or usbc?
How can I ensure the script doesnt return “Device or resource busy”?

I’ve inserted pyb.LED(3).on() before while within main.py on camera to see if it runs - LED didn’t light up.

Hi, can you provide an example for how you are running the script. The OpenMV Cam literally executes MicroPython code. We supply the IDE so that you can easily run scripts on it. Were you trying the LED via that?

As for the send images to the PC part. Getting the camera to do that is pretty easy. However, you need to decide on a serialization format for how the image will be sent in the byte stream and what script running on the PC side will receive the data.

That said, the whole point of the camera is not to send images off it however. It runs MicroPython and has methods on board so that you can process the image onboard.

Does it have to be done with the IDE? I’m simply running it from terminal with ‘python3’. I used what I linked from the Github. Saved the code to the camera main.py and the commented part on PC and ran it.

Um, you can run code from the PC on the camera. That is fine. However, it’s hard to debug because if there are any issues in the code it will just print a stack trace and exit. It’s better to use the IDE to get everything but the serial output working and then once that is done switch the serial output on. Doing work with cameras is quite visual. It’s important to be able to see the frame buffer.

The camera is running as intended (snapshot() placed before if(cmd…) to check it in OpenMV IDE).

Here is a screenshot of what’s happening. I had to change

sp.write("snap")

to

sp.write(input("snap"))

from this GitHub code, as it threw me an error - serial message must be bytes.

EDIT: So, I haven’t changed anything and it suddenly created the file snapshot.jpg, however its empty (0 bytes).

Hi, the string “snap” is suppose to be sent to the camera. Not to the screen asking the user. If the message must be in bytes change “snap” to b"snap" or ‘snap’.encode(‘utf-8’)

Oh, nice - b"snap" worked.

Okay, so variable size returns cca 2200 every time I run the code. The error is on:

with open("snapshot.jpg", "w") as f:
    f.write(img)

Specifically _f.write(img)_. TypeError: write() argument must be str, not bytes

Change the “w” in open to “wb”.

Holy-Moly, you’re an angel. Thank you!

You might be running the wrong python version if these were problems. 3 versus 2.7

Suddenly my camera doesn’t work. I’ve solved the issue with it sending the image. Now if I connect the camera to the PC (Ubuntu) I can open the camera folder, but can’t open the file within it (main.py) - Either the file does not exist or you have no permission to it (I always had permission). The camera works in OpenMV IDE still, if I run the saved code with the Play button in left bottom, the camera runs. However, I cant open the main.py, nor save a file to the camera (Tools → Save open script to camera). What can I do about it?

The camera after plugging in flashes in green a couple of times, one white, then multiple red (as I remember it lit up for some seconds in red, didn’t flash).

Hmm, I’ve seen similar things before due to weird file permissions. Um, format the onboard drive. Then save a new main.py to the camera using OpenMV IDE.

You can format the uPy drive in Linux or just reflash the whole camera firmware to fix this.

Is that also done by Tools->Reset OpenMV Cam in OpenMV IDE?

No, that just does a hard reset of the board as if you pressed a reset switch.

Solved. If someone browsed for an answer: Clicking on bottom line in IDE on Firmware version, remove filesytem- Yes, blahblah, solves it.

One (hopefully last) question: what causes the device to be busy? Sometimes I can’t send the Serial.write as it throws the error

[Errno 16] could not open port /dev/ttyACM0: [Errno 16] Device or resource busy: '/dev/ttyACM0'

or

serial.serialutil.SerialException: read failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

Why is it busy? The red led for saving is turned off, the camera is running, no other device is connected to the PC. Can I somehow wait for the camera to get ready?

If the IDE is connected to the camera you can’t use the serial port.

I am working on a GUI project with Qt5 and I want to connect Arduino Portent with GUI on Windows via USB connection. I want to send some parameters and receive an image from Arduino, I tried with USB_VCP example but I do not receive image. I can send bits but not an image. If I set “timeout: none” then the program is in step 7. Is there any better way to display the image in the user program?


If you are using Qt then you should just take the IDE code and integrate that into your app. The IDE has all the hooks in it already to talk to the camera. The openmv plugin is under qtcreator/SRC/plugins/openmv

I tried with OpenMV Cam H7 Plus and this code works but with Arduino Portenta it doesn’t work.

The Portenta is really new right now. Please submit bug issues on what’s not working. The debug protocol definitely should work however.