Adding urequest to OpenMV

Aloha OpenMV developers,

As you know requests library is one of the most popular libraries in Python, and I think adding it to OpenMV would be cool.

I need to send an image from OpenMV to my website, so I first tried sending it from my PC
with requests library and it works, but OpenMV does not support requests or urequests for now, so I was wondering how to do so.

There are some urequest codes on Github, I tried adding urequests.py and import it in OpenMV which did not work. Would you please add urequests to your firmware, or let me know how to add and import it myself?

Thanks,
Mohsen

It’s on my todo list to make a more robust one: https://github.com/micropython/micropython-lib/blob/master/urequests/urequests.py

This just does some basics.

1 Like

Specifically, I added urequests.py to OpenMV USB Drive from Github;
https://github.com/micropython/micropython-lib/blob/master/urllib.urequest/urllib/urequest.py

And I use the following code to send the image saved on OpenMV that was working with request library;
r = urequests.post(url, files=file, headers=headers)

The error I get;
AttributeError: ā€˜module’ object has no attribute ā€˜post’

Any suggestions?

Regards

Thanks, Kwagyeman,

I tried your urequest.py code instead and I got the following error;

TypeError: function takes 2 positional arguments but 4 were given
in line 53 of the new urequest.py code:
ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)

You’ll have to manually fix the script per our API. It’s not written by us. But, it should have 95% of everything you need.

1 Like

I’d like to share my experience with urequests on OPENMV, we have 4 boards from you and I hope we can use urequests in it; Our image classification part is done and we need to upload the saved image to the server;

So the errors I get which might happen to others too;

Here is my code;
url = ā€œhttp://www.X.com/upload.php/ā€ #url of the webpage which works in Spyder!
headers = {
ā€˜User-Agent’: ā€˜Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0’,
}
file = {ā€˜file’: open(ā€˜0.png’, ā€˜rb’)} #open file to post
r = urequests.post(url, files=file, headers=headers) #post request of file to url
print(r.status_code) #status code 200 means success
c = r.content #content of the response in bytes
print(c) #prints units

First error:
TypeError: function takes 2 positional arguments but 4 were given
in line 53 of the new urequest.py code:
ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)

I removed the 0 and usocket.sock_stream so this was resolved

2nd error:
TypeError: object with buffer protocol required
some forums suggested changed it to json and it was resolved;
r = requests.post(url, json=file, headers=headers)

3rd error;
AttributeError: ā€˜socket’ object has no attribute ā€˜readline’

It looks like usocket.socket does not have a readline attribute.

And I got stuck there, sorry for the long comment, I thought sharing this might help we have urequest faster on OpenMV. I saw in the threads many people requested a method to send image from OpenMV to a server and I hope you help us solve this issue with urequests…

Thank you

The last error is because in the socket/usocket there is not a readline attribute. What you’ll have to do - and what I did to make an smtp program work - was wherever you see readline, change it to read. Now what’s going to happen is instead of reading line by line, it’s going to read the whole buffer full of data at once. So then, you setup a loop (for loop) whereby you’ll handle each line (which is terminated by a \n or \r or both. I was able to make that work fine, but the program kept crashing at the end because socket/usocket takes up a lot of memory, so be prepared for that issue. In my case all was fine until I got to the end of sending an email and tried to close the socket - BOOM! CRASH! OUT OF MEMORY ERROR! I even put gc.collect() in various places in the code to try to recoup memory to no avail…some boards just don’t have enough memory to work with in some cases.

Thanks, John and Kwagyeman for your feedback.

Dear Kwagyeman, I think it is important to add urequests library to OpenMV and I hope you increase its priority on your to-do list.

Because OpenMV is a Machine Vision Camera board classifying images or doing event detection, etc. So sharing that desired image (positively classified object saved on OpenMV or event) is very important and it is not a pressure/temperature sensor that you care about sending the sensor data with MQTT.

So even if you did not have MQTT or browser-based image sharing(which are fancy), and focus on developing a code to send an image at the edge is what most and I can say all of the customers need and they stated it in different threads frequently.

To wrap it up, you are selling a camera board that works at the edge, so you need to develop a library to share those desired images. In my opinion, with respect, everything else is secondary!

Thanks,
Mohsen

Will do. But, just keep in mind… there’s nothing stopping you from doing this yourself as it’s straight forward python code. I expect to be able to get it done by the end of the year. But, again, we have socket level support. You can 100% implement web requests quite easily.

1 Like