[Diao Ye Learns Programming] MicroPython Manual OpenMV Cam LCD Display

Insert image description here
MicroPython is a lightweight version of the interpreter designed for running the Python 3 programming language in embedded systems. Compared with regular Python, the MicroPython interpreter is small (only about 100KB) and is compiled into a binary Executable file to run, resulting in higher execution efficiency. It uses a lightweight garbage collection mechanism and removes most of the Python standard library to accommodate resource-constrained microcontrollers.

The main features of MicroPython include:
1. The syntax and functions are compatible with standard Python, making it easy to learn and use. Supports most core syntax of Python.
2. Directly access and control hardware, control GPIO, I2C, SPI, etc. like Arduino.
3. Powerful module system, providing file system, network, graphical interface and other functions.
4. Support cross-compilation to generate efficient native code, which is 10-100 times faster than the interpreter.
5. The amount of code is small and the memory usage is small. It is suitable for running on MCU and development boards with small memory.
6. Open source license, free to use. The Shell interactive environment provides convenience for development and testing.
7. The built-in I/O driver supports a large number of microcontroller platforms, such as ESP8266, ESP32, STM32, micro:bit, control board and PyBoard, etc. There is an active community.

MicroPython application scenarios include:
1. Rapidly build prototypes and user interactions for embedded products.
2. Make some small programmable hardware projects.
3. As an educational tool, it helps beginners learn Python and IoT programming.
4. Build smart device firmware to achieve advanced control and cloud connectivity.
5. Various microcontroller applications such as Internet of Things, embedded intelligence, robots, etc.

Things to note when using MicroPython:
1. Memory and Flash space are limited.
2. The explanation and execution efficiency is not as good as C language.
3. Some library functions are different from the standard version.
4. Optimize the syntax for the platform and correct the differences with standard Python.
5. Use memory resources rationally and avoid frequently allocating large memory blocks.
6. Use native code to improve the performance of speed-critical parts.
7. Use abstraction appropriately to encapsulate underlying hardware operations.

Generally speaking, MicroPython brings Python into the field of microcontrollers, which is an important innovation that not only lowers the programming threshold but also provides good hardware control capabilities. It is very suitable for the development of various types of Internet of Things and intelligent hardware.
Insert image description here
OpenMV Cam is a small, low-power microcontroller board that allows you to easily implement applications using machine vision in the real world. You can program OpenMV Cam using high-level Python scripts (provided by MicroPython OS) instead of C/C++. The technical parameters of OpenMV Cam include the following aspects:

1. Processor: OpenMV Cam H7 Plus uses STM32H743II ARM Cortex M7 processor, running at 480 MHz, with 32MB SDRAM + 1MB SRAM and 32 MB external flash memory + 2 MB internal flash memory. OpenMV Cam M4 V2 uses STM32F427VG ARM Cortex M4 processor running at 180 MHz with 256KB RAM and 1 MB flash memory.
2. Image sensor: Both OpenMV Cam H7 Plus and OpenMV Cam M4 V2 use the OV7725 image sensor, which can capture 320x240 8-bit grayscale images or 320x240 16-bit RGB565 images at 75 FPS at resolutions higher than 320x240. Capable of shooting at 150 FPS at 320x240.
3. I/O interface: OpenMV Cam H7 Plus and OpenMV Cam M4 V2 both have the following I/O interfaces:
(1) Full-speed USB (12Mbs) interface, connected to the computer. When the OpenMV Cam is plugged in, a virtual COM port and a "USB flash drive" will appear on your computer.
(2) The μSD card slot is capable of 100Mbs read/write, allowing your OpenMV Cam to record video and extract machine vision material from the μSD card.
(3) The SPI bus operates at a speed of up to 54Mbs, allowing you to simply transmit image stream data to the LCD expansion board, WiFi expansion board, or other controllers.
(4) I2C bus (up to 1Mb/s), CAN bus (up to 1Mb/s) and asynchronous serial bus (TX/RX, up to 7.5Mb/s) for connection with other controllers or sensors.
(5) A 12-bit ADC and a 12-bit DAC.
(6) There are interrupts and PWM on all I/O pins (there are 9 or 10 I/O pins on the board).
4. LED: Both OpenMV Cam H7 Plus and OpenMV Cam M4 V2 are equipped with one RGB LED (tri-color) and two bright 850nm IR LEDs (infrared).
5. Lens: Both OpenMV Cam H7 Plus and OpenMV Cam M4 V2 are equipped with a standard M12 lens interface and a default 2.8 mm lens. If you want to use a more professional lens with your OpenMV Cam, you can easily purchase it and install it yourself.

