Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
-
#
|
| 3 |
import numpy as np
|
| 4 |
import time
|
| 5 |
from flask import Flask, Response
|
|
@@ -11,81 +11,7 @@ frame_count = 0
|
|
| 11 |
|
| 12 |
@app.route('/')
|
| 13 |
def index():
|
| 14 |
-
return '''
|
| 15 |
-
<!DOCTYPE html>
|
| 16 |
-
<html>
|
| 17 |
-
<head>
|
| 18 |
-
<title>Flask Raw Data Test</title>
|
| 19 |
-
<style>
|
| 20 |
-
canvas { border: 3px solid red; background: black; display: block; margin: 20px auto; }
|
| 21 |
-
</style>
|
| 22 |
-
</head>
|
| 23 |
-
<body>
|
| 24 |
-
<h1 style="text-align:center;color:red;">FLASK RAW DATA TEST</h1>
|
| 25 |
-
<canvas id="canvas" width="400" height="300"></canvas>
|
| 26 |
-
|
| 27 |
-
<script>
|
| 28 |
-
const canvas = document.getElementById('canvas');
|
| 29 |
-
const ctx = canvas.getContext('2d');
|
| 30 |
-
let frames = 0;
|
| 31 |
-
|
| 32 |
-
function loadFrame() {
|
| 33 |
-
fetch('/raw')
|
| 34 |
-
.then(response => {
|
| 35 |
-
console.log('Status:', response.status, 'Type:', response.headers.get('content-type'));
|
| 36 |
-
return response.arrayBuffer();
|
| 37 |
-
})
|
| 38 |
-
.then(buffer => {
|
| 39 |
-
frames++;
|
| 40 |
-
console.log(`Frame ${frames}: ${buffer.byteLength} bytes`);
|
| 41 |
-
|
| 42 |
-
if (buffer.byteLength === 0) {
|
| 43 |
-
console.error('Empty buffer!');
|
| 44 |
-
return;
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
// Create ImageData
|
| 48 |
-
const imageData = new ImageData(400, 300);
|
| 49 |
-
const rgba = imageData.data;
|
| 50 |
-
const rgb = new Uint8Array(buffer);
|
| 51 |
-
|
| 52 |
-
// Check size
|
| 53 |
-
if (rgb.length !== 400*300*3) {
|
| 54 |
-
console.error(`Wrong size! Got ${rgb.length}, expected ${400*300*3}`);
|
| 55 |
-
return;
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
// Convert RGB to RGBA
|
| 59 |
-
for (let i = 0, j = 0; i < rgba.length; i += 4, j += 3) {
|
| 60 |
-
[i] = rgb[j];
|
| 61 |
-
rgba[i + 1] = rgb[j + 1];
|
| 62 |
-
rgba[i + 2] = rgb[j + 2];
|
| 63 |
-
rgba[i + 3] = 255;
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
// Draw to canvas
|
| 67 |
-
ctx.putImageData(imageData, 0, 0);
|
| 68 |
-
|
| 69 |
-
// Update title
|
| 70 |
-
document.title = `Frame ${frames}`;
|
| 71 |
-
})
|
| 72 |
-
.catch(error => {
|
| 73 |
-
console.error('Fetch error:', error);
|
| 74 |
-
});#!/usr/bin/env python3
|
| 75 |
-
# static_noise.py - Flask generates random noise
|
| 76 |
-
import numpy as np
|
| 77 |
-
import time
|
| 78 |
-
from flask import Flask, Response
|
| 79 |
-
|
| 80 |
-
app = Flask(__name__)
|
| 81 |
-
|
| 82 |
-
WIDTH, HEIGHT = 400, 300
|
| 83 |
-
frame_count = 0
|
| 84 |
-
|
| 85 |
-
@app.route('/')
|
| 86 |
-
def index():
|
| 87 |
-
return '''
|
| 88 |
-
<!DOCTYPE html>
|
| 89 |
<html>
|
| 90 |
<head>
|
| 91 |
<title>Static Noise Animation</title>
|
|
@@ -120,7 +46,7 @@ def index():
|
|
| 120 |
let frames = 0;
|
| 121 |
let lastTime = Date.now();
|
| 122 |
let fps = 0;
|
| 123 |
-
let colorMode = 0;
|
| 124 |
|
| 125 |
function loadNoiseFrame() {
|
| 126 |
const startTime = Date.now();
|
|
@@ -131,30 +57,23 @@ def index():
|
|
| 131 |
return response.arrayBuffer();
|
| 132 |
})
|
| 133 |
.then(buffer => {
|
| 134 |
-
// Create ImageData
|
| 135 |
const imageData = new ImageData(400, 300);
|
| 136 |
const rgba = imageData.data;
|
| 137 |
const rgb = new Uint8Array(buffer);
|
| 138 |
|
| 139 |
-
// Verify size
|
| 140 |
if (rgb.length !== 400*300*3) {
|
| 141 |
console.error('Wrong size:', rgb.length);
|
| 142 |
return;
|
| 143 |
}
|
| 144 |
|
| 145 |
-
// Convert RGB to RGBA
|
| 146 |
for (let i = 0, j = 0; i < rgba.length; i += 4, j += 3) {
|
| 147 |
-
|
| 148 |
rgba[i + 1] = rgb[j + 1];
|
| 149 |
rgba[i + 2] = rgb[j + 2];
|
| 150 |
rgba[i + 3] = 255;
|
| 151 |
}
|
| 152 |
|
| 153 |
-
|
| 154 |
-
if (colorMode === 1) {
|
| 155 |
-
// Color noise - already colored
|
| 156 |
-
} else if (colorMode === 2) {
|
| 157 |
-
// Sepia tone
|
| 158 |
for (let i = 0; i < rgba.length; i += 4) {
|
| 159 |
const r = rgba[i];
|
| 160 |
const g = rgba[i + 1];
|
|
@@ -165,12 +84,9 @@ def index():
|
|
| 165 |
rgba[i + 2] = Math.min(255, (r * 0.272) + (g * 0.534) + (b * 0.131));
|
| 166 |
}
|
| 167 |
}
|
| 168 |
-
// else: Grayscale - already from server
|
| 169 |
|
| 170 |
-
// Draw to canvas
|
| 171 |
ctx.putImageData(imageData, 0, 0);
|
| 172 |
|
| 173 |
-
// Update stats
|
| 174 |
frames++;
|
| 175 |
const now = Date.now();
|
| 176 |
if (now - lastTime >= 1000) {
|
|
@@ -182,11 +98,6 @@ def index():
|
|
| 182 |
|
| 183 |
document.getElementById('frameCount').textContent = parseInt(document.getElementById('frameCount').textContent) + 1;
|
| 184 |
document.getElementById('status').textContent = 'Streaming ✓';
|
| 185 |
-
|
| 186 |
-
const latency = Date.now() - startTime;
|
| 187 |
-
if (latency > 100) {
|
| 188 |
-
console.log(`Frame latency: ${latency}ms`);
|
| 189 |
-
}
|
| 190 |
})
|
| 191 |
.catch(error => {
|
| 192 |
console.error('Error:', error);
|
|
@@ -197,7 +108,7 @@ def index():
|
|
| 197 |
|
| 198 |
function startNoise() {
|
| 199 |
if (!noiseInterval) {
|
| 200 |
-
noiseInterval = setInterval(loadNoiseFrame, 33);
|
| 201 |
document.getElementById('status').textContent = 'Noise started ✓';
|
| 202 |
console.log('Noise animation started');
|
| 203 |
}
|
|
@@ -219,13 +130,11 @@ def index():
|
|
| 219 |
document.getElementById('status').textContent = 'Mode: ' + modes[colorMode];
|
| 220 |
}
|
| 221 |
|
| 222 |
-
// Start automatically after 1 second
|
| 223 |
setTimeout(startNoise, 1000);
|
| 224 |
console.log('Static noise page loaded');
|
| 225 |
</script>
|
| 226 |
</body>
|
| 227 |
-
</html>
|
| 228 |
-
'''
|
| 229 |
|
| 230 |
@app.route('/noise')
|
| 231 |
def get_noise():
|
|
@@ -234,22 +143,17 @@ def get_noise():
|
|
| 234 |
|
| 235 |
# Create random noise
|
| 236 |
if frame_count % 3 == 0:
|
| 237 |
-
# Grayscale noise
|
| 238 |
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
|
| 239 |
-
# Make it grayscale by setting all channels to same value
|
| 240 |
gray = np.random.randint(0, 256, (HEIGHT, WIDTH, 1), dtype=np.uint8)
|
| 241 |
noise[:, :, :] = gray
|
| 242 |
elif frame_count % 3 == 1:
|
| 243 |
-
# Color noise
|
| 244 |
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
|
| 245 |
else:
|
| 246 |
-
# TV static with scanlines
|
| 247 |
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
|
| 248 |
-
# Add scanlines every 4th row
|
| 249 |
noise[::4, :, :] = noise[::4, :, :] // 2
|
| 250 |
|
| 251 |
-
# Occasionally add a "glitch"
|
| 252 |
-
if np.random.random() < 0.02:
|
| 253 |
glitch_color = np.random.randint(0, 256, 3)
|
| 254 |
glitch_height = np.random.randint(20, 100)
|
| 255 |
glitch_y = np.random.randint(0, HEIGHT - glitch_height)
|
|
@@ -276,16 +180,9 @@ def get_noise():
|
|
| 276 |
@app.route('/test')
|
| 277 |
def test():
|
| 278 |
"""Test endpoint - returns simple pattern"""
|
| 279 |
-
# Create a simple test pattern
|
| 280 |
pixels = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8)
|
| 281 |
-
|
| 282 |
-
# Red top half
|
| 283 |
pixels[:HEIGHT//2, :, 0] = 255
|
| 284 |
-
|
| 285 |
-
# Green bottom half
|
| 286 |
pixels[HEIGHT//2:, :, 1] = 255
|
| 287 |
-
|
| 288 |
-
# Blue diagonal line
|
| 289 |
for i in range(min(WIDTH, HEIGHT)):
|
| 290 |
if i < HEIGHT and i < WIDTH:
|
| 291 |
pixels[i, i, 2] = 255
|
|
@@ -304,10 +201,5 @@ if __name__ == "__main__":
|
|
| 304 |
print(f"🎨 Resolution: {WIDTH}x{HEIGHT}")
|
| 305 |
print(f"📊 Data per frame: {WIDTH * HEIGHT * 3} bytes")
|
| 306 |
print("="*60)
|
| 307 |
-
print("✅ Endpoints:")
|
| 308 |
-
print(" / - Noise animation page")
|
| 309 |
-
print(" /noise - Raw noise data")
|
| 310 |
-
print(" /test - Test pattern")
|
| 311 |
-
print("="*60)
|
| 312 |
|
| 313 |
app.run(host='0.0.0.0', port=7860, debug=False, threaded=True)
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
# static_noise_fixed.py - Flask generates random noise
|
| 3 |
import numpy as np
|
| 4 |
import time
|
| 5 |
from flask import Flask, Response
|
|
|
|
| 11 |
|
| 12 |
@app.route('/')
|
| 13 |
def index():
|
| 14 |
+
return '''<!DOCTYPE html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
<html>
|
| 16 |
<head>
|
| 17 |
<title>Static Noise Animation</title>
|
|
|
|
| 46 |
let frames = 0;
|
| 47 |
let lastTime = Date.now();
|
| 48 |
let fps = 0;
|
| 49 |
+
let colorMode = 0;
|
| 50 |
|
| 51 |
function loadNoiseFrame() {
|
| 52 |
const startTime = Date.now();
|
|
|
|
| 57 |
return response.arrayBuffer();
|
| 58 |
})
|
| 59 |
.then(buffer => {
|
|
|
|
| 60 |
const imageData = new ImageData(400, 300);
|
| 61 |
const rgba = imageData.data;
|
| 62 |
const rgb = new Uint8Array(buffer);
|
| 63 |
|
|
|
|
| 64 |
if (rgb.length !== 400*300*3) {
|
| 65 |
console.error('Wrong size:', rgb.length);
|
| 66 |
return;
|
| 67 |
}
|
| 68 |
|
|
|
|
| 69 |
for (let i = 0, j = 0; i < rgba.length; i += 4, j += 3) {
|
| 70 |
+
rgba[i] = rgb[j];
|
| 71 |
rgba[i + 1] = rgb[j + 1];
|
| 72 |
rgba[i + 2] = rgb[j + 2];
|
| 73 |
rgba[i + 3] = 255;
|
| 74 |
}
|
| 75 |
|
| 76 |
+
if (colorMode === 2) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
for (let i = 0; i < rgba.length; i += 4) {
|
| 78 |
const r = rgba[i];
|
| 79 |
const g = rgba[i + 1];
|
|
|
|
| 84 |
rgba[i + 2] = Math.min(255, (r * 0.272) + (g * 0.534) + (b * 0.131));
|
| 85 |
}
|
| 86 |
}
|
|
|
|
| 87 |
|
|
|
|
| 88 |
ctx.putImageData(imageData, 0, 0);
|
| 89 |
|
|
|
|
| 90 |
frames++;
|
| 91 |
const now = Date.now();
|
| 92 |
if (now - lastTime >= 1000) {
|
|
|
|
| 98 |
|
| 99 |
document.getElementById('frameCount').textContent = parseInt(document.getElementById('frameCount').textContent) + 1;
|
| 100 |
document.getElementById('status').textContent = 'Streaming ✓';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
})
|
| 102 |
.catch(error => {
|
| 103 |
console.error('Error:', error);
|
|
|
|
| 108 |
|
| 109 |
function startNoise() {
|
| 110 |
if (!noiseInterval) {
|
| 111 |
+
noiseInterval = setInterval(loadNoiseFrame, 33);
|
| 112 |
document.getElementById('status').textContent = 'Noise started ✓';
|
| 113 |
console.log('Noise animation started');
|
| 114 |
}
|
|
|
|
| 130 |
document.getElementById('status').textContent = 'Mode: ' + modes[colorMode];
|
| 131 |
}
|
| 132 |
|
|
|
|
| 133 |
setTimeout(startNoise, 1000);
|
| 134 |
console.log('Static noise page loaded');
|
| 135 |
</script>
|
| 136 |
</body>
|
| 137 |
+
</html>'''
|
|
|
|
| 138 |
|
| 139 |
@app.route('/noise')
|
| 140 |
def get_noise():
|
|
|
|
| 143 |
|
| 144 |
# Create random noise
|
| 145 |
if frame_count % 3 == 0:
|
|
|
|
| 146 |
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
|
|
|
|
| 147 |
gray = np.random.randint(0, 256, (HEIGHT, WIDTH, 1), dtype=np.uint8)
|
| 148 |
noise[:, :, :] = gray
|
| 149 |
elif frame_count % 3 == 1:
|
|
|
|
| 150 |
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
|
| 151 |
else:
|
|
|
|
| 152 |
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
|
|
|
|
| 153 |
noise[::4, :, :] = noise[::4, :, :] // 2
|
| 154 |
|
| 155 |
+
# Occasionally add a "glitch"
|
| 156 |
+
if np.random.random() < 0.02:
|
| 157 |
glitch_color = np.random.randint(0, 256, 3)
|
| 158 |
glitch_height = np.random.randint(20, 100)
|
| 159 |
glitch_y = np.random.randint(0, HEIGHT - glitch_height)
|
|
|
|
| 180 |
@app.route('/test')
|
| 181 |
def test():
|
| 182 |
"""Test endpoint - returns simple pattern"""
|
|
|
|
| 183 |
pixels = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8)
|
|
|
|
|
|
|
| 184 |
pixels[:HEIGHT//2, :, 0] = 255
|
|
|
|
|
|
|
| 185 |
pixels[HEIGHT//2:, :, 1] = 255
|
|
|
|
|
|
|
| 186 |
for i in range(min(WIDTH, HEIGHT)):
|
| 187 |
if i < HEIGHT and i < WIDTH:
|
| 188 |
pixels[i, i, 2] = 255
|
|
|
|
| 201 |
print(f"🎨 Resolution: {WIDTH}x{HEIGHT}")
|
| 202 |
print(f"📊 Data per frame: {WIDTH * HEIGHT * 3} bytes")
|
| 203 |
print("="*60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
app.run(host='0.0.0.0', port=7860, debug=False, threaded=True)
|