popular_features_as_the_controller_device.py
# この作品はMITライセンスの下で提供されています。
# Copyright (c) 2013-2023 OpenMV LLC. 全著作権所有。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# リモートコントロール - コントローラーデバイスとして
#
# このスクリプトは、RPCライブラリを使用して別のOpenMV Camをリモート制御するようにOpenMV Camを設定します
# このスクリプトは、pyb モジュールを実装している任意のmicropythonボードで実行して
# OpenMV Camをリモート制御できます。
#
# このスクリプトは"popular_features_as_the_remote_device.py"とペアで使用するよう設計されています。
import json
import rpc
import struct
# 上記のRPCライブラリはOpenMV Camにインストールされており、以下のための複数のクラスを提供します
# OpenMV Camを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 Highをスレーブの
# CAN Highに、マスターのCAN Lowをスレーブのcan loに接続してください。CANバスは120オームで終端する必要があります。
#
# 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を
# スレーブのsdaに接続してください。外部プルアップ抵抗を使用する必要があります。最後に、両方のデバイスはグラウンドを共有する必要があります。
#
# interface = rpc.rpc_i2c_master(slave_addr=0x12, rate=100000)
# 以下の行のコメントを外すと、OpenMV CamをSPI経由で制御するように設定できます。
#
# * cs_pin - スレーブセレクトピン。
# * freq - SPIバスクロック周波数。
# * clk_polarity - アイドル時のクロックレベル(0または1)。
# * clk_phase - クロックの最初のエッジ(0)または2番目のエッジ(1)でデータをサンプリングします。
#
# 注: マスターとスレーブの設定は一致させる必要があります。CS、SCLK、MOSI、MISOをCS、SCLK、MOSI、MISOに接続してください。
# 最後に、両方のデバイスは共通のグラウンドを共有する必要があります。
#
# interface = rpc.rpc_spi_master(cs_pin="P3", freq=10000000, clk_polarity=1, clk_phase=0)
# 以下の行のコメントを外すと、OpenMV CamをUART経由で制御するように設定できます。
#
# * baudrate - シリアルボーレート。
#
# 注: マスターとスレーブのボーレートは一致させる必要があります。マスターのtxをスレーブのrxに、マスターのrxを
# スレーブのtxに接続してください。最後に、両方のデバイスは共通のグラウンドを共有する必要があります。
#
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
# ループ内でリモート関数を実行します。以下のリモート関数から1つを選択し、コメントを外してください。
# カメラがモードを切り替える必要がある場合、複数を同時に実行すると
# 実行ごとに動作が遅くなることがあります。
while True:
exe_face_detection() # 顔はカメラから約2フィート離れている必要があります。
# exe_qrcode_detection() # QRコードを約2フィート離れた場所に置いてください。
# exe_all_qrcode_detection() # QRコードを約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()