Spaces:
Sleeping
Sleeping
File size: 8,561 Bytes
a116340 e950527 674431d 17eebcf 336ec80 17eebcf 674431d 81a2977 e950527 17eebcf e950527 b8ec65a 17eebcf dcc2634 b8ec65a e950527 17eebcf 81a2977 17eebcf b8ec65a 81a2977 17eebcf 674431d b8ec65a 674431d 17eebcf 674431d e950527 17eebcf 0a14bc2 9bbdc2c b8ec65a dcc2634 b8ec65a 0a14bc2 17eebcf 0a14bc2 b8ec65a 17eebcf 0a14bc2 17eebcf b8ec65a 17eebcf b8ec65a 0a14bc2 17eebcf 0a14bc2 17eebcf b8ec65a 17eebcf b8ec65a 0a14bc2 17eebcf 0a14bc2 17eebcf 0a14bc2 674431d 17eebcf 0a14bc2 17eebcf b8ec65a 17eebcf e950527 b8ec65a e950527 81a2977 0a14bc2 17eebcf 0a14bc2 17eebcf b8ec65a 0a14bc2 17eebcf 674431d 17eebcf dcc2634 b8ec65a 17eebcf b8ec65a 17eebcf 0a14bc2 17eebcf b8ec65a 17eebcf 0a14bc2 17eebcf 0a14bc2 17eebcf b8ec65a 0a14bc2 17eebcf b8ec65a 17eebcf b8ec65a 17eebcf b8ec65a 17eebcf b8ec65a 17eebcf 0a14bc2 674431d 0a14bc2 b8ec65a 0a14bc2 17eebcf 0a14bc2 81a2977 336ec80 674431d b8ec65a 674431d b8ec65a 0a14bc2 81a2977 17eebcf b8ec65a 17eebcf b8ec65a 674431d b8ec65a 674431d | 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | import pygame
import time
import threading
import numpy as np
import base64
import zlib
import os
from flask import Flask, render_template_string
from flask_socketio import SocketIO, emit
# ===== PYGAME SETUP =====
os.environ['SDL_VIDEODRIVER'] = 'dummy'
pygame.init()
print("β
PyGame initialized with surfarray")
WIDTH, HEIGHT = 400, 300
# Create surface
screen = pygame.Surface((WIDTH, HEIGHT))
print(f"β
Surface: {WIDTH}x{HEIGHT}")
# Test surfarray
print("π§ͺ Testing surfarray...")
test_arr = pygame.surfarray.array3d(screen)
print(f"β
Surfarray works! Shape: {test_arr.shape}, Type: {test_arr.dtype}")
# ===== FLASK/SOCKETIO =====
app = Flask(__name__)
app.config['SECRET_KEY'] = 'surfarray_stream'
socketio = SocketIO(app, async_mode='threading', cors_allowed_origins="*")
# Shared state
shared = {
"running": True,
"x": 200,
"frame_count": 0,
"clients": 0
}
# ===== SURFARRAY THREAD =====
def surfarray_thread():
"""Send raw pixel arrays via WebSocket"""
print("π Surfarray thread STARTED")
x, speed = 200, 5
fps_counter = 0
last_log = time.time()
while shared["running"]:
try:
start_time = time.time()
# 1. UPDATE POSITION
x += speed
if x < 30 or x > WIDTH-30:
speed *= -1
# 2. DRAW
screen.fill((25, 25, 45)) # Dark blue
pygame.draw.circle(screen, (255, 80, 80), (int(x), 150), 20)
pygame.draw.circle(screen, (255, 255, 255), (int(x), 150), 20, 2)
# 3. GET PIXEL ARRAY (FAST!)
pixel_array = pygame.surfarray.array3d(screen) # Shape: (400, 300, 3)
# 4. COMPRESS (optional but recommended)
# Flatten and compress the array
flat_data = pixel_array.flatten().tobytes()
compressed = zlib.compress(flat_data, level=1) # Fast compression
encoded = base64.b64encode(compressed).decode('utf-8')
# 5. BROADCAST
shared["x"] = x
shared["frame_count"] += 1
socketio.emit('pixel_data', {
'pixels': encoded,
'shape': [WIDTH, HEIGHT, 3], # Send dimensions
'frame': shared["frame_count"],
'x': x,
'compressed': True
})
# 6. LOGGING
fps_counter += 1
current_time = time.time()
if current_time - last_log >= 1.0:
print(f"π Sent {fps_counter} frames | Frame {shared['frame_count']} | X={x}")
fps_counter = 0
last_log = current_time
# 7. FRAME RATE
elapsed = time.time() - start_time
if elapsed < 1.0/30:
time.sleep(1.0/30 - elapsed)
except Exception as e:
print(f"β Surfarray thread error: {e}")
import traceback
traceback.print_exc()
break
print("π Surfarray thread STOPPED")
# ===== START THREAD =====
thread = threading.Thread(target=surfarray_thread, daemon=True)
thread.start()
print(f"β
Thread alive: {thread.is_alive()}")
# ===== FLASK ROUTES =====
@app.route('/')
def index():
return render_template_string('''
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.socket.io/4.5.0/socket.io.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pako@2.1.0/dist/pako.min.js"></script>
<style>
body { background: #0f172a; color: white; text-align: center; padding: 20px; }
canvas {
border: 3px solid #60a5fa;
width: 400px;
height: 300px;
image-rendering: pixelated;
}
.stats {
background: #1e293b;
padding: 15px;
border-radius: 10px;
margin: 20px auto;
width: 400px;
}
</style>
</head>
<body>
<h1>π Surfarray Stream</h1>
<canvas id="pixelCanvas" width="400" height="300"></canvas>
<div class="stats">
<div>Frame: <span id="frame">0</span></div>
<div>Circle X: <span id="pos">200</span></div>
<div>Status: <span id="status">Connecting...</span></div>
<div>FPS: <span id="fps">0</span></div>
<div>Data size: <span id="size">0 KB</span></div>
</div>
<script>
const socket = io();
const canvas = document.getElementById('pixelCanvas');
const ctx = canvas.getContext('2d');
let framesReceived = 0;
let lastFpsTime = Date.now();
let totalBytes = 0;
// WebSocket connection
socket.on('connect', () => {
console.log('β
Connected to surfarray stream');
document.getElementById('status').textContent = 'Connected';
document.getElementById('status').style.color = '#10b981';
});
// PIXEL DATA RECEIVED
socket.on('pixel_data', (data) => {
framesReceived++;
totalBytes += data.pixels.length;
// Decode base64
const binaryString = atob(data.pixels);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
// Decompress if needed
let pixelData;
if (data.compressed) {
try {
// Use pako for zlib decompression
pixelData = pako.inflate(bytes);
} catch (e) {
console.error('Decompression failed:', e);
return;
}
} else {
pixelData = bytes;
}
// Create ImageData
const [width, height] = data.shape;
const imageData = new ImageData(width, height);
// Copy pixel data (assuming RGB format)
for (let i = 0; i < pixelData.length; i++) {
imageData.data[i] = pixelData[i];
}
// Draw to canvas
ctx.putImageData(imageData, 0, 0);
// Update stats
document.getElementById('frame').textContent = data.frame;
document.getElementById('pos').textContent = data.x;
document.getElementById('size').textContent =
Math.round(data.pixels.length / 1024) + ' KB';
// Calculate FPS
const now = Date.now();
if (now - lastFpsTime >= 1000) {
document.getElementById('fps').textContent = framesReceived;
framesReceived = 0;
lastFpsTime = now;
}
// Visual feedback
canvas.style.borderColor = '#10b981';
setTimeout(() => canvas.style.borderColor = '#60a5fa', 50);
});
socket.on('disconnect', () => {
document.getElementById('status').textContent = 'Disconnected';
document.getElementById('status').style.color = '#ef4444';
});
console.log('Surfarray client ready');
</script>
</body>
</html>
''')
@socketio.on('connect')
def handle_connect():
shared['clients'] += 1
print(f"π€ Client connected. Total: {shared['clients']}")
@socketio.on('disconnect')
def handle_disconnect():
shared['clients'] -= 1
print(f"π€ Client disconnected. Total: {shared['clients']}")
if __name__ == '__main__':
print("π Starting surfarray server...")
# Wait for thread
time.sleep(1)
print(f"π Initial: Thread alive={thread.is_alive()}, Frames={shared['frame_count']}")
socketio.run(
app,
host='0.0.0.0',
port=int(os.environ.get('PORT', 7860)),
debug=False,
use_reloader=False,
allow_unsafe_werkzeug=True
) |