how to send image to server by post to http?

Discussion related to "under the hood" OpenMV topics.
librazxc
Posts: 5
Joined: Wed Jan 09, 2019 2:46 am

how to send image to server by post to http?

Postby librazxc » Wed Jan 09, 2019 7:19 pm

Dear,
I am so sorry to send this message to you.I want to ask for some advice.
I am working on a project related to gesture recognition. Currently, there are OpenMV3 and WIFI extensions. I want to capture images through OpenMV CAM, convert the images to binary data or base64 encoding, use wifi to access the server, and access the server through the browser. The stream post is sent to the server and the server returns the result. There are two problems here: The first one is how to get a frame of image obtained in sensor.snapshot() into binary data or base64 encoded data? The second problem is that the request format required by the server API is the POST request http method, and the OpenMV3 has no related requests module or urllib module. How to set the POST entity header in the OpenMV3?
This is the server API request:
1.HTTP method:POST
url :https://aip.baidubce.com/rest/2.0/image ... 5-15254242
2.Content-Type application/x-www-form-urlencoded
3.Header:Content-Type application/x-www-form-urlencoded
4.Body:image: base64 string

Thank you very much!
Looking forward to your reply!
User avatar
kwagyeman
Posts: 4236
Joined: Sun May 24, 2015 2:10 pm

Re: how to send image to server by post to http?

Postby kwagyeman » Thu Jan 10, 2019 12:03 pm

Hi, use this to encode:

http://docs.micropython.org/en/v1.9.3/p ... ascii.html

And call .compress() on the image to get a jpeg byte stream.

E.g.

Code: Select all

base63_data = ubinascii.b2a_base64(img.compress(quality=90))
Then transmit that data.
Nyamekye,
librazxc
Posts: 5
Joined: Wed Jan 09, 2019 2:46 am

Re: how to send image to server by post to http?

Postby librazxc » Thu Jan 10, 2019 7:19 pm

thanks so much. :D
Do you know how to post to http in the openmv ?just follow the request which i proposed.how to set the header about client.send()?
And,How to parse url?Is there a function in OpenMV just like Python function-urlopen() to parse urls?
User avatar
kwagyeman
Posts: 4236
Joined: Sun May 24, 2015 2:10 pm

Re: how to send image to server by post to http?

Postby kwagyeman » Thu Jan 10, 2019 11:07 pm

Ibrahim will have to answer this. The WiFi part I'm not strong with.
Nyamekye,
librazxc
Posts: 5
Joined: Wed Jan 09, 2019 2:46 am

Re: how to send image to server by post to http?

Postby librazxc » Fri Jan 11, 2019 3:53 am

I can't find him in the Members.Can you help me to invite him to see this question?
best wishes
User avatar
iabdalkader
Posts: 1190
Joined: Sun May 24, 2015 3:53 pm

Re: how to send image to server by post to http?

Postby iabdalkader » Fri Jan 11, 2019 1:32 pm

You just send it with socket.send(). This is not a high level library, it's just plain sockets. See the MJPEG example.
librazxc
Posts: 5
Joined: Wed Jan 09, 2019 2:46 am

Re: how to send image to server by post to http?

Postby librazxc » Sun Jan 13, 2019 7:43 pm

Hi,the MJPEG example is based on http,but my server address is based on https.
when i use the function:usocket.getaddrinfo(),it can't parse url correctly,there was an error in the OpenMV IDE,Oserror -1.
My URL is https://aip.baidubce.com/rest/2.0/image ... 5-15254242
I thought it just related to Https ssl encryption. Can this socket module solve it? I just want to realize that send an image to my specified https server via wifi or lan.
Do you have any other ideas for my purpose?
User avatar
iabdalkader
Posts: 1190
Joined: Sun May 24, 2015 3:53 pm

Re: how to send image to server by post to http?

Postby iabdalkader » Sun Jan 13, 2019 8:21 pm

