Update marioai.py
Browse files- marioai.py +104 -0
marioai.py
CHANGED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from nes_py import NESEnv
|
4 |
+
from flask import Flask, Response, render_template_string
|
5 |
+
import time
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Initialize Flask app
|
9 |
+
app = Flask(__name__)
|
10 |
+
|
11 |
+
# Global variables
|
12 |
+
running = False
|
13 |
+
reset_flag = False
|
14 |
+
|
15 |
+
# ROM path (update this if your ROM is in a different location)
|
16 |
+
rom_path = "/home/ubuntu/mario/Super Mario Bros.nes"
|
17 |
+
|
18 |
+
# Set up NES environment
|
19 |
+
try:
|
20 |
+
env = NESEnv(rom_path)
|
21 |
+
obs = env.reset()
|
22 |
+
done = False
|
23 |
+
print("NES environment initialized successfully with ROM:", rom_path)
|
24 |
+
except Exception as e:
|
25 |
+
print("Error initializing NES environment:", e)
|
26 |
+
exit(1)
|
27 |
+
|
28 |
+
# Capture and stream frames
|
29 |
+
def generate_frames():
|
30 |
+
global running, reset_flag, done, obs
|
31 |
+
frame_time = 1 / 30.0 # Stream at 30 FPS to reduce load
|
32 |
+
print("Starting frame generation loop...")
|
33 |
+
while running:
|
34 |
+
start = time.time()
|
35 |
+
if reset_flag:
|
36 |
+
obs = env.reset()
|
37 |
+
reset_flag = False
|
38 |
+
done = False
|
39 |
+
print("Game reset to title screen")
|
40 |
+
if done:
|
41 |
+
obs = env.reset()
|
42 |
+
done = False
|
43 |
+
print("Game reset due to done state")
|
44 |
+
# Perform a NOOP action to keep the title screen
|
45 |
+
obs, reward, done, info = env.step(0)
|
46 |
+
print("Step executed, Reward:", reward, "Done:", done) # Debug step info
|
47 |
+
# Capture the frame
|
48 |
+
frame = env.get_image() # Get the raw RGB frame
|
49 |
+
if frame is None:
|
50 |
+
print("Warning: Frame is None!")
|
51 |
+
time.sleep(frame_time)
|
52 |
+
continue
|
53 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # Convert to BGR for OpenCV
|
54 |
+
print("Captured frame shape:", frame.shape) # Debug frame shape
|
55 |
+
# Encode the frame as JPEG
|
56 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
57 |
+
if not ret:
|
58 |
+
print("Warning: Failed to encode frame!")
|
59 |
+
time.sleep(frame_time)
|
60 |
+
continue
|
61 |
+
frame_bytes = buffer.tobytes()
|
62 |
+
yield (b'--frame\r\n'
|
63 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
|
64 |
+
elapsed = time.time() - start
|
65 |
+
if elapsed < frame_time:
|
66 |
+
time.sleep(frame_time - elapsed)
|
67 |
+
print("Frame sent, elapsed time:", elapsed)
|
68 |
+
|
69 |
+
# Flask route for the video feed
|
70 |
+
@app.route('/video_feed')
|
71 |
+
def video_feed():
|
72 |
+
if not running:
|
73 |
+
return "Stream not running", 503
|
74 |
+
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
75 |
+
|
76 |
+
# Home page route to display the stream
|
77 |
+
@app.route('/')
|
78 |
+
def index():
|
79 |
+
return render_template_string('''
|
80 |
+
<!DOCTYPE html>
|
81 |
+
<html>
|
82 |
+
<head>
|
83 |
+
<title>Mario Title Screen Stream</title>
|
84 |
+
<style>
|
85 |
+
body { background: #1e1e1e; color: white; font-family: Arial, sans-serif; text-align: center; }
|
86 |
+
img { border: 2px solid #00ff00; }
|
87 |
+
</style>
|
88 |
+
</head>
|
89 |
+
<body>
|
90 |
+
<h1>Mario Title Screen Live Stream</h1>
|
91 |
+
<img src="/video_feed" width="256" height="240">
|
92 |
+
</body>
|
93 |
+
</html>
|
94 |
+
''')
|
95 |
+
|
96 |
+
# Main function to run the game and stream
|
97 |
+
if __name__ == "__main__":
|
98 |
+
print("Starting Flask server on http://0.0.0.0:5111...")
|
99 |
+
running = True
|
100 |
+
try:
|
101 |
+
app.run(host='0.0.0.0', port=5111, debug=True, use_reloader=False)
|
102 |
+
except KeyboardInterrupt:
|
103 |
+
print("Stopping the stream...")
|
104 |
+
running = False
|