Spaces:
Sleeping
Sleeping
File size: 7,528 Bytes
9f8b91e d9883cb 8424eb1 9f8b91e 8424eb1 9f8b91e 8f79a8f 8424eb1 d9883cb 1f2f165 8424eb1 1f2f165 42f531a 1f2f165 42f531a 1f2f165 9f8b91e 1f2f165 d9883cb 1f2f165 d9883cb 1f2f165 d9883cb 1f2f165 d9883cb 1f2f165 d9883cb 1f2f165 8f79a8f 1f2f165 8f79a8f d9883cb 1f2f165 8f79a8f 1f2f165 8f79a8f 1f2f165 8f79a8f 1f2f165 8f79a8f 9f8b91e 8f79a8f 1f2f165 9f8b91e 8f79a8f 1f2f165 8f79a8f 1f2f165 | 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 | #!/usr/bin/env python3
# static_noise_fixed.py - Flask generates random noise
import numpy as np
import time
from flask import Flask, Response
app = Flask(__name__)
WIDTH, HEIGHT = 400, 300
frame_count = 0
@app.route('/')
def index():
return '''<!DOCTYPE html>
<html>
<head>
<title>Static Noise Animation</title>
<style>
body { margin: 0; padding: 20px; background: #111; color: white; font-family: monospace; text-align: center; }
canvas { border: 3px solid #0af; background: black; display: block; margin: 20px auto; }
.stats { background: #222; padding: 15px; border-radius: 8px; display: inline-block; margin: 10px; }
button { background: #0af; color: white; border: none; padding: 10px 20px; margin: 5px; border-radius: 5px; cursor: pointer; }
</style>
</head>
<body>
<h1 style="color: #0af;">πΊ Static Noise Animation</h1>
<canvas id="canvas" width="400" height="300"></canvas>
<div class="stats">
<div>Frames: <span id="frameCount">0</span></div>
<div>FPS: <span id="fps">0</span></div>
<div>Status: <span id="status" style="color: #0af;">Loading...</span></div>
</div>
<div>
<button onclick="startNoise()">Start Noise</button>
<button onclick="stopNoise()">Stop Noise</button>
<button onclick="changeColor()">Change Color</button>
</div>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let noiseInterval = null;
let frames = 0;
let lastTime = Date.now();
let fps = 0;
let colorMode = 0;
function loadNoiseFrame() {
const startTime = Date.now();
fetch('/noise')
.then(response => {
if (!response.ok) throw new Error('HTTP ' + response.status);
return response.arrayBuffer();
})
.then(buffer => {
const imageData = new ImageData(400, 300);
const rgba = imageData.data;
const rgb = new Uint8Array(buffer);
if (rgb.length !== 400*300*3) {
console.error('Wrong size:', rgb.length);
return;
}
for (let i = 0, j = 0; i < rgba.length; i += 4, j += 3) {
rgba[i] = rgb[j];
rgba[i + 1] = rgb[j + 1];
rgba[i + 2] = rgb[j + 2];
rgba[i + 3] = 255;
}
if (colorMode === 2) {
for (let i = 0; i < rgba.length; i += 4) {
const r = rgba[i];
const g = rgba[i + 1];
const b = rgba[i + 2];
rgba[i] = Math.min(255, (r * 0.393) + (g * 0.769) + (b * 0.189));
rgba[i + 1] = Math.min(255, (r * 0.349) + (g * 0.686) + (b * 0.168));
rgba[i + 2] = Math.min(255, (r * 0.272) + (g * 0.534) + (b * 0.131));
}
}
ctx.putImageData(imageData, 0, 0);
frames++;
const now = Date.now();
if (now - lastTime >= 1000) {
fps = frames;
frames = 0;
lastTime = now;
document.getElementById('fps').textContent = fps;
}
document.getElementById('frameCount').textContent = parseInt(document.getElementById('frameCount').textContent) + 1;
document.getElementById('status').textContent = 'Streaming β';
})
.catch(error => {
console.error('Error:', error);
document.getElementById('status').textContent = 'Error: ' + error.message;
document.getElementById('status').style.color = 'red';
});
}
function startNoise() {
if (!noiseInterval) {
noiseInterval = setInterval(loadNoiseFrame, 33);
document.getElementById('status').textContent = 'Noise started β';
console.log('Noise animation started');
}
}
function stopNoise() {
if (noiseInterval) {
clearInterval(noiseInterval);
noiseInterval = null;
document.getElementById('status').textContent = 'Stopped';
console.log('Noise animation stopped');
}
}
function changeColor() {
colorMode = (colorMode + 1) % 3;
const modes = ['Grayscale', 'Color', 'Sepia'];
console.log('Color mode:', modes[colorMode]);
document.getElementById('status').textContent = 'Mode: ' + modes[colorMode];
}
setTimeout(startNoise, 1000);
console.log('Static noise page loaded');
</script>
</body>
</html>'''
@app.route('/noise')
def get_noise():
"""Generate random static noise"""
global frame_count
# Create random noise
if frame_count % 3 == 0:
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
gray = np.random.randint(0, 256, (HEIGHT, WIDTH, 1), dtype=np.uint8)
noise[:, :, :] = gray
elif frame_count % 3 == 1:
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
else:
noise = np.random.randint(0, 256, (HEIGHT, WIDTH, 3), dtype=np.uint8)
noise[::4, :, :] = noise[::4, :, :] // 2
# Occasionally add a "glitch"
if np.random.random() < 0.02:
glitch_color = np.random.randint(0, 256, 3)
glitch_height = np.random.randint(20, 100)
glitch_y = np.random.randint(0, HEIGHT - glitch_height)
noise[glitch_y:glitch_y + glitch_height, :, :] = glitch_color
# Convert to bytes
raw_bytes = noise.tobytes()
frame_count += 1
if frame_count % 30 == 0:
print(f"π‘ Generated noise frame {frame_count}")
return Response(
raw_bytes,
mimetype='application/octet-stream',
headers={
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
'X-Frame-Count': str(frame_count)
}
)
@app.route('/test')
def test():
"""Test endpoint - returns simple pattern"""
pixels = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8)
pixels[:HEIGHT//2, :, 0] = 255
pixels[HEIGHT//2:, :, 1] = 255
for i in range(min(WIDTH, HEIGHT)):
if i < HEIGHT and i < WIDTH:
pixels[i, i, 2] = 255
return Response(
pixels.tobytes(),
mimetype='application/octet-stream',
headers={'Cache-Control': 'no-cache'}
)
if __name__ == "__main__":
print("="*60)
print("πΊ STATIC NOISE ANIMATION SERVER")
print("="*60)
print(f"π‘ Port: 7860")
print(f"π¨ Resolution: {WIDTH}x{HEIGHT}")
print(f"π Data per frame: {WIDTH * HEIGHT * 3} bytes")
print("="*60)
app.run(host='0.0.0.0', port=7860, debug=False, threaded=True) |