librazxc wrote:
Sun Jan 13, 2019 7:43 pm
Hi,the MJPEG example is based on http,but my server address is based on https.
when i use the function:usocket.getaddrinfo(),it can't parse url correctly,there was an error in the OpenMV IDE,Oserror -1.
My URL is https://aip.baidubce.com/rest/2.0/image ... 5-15254242
I thought it just related to Https ssl encryption. Can this socket module solve it? I just want to realize that send an image to my specified https server via wifi or lan.
Do you have any other ideas for my purpose?
The socket module doesn't support SSL.
librazxc
Posts: 5
Joined: Wed Jan 09, 2019 2:46 am

Re: how to send image to server by post to http?

Postby librazxc » Mon Jan 14, 2019 2:44 am

Thanks.By the way,Is the ATwinC1500 hardware itself not supported SSL or just does not transplant the relevant ssl code in OpenMV3?
User avatar
iabdalkader
Posts: 1190
Joined: Sun May 24, 2015 3:53 pm

Re: how to send image to server by post to http?

Postby iabdalkader » Mon Jan 14, 2019 10:08 am

librazxc wrote:
Mon Jan 14, 2019 2:44 am
Thanks.By the way,Is the ATwinC1500 hardware itself not supported SSL or just does not transplant the relevant ssl code in OpenMV3?
No the hardware supports it, but currently there's no way to download certificates to the module flash.
Nezra
Posts: 11
Joined: Fri Apr 19, 2019 9:08 am

Re: how to send image to server by post to http?

Postby Nezra » Fri Apr 19, 2019 9:10 am

iabdalkader wrote:
Mon Jan 14, 2019 10:08 am
librazxc wrote:
Mon Jan 14, 2019 2:44 am
Thanks.By the way,Is the ATwinC1500 hardware itself not supported SSL or just does not transplant the relevant ssl code in OpenMV3?
No the hardware supports it, but currently there's no way to download certificates to the module flash.
Sorry to resurrect an old post.

Given the wifi shield uses the same atwinc1500 chip as both the Adafruit feather M0 and their standalone winc1500, couldn't this same concept here:

https://learn.adafruit.com/adafruit-fea ... rtificates

be used to flash the SSL certificates? or have there been sufficient modifications to the winc1500 firmware that would prevent this?
User avatar
iabdalkader
Posts: 1190
Joined: Sun May 24, 2015 3:53 pm

Re: how to send image to server by post to http?

Postby iabdalkader » Fri Apr 19, 2019 6:16 pm

Nezra wrote: Given the wifi shield uses the same atwinc1500 chip as both the Adafruit feather M0 and their standalone winc1500, couldn't this same concept here:

https://learn.adafruit.com/adafruit-fea ... rtificates

be used to flash the SSL certificates? or have there been sufficient modifications to the winc1500 firmware that would prevent this?
You could try to update the certificates with that (all the pins are broken out, including the debugging uart), but you should Not update the firmware because it needs to match the host driver (the driver OpenMV uses), otherwise it won't work with OpenMV after a firmware update.

I'll work on a way to update the certificates, but this may take some time.
User avatar
iabdalkader
Posts: 1190
Joined: Sun May 24, 2015 3:53 pm

Re: how to send image to server by post to http?

Postby iabdalkader » Sat Oct 26, 2019 7:29 am

Hi, just an update on this, we now support SSL with mod ussl (mbedtls) it's available in the latest beta release with mbedtls, additionally the WINC1500 host driver and firmware have been updated to the latest (v19.3.0/v19.6.1). I'm not sure if there's a way to add certificates, but the client seems to not validate them anyway.. Note if you want to use the the new release it requires a WINC1500 firmware update (included with the release).
sonsetc
Posts: 5
Joined: Fri Apr 24, 2020 6:24 pm

Re: how to send image to server by post to http?

Postby sonsetc » Fri Apr 24, 2020 6:48 pm

I'm trying to send images coninuously to a php by http POST and after two times, three four the camera reboot or disconnect. Not all the time in the same line nor in the same snapshot. I have tried many things. Here is the last code attached.
Could be a memory issue that I have to manage? It is a problem of the Openmv H7 that might be solved in the Plus version or it is the Wifi Shield or maybe the code?
Attachments
mainfull.py
(2.96 KiB) Downloaded 27 times
User avatar
iabdalkader
Posts: 1190
Joined: Sun May 24, 2015 3:53 pm

Re: how to send image to server by post to http?

Postby iabdalkader » Fri Apr 24, 2020 7:14 pm

sonsetc wrote:
Fri Apr 24, 2020 6:48 pm
I'm trying to send images coninuously to a php by http POST and after two times, three four the camera reboot or disconnect. Not all the time in the same line nor in the same snapshot. I have tried many things. Here is the last code attached.
Could be a memory issue that I have to manage? It is a problem of the Openmv H7 that might be solved in the Plus version or it is the Wifi Shield or maybe the code?
I can't say for sure without testing the code, and I can't test this code because I need a server to post the images. However, I noticed that there's a commented print line in the IRQ callback, you can't call any functions that allocate memory (including print statements) in an IRQ callback. Also, you use ping inside the callback it must be global...

Code: Select all

def callback(line):
    global ping
    ....
User avatar
kwagyeman
Posts: 4236
Joined: Sun May 24, 2015 2:10 pm

Re: how to send image to server by post to http?

Postby kwagyeman » Fri Apr 24, 2020 7:26 pm

Hi, we will have a rather robust demo for how to do good TCP/UDP connections soon in our upcoming RPC library.
Nyamekye,
sonsetc
Posts: 5
Joined: Fri Apr 24, 2020 6:24 pm

Re: how to send image to server by post to http?

Postby sonsetc » Sat Apr 25, 2020 9:47 am

iabdalkader wrote:
Fri Apr 24, 2020 7:14 pm
sonsetc wrote:
Fri Apr 24, 2020 6:48 pm
I'm trying to send images coninuously to a php by http POST and after two times, three four the camera reboot or disconnect. Not all the time in the same line nor in the same snapshot. I have tried many things. Here is the last code attached.
Could be a memory issue that I have to manage? It is a problem of the Openmv H7 that might be solved in the Plus version or it is the Wifi Shield or maybe the code?
I can't say for sure without testing the code, and I can't test this code because I need a server to post the images. However, I noticed that there's a commented print line in the IRQ callback, you can't call any functions that allocate memory (including print statements) in an IRQ callback. Also, you use ping inside the callback it must be global...

Code: Select all

def callback(line):
    global ping
    ....
Alright, thanks, I understand, but it was added at the end. I was trying the post code for a few days without the callback. I tried also many other options. Also trying to send with PUT instead POST.
The server and php is working right now for testing. You can use it. It is only for development. You only need to change the SSID and Pass in the py code.
I will try with the PUS version of the camera that we also bought and maybe we might try with a multipart POST method. I'm not sure but the last wifi shield is blue, not red that we also bought some month ago.
We are testing the camera for a wildlife conservation project (biodiversity monitoring). If we succedd we will buy 20-40 units in the first deployment and then 1000 units to cover the whole Amazon Rainforest.
sonsetc
Posts: 5
Joined: Fri Apr 24, 2020 6:24 pm

Re: how to send image to server by post to http?

Postby sonsetc » Sat Apr 25, 2020 9:49 am

kwagyeman wrote:
Fri Apr 24, 2020 7:26 pm
Hi, we will have a rather robust demo for how to do good TCP/UDP connections soon in our upcoming RPC library.
This will be great! Do you have an expected release date?
User avatar
kwagyeman
Posts: 4236
Joined: Sun May 24, 2015 2:10 pm

Re: how to send image to server by post to http?

Postby kwagyeman » Sat Apr 25, 2020 10:51 am

Next week. I'm finishing up the library right now. I just got TCP and UDP raw image transfer from one OpenMV Cam to another working. Wifi can nominally move between 1 to 2 Mbps. I am writing a script for the computer now and I'll have the ability to transfer images wirelessly to a desktop working soon.

Note, the library shows off a custom protocol I designed called RPC which let's you remote call a method on the OpenMV Cam. You are allowed to pass an arbitrary length bytearray to the procedure that can contain anything... and it will be delivered to that method on the OpenMV Cam. That method can then respond with an arbitrary length bytearray which will be delivered back to the caller. Both the argument bytearray and response can be variable length per call response.