Insert image description here
MicroPython's OpenMV Cam can realize image display and user interface interaction through LCD display. Below I will explain in detail its main features, application scenarios and matters needing attention.

main feature:

Display image: OpenMV Cam’s LCD display function can display image data on the LCD screen in real time. This allows users to visually observe the results of image processing and computer vision algorithms.

User interface interaction: Through the LCD display, OpenMV Cam can provide user interface interaction capabilities. Users can interact with the device through buttons, touch screens, etc., and perform parameter settings, mode selection and other operations.

Multiple display modes: LCD display functions usually support multiple display modes, such as monochrome, color, grayscale, etc. The appropriate display mode can be selected according to specific needs to meet different application needs.

Application scenarios:

Image processing and computer vision: OpenMV Cam is widely used in the fields of image processing and computer vision. Through the LCD display, the results of image processing algorithms, such as edge detection, target tracking, object recognition, etc., can be displayed in real time.

Real-time monitoring and debugging: LCD display can be used for real-time monitoring and debugging of the system. By displaying real-time images or related information, you can easily observe system status, debug code logic and parameter settings.

User interface display: Through the LCD display, a friendly user interface display can be provided. For example, in embedded systems, user operations and feedback can be achieved by displaying menus, buttons and other interface elements.

Things to note:

Display resolution and size: When selecting an LCD display, you need to consider whether its resolution and size are suitable for specific application needs. Higher resolution and larger size can provide better display effects, but also require more system resources and power consumption.

Display speed and refresh rate: The speed and refresh rate of LCD display will affect the smoothness and real-time nature of the image. When designing and writing code, the display speed needs to be reasonably controlled to ensure the real-time and stability of image display.

Display interface and driver: When using the LCD display function, you need to understand and choose the appropriate display interface and driver. Make sure the OpenMV Cam can correctly connect and drive the LCD display to achieve normal display functionality.

Energy consumption and resource management: LCD display functions usually require more system resources and power consumption. During use, system resources and power need to be properly managed to avoid excessive resource and energy consumption.

In summary, MicroPython's OpenMV Cam implements image display and user interface interaction through an LCD display. It plays an important role in image processing and computer vision, real-time monitoring and debugging, user interface display and other application scenarios. When using it, you need to pay attention to display resolution and size, display speed and refresh rate, display interface and driver, as well as energy consumption and resource management to ensure the quality of the display effect and the stability of the system.

Case 1: Display images captured by the camera

import sensor, image, time

sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 设置像素格式为RGB565
sensor.set_framesize(sensor.QVGA) # 设置帧大小为320x240
sensor.skip_frames(time = 2000) # 等待设置生效,延时2秒

while(True):
    img = sensor.snapshot() # 捕获一帧图像
    lcd.display(img) # 在LCD上显示图像
    time.sleep_ms(100) # 延时100毫秒

Interpretation: This program first imports the sensor, image and time modules. Then initialize the camera, set the pixel format to RGB565, the frame size to 320x240, and wait for the settings to take effect. In the while loop, the image of the camera is continuously captured and displayed on the LCD, with a delay of 100 milliseconds after each display.

Case 2: Implementing the autofocus function of the camera

import sensor, image, time

sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 设置像素格式为RGB565
sensor.set_framesize(sensor.QVGA) # 设置帧大小为320x240
sensor.skip_frames(time = 2000) # 等待设置生效,延时2秒

focus_state = False # 焦点状态,初始值为False

while(True):
    img = sensor.snapshot() # 捕获一帧图像
    focus_value = img.find_blobs([(0, 255)], area_threshold = 100, pixels_threshold = 100, merge=True) # 寻找红色斑点
    
    if focus_value: # 如果找到红色斑点
        focus_state = not focus_state # 切换焦点状态
        lcd.display("Focus: {}".format(focus_state)) # 在LCD上显示焦点状态
    else:
        lcd.display("No focus") # 如果没有找到红色斑点,显示"No focus"
    
    time.sleep_ms(100) # 延时100毫秒

Interpretation: This program also imports the sensor, image and time modules. After initializing the camera, set the pixel format to RGB565, the frame size to 320x240, and wait for the settings to take effect. In the while loop, continuously capture images from the camera and look for red spots. If a red spot is found, the focus state is switched and displayed on the LCD; if no red spot is found, "No focus" is displayed. There is a delay of 100 milliseconds after each display.

Case 3: Implementing the horizontal flip function of the camera

import sensor, image, time

sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 设置像素格式为RGB565
sensor.set_framesize(sensor.QVGA) # 设置帧大小为320x240
sensor.skip_frames(time = 2000) # 等待设置生效,延时2秒

flip_state = False # 水平翻转状态,初始值为False

while(True):
    img = sensor.snapshot() # 捕获一帧图像
    flip_value = img.width > img.height # 判断是否为横向照片
    
    if flip_value != flip_state: # 如果当前照片方向与上次拍照方向不同
        flip_state = not flip_state # 切换水平翻转状态
        lcd.display("Flip: {}".format(flip_state)) # 在LCD上显示水平翻转状态
    else:
        lcd.display("No flip") # 如果照片方向与上次拍照方向相同,显示"No flip"
    
    time.sleep_ms(100) # 延时100毫秒

Interpretation: This program also imports the sensor, image and time modules. After initializing the camera, set the pixel format to RGB565, the frame size to 320x240, and wait for the settings to take effect. In the while loop, the camera image is continuously captured and the photo direction is determined. If the photo direction is different from the last photo direction, switch to the horizontal flip state and display it on the LCD; if the photo direction is the same as the last photo direction, "No flip" is displayed. There is a delay of 100 milliseconds after each display.

Case 4: Display image:

import sensor
import lcd

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)

lcd.init()
lcd.rotation(2)

while True:
    img = sensor.snapshot()
    lcd.display(img)

Key point interpretation:
This program uses the LCD display function of OpenMV Cam to display the image captured by the camera in real time.
Use sensor.reset() to reset the camera sensor.
Use sensor.set_pixformat() to set the image pixel format to RGB565.
Use sensor.set_framesize() to set the image frame size to QVGA (320x240).
Use sensor.run(1) to start the camera.
Use lcd.init() to initialize the LCD display.
Use lcd.rotation(2) to set the LCD display rotation angle.
Use sensor.snapshot() to capture the image and store it in the img variable.
Use lcd.display() to display the captured image on the LCD.
In an infinite loop, cause the program to continuously capture and display images.

Case 5: Display text information:

import lcd

lcd.init()
lcd.rotation(2)
lcd.clear()

while True:
    lcd.draw_string(10, 10, "Hello, World!", lcd.WHITE, lcd.BLACK)

Interpretation of key points:
This program uses the LCD display function of OpenMV Cam to display text information on the LCD screen.
Use lcd.init() to initialize the LCD display.
Use lcd.rotation(2) to set the LCD display rotation angle.
Use lcd.clear() to clear the contents of the LCD screen.
In an infinite loop, have the program continue to do the following:
Use lcd.draw_string() to draw text on the LCD screen.
The parameters are the position of the text (x, y coordinates), text content, foreground color (text color), and background color.
In this example, the string "Hello, World!" is drawn on the LCD screen at position (10, 10), using a white text color and a black background color.

Case 6: Display sensor data:

import sensor
import lcd

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)

lcd.init()
lcd.rotation(2)
lcd.clear()

while True:
    img = sensor.snapshot()
    lcd.display(img)

    temp = sensor.temperature()
    lcd.draw_string(10, 10, "Temperature: {} C".format(temp), lcd.WHITE, lcd.BLACK)

Interpretation of key points:
This program uses the LCD display function of OpenMV Cam to display the image captured by the camera and the data from the temperature sensor on the LCD screen.
Use sensor.reset() to reset the camera sensor.
Use sensor.set_pixformat() to set the image pixel format to RGB565.
Use sensor.set_framesize() to set the image frame size to QVGA (320x240).
Use sensor.run(1) to start the camera.
Use lcd.init() to initialize the LCD display.
Use lcd.rotation(2) to set the LCD display rotation angle.
Use lcd.clear() to clear the contents of the LCD screen.
In an infinite loop, have the program continue to do the following:
capture an image using sensor.snapshot() and store it in the img variable.
Use lcd.display() to display the captured image on the LCD screen.
Use sensor.temperature() to read the temperature sensor data.
Use lcd.draw_string() to draw temperature information on the LCD screen.
In this example, the temperature information is plotted on the LCD screen at location (10, 10).
These practical application examples show how to use MicroPython and the LCD display capabilities of the OpenMV Cam. The first example shows a live display of a camera image. The second example shows displaying text information on the LCD. The third example shows how to display camera images and sensor data simultaneously on the LCD. These examples can help you understand how to leverage LCD display capabilities for a variety of applications, such as image processing, data visualization, and sensor monitoring. Depending on your specific needs, you can modify and extend these examples to implement more complex functionality and applications.

