【yolov8】3分钟安装yolov8?yolov8安装补充与yolo8简单实现

# 修改截图方式 # 优化了窗口捕获和推理的帧数(约30帧) # 修改为按Esc结束(请务必使用Esc结束窗口运行,否则极容易出现“无权限访问”的问题,需要焦点在窗口上按Esc) # 新增多线程调用优化 # 新增names显示,修改数字为names里面的名字(兼容中文) # 新增窗口置顶和大小调整 # 新增名称/置信度,FPS显示 # forecast.py import os import threading import time from queue import Queue import CV2 import numpy as np import pygetwindow as gw import win32con import win32gui import yaml from mss import mss from ultralytics import YOLO def load_names(file_path): with open(file_path, 'r', encoding='utf-8') as f: data = yaml.safe_load(f) return data['names'] # 替换为你的data.yaml 里面保存有nc和names的那个,可以使用中文,推理出来的就是你names的名字,不是0,1数字了 names_path = "data.yaml" names = load_names(names_path) # 窗口名称和大小 ,推理时允许手动拖拽窗口大小 window_name = "YOLOv8 Predict Test" display_window_width = 768 display_window_height = 432 # 27 按Esc结束 exit_code = 27 # exit_code = ord("q") 按q结束 def capture_screen(img_queue): with mss() as sct: monitor = sct.monitors[0] while True: sct_img = sct.grab(monitor) img = np.array(sct_img) img = CV2.cvtColor(img, CV2.COLOR_BGRA2BGR) img_queue.put(img) class YoloThread(threading.Thread): def __init__(self, model, img_queue, result_queue): threading.Thread.__init__(self) self.model = model self.img_queue = img_queue self.result_queue = result_queue def run(self): while True: img = self.img_queue.get() results = self.model.predict(source=img, conf=0.25, iou=0.75) self.result_queue.put((img, results)) def run(model, top_most=True): window_flag = CV2.WINDOW_NORMAL fps_update_interval = 0.5 # 每0.5秒更新一次 frame_counter = 0 last_fps_update_time = time.time() fps = 0 img_queue = Queue() result_queue = Queue() capture_thread = threading.Thread(target=capture_screen, args=(img_queue,)) capture_thread.daemon = True capture_thread.start() yolo_thread = YoloThread(model, img_queue, result_queue) yolo_thread.daemon = True yolo_thread.start() # 将这两个函数放在 while True 循环之外 CV2.namedWindow(window_name, window_flag) CV2.resizeWindow(window_name, display_window_width, display_window_height) while True: current_frame_time = time.time() img, results = result_queue.get() for result in results: if len(result.boxes.xyxy) > 0: boxes_conf = np.array(result.boxes.conf.tolist()) boxes_xyxy = result.boxes.xyxy.tolist() boxes_cls = result.boxes.cls.tolist() for i, box_xyxy in enumerate(boxes_xyxy): CV2.rectangle(img, (int(box_xyxy[0]), int(box_xyxy[1])), (int(box_xyxy[2]), int(box_xyxy[3])), (0, 0, 150), 2) class_name = names[int(boxes_cls[i])] confidence_text = f"{class_name}: {boxes_conf[i]:.2f}" CV2.putText(img, confidence_text, (int(box_xyxy[0]), int(box_xyxy[1]) - 20), CV2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 150), 2, CV2.LINE_AA) frame_counter += 1 elapsed_time = current_frame_time - last_fps_update_time fps_text = f"FPS: {fps}" CV2.putText(img, fps_text, (display_window_width - 700, 40), CV2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 150), 2, CV2.LINE_AA) if elapsed_time >= fps_update_interval: fps = int(frame_counter / elapsed_time) frame_counter = 0 last_fps_update_time = current_frame_time CV2.imshow(window_name, img) if top_most: window = gw.getWindowsWithTitle(window_name)[0] window_handle = window._hWnd win32gui.SetWindowPos(window_handle, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE) if CV2.waitKey(1) == exit_code: CV2.destroyAllWindows() os._exit(0) if __name__ == '__main__': # 是否默认窗口置顶 top_most = True # 你的best.pt模型(生成模型之后千万记得替换模型,我就因为忘记替换模型还以为我数据集有问题hh) model = YOLO("../best.pt") run(model, top_most)
效果如下:
