I have been attempting to make a program that can identify objects on my screen using python. With the help of this tutorial: https://stackabuse.com/object-detection-with-imageai-in-python, I have created the following code.
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))
I would like to be able to achieve 20-30 fps, however, I can only get around 1 fps and I do not know what is slowing it down.
My specs are:
- 2 x RTX 3060
- Intel i7 4770
- 24GB RAM
All help is greatly appreciated.