1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
import os, sys, gc, time
from media.sensor import * from media.display import * from media.media import *
from libs.PipeLine import PipeLine, ScopedTiming from libs.YOLO import YOLOv5 import ulab.numpy as np import struct
from ybUtils.YbUart import YbUart
uart = YbUart(baudrate=115200)
def task_detect_yolo_10(): """ 执行YOLO目标检测任务。 由指令 b'10' 触发。 """ print("\n--- 系统模式切换:进入 [YOLO目标检测] 模式 ---")
kmodel_path = "/data/best.kmodel" labels = ["stm"] confidence_threshold = 0.3 nms_threshold = 0.5 model_input_size = [320, 320]
pl = PipeLine(rgb888p_size=[224, 224], display_size=[640, 480], display_mode="lcd") pl.create()
yolo=YOLOv5(task_type="detect",mode="video",kmodel_path=kmodel_path,labels=labels,rgb888p_size=[224, 224],model_input_size=model_input_size,display_size=[640, 480],conf_thresh=confidence_threshold,nms_thresh=nms_threshold,max_boxes_num=50,debug_mode=0) yolo.config_preprocess() print("[YOLO目标检测] 初始化完成,开始运行...")
while True:
if uart: command = uart.read(decode=False) if command == b'100': print(f"[YOLO目标检测] 收到停止指令 {command},准备退出...") break
img = pl.get_frame() res = yolo.run(img)
if res is not None and len(res) > 0: for box_data in res: x_min, y_min, x_max, y_max = map(int, box_data[:4]) w = x_max - x_min h = y_max - y_min center_x = x_min + w // 2 center_y = y_min + h // 2 label_name = labels[int(box_data[5])] label_id = int(box_data[5])
header = b'\x3C\x3b'
payload_format = '>HHHHHHB' payload = struct.pack(payload_format, x_min, y_min, x_max, y_max, center_x, center_y, label_id) data_to_send = header + payload print(f"发送YOLO二进制数据 ({len(data_to_send)} bytes): {data_to_send.hex(' ')}")
pto_data = f"${x_min},{y_min},{x_max},{y_max},{center_x},{center_y},{label_name}#" print("发送串口数据:", pto_data)
if uart: uart.send(data_to_send)
yolo.draw_result(res, pl.osd_img) pl.show_image() gc.collect()
yolo.deinit() pl.destroy()
print("--- 系统模式切换:已退出 [YOLO目标检测] 模式 ---\n")
def task_detect_rects_1000(): """ 执行矩形检测任务。 由指令 b'1000' 触发。 """ print("\n--- 系统模式切换:进入 [矩形检测] 模式 ---")
sensor = None try: PICTURE_WIDTH = 400 PICTURE_HEIGHT = 240 DISPLAY_WIDTH = 640 DISPLAY_HEIGHT = 480
sensor = Sensor() sensor.reset() sensor.set_framesize(width=PICTURE_WIDTH, height=PICTURE_HEIGHT, chn=CAM_CHN_ID_0) sensor.set_pixformat(Sensor.RGB565, chn=CAM_CHN_ID_0)
Display.init(Display.ST7701, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, to_ide=True)
MediaManager.init() sensor.run() print("[矩形检测] 初始化完成,开始运行...")
while True: os.exitpoint()
if uart: command = uart.read(decode=False) if command == b'100': print(f"[矩形检测] 收到停止指令 {command},准备退出...") break
img = sensor.snapshot(chn=CAM_CHN_ID_0) for r in img.find_rects(threshold=8000): img.draw_rectangle(r.rect(), color=(40, 167, 225), thickness=2) for p in r.corners(): img.draw_circle(p[0], p[1], 8, color=(78, 90, 34))
corners = r.corners() if len(corners) == 4: p1 = corners[0] p2 = corners[1] p3 = corners[2] p4 = corners[3]
pto_data = f"${p1[0]},{p1[1]},{p2[0]},{p2[1]},{p3[0]},{p3[1]},{p4[0]},{p4[1]}#"
print("发送串口数据:", pto_data) uart.send(pto_data)
x = int((DISPLAY_WIDTH - PICTURE_WIDTH) / 2) y = int((DISPLAY_HEIGHT - PICTURE_HEIGHT) / 2) Display.show_image(img, x=x, y=y)
except Exception as e: print(f"[矩形检测] 任务执行出错: {e}") finally: print("[矩形检测] 正在清理资源...") if isinstance(sensor, Sensor): sensor.stop() Display.deinit() MediaManager.deinit() gc.collect() print("--- 系统模式切换:已退出 [矩形检测] 模式 ---\n")
if __name__ == "__main__":
print("系统启动成功,进入主循环调度器。") print("系统待机中,等待任务指令 (b'10', b'1000', b'100')...")
while True: try:
command = uart.read(decode=False)
if command == b'10': task_detect_yolo_10()
elif command == b'1000': task_detect_rects_1000()
elif command == b'100': print("系统已处于待机模式。") pass
else: if command: print(f"收到未知指令: {command}") time.sleep_ms(100)
except KeyboardInterrupt: print("用户通过Ctrl+C中断程序。") break except Exception as e: print(f"主调度器发生严重错误: {e}") break
print("程序已退出。")
|