Anyway, the code however shows off how to use sockets in a really robust way so if you don't like the protocol you can just lift code from it.

Finally, you can also just implement the protocol over another transport layer like http. However, it will work for raw sockets right out the box.
Nyamekye,
User avatar
CameraMan
Posts: 21
Joined: Thu Apr 09, 2020 3:41 pm

Re: how to send image to server by post to http?

Postby CameraMan » Sat Apr 25, 2020 11:13 am

I was working on a similar project using raspberry pi. What I did was use ffmpeg to pipe an image stream over UDP to a netcat listener on a server. You can stream and save these UDP streams once they get to your server. Here was my example for the Pi, you could replace ffmpeg with usocket on the OpenMV cam.

This is how I did it on the PI,

Code: Select all

ffmpeg -f v4l2 -video_size 1280x720 -re -i "0:0" -f mpegts -b:v 3M udp://YOURPUBLICIP:5555
On the Server

Code: Select all

nc -l -p 5555 | mplayer - -benchmark 
This example will just open up a live stream on your host server. If you want to say and stream you could do something like this:

Code: Select all

 nc -l -p 555 | tee  file_containing_the_video.mp4 | mplayer - -benchmark 
You can get creative on the server end to make it more robust.
-Chief Tinkering Officer
sonsetc
Posts: 5
Joined: Fri Apr 24, 2020 6:24 pm

Re: how to send image to server by post to http?

Postby sonsetc » Sat Apr 25, 2020 12:03 pm

CameraMan wrote:
Sat Apr 25, 2020 11:13 am
I was working on a similar project using raspberry pi. What I did was use ffmpeg to pipe an image stream over UDP to a netcat listener on a server. You can stream and save these UDP streams once they get to your server. Here was my example for the Pi, you could replace ffmpeg with usocket on the OpenMV cam.

This is how I did it on the PI,

Code: Select all

ffmpeg -f v4l2 -video_size 1280x720 -re -i "0:0" -f mpegts -b:v 3M udp://YOURPUBLICIP:5555
On the Server

Code: Select all

nc -l -p 5555 | mplayer - -benchmark 
This example will just open up a live stream on your host server. If you want to say and stream you could do something like this:

Code: Select all

 nc -l -p 555 | tee  file_containing_the_video.mp4 | mplayer - -benchmark 
You can get creative on the server end to make it more robust.
Good idea. I use the camera with a PIR sensor to be fully switched off to last for long time and when it is triggered it takes pictures, connect to the Wifi AP in a Raspberry PI 4 with Neural Nets to send the image for classification.
I was also considering also using the mpej_streamer.py example that works really fine and pick the pictures from the RPi. But it needs to run continuously in the other side (Rpi4) as the nc but seems to be a good solution. We might use php but better wget inside bash.Thanks
sonsetc
Posts: 5
Joined: Fri Apr 24, 2020 6:24 pm

Re: how to send image to server by post to http?

Postby sonsetc » Sat Apr 25, 2020 12:10 pm

kwagyeman wrote:
Sat Apr 25, 2020 10:51 am
Next week. I'm finishing up the library right now. I just got TCP and UDP raw image transfer from one OpenMV Cam to another working. Wifi can nominally move between 1 to 2 Mbps. I am writing a script for the computer now and I'll have the ability to transfer images wirelessly to a desktop working soon.

Note, the library shows off a custom protocol I designed called RPC which let's you remote call a method on the OpenMV Cam. You are allowed to pass an arbitrary length bytearray to the procedure that can contain anything... and it will be delivered to that method on the OpenMV Cam. That method can then respond with an arbitrary length bytearray which will be delivered back to the caller. Both the argument bytearray and response can be variable length per call response.

Anyway, the code however shows off how to use sockets in a really robust way so if you don't like the protocol you can just lift code from it.

Finally, you can also just implement the protocol over another transport layer like http. However, it will work for raw sockets right out the box.
Alright I will be checking. As I commented to CammeraMan, I will try with the example "mpeg_streamer.py" and run in the other side a wget or a php to read the images.
User avatar
kwagyeman
Posts: 4236
Joined: Sun May 24, 2015 2:10 pm

