Core Python Libraries for Computer Vision, ESP32, and Raspberry Pi Development

The following are core Python libraries for computer vision, ESP32 development, and Raspberry Pi development, all of which are frequently used tools in practical development:1. Computer VisionOpenCV-python: The most fundamental computer vision library, supporting image reading, filtering, edge detection, object tracking, and all basic operations, serving as the “foundation” for visual development.

TorchVision: A core library for deep learning in vision, with built-in pre-trained models like ResNet and YOLO, enabling quick implementation of image recognition and segmentation, seamlessly integrated with the PyTorch framework.

Pillow (PIL): A lightweight image processing library focused on simple operations such as image format conversion, cropping, and rotation, which is more concise than OpenCV and often used in conjunction with other libraries.2. ESP32 Developmentesptool.py: The official flashing tool library for ESP32, used to flash firmware onto the ESP32 chip and erase flash, making it an essential tool before development.

adafruit-ampy: A file transfer library for ESP32, allowing Python scripts (.py files) to be uploaded to the ESP32’s file system via serial port, facilitating development and debugging.

micropython-esp32: The core library for the MicroPython environment on ESP32 (requires flashing MicroPython firmware onto the ESP32), providing control functions for hardware interfaces such as GPIO, I2C, and SPI, enabling direct operation of sensors and actuators.3. Raspberry Pi DevelopmentRPi.GPIO: The official GPIO control library for Raspberry Pi, used to operate the Raspberry Pi’s pins (high/low levels, PWM output), controlling hardware such as LEDs, motors, and relays.

picamera2: The official library for the Raspberry Pi camera (replacing the old picamera), supporting camera preview, photo capture, and video recording, as well as obtaining real-time image data for visual processing.

smbus2: An I2C communication library for Raspberry Pi, used to connect I2C interface sensors (such as the SHT30 temperature and humidity sensor and the MPU6050 gyroscope) to read hardware data.Here is a concise example code for each library, such as using OpenCV to read from the camera and using RPi.GPIO to control an LED, which can be copied directly to run.

Core Library Minimal Example Code1. Computer Vision1. OpenCV-python (Reading from Camera)python

import cv2
cap = cv2.VideoCapture(0)  # 0 represents the default camera
while True:
    ret, frame = cap.read()
    cv2.imshow("Camera", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):  # Press 'q' to exit
        break
cap.release()
cv2.destroyAllWindows()

2. TorchVision (Using Pre-trained Model to Recognize Image)python

import torchvision.models as models
from torchvision import transforms
from PIL import Image
model = models.resnet18(pretrained=True).eval()  # Load pre-trained model
img = transforms.ToTensor()(Image.open("test.jpg")).unsqueeze(0)
print("Predicted class index:", model(img).argmax().item())

3. Pillow (Cropping and Saving Image)python

from PIL import Image
img = Image.open("test.jpg")
cropped_img = img.crop((100, 100, 400, 400))  # Coordinates: left, upper, right, lower
cropped_img.save("cropped_test.jpg")

2. ESP32 Development1. esptool.py (Erasing ESP32 Flash)python

# Install first: pip install esptool
# Run in command line (not Python script):
# esptool.py --port COM3 erase_flash  # Replace COM3 with the ESP32 port

2. adafruit-ampy (Uploading Files to ESP32)python

# Install first: pip install adafruit-ampy
# Run in command line (not Python script):
# ampy --port COM3 put main.py  # Upload main.py to ESP32

3. micropython-esp32 (Controlling ESP32 LED Blinking)python

# Flash MicroPython firmware onto ESP32, then run in serial tool
import machine
import time
led = machine.Pin(2, machine.Pin.OUT)  # 2 is the default LED pin for ESP32
while True:
    led.value(not led.value())
    time.sleep(1)

3. Raspberry Pi Development1. RPi.GPIO (Controlling Raspberry Pi LED Blinking)python

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)  # 18 is the Raspberry Pi pin
while True:
    GPIO.output(18, not GPIO.input(18))
    time.sleep(1)

2. picamera2 (Taking Photo with Raspberry Pi Camera)python

from picamera2 import Picamera2
picam2 = Picamera2()
picam2.start()
picam2.capture_file("pi_photo.jpg")  # Save photo
picam2.stop()

3. smbus2 (Reading I2C Sensor Data, Example with SHT30)python

from smbus2 import SMBus
bus = SMBus(1)  # 1 is the Raspberry Pi I2C bus number
bus.write_i2c_block_data(0x44, 0x2C, [0x06])  # 0x44 is the SHT30 address
data = bus.read_i2c_block_data(0x44, 0x00, 6)
temp = (data[0] << 8 | data[1]) / 65535 * 175 - 45  # Calculate temperature
print(f"Temperature: {temp:.2f}℃")

Please note that the above is just an overview of the application of three core libraries; actual applications require further expansion and code optimization!

OpenCV + PyTorch Minimal Code for Cat/Dog Recognition from Camera

Please open in the WeChat client

Leave a Comment