远程控制 - 作为远程设备

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 远程控制 - 作为远程设备
#
# 此脚本将您的OpenMV摄像头配置为可被远程控制的协处理器,
# 控制设备可以是Arduino、ESP8266/ESP32、RaspberryPi等微控制器或计算机,
# 甚至另一台OpenMV摄像头。
#
# 此脚本设计用于与“popular_features_as_the_controller_device.py”配对使用,

import image
import math
import rpc
import csi
import struct

csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QVGA)
csi0.snapshot(time=2000)

# 上述RPC库已安装在您的OpenMV Cam上,并提供了多个类,
# 使您的OpenMV摄像头可以通过CAN、I2C、SPI或UART进行控制。

################################################################
# 选择您希望通过哪种接口控制您的OpenMV Cam。
################################################################

# 取消注释以下行以设置您的OpenMV Cam通过CAN进行控制。
#
# * message_id - 用于在CAN总线上传输数据的CAN消息(11位)。
# * bit_rate - CAN比特率。
# * sample_point - Tseg1/Tseg2比率。通常为75%。(50.0、62.5、75、87.5等)
#
# 注意:主设备和从设备的消息ID和CAN比特率必须匹配。将主设备的CAN高连接到从设备。
# CAN高对应主CAN低对应从CAN低。CAN总线必须用120欧姆电阻端接。
#
# interface = rpc.rpc_can_slave(message_id=0x7FF, bit_rate=250000, sample_point=75)

# 取消注释以下行以设置您的OpenMV Cam通过I2C进行控制。
#
# * slave_addr - I2C地址。
#
# 注意:主设备和从设备地址必须匹配。将主设备的scl连接到从设备的scl,主设备的sda
# 连接到从设备的sda。必须使用外部上拉电阻。最后,两个设备必须共地。
#
# interface = rpc.rpc_i2c_slave(slave_addr=0x12)

# 取消注释以下行以设置您的OpenMV Cam通过SPI进行控制。
#
# * cs_pin - 从设备选择引脚。
# * clk_polarity - 空闲时钟电平(0或1)。
# * clk_phase - 在时钟的第一个边沿(0)或第二个边沿(1)采样数据。
#
# 注意:主设备和从设备的设置必须匹配。将CS、SCLK、MOSI、MISO分别连接到CS、SCLK、MOSI、MISO。
# 最后,两个设备必须共地。
#
# interface = rpc.rpc_spi_slave(cs_pin="P3", clk_polarity=1, clk_phase=0)

# 取消注释以下行以设置您的OpenMV Cam通过UART进行控制。
#
# * baudrate - 串行波特率。
#
# 注意:主从设备的波特率必须一致。将主设备的tx连接到从设备的rx,主设备的rx连接到
# 从设备的tx。最后,两个设备必须共地。
#
interface = rpc.rpc_uart_slave(baudrate=115200)

################################################################
# 回调
################################################################

# 以下回调函数使用的辅助方法。


def draw_detections(img, dects):
    for d in dects:
        c = d.corners
        c_len = len(c)
        for i in range(c_len):
            img.draw_line(c[(i + 0) % c_len] + c[(i + 1) % c_len], color=(0, 255, 0))
        img.draw_rectangle(d.rect, color=(255, 0, 0))


# 远程控制通过回调方法实现,控制器设备
# 通过本设备的rpc模块调用这些方法。回调
# 是接收bytes()对象作为参数并返回
# bytes()对象作为结果的函数。rpc模块
# 负责在链路上传输这些bytes()对象。
# bytes()可能是micropython int的最大大小。


# 调用时返回视野中最大人脸的x、y、w和h。
#
# data未使用
def face_detection(data):
    csi0.pixformat(csi.GRAYSCALE)
    csi0.framesize(csi.QVGA)
    img = csi0.snapshot()
    faces = (
        img.gamma_corr(contrast=1.5)
        .find_features(image.HaarCascade("/rom/haarcascade_frontalface.cascade"))
    )
    if not faces:
        return bytes()  # 没有检测结果。
    for f in faces:
        img.draw_rectangle(f, color=(255, 255, 255))
    out_face = max(faces, key=lambda f: f[2] * f[3])
    return struct.pack("<HHHH", out_face[0], out_face[1], out_face[2], out_face[3])


# 调用时返回最大二维码的载荷字符串,
# 范围限于OpenMV摄像头的视野内。
#
# data未使用
def qrcode_detection(data):
    csi0.pixformat(csi.RGB565)
    csi0.framesize(csi.VGA)
    csi0.window((320, 240))
    img = csi0.snapshot()
    codes = img.find_qrcodes()
    if not codes:
        return bytes()  # 没有检测结果。
    draw_detections(img, codes)
    return max(codes, key=lambda c: c.w * c.h).payload.encode()


# 调用时返回视野内所有二维码的json对象列表。
#
# data未使用
def all_qrcode_detection(data):
    csi0.pixformat(csi.RGB565)
    csi0.framesize(csi.VGA)
    csi0.window((320, 240))
    img = csi0.snapshot()
    codes = img.find_qrcodes()
    if not codes:
        return bytes()  # 没有检测结果。
    draw_detections(img, codes)
    return str(codes).encode()


