你好连帽衫我已经为yolov8训练了一个模型。
我想在 python exe 中使用它。
下面是我的代码:
try:
import time
import cv2
import mss
import numpy as np
from ultralytics import YOLO
import math
import os
import sys
def screen_capture():
with mss.mss() as sct:
monitor = sct.monitors[2]
sct_img = sct.grab(monitor)
frame = np.array(sct_img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
return frame
def detect_realtime(model):
while True:
frame = screen_capture()
results = model(frame,device="cpu")
highest_conf = 0
best_box = None
for r in results:
boxes = r.boxes
for box in boxes:
conf = box.conf[0]
if conf > highest_conf:
highest_conf = conf
best_box = box
if best_box:
x1, y1, x2, y2 = best_box.xyxy[0]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
cls_id = int(best_box.cls[0])
conf = math.ceil(highest_conf * 100) / 100
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, f'{model.names[cls_id]} {conf:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
print([(x1+x2)//2, (y1+y2)//2])
resized_frame = cv2.resize(frame, (1366, 768))
cv2.imshow('YOLO Realtime Detection', resized_frame)
cv2.moveWindow('YOLO Realtime Detection', 2000, 100)
if cv2.waitKey(500) == ord('q'):
break
cv2.destroyAllWindows()
if __name__ == '__main__':
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
print(base_path)
anvil_model= os.path.join(base_path,'best.pt')
model = YOLO(anvil_model)
detect_realtime(model)
except Exception as e:
time.sleep(5555)
之后,我用这个命令创建一个exe:
pyinstaller --onefile --add-data "best.pt;"。主要.py
控制台写出这个错误: [Errno 2] 没有这样的文件或目录:'C:\Users\User\AppData\Local\Temp\_MEI119602\ultralytic