作者小头像 Lv.1
3 成长值

个人介绍

这个人很懒,什么都没有留下

感兴趣或擅长的领域

暂无数据
个人勋章
TA还没获得勋章~
成长雷达
0
3
0
0
0

个人资料

个人介绍

这个人很懒,什么都没有留下

感兴趣或擅长的领域

暂无数据

达成规则

他的回复:
import osimport torchimport torch_npufrom torch_npu.contrib import transfer_to_npufrom customize_service import yolov10_detectionimport json, numpy as npimport cv2import time # 引入time模块image_name = "02_Short_Img.bmp"test_path = f"data/{image_name}"service = yolov10_detection(model_name="yolov10", model_path="weights/best.pt")post_data = {"input_txt": {os.path.basename(test_path): open(test_path, "rb")}}# 前处理service._preprocess(post_data)# 记录检测前的时间start_time = time.time()# 推理data = service._inference('ok')# 记录检测后的时间end_time = time.time()# 后处理result = service._postprocess(data)# 计算时间差elapsed_time = end_time - start_timeprint(f"Detection completed in {elapsed_time:.2f} seconds")# 将结果保存为JSON文件with open("result.json", "w") as f: json.dump(result, f, indent=4) print("result.json saved.")# 生成一张图片,将检测结果画在图片上image = cv2.imread(test_path)# 定义一些绘图参数font = cv2.FONT_HERSHEY_SIMPLEXfont_scale = 0.5font_color = (0, 255, 0)line_type = 2# 遍历检测结果并绘制矩形框和类别名称for i in range(len(result['detection_boxes'])): box = result['detection_boxes'][i] label = result['detection_classes'][i] score = result['detection_scores'][i] ymin, xmin, ymax, xmax = box # 画矩形框 cv2.rectangle(image, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 0, 0), 2) # 添加标签和置信度 label_text = f"{label}: {score:.2f}" cv2.putText(image, label_text, (int(xmin), int(ymin) - 10), font, font_scale, font_color, line_type)# 保存生成的图片output_path = f"tmp_output/{image_name}"cv2.imwrite(output_path, image)print(f"Result image saved to {output_path}.")# 打开生成的图片# current_dir = os.path.dirname(os.path.abspath(__file__))# os.startfile(os.path.join(current_dir, output_path))test_service.py内容