如何用YOLOv8在屏幕截图上直接显示检测边界框?
屏幕捕获+YOLO目标检测并可视化边界框
你的代码已经成功完成了屏幕捕获和目标检测(输出显示检测到3个物体),但缺少检测结果可视化的步骤。以下是两种实现边界框显示的方案:
方案1:使用YOLO内置可视化方法(最简单)
直接利用Ultralytics YOLO提供的plot()方法生成带边界框的图像,再用OpenCV显示:
from ultralytics import YOLO import pyautogui import cv2 import numpy as np # Load the model model_path = 'C:/Users/toto/Desktop/DETECT/best.pt' model = YOLO(model_path) # Set the confidence threshold conf_threshold = 0.4 # Capture the entire screen screen_image = pyautogui.screenshot() screen_image = cv2.cvtColor(np.array(screen_image), cv2.COLOR_RGB2BGR) # Perform object detection on the captured screen image results = model(screen_image, conf=conf_threshold) # 传入置信度过滤结果 # 生成带边界框、类别和置信度的可视化图像 annotated_image = results[0].plot() # 显示检测结果窗口 cv2.imshow("Screen Detection Result", annotated_image) cv2.waitKey(0) # 按任意键关闭窗口 cv2.destroyAllWindows()
方案2:手动绘制边界框(自定义样式)
如果需要自定义边界框颜色、文本样式等,可手动提取检测结果并绘制:
from ultralytics import YOLO import pyautogui import cv2 import numpy as np # Load the model model_path = 'C:/Users/toto/Desktop/DETECT/best.pt' model = YOLO(model_path) # Set the confidence threshold conf_threshold = 0.4 # Capture the entire screen screen_image = pyautogui.screenshot() screen_image = cv2.cvtColor(np.array(screen_image), cv2.COLOR_RGB2BGR) # Perform object detection on the captured screen image results = model(screen_image, conf=conf_threshold) # 遍历检测结果并手动绘制 for result in results: boxes = result.boxes for box in boxes: # 提取边界框坐标(xyxy格式:左上角x1,y1,右下角x2,y2) x1, y1, x2, y2 = map(int, box.xyxy[0]) # 提取类别名称和置信度 cls_idx = int(box.cls[0]) confidence = box.conf[0] class_name = model.names[cls_idx] # 绘制绿色边界框(线宽2) cv2.rectangle(screen_image, (x1, y1), (x2, y2), (0, 255, 0), 2) # 绘制类别+置信度文本 text = f"{class_name}: {confidence:.2f}" cv2.putText(screen_image, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 显示检测结果窗口 cv2.imshow("Screen Detection Result", screen_image) cv2.waitKey(0) cv2.destroyAllWindows()
关键说明
- 调用
model()时传入conf=conf_threshold,确保只保留置信度达标的检测结果; cv2.waitKey(0)会让窗口保持打开,直到按下任意键关闭;若需自动关闭,可替换为cv2.waitKey(3000)(3秒后关闭);- 手动绘制方案中,可修改边界框颜色(如
(0,0,255)为红色)、线宽、文本字体等参数,实现个性化可视化。
内容的提问来源于stack exchange,提问作者shiny
相关产品推荐
相关产品推荐

