logo

VGA格式,使用img = sensor.snapshot().copy(),报错内存

2024-05-10 12:44
2
gk2p
import sensor, image, time, json, utime
from machine import UART

sensor.reset()
sensor.set_pixformat(sensor.RGB565)  # 灰度更快
sensor.set_framesize(sensor.VGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()


# 颜色阈值
red_threshold = (76, 53, 58, 15, -21, 52) # 红色激光点

uart = UART(3, 115200)  # 初始化串口

# 初始化列表
coordinates_red = [("", "")] * 1  # 初始化红色激光点列表,假设存储1个点
enum_data = [("", "")] * 4  # 存储矩形角点,假设存储4个角点

# 寻找像素最多的红色色块
def find_largest_red_blob(img):
    largest_blob = None
    max_pixels = 0
    for blob in img.find_blobs([red_threshold], pixels_threshold=10, area_threshold=20, merge=True):
        if blob.pixels() > max_pixels:
            largest_blob = blob
            max_pixels = blob.pixels()
    return largest_blob


while True:
    clock.tick()
    img = sensor.snapshot().copy()
   # enum_data.clear()  # 每次循环开始时清空矩形数据列表
   # coordinates_red.clear()  # 清空红色激光点列表


    largest_blob = find_largest_red_blob(img)
    if largest_blob:
        cx = largest_blob.cx()
        cy = largest_blob.cy()
        img.draw_circle(cx, cy, 2, color=(0, 255, 255), thickness=2)
        cx_str = "{:03d}".format(cx)  # 将坐标格式化为3位数的字符串
        cy_str = "{:03d}".format(cy)  # 将坐标格式化为3位数的字符串
        coordinates_red[0] = (cx_str, cy_str)
        #print("Blob at ({}, {})".format(cx, cy))

    r = img.find_rects(threshold=26000)  # 找到矩形
    if r:
        r = r[0]  # 仅处理第一个矩形
        for i, p in enumerate(r.corners()):
            img.draw_circle(p[0], p[1], 2, color = (0, 255, 0))
            px = "{:03d}".format(p[0])  # 将横坐标转换为3位数的字符串
            py = "{:03d}".format(p[1])  # 将纵坐标转换为3位数的字符串
            enum_data[i] = (px, py)
        #print("rect:({})".format(enum_data))

    del img

    # 发送所有收集的数据
#    if coordinates_red:
#        uart.write("R" + str(coordinates_red) +"250"+"E" + "\r\n")
#        #coordinates_red.clear()  # 清空红色激光点列表

    uart.write("T"+str(enum_data)+str(coordinates_red)+"E"+"E"+"E"+"\r\n")
    print(str(enum_data))
    print(str(coordinates_red))
    print("FPS %f" % clock.fps())
    print("\r\n")

    utime.sleep_ms(10)  # 延迟100毫秒


请在这里粘贴代码
447a79c7-8981-4a03-999b-aaf2f44970ad.png
2
gk2p

板子信息为:板子:OpenMV Cam H7(STM32H743)

传感器:OV7725固件版本:4.5.4-[最新]

2024-05-13 20:02
I
网红小智智 星瞳科技

在OpenMV4 H7 使用VGA的情况下用的是JPEG是不能运行算法的。在OpenMV4 H7 Plus上是可以运行算法,因为内存大。