重现步骤
from libs.PipeLine import PipeLine, ScopedTiming
from libs.YOLO import YOLOv8
import os,sys,gc
import ulab.numpy as np
import image
from machine import UART, FPIOA
from media.sensor import *
sensor=Sensor(id=0, fps=60, vflip=True)
if __name__=="__main__":
# 显示模式,默认"hdmi",可以选择"hdmi"和"lcd"
display_mode="lcd"
rgb888p_size=[640,640]
if display_mode=="lcd":
display_size=[640,480]
else:
display_size=[640,640]
kmodel_path="/data/best.kmodel"
labels = ["1","2","3","4","5","6"]
confidence_threshold = 0.8
nms_threshold=0.45
model_input_size=[640,640]
# 自定义六个区域的坐标范围 (x1, y1, x2, y2),基于模型输入尺寸 [640, 640]
custom_regions = [
(0, 0, 97,640), # 区域0: 左上
(97, 0, 208, 640), # 区域1: 中上
(208, 0, 319, 640), # 区域2: 右上
(319, 0, 430, 640), # 区域3: 左下
(430, 0, 520, 640), # 区域4: 中下
(520, 0, 640, 640), # 区域5: 右下
]
fpioa = FPIOA()
fpioa.set_function(3, FPIOA.UART1_TXD)
fpioa.set_function(4, FPIOA.UART1_RXD)
uart = UART(UART.UART1, 115200)
# 初始化PipeLine
pl=PipeLine(rgb888p_size=rgb888p_size,display_size=display_size,display_mode=display_mode)
pl.create(sensor=sensor,hmirror=True)
# 初始化YOLOv8实例
yolo=YOLOv8(task_type="detect",mode="video",kmodel_path=kmodel_path,labels=labels,rgb888p_size=rgb888p_size,model_input_size=model_input_size,display_size=display_size,conf_thresh=confidence_threshold,nms_thresh=nms_threshold,max_boxes_num=50,debug_mode=0)
yolo.config_preprocess()
detection_regions = [{} for _ in range(6)]
frame_counter = 0
ANALYSIS_FRAMES = 5
try:
while True:
os.exitpoint()
with ScopedTiming("total",1):
# 逐帧推理
img=pl.get_frame()
res=yolo.run(img)
for det in res:
x1, y1, x2, y2 = det[:4]
cx = (x1 + x2) / 2
cy = (y1 + y2) / 2
# 判断目标中心点位于哪个自定义区域
region_idx = -1 # 默认值,表示未匹配到任何区域
for i, region in enumerate(custom_regions):
rx1, ry1, rx2, ry2 = region
if rx1 <= cx < rx2 and ry1 <= cy < ry2:
region_idx = i
break
if 0 <= region_idx < 6:
label = labels[int(det[5])]
detection_regions[region_idx][label] = detection_regions[region_idx].get(label, 0) + 1
frame_counter += 1
if frame_counter >= ANALYSIS_FRAMES:
output = []
for region in detection_regions:
if region:
output.append(max(region, key=region.get))
else:
output.append("0") # 区域内没有识别内容,返回0
output_str = " ".join(output)
uart.write(output_str)
print(f"Sent: {output_str}")
detection_regions = [{} for _ in range(6)]
frame_counter = 0
gc.collect()
yolo.draw_result(res,pl.osd_img)
pl.show_image()
except Exception as e:
print(e)
finally:
yolo.deinit()
pl.destroy()
uart.deinit()
from libs.PipeLine import PipeLine, ScopedTiming
from libs.YOLO import YOLOv8
import os,sys,gc
import ulab.numpy as np
import image
from machine import UART, FPIOA
if __name__=="__main__":
# 显示模式,默认"hdmi",可以选择"hdmi"和"lcd"
display_mode="lcd"
rgb888p_size=[640,640]
if display_mode=="lcd":
display_size=[640,480]
else:
display_size=[640,640]
kmodel_path="/data/best.kmodel"
labels = ["1","2","3","4","5","6"]
confidence_threshold = 0.6
nms_threshold=0.45
model_input_size=[640,640]
# 自定义六个区域的坐标范围 (x1, y1, x2, y2),基于模型输入尺寸 [640, 640]
custom_regions = [
(0, 0, 251,400), # 区域0: 左上
(251, 0, 398, 400), # 区域1: 中上
(398, 0, 640, 400), # 区域2: 右上
(0, 400, 259, 640), # 区域3: 左下
(259, 400, 385, 640), # 区域4: 中下
(385, 400, 640, 640), # 区域5: 右下
]
fpioa = FPIOA()
fpioa.set_function(44, FPIOA.UART2_TXD)
fpioa.set_function(45, FPIOA.UART2_RXD)
uart = UART(UART.UART2, 115200)
# 初始化PipeLine
pl=PipeLine(rgb888p_size=rgb888p_size,display_size=display_size,display_mode=display_mode)
pl.create()
# 初始化YOLOv8实例
yolo=YOLOv8(task_type="detect",mode="video",kmodel_path=kmodel_path,labels=labels,rgb888p_size=rgb888p_size,model_input_size=model_input_size,display_size=display_size,conf_thresh=confidence_threshold,nms_thresh=nms_threshold,max_boxes_num=50,debug_mode=0)
yolo.config_preprocess()
detection_regions = [{} for _ in range(6)]
frame_counter = 0
ANALYSIS_FRAMES = 5
try:
while True:
os.exitpoint()
with ScopedTiming("total",1):
# 逐帧推理
img=pl.get_frame()
res=yolo.run(img)
for det in res:
x1, y1, x2, y2 = det[:4]
cx = (x1 + x2) / 2
cy = (y1 + y2) / 2
# 判断目标中心点位于哪个自定义区域
region_idx = -1 # 默认值,表示未匹配到任何区域
for i, region in enumerate(custom_regions):
rx1, ry1, rx2, ry2 = region
if rx1 <= cx < rx2 and ry1 <= cy < ry2:
region_idx = i
break
if 0 <= region_idx < 6:
label = labels[int(det[5])]
detection_regions[region_idx][label] = detection_regions[region_idx].get(label, 0) + 1
frame_counter += 1
if frame_counter >= ANALYSIS_FRAMES:
output = []
for region in detection_regions:
if region:
output.append(max(region, key=region.get))
else:
output.append("")
output_str = " ".join([x for x in output if x])
if output_str:
uart.write(output_str)
print(f"Sent: {output_str}")
detection_regions = [{} for _ in range(6)]
frame_counter = 0
gc.collect()
yolo.draw_result(res,pl.osd_img)
pl.show_image()
except Exception as e:
sys.print_exception(e)
finally:
yolo.deinit()
pl.destroy()
uart.deinit()
`期待结果和实际结果`
<!-- 你期待的结果是什么?实际看到的结果是什么? -->
`软硬件版本信息`
<!-- 硬件版本信息?软件版本信息? -->
`错误日志`
<!-- 是否有任何错误信息或日志?请附上相关内容。 -->
`尝试解决过程`
<!-- 你为解决这个问题尝试过哪些方法?请提供更多详细信息。 -->
`补充材料`
<!-- 请提供其他相关信息(如代码片段、配置文件、截图等)。 -->