vishal soni
Published © MIT

2.8' Screen Retro camera (Capturing Asthetics)

A Python Unihiker boared with ESP32s3 sense transform into a retro Camera

AdvancedFull instructions providedOver 2 days388
2.8' Screen Retro camera (Capturing Asthetics)

Things used in this project

Hardware components

UNIHIKER - IoT Python Programming Single Board Computer with Touchscreen
DFRobot UNIHIKER - IoT Python Programming Single Board Computer with Touchscreen
×1
Seeed Studio XIAO ESP32S3 Sense
Seeed Studio XIAO ESP32S3 Sense
×1
Linear Regulator (7805)
Linear Regulator (7805)
×1
Capacitor 10 µF
Capacitor 10 µF
×1
Toggle Switch, (On)-Off-(On)
Toggle Switch, (On)-Off-(On)
×1
Battery, 3.7 V
Battery, 3.7 V
×2
Machine Screw, M2
Machine Screw, M2
×3
Jumper wires (generic)
Jumper wires (generic)
×1
Universal PCB
×1
Acrylic Sheet
×1

Software apps and online services

Visual Studio 2017
Microsoft Visual Studio 2017
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering Station, 110 V
Soldering Station, 110 V
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Schematic for Retro Camera

Code

Code for esp32s3 sense

C/C++
#include "esp_camera.h"
#include <WiFi.h>

//
// WARNING!!! PSRAM IC required for UXGA resolution and high JPEG quality
//            Ensure ESP32 Wrover Module or other board with PSRAM is selected
//            Partial images will be transmitted if image exceeds buffer size
//
//            You must select partition scheme from the board menu that has at least 3MB APP space.
//            Face Recognition is DISABLED for ESP32 and ESP32-S2, because it takes up from 15
//            seconds to process single frame. Face Detection is ENABLED if PSRAM is enabled as well

// ===================
// Select camera model
// ===================
#define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM
#include "camera_pins.h"

// ===========================
// Enter your WiFi credentials
// ===========================
const char *ssid = "VISHAL SONI";
const char *password = "ryukendo2006";

void startCameraServer();
void setupLedFlash(int pin);

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Serial.println();

  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sccb_sda = SIOD_GPIO_NUM;
  config.pin_sccb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.frame_size = FRAMESIZE_UXGA;
  config.pixel_format = PIXFORMAT_JPEG;  // for streaming
  //config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
  config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  config.fb_location = CAMERA_FB_IN_PSRAM;
  config.jpeg_quality = 12;
  config.fb_count = 1;

  // if PSRAM IC present, init with UXGA resolution and higher JPEG quality
  //                      for larger pre-allocated frame buffer.
  if (config.pixel_format == PIXFORMAT_JPEG) {
    if (psramFound()) {
      config.jpeg_quality = 10;
      config.fb_count = 2;
      config.grab_mode = CAMERA_GRAB_LATEST;
    } else {
      // Limit the frame size when PSRAM is not available
      config.frame_size = FRAMESIZE_SVGA;
      config.fb_location = CAMERA_FB_IN_DRAM;
    }
  } else {
    // Best option for face detection/recognition
    config.frame_size = FRAMESIZE_240X240;
#if CONFIG_IDF_TARGET_ESP32S3
    config.fb_count = 2;
#endif
  }

#if defined(CAMERA_MODEL_ESP_EYE)
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
#endif

  // camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

  sensor_t *s = esp_camera_sensor_get();
  // initial sensors are flipped vertically and colors are a bit saturated
  if (s->id.PID == OV3660_PID) {
    s->set_vflip(s, 1);        // flip it back
    s->set_brightness(s, 1);   // up the brightness just a bit
    s->set_saturation(s, -2);  // lower the saturation
  }
  // drop down frame size for higher initial frame rate
  if (config.pixel_format == PIXFORMAT_JPEG) {
    s->set_framesize(s, FRAMESIZE_QVGA);
  }

#if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
  s->set_vflip(s, 1);
  s->set_hmirror(s, 1);
#endif

#if defined(CAMERA_MODEL_ESP32S3_EYE)
  s->set_vflip(s, 1);
#endif

// Setup LED FLash if LED pin is defined in camera_pins.h
#if defined(LED_GPIO_NUM)
  setupLedFlash(LED_GPIO_NUM);
#endif

  WiFi.begin(ssid, password);
  WiFi.setSleep(false);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  startCameraServer();

  Serial.print("Camera Ready! Use 'http://");
  Serial.print(WiFi.localIP());
  Serial.println("' to connect");
}

void loop() {
  // Do nothing. Everything is done in another task by the web server
  delay(10000);
}

Code for Unihiker Boared

Python
# -*- coding: utf-8 -*-

from pinpong.board import *
from pinpong.extension.unihiker import *
from unihiker import GUI  # Import GUI from the unihiker module
import cv2
import requests
import numpy as np
from datetime import datetime
import time
import os  # Import os module to handle directory operations

# Initialize the UniHiker and the screen GUI
Board().begin()
gui = GUI()  # Create a GUI object to handle the display

# Use the correct capture URL from your ESP32 camera
stream_url = "http://192.168.249.253/capture"  # Update as needed

# Directory to save captured images
photo_directory = "/root/photo"

# Create the photo directory if it doesn't exist
if not os.path.exists(photo_directory):
    os.makedirs(photo_directory)
    print(f"Created directory: {photo_directory}")

last_button_b_press = 0  # To handle debounce
debounce_time = 0.3  # 300 milliseconds debounce time

