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代表默认摄像头