有谁能帮我看看嘛,我用aidlux部署在自己的开发板上效果很差,但是在自己的电脑上用pt检测就很好
import cv2
import argparse
import subprocess
from cvs import
import aidlite_gpu
import numpy as np
from utils import detect_postprocess, preprocess_img, draw_detect_res # 请确保这些工具函数可用
pred = [
, # class 0 没检测到
[array([100, 150, 50, 80])], # class 1 检测到
, # class 2 没检测到
[array([200, 250, 40, 70])], # class 3 检测到
]
coco_class = [‘bottle-off’, ‘bottle-on’, ‘food’, ‘person’, ‘tap-off’, ‘tap-on’]
CLASS_BOTTLE_OFF = coco_class.index(‘bottle-off’) # 0
CLASS_BOTTLE_ON = coco_class.index(‘bottle-on’) # 1
CLASS_FOOD = coco_class.index(‘food’) # 2
CLASS_PERSON = coco_class.index(‘person’) # 3
CLASS_TAP_OFF = coco_class.index(‘tap-off’) # 4
CLASS_TAP_ON = coco_class.index(‘tap-on’) # 5
持续判定时长(秒)
PERSISTENCE_TIME = 5.0
初始化参数解析
parser = argparse.ArgumentParser()
parser.add_argument(–model, type=str, default=‘new.tflite’)
args = parser.parse_args()
模型参数设置
h, w = 640, 640
model_path = args.model
in_shape = [1 h w 3 4, ]
out_shape = [1 25200114] # 输出形状(根据实际模型调整)
初始化AIDLITE
aidlite = aidlite_gpu.aidlite()
ret = aidlite.ANNModel(model_path, in_shape, out_shape, 3, 0)
if not hasattr(ret, id) or ret.id != 1
print(模型加载失败,返回错误码, ret)
exit(1)
print(模型加载完成)
初始化摄像头
cap = cvs.VideoCapture(-1)
if cap is None
print(无法打开摄像头!)
exit(1)
else
print(摄像头打开成功)
frame = cap.read()
#ffmpeg
height, width, channel = frame.shape
frame_rate = 20.0
command = [‘ffmpeg’, # linux不用指定
‘-y’, ‘-an’,
‘-f’, ‘rawvideo’,
‘-vcodec’, ‘rawvideo’,
‘-pix_fmt’, ‘bgr24’, # 像素格式
‘-s’, {}x{}.format(width, height),
‘-r’, str(frame_rate), # 自己的摄像头的fps是0,若用自己的notebook摄像头,设置为15、20、25都可。
‘-i’, ‘-’,
‘-cv’, ‘libx264’, # 视频编码方式
‘-pix_fmt’, ‘yuv420p’,
‘-preset’, ‘fast’,
‘-f’, ‘flv’, # flv rtsp
‘test_save.flv’] # rtsp rtmp
pipe = subprocess.Popen(command, shell=False, stdin=subprocess.PIPE)
行为判定定时器
bottle_timer = None
tap_timer = None
while True
frame = cap.read()
#if ret
#cvs.imwrite(tesimg,frame)
#else
#print(无法读取帧)
if frame is None
continue
# 预处理(请确保 preprocess_img 函数正确归一化、调整数据类型等)
img = preprocess_img(frame, target_shape=(640, 640), div_num=255, means=None, stds=None)
aidlite.setInput_Float32(img, 640, 640)
#print(预处理后图像尺寸, img.shape)
#print(预处理后图像数据类型, img.dtype)
# 执行推理
aidlite.invoke()
try
pred = aidlite.getOutput_Float32(0)
except Exception as e
print(f获取输出失败 {e})
pred = aidlite.getOutput_Float32(0)
pred = pred.reshape(1, 25200, 11)[0]
#pred = detect_postprocess(pred, frame.shape, [h, w, 3], conf_thres=0.25, iou_thres=0.45)
# 后处理获取检测结果
pred = detect_postprocess(pred, frame.shape, [h, w, 3], conf_thres=0.25, iou_thres=0.45)
# 绘制检测结果
res_img = draw_detect_res(frame, pred)
# 显示结果
cvs.imshow(res_img)
# 当前检测到的类别列表
classes = []
for class_id, boxes in enumerate(pred)
if len(boxes) 0
classes.append(class_id)
print(当前检测类别, classes)
now = time.time()
violation = None
print(f当前检测类别 {[coco_class[i] for i in classes]})
# 违规判断:饮食
if CLASS_PERSON in classes and CLASS_FOOD in classes
violation = eat_in_lab
print(触发违规:饮食)
# 违规判断:试剂瓶未盖
if CLASS_BOTTLE_ON in classes
if bottle_timer is None
bottle_timer = now
elif now - bottle_timer = PERSISTENCE_TIME
violation = bottle_not_closed
bottle_timer = None
else
bottle_timer = None
# 违规判断:水龙头未关
if CLASS_TAP_ON in classes
if tap_timer is None
tap_timer = now
elif now - tap_timer = PERSISTENCE_TIME
violation = tap_not_closed
tap_timer = None
else
tap_timer = None
# 若检测到违规,记录并触发后续处理
if violation
timestamp = time.strftime(%Y-%m-%d %H%M%S, time.localtime(now))
# 写入日志文件
with open(violations.txt, a) as logf
logf.write(f{timestamp} {violation}n)
print(fDetected violation {violation} at {timestamp})
# 可调用 FFmpeg 脚本剪辑并编码对应时段视频
#subprocess.call([.clip_and_encode.sh, timestamp])
# 将结果帧推送给 FFmpeg 进行实时编码
pipe.stdin.write(res_img.tostring())
pipe.terminate()