sipeed / MaixPy-v1_scripts

micropython scripts for MaixPy
https://maixpy.sipeed.com
MIT License
628 stars 432 forks source link

Image download maixduino #76

Open ChahidTALHA opened 4 years ago

ChahidTALHA commented 4 years ago

ESP32 firmware version: 1.4.0 Maixduino firmware: MicroPython v0.5.0-221-g6460c58.

Hi everyone, I am using Maixduino for my demo project. Where i should first download image from a localhost url (using Xampp). The problem I am facing now is that the code takes too long to download the image (Alice.jpg, size: 42.7 KB), it takes 4 seconds to download the image (Where the expected download time should take 200ms as a maximum for a light image). and sometimes it doesn't get all the data . Is there any way to optimize my code in this case. Or should I look for an alternative solution. Code:

#-------------- IMPORT PHASE -----------------
import network, socket
from Maix import GPIO
from fpioa_manager import fm, board_info
import machine
import time
import image
import sensor
import os
#---------------------------------------------
#------------- INIT PHASE --------------------
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_vflip(0)
sensor.set_hmirror(0)
sensor.run(1)
WIFI_SSID = "WIFI_SSID"
WIFI_PASSWD = "WIFI_PASSWD"
#-----------------------------------------------
def display_on_buffer(img): #display the image object on the buffer and returns nothing
    img2=img.copy(copy_to_fb=True)
    sensor.snapshot()
###----------------------------------------
# IO map for ESP32 on Maixduino
fm.register(25,fm.fpioa.GPIOHS10)#cs
fm.register(8,fm.fpioa.GPIOHS11)#rst
fm.register(9,fm.fpioa.GPIOHS12)#rdy
fm.register(28,fm.fpioa.GPIOHS13)#mosi
fm.register(26,fm.fpioa.GPIOHS14)#miso
fm.register(27,fm.fpioa.GPIOHS15)#sclk
nic = network.ESP32_SPI(cs=fm.fpioa.GPIOHS10,rst=fm.fpioa.GPIOHS11,rdy=fm.fpioa.GPIOHS12, mosi=fm.fpioa.GPIOHS13,miso=fm.fpioa.GPIOHS14,sclk=fm.fpioa.GPIOHS15)
###----------------------------------------
print("ESP32_SPI firmware version:", nic.version())
err = 0
while 1:
    try:
        nic.connect(WIFI_SSID, WIFI_PASSWD)
    except Exception:
        err += 1
        print("Connect AP failed, now try again")
        if err > 3:
            raise Exception("Conenct AP fail")
        continue
    break
print(nic.ifconfig())
print(nic.isconnected())

FULL_URL ="http://localhost/Alice.jpg"
HOST = "192.168.1.54"

HTTP_HEADER =b"GET %s HTTP/1.1\r\nHost: %s\r\n\r\n"%(FULL_URL,HOST)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
err = 0
while 1:
    try:
        addr = socket.getaddrinfo(HOST, 80)[0][-1]
        break
    except Exception:
        err += 1
    if err > 5:
        raise Exception("get ip failed!")

sock.connect(addr)
bytes_sent = sock.send(HTTP_HEADER)
time.sleep_ms(300)
data_length = 0
img = b""
sock.settimeout(5)
print("start receiving")
start_time = time.ticks_ms()
while True:
    try:
        data = sock.recv(4096)
        print(len(data))
        if len(data) == 0:
            break
        else:
            img=img+data
            data_length = data_length + len(data)
    except Exception as e:
        print("exception: ",e)
        break
sock.close()
finish_time = time.ticks_ms() - start_time
print("download time: ",finish_time)
print("data length:",data_length)
img = img[img.find(b"\r\n\r\n")+4:]
fis = open("/flash/Alice.jpg","wb")
fis.write(img)
fis.close()
img = image.Image("/flash/Alice.jpg")
display_on_buffer(img)
print("DONE")

finish time prints 4 to 5 seconds for such image. Can I have a clarification about this

michaelteo commented 3 years ago

Look like they have released an example MaixPY Script to do HTTP download of an image. https://github.com/sipeed/MaixPy_scripts/blob/master/network/demo_http_get_jpg.py

Maybe worth a shot if that implementation works better