-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
72 lines (58 loc) · 2.11 KB
/
test.py
File metadata and controls
72 lines (58 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from utils import process_results
from mario_env import MarioEnv
import cv2
import numpy as np
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT
from stable_baselines3 import PPO
from nes_py.wrappers import JoypadSpace
from inference import get_model
MAX_BOXES = 10
FPS = 60
SKIP = 4
env = MarioEnv()
env = JoypadSpace(env, SIMPLE_MOVEMENT)
model = PPO.load("./tmp/best_model.zip")
img_model = get_model(model_id="mario-ibyfv/2", api_key="ITukAND4XqHSos8UA9me")
images = []
prev_obs = None
env.reset()
while True:
action = None
done = False
for i in range(SKIP):
# processes frame into bounding boxes
frame = env.render(mode='rgb_array')
image = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
results = img_model.infer(image)
obs = process_results(results, MAX_BOXES)
prev_obs = obs if prev_obs is None else prev_obs
# save image to images
for prediction in results[0].predictions:
# Get bounding box coordinates
x1 = int(prediction.x - prediction.width / 2)
y1 = int(prediction.y - prediction.height / 2)
x2 = int(prediction.x + prediction.width / 2)
y2 = int(prediction.y + prediction.height / 2)
# Get class label and confidence score
label = prediction.class_id
confidence = prediction.confidence
# Draw bounding box and label on the image
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(image, f'{label}: {confidence:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
images.append(image)
# performs action on environment
if i == 0:
action, _states = model.predict([prev_obs, obs])
prev_obs = obs
_, _, done, _ = env.step(action.item())
if done:
break
if done:
break
env.close()
# write to mp4 file
height, width, _ = images[0].shape
vidwriter = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*"mp4v"), FPS, (width, height))
for image in images:
vidwriter.write(image)
vidwriter.release()