awacke1 commited on
Commit
29c477e
Β·
verified Β·
1 Parent(s): 7af0bfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -44
app.py CHANGED
@@ -3,6 +3,8 @@ import pandas as pd
3
  from datetime import datetime
4
  import os
5
  import base64
 
 
6
 
7
  # 🌟πŸ”₯ Initialize session state like a galactic DJ spinning tracks!
8
  if 'file_history' not in st.session_state:
@@ -33,79 +35,101 @@ with st.sidebar:
33
  st.write("πŸ•³οΈ Empty Stash!")
34
 
35
  # 🌍🎨 Main UI kicks off like a cosmic art show!
36
- st.title("πŸ“Έ Multi-Cam Snap Craze")
37
 
38
- # πŸ“ΈπŸ“· JS camera zone with multiple streams!
39
- st.header("πŸ“ΈπŸŽ₯ Snap Zone")
40
- multi_cam_html = """
41
- <div id="videoContainer"></div>
 
 
 
 
 
 
42
  <script>
43
- let streams = [];
44
- const container = document.getElementById('videoContainer');
45
- let autoCaptureIntervals = [];
 
 
46
 
47
  // πŸ“ΉπŸ” Enumerate cameras like a tech detective!
48
- async function enumerateAndStartCameras() {
49
  const devices = await navigator.mediaDevices.enumerateDevices();
50
  const videoDevices = devices.filter(device => device.kind === 'videoinput');
 
 
51
  videoDevices.forEach((device, index) => {
52
- const div = document.createElement('div');
53
- div.innerHTML = `
54
- <p>${device.label || 'Camera ' + index}</p>
55
- <video id="video${index}" autoplay playsinline style="width:100%;"></video>
56
- <canvas id="canvas${index}" style="display:none;"></canvas>
57
- <button onclick="takeSnapshot(${index})">πŸ“Έ Snap Cam ${index}</button>
58
- `;
59
- container.appendChild(div);
60
- startStream(device.deviceId, index);
61
  });
 
 
 
62
  }
63
 
64
- // πŸ“ΈπŸŽ₯ Start streaming like a live broadcast pro!
65
- async function startStream(deviceId, index) {
66
- const constraints = { video: { deviceId: { exact: deviceId } } };
67
- const stream = await navigator.mediaDevices.getUserMedia(constraints);
68
- const video = document.getElementById(`video${index}`);
69
- video.srcObject = stream;
70
- streams.push(stream);
71
- // Auto-capture every 10 seconds
72
- autoCaptureIntervals[index] = setInterval(() => takeSnapshot(index), 10000);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  }
74
 
75
  // πŸ“Έβœ‚οΈ Snap a pic like a stealthy paparazzi!
76
- function takeSnapshot(index) {
77
- const video = document.getElementById(`video${index}`);
78
- const canvas = document.getElementById(`canvas${index}`);
79
  canvas.width = video.videoWidth;
80
  canvas.height = video.videoHeight;
81
  canvas.getContext('2d').drawImage(video, 0, 0);
82
  const dataUrl = canvas.toDataURL('image/jpeg');
83
- window.parent.postMessage({ type: 'snapshot', data: dataUrl, camIndex: index }, '*');
84
  }
85
 
86
- // βΉοΈπŸ“΄ Stop all streams like a broadcast kill switch!
87
- function stopStreams() {
88
- streams.forEach(stream => stream.getTracks().forEach(track => track.stop()));
89
- streams = [];
90
- autoCaptureIntervals.forEach(interval => clearInterval(interval));
91
- autoCaptureIntervals = [];
 
 
92
  }
93
 
94
  // 🎬 Kick off the camera party!
95
- enumerateAndStartCameras();
96
- window.onunload = stopStreams;
97
  </script>
98
  """
99
- st.markdown(multi_cam_html, unsafe_allow_html=True)
100
 
101
  # πŸ“ΈπŸ“₯ Handle snapshots from JS
102
  def handle_snapshot():
103
  if "snapshot" in st.session_state:
104
  snapshot_data = st.session_state["snapshot"]
105
- cam_index = st.session_state.get("camIndex", 0)
106
- filename = f"cam{cam_index}_snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
107
  save_to_history("πŸ–ΌοΈ Image", filename, snapshot_data)
108
- st.image(Image.open(BytesIO(base64.b64decode(snapshot_data.split(',')[1]))), caption=f"Cam {cam_index} Snap", use_container_width=True)
109
  del st.session_state["snapshot"]
110
 
