From 47cfade856c669efae94199af5d820b3a71501e3 Mon Sep 17 00:00:00 2001 From: wonder Date: Wed, 5 Nov 2025 19:03:35 +0800 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=94=84Update:=20[Lab-4]=20yolov8n=20De?= =?UTF-8?q?tect?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lab-4/src/detect.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Lab-4/src/detect.py diff --git a/Lab-4/src/detect.py b/Lab-4/src/detect.py new file mode 100644 index 0000000..dc0db77 --- /dev/null +++ b/Lab-4/src/detect.py @@ -0,0 +1,20 @@ +from ultralytics import YOLO +import cv2 + +# 加载最佳模型 +model = YOLO('runs/detect/yolov8n_coco128_exp1/weights/best.pt') + +# 在图像上进行推理 +source = 'datasets/img3.png' # 替换成你自己的测试图片路径 +results = model(source, save=True, conf=0.5) # conf为置信度阈值 + +# 可视化结果(可选,使用OpenCV) +# 读取带标注的结果图像 +result_image = results[0].plot() # 获取第一张图片的绘制结果 +cv2.imshow('Detection Result', result_image) +cv2.waitKey(0) +cv2.destroyAllWindows() + +# 也可以在视频或摄像头上进行推理 +# results = model('path/to/video.mp4', save=True, show=True) +# results = model(0, show=True) # 0代表默认摄像头