Send image via serialport

General discussion about topics related to OpenMV.
Houkouha
Posts: 8
Joined: Sun Mar 31, 2019 6:18 pm

Send image via serialport

Postby Houkouha » Sun Mar 31, 2019 6:27 pm

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.
Last edited by Houkouha on Sun Apr 07, 2019 8:11 pm, edited 1 time in total.
User avatar
kwagyeman
Posts: 3060
Joined: Sun May 24, 2015 2:10 pm

Re: Send image via serialport

Postby kwagyeman » Sun Mar 31, 2019 6:30 pm

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.
Nyamekye,
Houkouha
Posts: 8
Joined: Sun Mar 31, 2019 6:18 pm

Re: Send image via serialport

Postby Houkouha » Sun Mar 31, 2019 6:42 pm

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.
User avatar
kwagyeman
Posts: 3060
Joined: Sun May 24, 2015 2:10 pm

Re: Send image via serialport

Postby kwagyeman » Sun Mar 31, 2019 7:07 pm

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.
Nyamekye,
Houkouha
Posts: 8
Joined: Sun Mar 31, 2019 6:18 pm

Re: Send image via serialport

Postby Houkouha » Fri Apr 05, 2019 8:52 am

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

Code: Select all

sp.write("snap")
to

Code: Select all

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).
User avatar
kwagyeman
Posts: 3060
Joined: Sun May 24, 2015 2:10 pm

Re: Send image via serialport

Postby kwagyeman » Fri Apr 05, 2019 11:42 am

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')
Nyamekye,
Houkouha
Posts: 8
Joined: Sun Mar 31, 2019 6:18 pm

Re: Send image via serialport

Postby Houkouha » Fri Apr 05, 2019 12:08 pm

Oh, nice - b"snap" worked.

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

Code: Select all

with open("snapshot.jpg", "w") as f:
    f.write(img)
Specifically `f.write(img)`. TypeError: write() argument must be str, not bytes
User avatar
kwagyeman
Posts: 3060
Joined: Sun May 24, 2015 2:10 pm

Re: Send image via serialport

Postby kwagyeman » Fri Apr 05, 2019 12:13 pm

Change the "w" in open to "wb".
Nyamekye,
Houkouha
Posts: 8
Joined: Sun Mar 31, 2019 6:18 pm

Re: Send image via serialport

Postby Houkouha » Fri Apr 05, 2019 12:15 pm

kwagyeman wrote:
Fri Apr 05, 2019 12:13 pm
Change the "w" in open to "wb".
Holy-Moly, you're an angel. Thank you!
User avatar
kwagyeman
Posts: 3060
Joined: Sun May 24, 2015 2:10 pm

Re: Send image via serialport

Postby kwagyeman » Fri Apr 05, 2019 12:47 pm

You might be running the wrong python version if these were problems. 3 versus 2.7
Nyamekye,
Houkouha
Posts: 8
Joined: Sun Mar 31, 2019 6:18 pm

Re: Send image via serialport

Postby Houkouha » Sun Apr 07, 2019 7:46 pm

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).
User avatar
kwagyeman
Posts: 3060
Joined: Sun May 24, 2015 2:10 pm

Re: Send image via serialport

Postby kwagyeman » Sun Apr 07, 2019 8:02 pm

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.
Nyamekye,
Houkouha
Posts: 8
Joined: Sun Mar 31, 2019 6:18 pm

Re: Send image via serialport

Postby Houkouha » Sun Apr 07, 2019 8:05 pm

Is that also done by `Tools->Reset OpenMV Cam` in OpenMV IDE?
User avatar
kwagyeman
Posts: 3060
Joined: Sun May 24, 2015 2:10 pm

Re: Send image via serialport

Postby kwagyeman » Sun Apr 07, 2019 8:07 pm

No, that just does a hard reset of the board as if you pressed a reset switch.
Nyamekye,
Houkouha
Posts: 8
Joined: Sun Mar 31, 2019 6:18 pm

Re: Send image via serialport

Postby Houkouha » Sun Apr 07, 2019 8:22 pm

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

Code: Select all

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

Code: Select all

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?
User avatar
iabdalkader
Posts: 845
Joined: Sun May 24, 2015 3:53 pm

Re: Send image via serialport

Postby iabdalkader » Sun Apr 07, 2019 10:10 pm

Houkouha wrote:
Sun Apr 07, 2019 8:22 pm
One (hopefully last) question: what causes the device to be busy? Sometimes I can't send the Serial.write as it throws the error

Code: Select all

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

Code: Select all

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.

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 117 guests