111
  st.components.v1.html(
@@ -113,7 +137,7 @@ st.components.v1.html(
113
  <script>
114
  window.addEventListener('message', function(event) {
115
  if (event.data.type === 'snapshot') {
116
- window.streamlitAPI.setComponentValue({snapshot: event.data.data, camIndex: event.data.camIndex});
117
  }
118
  });
119
  </script>
 
3
  from datetime import datetime
4
  import os
5
  import base64
6
+ from io import BytesIO
7
+ from PIL import Image
8
 
9
  # 🌟πŸ”₯ Initialize session state like a galactic DJ spinning tracks!
10
  if 'file_history' not in st.session_state:
 
35
  st.write("πŸ•³οΈ Empty Stash!")
36
 
37
  # 🌍🎨 Main UI kicks off like a cosmic art show!
38
+ st.title("πŸ“Έ Live Camera Snap Craze")
39
 
40
+ # πŸ“ΈπŸ“· JS live camera zone!
41
+ st.header("πŸ“ΈπŸŽ₯ Live Snap Zone")
42
+ live_camera_html = """
43
+ <div>
44
+ <select id="cameraSelect"></select>
45
+ <button id="startBtn" onclick="toggleStream()">πŸ“· Start Live</button>
46
+ <button onclick="takeSnapshot()">πŸ“Έ Snap Now</button>
47
+ <video id="video" autoplay playsinline style="width:100%;"></video>
48
+ <canvas id="canvas" style="display:none;"></canvas>
49
+ </div>
50
  <script>
51
+ let stream = null;
52
+ const video = document.getElementById('video');
53
+ const canvas = document.getElementById('canvas');
54
+ let autoCaptureInterval = null;
55
+ let isStreaming = false;
56
 
57
  // πŸ“ΉπŸ” Enumerate cameras like a tech detective!
58
+ async function enumerateCameras() {
59
  const devices = await navigator.mediaDevices.enumerateDevices();
60
  const videoDevices = devices.filter(device => device.kind === 'videoinput');
61
+ const select = document.getElementById('cameraSelect');
62
+ select.innerHTML = '';
63
  videoDevices.forEach((device, index) => {
64
+ const option = document.createElement('option');
65
+ option.value = device.deviceId;
66
+ option.text = device.label || `Camera ${index}`;
67
+ select.appendChild(option);
 
 
 
 
 
68
  });
69
+ if (videoDevices.length === 0) {
70
+ select.innerHTML = '<option>No cameras found</option>';
71
+ }
72
  }
73
 
74
+ // πŸ“ΈπŸŽ₯ Toggle live stream like a broadcast pro!
75
+ async function toggleStream() {
76
+ const startBtn = document.getElementById('startBtn');
77
+ if (isStreaming) {
78
+ stopStream();
79
+ startBtn.textContent = 'πŸ“· Start Live';
80
+ isStreaming = false;
81
+ } else {
82
+ const cameraId = document.getElementById('cameraSelect').value;
83
+ if (!cameraId) {
84
+ alert('No camera selected or available!');
85
+ return;
86
+ }
87
+ const constraints = { video: { deviceId: { exact: cameraId } } };
88
+ try {
89
+ stream = await navigator.mediaDevices.getUserMedia(constraints);
90
+ video.srcObject = stream;
91
+ startBtn.textContent = '⏹️ Stop Live';
92
+ isStreaming = true;
93
+ autoCaptureInterval = setInterval(takeSnapshot, 10000); // Auto-save every 10s
94
+ } catch (e) {
95
+ alert('Failed to start camera: ' + e.message);
96
+ }
97
+ }
98
  }
99
 
100
  // πŸ“Έβœ‚οΈ Snap a pic like a stealthy paparazzi!
101
+ function takeSnapshot() {
102
+ if (!stream) return;
 
103
  canvas.width = video.videoWidth;
104
  canvas.height = video.videoHeight;
105
  canvas.getContext('2d').drawImage(video, 0, 0);
106
  const dataUrl = canvas.toDataURL('image/jpeg');
107
+ window.parent.postMessage({ type: 'snapshot', data: dataUrl }, '*');
108
  }
109
 
110
+ // βΉοΈπŸ“΄ Stop the stream like a broadcast kill switch!
111
+ function stopStream() {
112
+ if (stream) {
113
+ stream.getTracks().forEach(track => track.stop());
114
+ stream = null;
115
+ video.srcObject = null;
116
+ clearInterval(autoCaptureInterval);
117
+ }
118
  }
119
 
120
  // 🎬 Kick off the camera party!
121
+ enumerateCameras();
 
122
  </script>
123
  """
124
+ st.markdown(live_camera_html, unsafe_allow_html=True)
125
 
126
  # πŸ“ΈπŸ“₯ Handle snapshots from JS
127
  def handle_snapshot():
128
  if "snapshot" in st.session_state:
129
  snapshot_data = st.session_state["snapshot"]
130
+ filename = f"snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg"
 
131
  save_to_history("πŸ–ΌοΈ Image", filename, snapshot_data)
132
+ st.image(Image.open(BytesIO(base64.b64decode(snapshot_data.split(',')[1]))), caption="Latest Snap", use_container_width=True)
133
  del st.session_state["snapshot"]
134
 
135
  st.components.v1.html(
 
137
  <script>
138
  window.addEventListener('message', function(event) {
139
  if (event.data.type === 'snapshot') {
140
+ window.streamlitAPI.setComponentValue({snapshot: event.data.data});
141
  }
142
  });
143
  </script>