0

我一直在尝试制作一个可以使用 python 识别屏幕上对象的程序。在本教程的帮助下:https ://stackabuse.com/object-detection-with-imageai-in-python ,我创建了以下代码。

import pyautogui
import cv2
import time
import os

os.add_dll_directory("C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.4/bin")

from imageai.Detection import ObjectDetection
import tensorflow as tf

detector = ObjectDetection()

model_path = "./models/yolo-tiny.h5"
input_path = "./input/test45.jpg"
output_path = "./output/newimage.jpg"

detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(model_path)
detector.loadModel(detection_speed="fastest")
while True:
    start_time = time.time()
    x = np.array(pyautogui.screenshot())
    x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)
    y, detection = detector.detectObjectsFromImage(input_type="array", input_image=x, output_type="array")
    for eachItem in detection:
        print(eachItem["name"] , " : ", eachItem["percentage_probability"])
    print("FPS: ", 1.0 / (time.time() - start_time))

我希望能够达到 20-30 fps,但是,我只能达到 1 fps 左右,我不知道是什么让它变慢了。

我的规格是:

  • 2 个 RTX 3060
  • 英特尔 i7 4770
  • 24GB 内存

非常感谢所有帮助。

4

0 回答 0