Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,9 @@ import time
|
|
| 7 |
from dataclasses import dataclass
|
| 8 |
import zipfile
|
| 9 |
import logging
|
| 10 |
-
import
|
| 11 |
-
from
|
|
|
|
| 12 |
|
| 13 |
# Logging setup
|
| 14 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
@@ -189,34 +190,6 @@ def zip_files(files, zip_name):
|
|
| 189 |
zipf.write(file, os.path.basename(file))
|
| 190 |
return zip_name
|
| 191 |
|
| 192 |
-
# Video Processor for WebRTC
|
| 193 |
-
class CameraProcessor(VideoProcessorBase):
|
| 194 |
-
def __init__(self):
|
| 195 |
-
self.snapshot = None
|
| 196 |
-
self.frames = []
|
| 197 |
-
self.recording = False
|
| 198 |
-
|
| 199 |
-
def recv(self, frame):
|
| 200 |
-
from PIL import Image
|
| 201 |
-
img = frame.to_image()
|
| 202 |
-
self.snapshot = img
|
| 203 |
-
if self.recording:
|
| 204 |
-
self.frames.append(frame.to_ndarray(format="bgr24"))
|
| 205 |
-
return av.VideoFrame.from_image(img)
|
| 206 |
-
|
| 207 |
-
def capture_frame(self):
|
| 208 |
-
from PIL import Image
|
| 209 |
-
return self.snapshot
|
| 210 |
-
|
| 211 |
-
def capture_video(self, duration=10):
|
| 212 |
-
self.recording = True
|
| 213 |
-
self.frames = []
|
| 214 |
-
start_time = time.time()
|
| 215 |
-
while time.time() - start_time < duration and self.recording:
|
| 216 |
-
time.sleep(0.033) # ~30 FPS
|
| 217 |
-
self.recording = False
|
| 218 |
-
return self.frames
|
| 219 |
-
|
| 220 |
# Main App
|
| 221 |
st.title("SFT Tiny Titans 🚀 (Dual Cam Action!)")
|
| 222 |
|
|
@@ -231,7 +204,6 @@ def update_gallery():
|
|
| 231 |
for idx, file in enumerate(media_files[:4]):
|
| 232 |
with cols[idx % 2]:
|
| 233 |
if file.endswith(".png"):
|
| 234 |
-
from PIL import Image
|
| 235 |
st.image(Image.open(file), caption=file.split('/')[-1], use_container_width=True)
|
| 236 |
elif file.endswith(".mp4"):
|
| 237 |
st.video(file)
|
|
@@ -277,82 +249,82 @@ with tab1:
|
|
| 277 |
|
| 278 |
with tab2:
|
| 279 |
st.header("Camera Snap 📷 (Dual Live Feed!)")
|
|
|
|
| 280 |
cols = st.columns(2)
|
| 281 |
-
processors = {}
|
| 282 |
for i in range(2):
|
| 283 |
with cols[i]:
|
| 284 |
st.subheader(f"Camera {i}")
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
if st.button(f"Capture Frame 📸 Cam {i}", key=f"snap_{i}"):
|
| 293 |
logger.info(f"Capturing frame from Camera {i}")
|
| 294 |
try:
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
st.session_state['captured_images']
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
st.error("No frame captured!")
|
| 308 |
-
logger.error("No frame available for snapshot")
|
| 309 |
else:
|
| 310 |
-
st.error("
|
| 311 |
-
logger.error("
|
| 312 |
except Exception as e:
|
| 313 |
st.error(f"Frame capture failed: {str(e)}")
|
| 314 |
logger.error(f"Error capturing frame: {str(e)}")
|
|
|
|
| 315 |
if st.button(f"Capture Video 🎥 Cam {i}", key=f"rec_{i}"):
|
| 316 |
logger.info(f"Capturing 10s video from Camera {i}")
|
| 317 |
try:
|
| 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 |
-
update_gallery()
|
| 347 |
-
else:
|
| 348 |
-
st.error("No frames recorded!")
|
| 349 |
-
logger.error("No frames captured during video recording")
|
| 350 |
-
else:
|
| 351 |
-
st.error("Camera processor not initialized!")
|
| 352 |
-
logger.error("Processor not ready for Camera {i}")
|
| 353 |
except Exception as e:
|
| 354 |
st.error(f"Video capture failed: {str(e)}")
|
| 355 |
logger.error(f"Error capturing video: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
with tab3:
|
| 358 |
st.header("Fine-Tune Titans 🔧 (Tune Fast!)")
|
|
@@ -379,7 +351,6 @@ with tab3:
|
|
| 379 |
if st.button("Tune CV 🔄"):
|
| 380 |
logger.info("Initiating CV fine-tune")
|
| 381 |
try:
|
| 382 |
-
from PIL import Image
|
| 383 |
images = [Image.open(img) for img in captured_images]
|
| 384 |
st.session_state['builder'].fine_tune(images, texts)
|
| 385 |
st.success("CV polished! 🎉")
|
|
@@ -410,7 +381,6 @@ with tab4:
|
|
| 410 |
if st.button("Run CV Demo ▶️"):
|
| 411 |
logger.info("Running CV image set demo")
|
| 412 |
try:
|
| 413 |
-
from PIL import Image
|
| 414 |
images = [Image.open(img) for img in captured_images[:10]]
|
| 415 |
prompts = ["Neon " + os.path.basename(img).split('.')[0] for img in captured_images[:10]]
|
| 416 |
generated_images = []
|
|
|
|
| 7 |
from dataclasses import dataclass
|
| 8 |
import zipfile
|
| 9 |
import logging
|
| 10 |
+
import cv2
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import numpy as np
|
| 13 |
|
| 14 |
# Logging setup
|
| 15 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
|
|
| 190 |
zipf.write(file, os.path.basename(file))
|
| 191 |
return zip_name
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
# Main App
|
| 194 |
st.title("SFT Tiny Titans 🚀 (Dual Cam Action!)")
|
| 195 |
|
|
|
|
| 204 |
for idx, file in enumerate(media_files[:4]):
|
| 205 |
with cols[idx % 2]:
|
| 206 |
if file.endswith(".png"):
|
|
|
|
| 207 |
st.image(Image.open(file), caption=file.split('/')[-1], use_container_width=True)
|
| 208 |
elif file.endswith(".mp4"):
|
| 209 |
st.video(file)
|
|
|
|
| 249 |
|
| 250 |
with tab2:
|
| 251 |
st.header("Camera Snap 📷 (Dual Live Feed!)")
|
| 252 |
+
caps = {0: cv2.VideoCapture(0), 1: cv2.VideoCapture(1)}
|
| 253 |
cols = st.columns(2)
|
|
|
|
| 254 |
for i in range(2):
|
| 255 |
with cols[i]:
|
| 256 |
st.subheader(f"Camera {i}")
|
| 257 |
+
if caps[i].isOpened():
|
| 258 |
+
ret, frame = caps[i].read()
|
| 259 |
+
if ret:
|
| 260 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 261 |
+
st.image(frame_rgb, caption=f"Live Feed Cam {i}", use_container_width=True)
|
| 262 |
+
else:
|
| 263 |
+
st.warning(f"Camera {i} failed to read frame!")
|
| 264 |
+
logger.error(f"Failed to read frame from Camera {i}")
|
| 265 |
+
else:
|
| 266 |
+
st.warning(f"Camera {i} not detected!")
|
| 267 |
+
logger.error(f"Camera {i} not opened")
|
| 268 |
+
|
| 269 |
if st.button(f"Capture Frame 📸 Cam {i}", key=f"snap_{i}"):
|
| 270 |
logger.info(f"Capturing frame from Camera {i}")
|
| 271 |
try:
|
| 272 |
+
ret, frame = caps[i].read()
|
| 273 |
+
if ret:
|
| 274 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 275 |
+
img = Image.fromarray(frame_rgb)
|
| 276 |
+
filename = generate_filename(i)
|
| 277 |
+
img.save(filename)
|
| 278 |
+
st.image(img, caption=filename, use_container_width=True)
|
| 279 |
+
logger.info(f"Saved snapshot: {filename}")
|
| 280 |
+
if 'captured_images' not in st.session_state:
|
| 281 |
+
st.session_state['captured_images'] = []
|
| 282 |
+
st.session_state['captured_images'].append(filename)
|
| 283 |
+
update_gallery()
|
|
|
|
|
|
|
| 284 |
else:
|
| 285 |
+
st.error("Failed to capture frame!")
|
| 286 |
+
logger.error(f"No frame captured from Camera {i}")
|
| 287 |
except Exception as e:
|
| 288 |
st.error(f"Frame capture failed: {str(e)}")
|
| 289 |
logger.error(f"Error capturing frame: {str(e)}")
|
| 290 |
+
|
| 291 |
if st.button(f"Capture Video 🎥 Cam {i}", key=f"rec_{i}"):
|
| 292 |
logger.info(f"Capturing 10s video from Camera {i}")
|
| 293 |
try:
|
| 294 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 295 |
+
mp4_filename = generate_filename(i, "mp4")
|
| 296 |
+
out = cv2.VideoWriter(mp4_filename, fourcc, 30.0, (int(caps[i].get(3)), int(caps[i].get(4))))
|
| 297 |
+
frames = []
|
| 298 |
+
start_time = time.time()
|
| 299 |
+
while time.time() - start_time < 10:
|
| 300 |
+
ret, frame = caps[i].read()
|
| 301 |
+
if ret:
|
| 302 |
+
frames.append(frame)
|
| 303 |
+
out.write(frame)
|
| 304 |
+
time.sleep(0.033) # ~30 FPS
|
| 305 |
+
out.release()
|
| 306 |
+
st.video(mp4_filename)
|
| 307 |
+
logger.info(f"Saved video: {mp4_filename}")
|
| 308 |
+
# Slice into 10 frames
|
| 309 |
+
sliced_images = []
|
| 310 |
+
step = max(1, len(frames) // 10)
|
| 311 |
+
for j in range(0, len(frames), step):
|
| 312 |
+
if len(sliced_images) < 10:
|
| 313 |
+
frame_rgb = cv2.cvtColor(frames[j], cv2.COLOR_BGR2RGB)
|
| 314 |
+
img = Image.fromarray(frame_rgb)
|
| 315 |
+
img_filename = generate_filename(f"{i}_{len(sliced_images)}")
|
| 316 |
+
img.save(img_filename)
|
| 317 |
+
sliced_images.append(img_filename)
|
| 318 |
+
st.image(img, caption=img_filename, use_container_width=True)
|
| 319 |
+
st.session_state['captured_images'] = st.session_state.get('captured_images', []) + sliced_images
|
| 320 |
+
logger.info(f"Sliced video into {len(sliced_images)} images")
|
| 321 |
+
update_gallery()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
except Exception as e:
|
| 323 |
st.error(f"Video capture failed: {str(e)}")
|
| 324 |
logger.error(f"Error capturing video: {str(e)}")
|
| 325 |
+
# Release cameras after use
|
| 326 |
+
for cap in caps.values():
|
| 327 |
+
cap.release()
|
| 328 |
|
| 329 |
with tab3:
|
| 330 |
st.header("Fine-Tune Titans 🔧 (Tune Fast!)")
|
|
|
|
| 351 |
if st.button("Tune CV 🔄"):
|
| 352 |
logger.info("Initiating CV fine-tune")
|
| 353 |
try:
|
|
|
|
| 354 |
images = [Image.open(img) for img in captured_images]
|
| 355 |
st.session_state['builder'].fine_tune(images, texts)
|
| 356 |
st.success("CV polished! 🎉")
|
|
|
|
| 381 |
if st.button("Run CV Demo ▶️"):
|
| 382 |
logger.info("Running CV image set demo")
|
| 383 |
try:
|
|
|
|
| 384 |
images = [Image.open(img) for img in captured_images[:10]]
|
| 385 |
prompts = ["Neon " + os.path.basename(img).split('.')[0] for img in captured_images[:10]]
|
| 386 |
generated_images = []
|