Example: 50-OpenMV-Boards/99-Tests/selftest.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 自检示例
#
# 此示例展示您的OpenMV Cam在出厂前如何完成自检
# 每台OpenMV Cam都应通过此项测试

import sensor
import pyb


def test_int_adc():
    adc = pyb.ADCAll(12)
    # 测试VBAT(电池电压)
    vbat = adc.read_core_vbat()
    vbat_diff = abs(vbat - 3.3)
    if vbat_diff > 0.1:
        raise Exception("INTERNAL ADC TEST FAILED VBAT=%fv" % vbat)

    # 测试VREF(参考电压)
    vref = adc.read_core_vref()
    vref_diff = abs(vref - 1.2)
    if vref_diff > 0.1:
        raise Exception("INTERNAL ADC TEST FAILED VREF=%fv" % vref)
    adc = None
    print("INTERNAL ADC TEST PASSED...")


def test_color_bars():
    sensor.reset()
    # 设置传感器参数
    sensor.set_brightness(0)
    sensor.set_saturation(3)
    sensor.set_gainceiling(8)
    sensor.set_contrast(2)

    # 设置传感器像素格式
    sensor.set_framesize(sensor.QVGA)
    sensor.set_pixformat(sensor.RGB565)

    # 启用色条测试模式
    sensor.set_colorbar(True)

    # 跳过几帧以使传感器稳定下来
    for i in range(0, 100):
        image = sensor.snapshot()

    # 色条阈值
    t = [
        lambda r, g, b: r < 70 and g < 70 and b < 70,  # 黑色
        lambda r, g, b: r < 70 and g < 70 and b > 200,  # 蓝色
        lambda r, g, b: r > 200 and g < 70 and b < 70,  # 红色
        lambda r, g, b: r > 200 and g < 70 and b > 200,  # 紫色
        lambda r, g, b: r < 70 and g > 200 and b < 70,  # 绿色
        lambda r, g, b: r < 70 and g > 200 and b > 200,  # 青色
        lambda r, g, b: r > 200 and g > 200 and b < 70,  # 黄色
        lambda r, g, b: r > 200 and g > 200 and b > 200,
    ]  # 白色

    # OV7725的色条显示为反相
    if sensor.get_id() == sensor.OV7725:
        t = t[::-1]

    # 320x240图像含8条色带,每条约40像素宽。
    # 我们从帧缓冲区中心开始,对
    # 每个色条中心区域的10个采样像素取平均值。
    for i in range(0, 8):
        avg = (0, 0, 0)
        idx = 40 * i + 20  # 中心 of colorbars
        for off in range(0, 10):  # 取10像素平均值
            rgb = image.get_pixel(idx + off, 120)
            avg = tuple(map(sum, zip(avg, rgb)))

        if not t[i](avg[0] / 10, avg[1] / 10, avg[2] / 10):
            raise Exception(
                "COLOR BARS TEST FAILED."
                "BAR#(%d): RGB(%d,%d,%d)"
                % (i + 1, avg[0] / 10, avg[1] / 10, avg[2] / 10)
            )

    print("COLOR BARS TEST PASSED...")


if __name__ == "__main__":
    print("")
    test_int_adc()
    test_color_bars()

results matching ""

    No results matching ""