-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
359 lines (316 loc) · 9.37 KB
/
app.py
File metadata and controls
359 lines (316 loc) · 9.37 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
Mobile Camera & Microphone Access via Web Browser
--------------------------------------------------
Run this on your computer/server, then open the URL on your mobile phone.
Both devices must be on the same WiFi network.
Install: pip install flask
Run: python app.py
Open: http://<your-computer-ip>:5000 (on your phone's browser)
"""
from flask import Flask, render_template_string
import socket
app = Flask(__name__)
HTML = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Camera & Mic Access</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Courier New', monospace;
background: #0a0a0a;
color: #00ff88;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
@keyframes sparkleFlow {
0% { background-position: 200% center; }
100% { background-position: -200% center; }
}
h1 {
font-size: 1.4rem;
letter-spacing: 0.15em;
text-transform: uppercase;
margin: 20px 0 8px;
background: linear-gradient(90deg, #00ff88 0%, #ffffff 25%, #00ff88 50%, #ffffff 75%, #00ff88 100%);
background-size: 200% auto;
color: transparent;
-webkit-background-clip: text;
background-clip: text;
animation: sparkleFlow 3s linear infinite;
filter: drop-shadow(0 0 8px rgba(0, 255, 136, 0.4));
}
.subtitle {
font-size: 0.75rem;
color: #00cc6a;
letter-spacing: 0.1em;
margin-bottom: 24px;
}
#videoContainer {
position: relative;
width: 100%;
max-width: 480px;
border-radius: 4px;
overflow: hidden;
border: 1px solid #222;
background: #111;
aspect-ratio: 4/3;
}
video {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
transform: scaleX(-1); /* mirror selfie cam */
}
#overlay {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: #111;
transition: opacity 0.4s;
}
#overlay.hidden { opacity: 0; pointer-events: none; }
#overlay p { color: #00cc6a; font-size: 0.85rem; }
.controls {
display: flex;
gap: 12px;
margin-top: 16px;
flex-wrap: wrap;
justify-content: center;
width: 100%;
max-width: 480px;
}
button {
flex: 1;
min-width: 120px;
padding: 12px 16px;
border: 1px solid #005522;
background: #141414;
color: #00ff88;
font-family: 'Courier New', monospace;
font-size: 0.8rem;
letter-spacing: 0.08em;
cursor: pointer;
border-radius: 3px;
transition: all 0.2s;
}
button:hover { border-color: #00ff88; color: #00ff88; }
button:disabled { opacity: 0.3; cursor: not-allowed; }
button.active { border-color: #ff4444; color: #ff4444; background: #1a0000; }
#status {
margin-top: 16px;
font-size: 0.75rem;
color: #00cc6a;
letter-spacing: 0.08em;
min-height: 1.2em;
text-align: center;
}
#vuMeter {
width: 100%;
max-width: 480px;
margin-top: 12px;
height: 6px;
background: #1a1a1a;
border-radius: 3px;
overflow: hidden;
display: none;
}
#vuBar {
height: 100%;
width: 0%;
background: linear-gradient(90deg, #00ff88, #ffcc00, #ff4444);
border-radius: 3px;
transition: width 0.05s;
}
#photoStrip {
display: flex;
gap: 8px;
flex-wrap: wrap;
justify-content: center;
width: 100%;
max-width: 480px;
margin-top: 16px;
}
#photoStrip img {
width: 100px;
height: 75px;
object-fit: cover;
border: 1px solid #222;
border-radius: 2px;
cursor: pointer;
}
canvas { display: none; }
.cam-toggle {
width: 100%;
max-width: 480px;
margin-top: 8px;
}
</style>
</head>
<body>
<h1>● Camera & Mic</h1>
<p class="subtitle">python · flask · webrtc</p>
<div id="videoContainer">
<video id="video" autoplay playsinline muted></video>
<div id="overlay"><p>[ stream inactive ]</p></div>
</div>
<div class="controls">
<button id="btnStart" onclick="startStream()">▶ Start Stream</button>
<button id="btnPhoto" onclick="takePhoto()" disabled>⬡ Capture</button>
<button id="btnMic" onclick="toggleMic()" disabled>⬡ Mic Monitor</button>
<button id="btnStop" onclick="stopStream()" disabled>■ Stop</button>
</div>
<button class="cam-toggle" id="btnFlip" onclick="flipCamera()" disabled style="max-width:480px;margin-top:8px;">
⇄ Flip Camera
</button>
<div id="vuMeter"><div id="vuBar"></div></div>
<div id="status">press start to request permissions</div>
<canvas id="canvas"></canvas>
<div id="photoStrip"></div>
<script>
let stream = null;
let audioCtx = null;
let analyser = null;
let vuRaf = null;
let micActive = false;
let facingMode = 'user'; // 'user' = front, 'environment' = back
const video = document.getElementById('video');
const overlay = document.getElementById('overlay');
const vuMeter = document.getElementById('vuMeter');
const vuBar = document.getElementById('vuBar');
const status = document.getElementById('status');
const canvas = document.getElementById('canvas');
const strip = document.getElementById('photoStrip');
function setStatus(msg) { status.textContent = msg; }
async function startStream() {
try {
setStatus('requesting permissions…');
stream = await navigator.mediaDevices.getUserMedia({
video: { facingMode, width: { ideal: 1280 }, height: { ideal: 960 } },
audio: true
});
video.srcObject = stream;
overlay.classList.add('hidden');
document.getElementById('btnStart').disabled = true;
document.getElementById('btnPhoto').disabled = false;
document.getElementById('btnMic').disabled = false;
document.getElementById('btnStop').disabled = false;
document.getElementById('btnFlip').disabled = false;
setStatus('streaming — camera + mic active');
} catch (e) {
setStatus('error: ' + e.message);
console.error(e);
}
}
function stopStream() {
if (stream) stream.getTracks().forEach(t => t.stop());
stream = null;
video.srcObject = null;
overlay.classList.remove('hidden');
stopMicMonitor();
document.getElementById('btnStart').disabled = false;
document.getElementById('btnPhoto').disabled = true;
document.getElementById('btnMic').disabled = true;
document.getElementById('btnStop').disabled = true;
document.getElementById('btnFlip').disabled = true;
setStatus('stream stopped');
}
function takePhoto() {
if (!stream) return;
const w = video.videoWidth, h = video.videoHeight;
canvas.width = w; canvas.height = h;
const ctx = canvas.getContext('2d');
ctx.save();
ctx.scale(-1, 1); ctx.drawImage(video, -w, 0, w, h); // un-mirror
ctx.restore();
const dataUrl = canvas.toDataURL('image/jpeg', 0.92);
const img = document.createElement('img');
img.src = dataUrl;
img.title = 'Click to download';
img.onclick = () => {
const a = document.createElement('a');
a.href = dataUrl;
a.download = 'capture_' + Date.now() + '.jpg';
a.click();
};
strip.prepend(img);
setStatus('photo captured — click thumbnail to download');
}
function toggleMic() {
micActive ? stopMicMonitor() : startMicMonitor();
}
function startMicMonitor() {
if (!stream) return;
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const source = audioCtx.createMediaStreamSource(stream);
analyser = audioCtx.createAnalyser();
analyser.fftSize = 256;
source.connect(analyser);
vuMeter.style.display = 'block';
micActive = true;
document.getElementById('btnMic').classList.add('active');
document.getElementById('btnMic').textContent = '⬡ Mic ON';
drawVU();
setStatus('microphone monitor active');
}
function stopMicMonitor() {
if (vuRaf) cancelAnimationFrame(vuRaf);
if (audioCtx) audioCtx.close();
vuMeter.style.display = 'none';
vuBar.style.width = '0%';
micActive = false;
const btn = document.getElementById('btnMic');
btn.classList.remove('active');
btn.textContent = '⬡ Mic Monitor';
}
function drawVU() {
const data = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(data);
const avg = data.reduce((a, b) => a + b, 0) / data.length;
vuBar.style.width = Math.min(100, avg * 2.5) + '%';
vuRaf = requestAnimationFrame(drawVU);
}
async function flipCamera() {
facingMode = facingMode === 'user' ? 'environment' : 'user';
if (stream) { stream.getTracks().forEach(t => t.stop()); }
await startStream();
// re-disable btnStart after flip
document.getElementById('btnStart').disabled = true;
}
</script>
</body>
</html>
"""
def get_local_ip():
"""Get the machine's local network IP address."""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
except Exception:
return "127.0.0.1"
finally:
s.close()
@app.route("/")
def index():
return render_template_string(HTML)
if __name__ == "__main__":
ip = get_local_ip()
print("\n" + "="*50)
print(" Mobile Camera & Mic Access")
print("="*50)
print(f"\n Local: https://127.0.0.1:5000")
print(f" Network: https://{ip}:5000 ← open this on your phone")
print("\n Both devices must be on the same WiFi network.")
print(" Note: Your browser may show a 'Not Secure' warning. You can safely bypass it.\n")
app.run(host="0.0.0.0", port=5000, debug=False, ssl_context='adhoc')