# 调用时返回OpenMV摄像头视野内最大AprilTag的
# x/y质心、ID号和旋转角度。
#
# data未使用
def apriltag_detection(data):
    csi0.pixformat(csi.RGB565)
    csi0.framesize(csi.QQVGA)
    img = csi0.snapshot()
    tags = img.find_apriltags()
    if not tags:
        return bytes()  # 没有检测结果。
    draw_detections(img, tags)
    output_tag = max(tags, key=lambda t: t.w * t.h)
    return struct.pack(
        "<HHHH",
        output_tag.cx,
        output_tag.cy,
        output_tag.id,
        int(math.degrees(output_tag.rotation)),
    )


# 调用时返回视野内所有AprilTag的json对象列表。
#
# data未使用
def all_apriltag_detection(data):
    csi0.pixformat(csi.RGB565)
    csi0.framesize(csi.QQVGA)
    img = csi0.snapshot()
    tags = img.find_apriltags()
    if not tags:
        return bytes()  # 没有检测结果。
    draw_detections(img, tags)
    return str(tags).encode()


# 调用时返回最大Data Matrix码的载荷字符串,
# 范围限于OpenMV摄像头的视野内。
#
# data未使用
def datamatrix_detection(data):
    csi0.pixformat(csi.RGB565)
    csi0.framesize(csi.VGA)
    csi0.window((320, 240))
    img = csi0.snapshot()
    codes = img.find_datamatrices()
    if not codes:
        return bytes()  # 没有检测结果。
    draw_detections(img, codes)
    return max(codes, key=lambda c: c.w * c.h).payload.encode()


# 调用时返回视野内所有Data Matrix码的json对象列表。
#
# data未使用
def all_datamatrix_detection(data):
    csi0.pixformat(csi.RGB565)
    csi0.framesize(csi.VGA)
    csi0.window((320, 240))
    img = csi0.snapshot()
    codes = img.find_datamatrices()
    if not codes:
        return bytes()  # 没有检测结果。
    draw_detections(img, codes)
    return str(codes).encode()


# 调用时返回最大条形码的载荷字符串,
# 范围限于OpenMV摄像头的视野内。
#
# data未使用
def barcode_detection(data):
    csi0.pixformat(csi.GRAYSCALE)
    csi0.framesize(csi.VGA)
    csi0.window((csi0.width(), csi0.height() // 8))
    img = csi0.snapshot()
    codes = img.find_barcodes()
    if not codes:
        return bytes()  # 没有检测结果。
    return max(codes, key=lambda c: c.w * c.h).payload.encode()


# 调用时返回视野中所有条形码的json条形码对象的json列表。
#
# data未使用
def all_barcode_detection(data):
    csi0.pixformat(csi.GRAYSCALE)
    csi0.framesize(csi.VGA)
    csi0.window((csi0.width(), csi0.height() // 8))
    img = csi0.snapshot()
    codes = img.find_barcodes()
    if not codes:
        return bytes()  # 没有检测结果。
    return str(codes).encode()


# 调用时返回最大色块的x/y质心,
# 范围限于OpenMV摄像头的视野内。
#
# data是6字节的颜色追踪阈值元组:L_MIN、L_MAX、A_MIN、A_MAX、B_MIN、B_MAX。
def color_detection(data):
    csi0.pixformat(csi.RGB565)
    csi0.framesize(csi.QVGA)
    thresholds = struct.unpack("<bbbbbb", data)
    img = csi0.snapshot()
    blobs = img.find_blobs(
        [thresholds], pixels_threshold=500, area_threshold=500, merge=True, margin=20
    )
    if not blobs:
        return bytes()  # 没有检测结果。
    for b in blobs:
        img.draw_detection(b, color1=(255, 0, 0), color2=(0, 255, 0))
    out_blob = max(blobs, key=lambda b: b.density)
    return struct.pack("<HH", out_blob.cx, out_blob.cy)


# 调用时通过一次RPC调用返回来自OpenMV摄像头的
# jpeg压缩图像。
#
# data未使用
def jpeg_snapshot(data):
    csi0.pixformat(csi.RGB565)
    csi0.framesize(csi.QVGA)
    img = csi0.snapshot()
    img.to_jpeg(quality=90)
    return img.bytearray()


# 注册回调。

interface.register_callback(face_detection)
interface.register_callback(qrcode_detection)
interface.register_callback(all_qrcode_detection)
interface.register_callback(apriltag_detection)
interface.register_callback(all_apriltag_detection)
interface.register_callback(datamatrix_detection)
interface.register_callback(all_datamatrix_detection)
interface.register_callback(barcode_detection)
interface.register_callback(all_barcode_detection)
interface.register_callback(color_detection)
interface.register_callback(jpeg_snapshot)

# 一旦所有回调都已注册,我们就可以开始
# 处理远程事件。interface.loop()不会返回。

interface.loop()

results matching ""

    No results matching ""