If you want to use ESP32-CAM quickly, you can use Arduino IDE.
That is very troublesome.
That's where MicroPython comes in.
Quoted from official documentation
MicroPython works on a variety of system and hardware platforms.
However, since it does not currently support ESP32-CAM, there is no library for using the camera. (There is a custom FW that can use the camera, and this time I used it) In addition, MicroPython is a subset of Python, so some methods and modules are not supported.
MicroPython has a library called urequest, which can be used almost like requests. Easy to install.
import upip
upip.install('urequests')
However, when I installed it this way, I couldn't call the method. So I transferred the one pulled from the official MicroPython repository to esp32 and used it. urequest.py Transferring to esp32 is easy with ampy.
#installation of ampy
$ pip install adafruit-ampy
#File transfer to esp32
$ ampy -p /dev/tty.<According to the environment> put <File>
Initially, just like when using Python requests,
urequests.post(url, files={'image': open('Image file', 'rb)})
I was wondering if I could go there, but urequests doesn't have files.
As a result of various investigations, I found that I could go by converting to JSON once.
buf = camera.capture()
img_byte = base64.b64encode(buf)
img_json = ujson.dumps({"image": img_byte})
res = urequests.post(url, data=img_json)
I referred to this article. Send / receive image data as JSON in Python over the network
The processing on the received server side (Flask) looks like this
data = request.data.decode('utf-8')
data_json = json.loads(data)
image = data_json['image']
image_dec = base64.b64decode(image)
data_np = np.frombuffer(image_dec, dtype='uint8')
decimg = cv2.imdecode(data_np, 1)
cv2.imwrite(filename, decimg)
For the time being, the purpose of "POST the image taken by ESP32-CAM" was fulfilled. Click here for what I made ↓ ESP32-CAM_ImagePost
Recommended Posts