Files
wrobot/v1.4/maixcam/main.py
T

80 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# main.py - 主业务逻辑(人脸跟踪、舵机控制、LED)
from maix import camera, display, image, nn, app, sys
from maix import uart, pinmap, time, err, gpio
from maix import app, nn
import traceback
from config import *
from test_face import *
from test_ball import *
# ------------------------------
# 系统初始化
# ------------------------------
def init_system():
"""初始化相机、舵机、串口、LED"""
global serial_dev # 声明使用全局serial_dev
init_led()
# 人脸检测器
detector = nn.YOLOv8(model="/root/models/yolov8n_face.mud", dual_buff=True)
img_width = detector.input_width()
img_height = detector.input_height()
print(f"[系统] 图像尺寸: {img_width}x{img_height}")
image_center = (img_width // 2, img_height // 2)
# 相机/显示器
cam = camera.Camera(img_width, img_height, detector.input_format())
disp = display.Display()
# 串口(舵机)
pin_function = {"A19": "UART1_TX", "A18": "UART1_RX"}
device = "/dev/ttyS1"
for pin, func in pin_function.items():
err.check_raise(pinmap.set_pin_function(pin, func),
f"Failed set pin{pin} function to {func}")
serial_dev = uart.UART(device, 115200)
# 初始化值 距离中间点像素距离
initial_cmd = f"DX:0,DY:0;"
serial_dev.write_str(initial_cmd)
print(f"[舵机] 初始角度: {initial_cmd}")
time.sleep(1)
return {
"detector": detector, "cam": cam, "disp": disp, "serial_dev": serial_dev,
"img_width": img_width, "img_height": img_height, "image_center": image_center
}
def callback(data: list[float], len: int):
global track_mode
# 2. 找到概率最大的关键词索引
max_prob = max(data) # 获取最大概率值
max_index = data.index(max_prob) # 获取最大概率对应的kw索引
# 3. 判断:仅当最大概率>0时,输出对应value
if max_prob > 0:
# 根据索引映射到kw_list的value(kw0→0,kw1→1,kw2→2)
output_value = [0, 1, 2][max_index]
print(f"识别结果:{output_value}") # 输出最终的value
track_mode = output_value
# ------------------------------
# 系统执行
# ------------------------------
try:
# 初始化系统资源
system_resources = init_system()
# 进入主循环
# trace_face_main(system_resources)
trace_ball_main(system_resources)
except Exception as e:
print(f"[异常] 程序异常: {e}")
traceback.print_exc()