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?
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ā
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)
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ā¦
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.
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!
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.