How to send a captured image from FiPy to PC (to make it visible)?



  • Hi guys,

    How can I send an image that I captured with the OV2640 camera connected with my FiPy board to the PC? I want to test if the camera works properly (to see the output after the capture function is performed).

    Here is my code:

    import ov2640
    import gc
    import time
    import sys
    from machine import SD
    import os
    import math
    #import struct 
    
    FNAME = 'image2.jpg'
    
    def main():
        try:
            print("initializing camera")
            cam = ov2640.ov2640(resolution=ov2640.OV2640_320x240_JPEG)
            #cam = ov2640.ov2640(resolution=ov2640.OV2640_1024x768_JPEG)
            print(gc.mem_free())
    
            clen = cam.capture_to_file(FNAME, True)
            print("captured image is %d bytes" % clen)
            print("image is saved to %s" % FNAME)
    
            #databytes = struct.pack('d', clen)
    
            time.sleep(10)
            try:
                f = open(FNAME, "r")
                #print(f.read())
                exists = True
                print("real")
                f.close()
            except FileNotFoundError:
                exists = False
            sys.exit()
    
        except KeyboardInterrupt:
            print("exiting...")
            sys.exit()
    
    
    if __name__ == '__main__':
        main()
    

    Every kind of help is welcome! Thanks in advance.

    Kind regards,
    Adnan



  • @Adnan-Sabovic-0 Flat I could help. You may want to share your final working code so that others can build upon it in the future.



  • @jcaron I changed the code a bit. First of all, based on the simple wifi example, I added this while loop that checks if my board is connected to WiFi, and as long as it is not, the code will not go to the next step. This gave my board more time to connect to the network (as before, after the init function I only had this check message "Wifi is up"). Also, I changed the content type and set it to be an image. And finally, I adapted a bit this part with GET request, sending the captured image as a file to the HTTP client. Now it works as expected and I am able to see the captured image in my browser. Thanks a lot for this help!



  • @Adnan-Sabovic-0 are the FiPy and computer connected to the same WiFi network? Does the FiPy say it’s connected? You should log the IP address it gets. You should not need to bind to a specific address on the FiPy, use 0.0.0.0 instead.

    Can you ping the FiPy from your computer? Connect via telnet? Does the FiPy log anything when you connect with your browser?



  • @jcaron
    Here is my code where I am trying to send an image captured with a camera to the client. I tried different addresses but every time I got the same message that the site can't be reached. What do you think can cause an issue here?

    import usocket
    import _thread
    import time
    from network import WLAN
    import pycom
    
    availablecolor = 0x001100
    connectioncolor = 0x110000
    
    # Thread for handling a client
    def client_thread(clientsocket,n):
        # Receive maxium of 12 bytes from the client
        r = clientsocket.recv(4096)
    
        # If recv() returns with 0 the other end closed the connection
        if len(r) == 0:
            clientsocket.close()
            return
        else:
            # Do something wth the received data...
            print("Received: {}".format(str(r))) #uncomment this line to view the HTTP request
    
        #this is a captured image
        cam = ov2640.ov2640(resolution=ov2640.OV2640_320x240_JPEG)
    
        #send a captured image
        clientsocket.send(cam)
        # Close the socket and terminate the thread
    
        clientsocket.close()
        pycom.rgbled(connectioncolor)
        time.sleep_ms(500)
        pycom.rgbled(availablecolor)
    
    time.sleep(1)
    wifi = WLAN()
    wifi.init(mode=WLAN.AP, ssid="my_network_name", auth=(WLAN.AP, 'my_wifi_pass'), channel=1)
    print("WiFi is up!")
    time.sleep(1)
    pycom.heartbeat(False)
    pycom.rgbled(availablecolor)
    
    # Set up server socket
    serversocket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
    serversocket.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1)
    serversocket.bind(("my_ipp_add", 80))
    
    # Accept maximum of 5 connections at the same time
    serversocket.listen(5)
    
    # Unique data to send back
    c = 1
    while True:
        # Accept the connection of the clients
        (clientsocket, address) = serversocket.accept()
        # Start a new thread to handle the client
        _thread.start_new_thread(client_thread, (clientsocket, c))
        c = c+1
    serversocket.close()
    
    


  • @Adnan-Sabovic-0 this looks OK except for the Content-Type which needs to be changed since you are sending back an image, not HTML. But I haven’t tested it, just glanced at the code.

    Now when you open the address of your FiPy in your web browser on you mr computer, what do you get?

    NB: the HTTP code in the example I gave you is very crude, There are probably better libraries to handle that more cleanly, but as a proof of concept it should work.



  • @jcaron
    This is my code, changed based on my requirements. I also added all the needed files that go with the ov2640 camera. HTTP server example is for FiPy and HTTP client code is for my PC, or I did not understand your answer correctly?

    import usocket
    import _thread
    import time
    from network import WLAN
    import pycom
    
    availablecolor = 0x001100
    connectioncolor = 0x110000
    
    # Thread for handling a client
    def client_thread(clientsocket,n):
       # Receive maxium of 12 bytes from the client
       r = clientsocket.recv(4096)
    
       # If recv() returns with 0 the other end closed the connection
       if len(r) == 0:
           clientsocket.close()
           return
       else:
           # Do something wth the received data...
           print("Received: {}".format(str(r))) #uncomment this line to view the HTTP request
    
       http = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection:close \r\n\r\n" #HTTP response
    
       cam = ov2640.ov2640(resolution=ov2640.OV2640_320x240_JPEG)
    
       clientsocket.send(cam)
       # Close the socket and terminate the thread
    
       clientsocket.close()
       pycom.rgbled(connectioncolor)
       time.sleep_ms(500)
       pycom.rgbled(availablecolor)
    
    time.sleep(1)
    wifi = WLAN()
    wifi.init(mode=WLAN.AP, ssid="my_network_name", auth=my_pass, channel=1)
    print("WiFi is up!")
    time.sleep(1)
    pycom.heartbeat(False)
    pycom.rgbled(availablecolor)
    
    # Set up server socket
    serversocket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
    serversocket.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1)
    serversocket.bind(("my_ip_addr", 80))
    
    # Accept maximum of 5 connections at the same time
    serversocket.listen(5)
    
    # Unique data to send back
    c = 1
    while True:
       # Accept the connection of the clients
       (clientsocket, address) = serversocket.accept()
       # Start a new thread to handle the client
       _thread.start_new_thread(client_thread, (clientsocket, c))
       c = c+1
    serversocket.close()
    
    
    
    


  • @Adnan-Sabovic-0 you take the code of the webserver, replace its current request handling with the code which gets the image from the camera, and return that image as the response to the request (with appropriate headers indicating it’s an image), run all that in your FiPy, and then open that from a browser on your PC.

    Of course your FiPy should be connected to your WiFi.

    I’m not sure where you would use localhost? The server runs on the FiPy and the client (web browser) on your PC.



  • @jcaron Do I need to adapt this code or I can use the same implementation? I tried this example but I am not able to see any output on localhost?



  • @Adnan-Sabovic-0 There are probably a hundred ways to do it, but one option is to create a webserver, and capture and return the image on request? You don't even need to save the image to the filesystem, just return it directly.


Log in to reply
 

Pycom on Twitter