YOLOv8,在屏幕上显示边界框

问题描述 投票:0回答:1

如何直接在屏幕上显示边界框?它不是视频,所以我无法使用跟踪。我的程序捕获整个屏幕并检查对象。我研究过简历可以帮助我,但我无法让它发挥作用

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)

我的代码输出

0: 384x640 3 Objects, 70.1ms
Speed: 4.5ms preprocess, 70.1ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)
python python-3.x opencv yolov8
1个回答
0
投票

您可以按照这样的代码:

from ultralytics import YOLO
import cv2
from ultralytics.utils.plotting import Annotator

model = YOLO('yolov8n.pt')
cap = cv2.VideoCapture("Your video path")

while True:
    _, img = cap.read()
    results = model.predict(img)

    for r in results:
        annotator = Annotator(img)
        boxes = r.boxes
        for box in boxes:
            b = box.xyxy[0]
            c = box.cls
            annotator.box_label(b, model.names[int(c)])
          
    img = annotator.result()  
    cv2.imshow('YOLO V8 Detection', img)     
    if cv2.waitKey(1) & 0xFF == ord(' '):
        break

cap.release()
cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.