This is a testing code for my school project:
import pyautogui as m
import time as t
import os
import cv2
import numpy as np
import keyboard
from PIL import Image
from roboflow import Roboflow
import io
import torch
path_song = "C:/Osu_test_data"
rf = Roboflow(api_key="API_KEY")
project = rf.workspace().project("osu_detection")
model = project.version(5).model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
m.FAILSAFE = False
Song_Not_End = True
multiper = 0.81
x1, y1 = 0, 0
i = 0
circle_list = []
Song_Name = input("Song Name:")
if not os.path.exists(f'{path_song}/{Song_Name}'):
os.makedirs(f'{path_song}/{Song_Name}')
path = f'{path_song}/{Song_Name}/{Song_Name}.txt'
screenx, screeny = m.size()
screenx = int(screenx)
screeny = int(screeny)
if not os.path.exists(f'{path_song}/{Song_Name}/{Song_Name}test.png'):
Screenshot = m.screenshot(f'{path_song}/{Song_Name}/{Song_Name}test.png')
image = cv2.imread(f'{path_song}/{Song_Name}/{Song_Name}test.png')
height, width = image.shape[:2]
with open(path, 'w+') as f:
WriteOrPlay = input("Writein or Play?")
if WriteOrPlay.lower() == "writein" or WriteOrPlay.lower() == "w":
while Song_Not_End:
if not os.path.exists(f'{path_song}/{Song_Name}/{Song_Name}{i}.png'):
Screenshot = m.screenshot(f'{path_song}/{Song_Name}/{Song_Name}{i}.png')
#image = cv2.cvtColor(np.array(Screenshot), cv2.COLOR_RGB2BGR)
image = Image.fromarray(np.array(Screenshot))
#retval, buffer = cv2.imencode('.jpg', image)
#image_bytes = buffer.tobytes()
image_tensor = torch.tensor(np.array(image)).unsqueeze(0).permute(0, 3, 1, 2).float().to(device)
with torch.no_grad():
image_array = image_tensor.cpu().numpy()
predictions = model.predict(image_array)
predictions = predictions.cpu().numpy()
if len(predictions) > 0:
x1 = int(predictions[0]['x'])
y1 = int(predictions[0]['y'])
circle_list.append({
'position': (x1, y1),
})
else:
x1, y1 = width / 2, height / 2
x1 = screenx / width * x1 * multiper
y1 = screeny / height * y1 * multiper
m.moveTo(x1, y1, duration=0)
i += 1
if keyboard.is_pressed("q"):
Song_Not_End = False
img = None
but it turns out that it has an error in it:
Unknown C++ exception from OpenCV code
File "C:\Users\00\osuai_gpuver.py", line 60, in <module>
predictions = model.predict(image_array)
^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: Unknown C++ exception from OpenCV code
How can I solve it?
Thanks.