Re: how to send image to server by post to http?

Postby kwagyeman » Sat Apr 25, 2020 1:41 pm

My library will work fine for you then. You can choose the following interfaces:

WiFi - 1 to 2 Mbs in heavy wifi traffic area (using our WiFi shield)
UART - 7.5 Mbps
CAN - 1 Mbps
SPI - 20 to 80 Mbps
I2C - 1 Mbs
USB VCP - 12 Mbps

The library is self healing meaning that I designed it to be reliable if a part of the link dies. E.g. the library will timeout gracefully and then start working again once the link comes back - no lockups. We won't have an example script for the RaspberryPi right away... but, the general purpose PC control script should run on the Pi as it's just for sockets and USB. SPI/I2C/UART for the Pi will come later.

Um, so, to be honest, the lowest power thing to use for you would actually be the OpenMV Cam RPC via UART using a generic serial to RF adapter: https://www.amazon.com/Wireless-Modules ... B01L3N4RYW - https://www.dfrobot.com/product-57.html

These are much lower power than WiFi. You would give each camera one of them and then have my RPC code running on each camera. Then the RPi would use the RPC protocol over USB through the FTDI serial port. The range will be longer than WiFi too. However, I'm not sure how you handle multiple channels at once. Probably should research that.

Note: You might want to use better hardware than I linked to above. But, the idea is the same.
Nyamekye,
rkguy
Posts: 16
Joined: Tue Jul 21, 2015 10:59 pm

Re: how to send image to server by post to http?

Postby rkguy » Wed May 13, 2020 8:32 am

If I use a modem instead of a WiFi shield to upload images, do you suggest using POST to http or ftp, or something else? Given that you made the RPC tool, what is the easiest way to write something to get the image uploaded? I think you suggest using sockets but what are the basic steps between snapping an image and sending it? I presume I should use AT commands to open a socket, etc on the modem or is there a better way? How do I access the image to begin sending it in the first place?
User avatar
kwagyeman
Posts: 4236
Joined: Sun May 24, 2015 2:10 pm

Re: how to send image to server by post to http?

Postby kwagyeman » Wed May 13, 2020 12:28 pm

The tool I built works on the socket level layer. It's not FTP/HTTP/POST/etc.

If you want to use a higher level protocol you need to implement that on top of the sockets layer.

With the latest firmware various bugs with the WiFi shield have been fixed. So, you should find it very straight forward to use sockets to implement the above high level protocols. Standard Python code on how to use sockets to do the above protocols can be used.

...

Anyway, a thing to think about for your problem. So, If you just use the RPC library I made and put that on the remote server it's possible to get streaming working to the remote server. This is the easiest way. However, keep in mind that the RPC timeouts aren't very long so you might have to modify the library code.

Otherwise, if you want to implement a higher level protocol you just need to use python sockets to do it.

https://stackoverflow.com/questions/286 ... parameters

Not all the above is probably something you can do. But, most of it.

Anyway, as for sending image data... you should jpeg compress it and then append that to the end of the message.

https://stackoverflow.com/questions/150 ... pi-service
Nyamekye,
User avatar
kwagyeman
Posts: 4236
Joined: Sun May 24, 2015 2:10 pm

Re: how to send image to server by post to http?

Postby kwagyeman » Wed May 13, 2020 12:36 pm

Nyamekye,
User avatar
kwagyeman
Posts: 4236
Joined: Sun May 24, 2015 2:10 pm

Re: how to send image to server by post to http?

Postby kwagyeman » Wed May 13, 2020 1:01 pm

Yeah, we'll add urequests and mqtt. Mqtt is already onboard. But, will document it with this:

https://makeblock-micropython-api.readt ... uests.html
https://makeblock-micropython-api.readt ... /mqtt.html
Nyamekye,
User avatar
kwagyeman
Posts: 4236
Joined: Sun May 24, 2015 2:10 pm

Re: how to send image to server by post to http?

Postby kwagyeman » Wed May 13, 2020 1:10 pm

Actually, we can include this:

https://github.com/lucien2k/wipy-urllib
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 14 guests