Case 7: One case uses OpenMV Cam to implement face recognition and display the recognition results on the LCD display. code show as below:

# 导入相关模块
import sensor, time, image, lcd

# 初始化摄像头和LCD
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
lcd.init()

# 加载人脸检测的级联分类器
face_cascade = image.HaarCascade("frontalface", stages=25)

# 循环检测人脸并在LCD上显示
while (True):
    # 捕获一帧图像
    img = sensor.snapshot()
    # 在图像上检测人脸
    objects = img.find_features(face_cascade, threshold=0.75, scale_factor=1.25)
    # 画出检测到的人脸区域
    for r in objects:
        img.draw_rectangle(r)
    # 在LCD上显示图像
    lcd.display(img)

Interpretation of key points:
This code uses the sensor module to control the camera, the image module to process images, and the lcd module to drive the LCD display.
The code uses the image.HaarCascade class to load a pre-trained cascade classifier for face detection, which can find the features of faces on images.
The code uses the image.find_features method to apply a classifier on the image and return a list containing the detected face regions.
The code uses the image.draw_rectangle method to draw the detected face area on the image for easy visualization.
This code uses the lcd.display method to display an image on the LCD display to implement the face recognition function.

Case 8: Another case uses OpenMV Cam to scan QR codes and display the scan results on the LCD display. code show as below:

# 导入相关模块
import sensor, image, time, lcd

# 初始化摄像头和LCD
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
lcd.init()

# 循环扫描二维码并在LCD上显示
while(True):
    # 捕获一帧图像
    img = sensor.snapshot()
    # 在图像上扫描二维码
    res = img.find_qrcodes()
    # 如果扫描到二维码,就在LCD上显示其内容
    if len(res) > 0:
        lcd.clear()
        lcd.draw_string(10,10,res[0].payload())
        lcd.display(img)
        time.sleep(2000)
        lcd.clear()
    else:
        # 如果没有扫描到二维码,就在LCD上显示"No QRCode"
        lcd.clear()
        lcd.draw_string(10,10,"No QRCode")
        lcd.display(img)

Interpretation of key points:
This code uses the image.find_qrcodes method to scan QR codes on the image and returns a list containing the scanned QR code objects.
Each QR code object has a payload attribute that represents its content.
This code uses the lcd.draw_string method to display text on the LCD display, such as the content of a QR code or "No QRCode".
This code uses the lcd.clear method to clear the contents of the LCD display for easy updating.

Case 9: Use OpenMV Cam to implement color tracking and display the tracking results on the LCD display. code show as below:

# 导入相关模块
import sensor, image, time, lcd

# 定义一个颜色阈值,表示要追踪的颜色范围
# 这里是红色的阈值,可以根据需要修改
red_threshold = (0, 100, 43, 127, -25, 127)

# 初始化摄像头和LCD
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
lcd.init()

# 循环追踪颜色并在LCD上显示
while(True):
    # 捕获一帧图像
    img = sensor.snapshot()
    # 在图像上找到符合颜色阈值的区域
    blobs = img.find_blobs([red_threshold])
    # 如果找到了符合颜色阈值的区域,就在LCD上显示其位置和大小
    if blobs:
        for b in blobs:
            # 画出区域的边界框
            img.draw_rectangle(b[0:4])
            # 画出区域的中心点
            img.draw_cross(b[5], b[6])
            # 在LCD上显示区域的位置和大小
            lcd.clear()
            lcd.draw_string(10,10,"x:%d y:%d w:%d h:%d"%(b[0],b[1],b[2],b[3]))
            lcd.display(img)
    else:
        # 如果没有找到符合颜色阈值的区域,就在LCD上显示"No color"
        lcd.clear()
        lcd.draw_string(10,10,"No color")
        lcd.display(img)

Interpretation of key points:
This code uses the image.find_blobs method to find areas on the image that meet the color threshold and returns a list containing area information.
Each area information is a tuple, including the location, size, center point and other data of the area.
The code uses the image.draw_rectangle and image.draw_cross methods to draw the bounding box and center point of the area on the image for easy visualization.
This code uses the lcd.draw_string method to display the location and size of the area on the LCD display, such as "x:10 y:20 w:30 h:40".

Please note that the above cases are only for expanding ideas and may contain errors or inapplicability. Different hardware platforms, usage scenarios and MicroPython versions may lead to different usage methods. In actual programming, you need to adjust it according to your hardware configuration and specific needs, and conduct multiple actual tests. It is important to ensure that the hardware is connected correctly and to understand the specifications and characteristics of the sensors and devices used.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/133575327