File size: 3,331 Bytes
f5c6fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import cv2
import numpy as np
from nes_py import NESEnv
from flask import Flask, Response, render_template_string
import time
import os

# Initialize Flask app
app = Flask(__name__)

# Global variables
running = False
reset_flag = False

# ROM path (update this if your ROM is in a different location)
rom_path = "/home/ubuntu/mario/Super Mario Bros.nes"

# Set up NES environment
try:
    env = NESEnv(rom_path)
    obs = env.reset()
    done = False
    print("NES environment initialized successfully with ROM:", rom_path)
except Exception as e:
    print("Error initializing NES environment:", e)
    exit(1)

# Capture and stream frames
def generate_frames():
    global running, reset_flag, done, obs
    frame_time = 1 / 30.0  # Stream at 30 FPS to reduce load
    print("Starting frame generation loop...")
    while running:
        start = time.time()
        if reset_flag:
            obs = env.reset()
            reset_flag = False
            done = False
            print("Game reset to title screen")
        if done:
            obs = env.reset()
            done = False
            print("Game reset due to done state")
        # Perform a NOOP action to keep the title screen
        obs, reward, done, info = env.step(0)
        print("Step executed, Reward:", reward, "Done:", done)  # Debug step info
        # Capture the frame
        frame = env.get_image()  # Get the raw RGB frame
        if frame is None:
            print("Warning: Frame is None!")
            time.sleep(frame_time)
            continue
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)  # Convert to BGR for OpenCV
        print("Captured frame shape:", frame.shape)  # Debug frame shape
        # Encode the frame as JPEG
        ret, buffer = cv2.imencode('.jpg', frame)
        if not ret:
            print("Warning: Failed to encode frame!")
            time.sleep(frame_time)
            continue
        frame_bytes = buffer.tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
        elapsed = time.time() - start
        if elapsed < frame_time:
            time.sleep(frame_time - elapsed)
        print("Frame sent, elapsed time:", elapsed)

# Flask route for the video feed
@app.route('/video_feed')
def video_feed():
    if not running:
        return "Stream not running", 503
    return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

# Home page route to display the stream
@app.route('/')
def index():
    return render_template_string('''
    <!DOCTYPE html>
    <html>
    <head>
        <title>Mario Title Screen Stream</title>
        <style>
            body { background: #1e1e1e; color: white; font-family: Arial, sans-serif; text-align: center; }
            img { border: 2px solid #00ff00; }
        </style>
    </head>
    <body>
        <h1>Mario Title Screen Live Stream</h1>
        <img src="/video_feed" width="256" height="240">
    </body>
    </html>
    ''')

# Main function to run the game and stream
if __name__ == "__main__":
    print("Starting Flask server on http://0.0.0.0:5111...")
    running = True
    try:
        app.run(host='0.0.0.0', port=5111, debug=True, use_reloader=False)
    except KeyboardInterrupt:
        print("Stopping the stream...")
        running = False