Example: 08-RPC-Library/34-Remote-Control/popular_features_as_the_controller_device.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 远程控制 - 作为控制器设备
#
# 此脚本配置您的OpenMV Cam以使用RPC远程控制另一台OpenMV Cam
# 库。本脚本可由任何实现pyb模块的micropython板运行,
# 远程控制OpenMV摄像头。
#
# 本脚本设计用于与“popular_features_as_the_remote_device.py”配对使用。
import json
import rpc
import struct
# 上述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 high and master can low to slave can lo. The can bus must be terminated with 120 ohms.
#
# interface = rpc.rpc_can_master(message_id=0x7FF, bit_rate=250000, sample_point=75)
# 取消注释以下行以设置您的OpenMV Cam通过I2C进行控制。
#
# * slave_addr - I2C地址。
# * rate - I2C总线时钟频率。
#
# 注意:主设备和从设备地址必须匹配。将主设备的scl连接到从设备的scl,主设备的sda
# to slave sda. You must use external pull ups. Finally, both devices must share a ground.
#
# interface = rpc.rpc_i2c_master(slave_addr=0x12, rate=100000)
# 取消注释以下行以设置您的OpenMV Cam通过SPI进行控制。
#
# * cs_pin - 从设备选择引脚。
# * freq - SPI总线时钟频率。.
# * clk_polarity - 空闲时钟电平(0或1)。
# * clk_phase - 在时钟的第一个边沿(0)或第二个边沿(1)采样数据。
#
# 注意:主设备和从设备的设置必须匹配。将CS、SCLK、MOSI、MISO分别连接到CS、SCLK、MOSI、MISO。
# Finally, both devices must share a common ground.
#
# interface = rpc.rpc_spi_master(cs_pin="P3", freq=10000000, clk_polarity=1, clk_phase=0)
# 取消注释以下行以设置您的OpenMV Cam通过UART进行控制。
#
# * baudrate - 串行波特率。
#
# 注意:主从设备的波特率必须一致。将主设备的tx连接到从设备的rx,主设备的rx连接到
# slave tx. Finally, both devices must share a common ground.
#
interface = rpc.rpc_uart_master(baudrate=115200)
##############################################################
# 回调处理程序
##############################################################
def exe_face_detection():
result = interface.call("face_detection")
if result is not None and len(result):
print(
"Largest Face Detected [x=%d, y=%d, w=%d, h=%d]"
% struct.unpack("<HHHH", result)
)
def exe_qrcode_detection():
result = interface.call("qrcode_detection")
if result is not None and len(result):
print(bytes(result).decode())
def exe_all_qrcode_detection():
result = interface.call("all_qrcode_detection")
if result is not None and len(result):
print("QR Codes Detected:")
for obj in json.loads(result):
print(obj)
def exe_apriltag_detection():
result = interface.call("apriltag_detection")
if result is not None and len(result):
print(
"Largest Tag Detected [cx=%d, cy=%d, id=%d, rot=%d]"
% struct.unpack("<HHHH", result)
)
def exe_all_apriltag_detection():
result = interface.call("all_apriltag_detection")
if result is not None and len(result):
print("Tags Detected:")
for obj in json.loads(result):
print(obj)
def exe_datamatrix_detection():
result = interface.call("datamatrix_detection")
if result is not None and len(result):
print(bytes(result).decode())
def exe_all_datamatrix_detection():
result = interface.call("all_datamatrix_detection")
if result is not None and len(result):
print("Data Matrices Detected:")
for obj in json.loads(result):
print(obj)
def exe_barcode_detection():
result = interface.call("barcode_detection")
if result is not None and len(result):
print(bytes(result).decode())
def exe_all_barcode_detection():
result = interface.call("all_barcode_detection")
if result is not None and len(result):
print("Bar Codes Detected:")
for obj in json.loads(result):
print(obj)
def exe_color_detection():
thresholds = (30, 100, 15, 127, 15, 127) # 通用红色阈值
# thresholds = (30, 100, -64, -8, -32, 32) # 通用绿色阈值
# thresholds = (0, 30, 0, 64, -128, 0) # 通用蓝色阈值
result = interface.call("color_detection", struct.pack("<bbbbbb", *thresholds))
if result is not None and len(result):
print("Largest Color Detected [cx=%d, cy=%d]" % struct.unpack("<HH", result))
number = 0
def exe_jpeg_snapshot():
global number
result = interface.call("jpeg_snapshot")
if result is not None:
name = "snapshot-%05d.jpg" % number
print("Writing jpeg %s..." % name)
with open(name, "wb") as snap:
snap.write(result)
number += 1
# 在循环中执行远程功能。请选择并取消注释下面的一个远程功能。
# 同时执行多个功能可能会运行缓慢,如果摄像头需要更改摄像头模式
# 每次执行时。
while True:
exe_face_detection() # 人脸应距离约2英尺。
# exe_qrcode_detection() # 将二维码放置在约2英尺远。
# exe_all_qrcode_detection() # 将二维码放置在约2英尺远。
# exe_apriltag_detection()
# exe_all_apriltag_detection()
# exe_datamatrix_detection() # 将数据矩阵放置在约2英尺远处。
# exe_all_datamatrix_detection() # 将数据矩阵放置在约2英尺远处。
# exe_barcode_detection() # 将条形码放置在约2英尺远处。
# exe_all_barcode_detection() # 将条形码放置在约2英尺远处。
# exe_color_detection()
# exe_jpeg_snapshot()