def capture_and_display_image():
    """Capture an image from the ESP32 camera, display it on the UniHiker screen, and save it."""
    try:
        response = requests.get(stream_url, timeout=5)  # Set a timeout for the request
        if response.status_code == 200:
            img_array = np.array(bytearray(response.content), dtype=np.uint8)
            frame = cv2.imdecode(img_array, cv2.IMREAD_COLOR)

            if frame is not None:
                # Rotate the frame 90 degrees counterclockwise
                frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)

                # Create a unique filename using a timestamp
                timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
                file_name = os.path.join(photo_directory, f"captured_image_{timestamp}.jpg")  # Save in the photo directory

                # Save the rotated image in the UniHiker photo folder
                success = cv2.imwrite(file_name, frame)  # Attempt to save the image

                if success:
                    print(f"Image captured, rotated, and saved as '{file_name}'")
                else:
                    print(f"Failed to save the image as '{file_name}'")

                # Clear the screen and display the saved image on the UniHiker screen
                gui.clear()  # Clear previous drawings
                gui.draw_image(x=120, y=160, w=420, h=320, image=file_name, origin='center', onclick=lambda: print("Image clicked"))
                
                # Add a delay to allow the image to be displayed before the next capture
                time.sleep(2)  # 2 seconds delay for better visibility

            else:
                print("Failed to decode image")
        else:
            print(f"Failed to connect to the camera stream. Status code: {response.status_code}")
    except requests.exceptions.Timeout:
        print("Request timed out. Please check the ESP32 connection.")
    except Exception as e:
        print(f"An error occurred: {e}")

def delete_all_images():
    """Delete all images in the photo directory."""
    try:
        for filename in os.listdir(photo_directory):
            file_path = os.path.join(photo_directory, filename)
            if os.path.isfile(file_path):
                os.remove(file_path)  # Remove the file
                print(f"Deleted image: {file_path}")
        print("All images deleted successfully.")
    except Exception as e:
        print(f"An error occurred while deleting images: {e}")

while True:
    current_time = time.time()

    # Check if button A is pressed (for deleting images)
    if button_a.is_pressed():
        print("Button A is pressed. Deleting all images...")
        delete_all_images()  # Call the function to delete images

    # Check if button B is pressed, with debounce handling
    if button_b.is_pressed():
        print("Button B is pressed.")  # Debug print
        if current_time - last_button_b_press > debounce_time:
            last_button_b_press = current_time  # Update last press time
            print("Button B pressed. Capturing, rotating, and displaying image...")
            capture_and_display_image()  # Capture and display the image

    # Sleep for a short time to avoid overloading CPU
    time.sleep(0.1)  # 100 milliseconds pause to reduce CPU load

Code for Toggeling Photos

Python
# -*- coding: utf-8 -*-

import os
import time
from pinpong.board import *
from pinpong.extension.unihiker import *
from unihiker import GUI

# Initialize the UniHiker and the GUI
Board().begin()
gui = GUI()

# Define the folder where the images are stored
photo_folder = "/root/photo/"

# Get the list of image files sorted by modification time
def get_sorted_image_files():
    """Get a list of image files sorted by modification time."""
    try:
        files = [f for f in os.listdir(photo_folder) if f.endswith(".jpg")]
        files.sort(key=lambda x: os.path.getmtime(os.path.join(photo_folder, x)))
        return files
    except Exception as e:
        print(f"Error reading photo folder: {e}")
        return []

# Display an image on the screen
def display_image(file_name):
    """Display the given image on the UniHiker screen."""
    image_path = os.path.join(photo_folder, file_name)
    try:
        gui.clear()  # Clear previous image
        gui.draw_image(x=120, y=160, w=420, h=320, image=image_path, origin='center')
        print(f"Displaying image: {file_name}")
    except Exception as e:
        print(f"Error displaying image: {e}")

# Display a message on the screen
def display_message(message):
    """Display a message on the UniHiker screen."""
    gui.clear()
    gui.draw_text(x=120, y=160, text=message, font_size=8, origin='center')
    time.sleep(2)  # Display the message for 2 seconds

# Make the buzzer beep on button press
def beep_buzzer():
    """Play a short beep sound using the built-in buzzer."""
    buzzer.pitch(250, 1)  # Play a short beep sound at 250 Hz for 100 milliseconds

# Main function to handle image navigation
def navigate_images():
    """Navigate through the images in the 'photo' folder."""
    image_files = get_sorted_image_files()
    if not image_files:
        print("No images found in the photo folder.")
        return

    current_index = 0
    display_image(image_files[current_index])

    while True:
        if button_a.is_pressed():  # Check for button A (next image)
            print("Button A pressed - Next image")
            beep_buzzer()  # Play beep sound
            current_index = (current_index + 1) % len(image_files)
            display_image(image_files[current_index])
            time.sleep(0.1)  # Shorter delay to make navigation feel more responsive

            if current_index == 0:  # Looping back to the start
                display_message("End of images. Starting over...")

        elif button_b.is_pressed():  # Check for button B (previous image)
            print("Button B pressed - Previous image")
            beep_buzzer()  # Play beep sound
            current_index = (current_index - 1) % len(image_files)
            display_image(image_files[current_index])
            time.sleep(0.1)  # Shorter delay for responsiveness

            if current_index == len(image_files) - 1:  # Looping back to the end
                display_message("End of images. Starting back...")

        time.sleep(0.1)  # Short delay to reduce CPU load but keep it responsive

# Start the image navigation
navigate_images()

Credits

vishal soni
7 projects • 8 followers
Engineer ,Electronic Enthusiast
Contact
Thanks to Sachin soni.

Comments

Please log in or sign up to comment.