• MicroPython
    2 min | 4087

    #MicroPython: Portable time-lapse camera using an ESP32-CAM

    MicroPython | 2 min | 4087


    This tutorial extends the article MicroPython: Taking photos with an ESP32 and in this case, the ESP32-CAM running MicroPython takes a photo every 5 seconds and saves it on the microSD card. The photos can be then combined into a time-lapse video! :)

    The MicroPython official version doesn't support I2S interface. However, tsaarni wrote a driver for the OV2640 and forked the MicroPython repository to include it. Thus, you need to follow the instructions in this article: MicroPython: Taking photos with an ESP32 to install the right firmware.

    Hardware and Software

    In the following table, you will find the hardware and software that you can use on this tutorial:

    There are two new articles about this topic:Check out those, before you keep reading this article and instructions.

    DIY

    After flashing the MicroPython firmware, clone the repository lemariva/uPyCam typing:

    git clone -b timelapse-camera https://github.com/lemariva/uPyCam.git

    then rename the file boot.sample.py to boot.py and configure the Wi-Fi access settings:

    # wlan access
    ssid_ = ""
    wpa2_pass = ""

    The ESP32-CAM uses Wi-Fi to connect to an NTP-server and to get the actual time. Additionally, you can check, if everything is working because the code enables an FTP server.

    Use VSCode with the PyMakr extension to upload all the files to the ESP32. If you need some help, follow the instructions from this tutorial.

    Then, connect your ESP32-CAM to a 5V battery and enjoy the results! :)

    If you need a datasheet of the ESP32-CAM, here is one available, I needed to define the pins that are connected to the MicroSD slot.

    Error on saving photos

    Some of the photos were saved with error on the MicroSD card, and I got an error when I tried to export the video sequence. So, I wrote some code lines to repair the photos. The error that I got was the following: Premature end of JPEG file.

    Create a file photo_repair.py with the following code:

    import os
    import cv2
    import shutil
    from os import listdir
    from os.path import isfile, join
    
    basedir = os.path.abspath(os.getcwd())
    
    onlyfiles = [f for f in listdir(basedir) if isfile(join(basedir, f))]
    new_folder = "numerated" 
    
    for idx, filename in enumerate(onlyfiles):   
        base, extension = os.path.splitext(filename)
        if extension == ".jpg":
            print(filename)
            img = cv2.imread(filename)
            cv2.imwrite(filename, img)

    Then, install the dependencies (opencv) using pip:

    pip install opencv-python

    and run the code typing python photo_repair.py.

    Conclusions

    This article presents an example code to use the ESP32-CAM as a time-lapse camera using MicroPython. The module takes a photo every 5 seconds and saves it on the SD. Then, using x264vfw and VirtualDub is possible to generate a time-lapse video. Some of the photos are saved on the microSD with errors and I included a some code lines to repair them.


    Comments

    Empty