Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -356,7 +356,7 @@ class HRCollectorVideoProcessor:
|
|
| 356 |
|
| 357 |
def stop_and_dump_to_file(self) -> Optional[str]:
|
| 358 |
self.recording = False
|
| 359 |
-
|
| 360 |
return None
|
| 361 |
h, w = self.frames[0].shape[:2]
|
| 362 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
|
@@ -382,6 +382,9 @@ if "captured_image_bgr" not in st.session_state:
|
|
| 382 |
st.session_state.captured_image_bgr = None
|
| 383 |
if "captured_video_path" not in st.session_state:
|
| 384 |
st.session_state.captured_video_path = None
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
mode = st.radio("Choose Input Mode", ["Image", "Video"], horizontal=True)
|
| 387 |
|
|
@@ -396,9 +399,10 @@ with col_left:
|
|
| 396 |
img_source = st.radio("Source", ["Camera", "Upload"], horizontal=True, key="img_source")
|
| 397 |
uploaded_img = None
|
| 398 |
if img_source == "Camera":
|
| 399 |
-
|
|
|
|
| 400 |
if cam_img is not None:
|
| 401 |
-
|
| 402 |
else:
|
| 403 |
file_up = st.file_uploader("Upload JPG/PNG", type=["jpg", "jpeg", "png"])
|
| 404 |
if file_up is not None:
|
|
@@ -406,11 +410,16 @@ with col_left:
|
|
| 406 |
|
| 407 |
submitted_img = st.form_submit_button("Save Image")
|
| 408 |
if submitted_img:
|
| 409 |
-
|
| 410 |
-
|
|
|
|
|
|
|
| 411 |
img_bgr = cv2.imdecode(arr, cv2.IMREAD_COLOR)
|
| 412 |
-
|
| 413 |
-
|
|
|
|
|
|
|
|
|
|
| 414 |
else:
|
| 415 |
st.warning("No image provided.")
|
| 416 |
|
|
@@ -484,6 +493,13 @@ report_placeholder = st.empty()
|
|
| 484 |
|
| 485 |
# Run analysis only when the explicit Analyze buttons are pressed
|
| 486 |
if mode == "Image" and 'analyze_image' in locals() and analyze_image:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
html, frame_rgb_blurred = analyze_face(st.session_state.captured_image_bgr)
|
| 488 |
if frame_rgb_blurred is not None:
|
| 489 |
preview_placeholder.image(frame_rgb_blurred, caption="Blurred Image", use_container_width=True)
|
|
|
|
| 356 |
|
| 357 |
def stop_and_dump_to_file(self) -> Optional[str]:
|
| 358 |
self.recording = False
|
| 359 |
+
if len(self.frames) < 30: # ~1s
|
| 360 |
return None
|
| 361 |
h, w = self.frames[0].shape[:2]
|
| 362 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
|
|
|
| 382 |
st.session_state.captured_image_bgr = None
|
| 383 |
if "captured_video_path" not in st.session_state:
|
| 384 |
st.session_state.captured_video_path = None
|
| 385 |
+
# NEW: buffer raw camera bytes in case user forgets to click "Save Image"
|
| 386 |
+
if "img_bytes_buffer" not in st.session_state:
|
| 387 |
+
st.session_state.img_bytes_buffer = None
|
| 388 |
|
| 389 |
mode = st.radio("Choose Input Mode", ["Image", "Video"], horizontal=True)
|
| 390 |
|
|
|
|
| 399 |
img_source = st.radio("Source", ["Camera", "Upload"], horizontal=True, key="img_source")
|
| 400 |
uploaded_img = None
|
| 401 |
if img_source == "Camera":
|
| 402 |
+
# KEY ADDED + auto-buffer captured bytes
|
| 403 |
+
cam_img = st.camera_input("Capture image", key="camera_img")
|
| 404 |
if cam_img is not None:
|
| 405 |
+
st.session_state.img_bytes_buffer = cam_img.getvalue()
|
| 406 |
else:
|
| 407 |
file_up = st.file_uploader("Upload JPG/PNG", type=["jpg", "jpeg", "png"])
|
| 408 |
if file_up is not None:
|
|
|
|
| 410 |
|
| 411 |
submitted_img = st.form_submit_button("Save Image")
|
| 412 |
if submitted_img:
|
| 413 |
+
# Prefer explicit upload; else fall back to auto-buffered camera bytes
|
| 414 |
+
raw_bytes = uploaded_img if uploaded_img else st.session_state.img_bytes_buffer
|
| 415 |
+
if raw_bytes:
|
| 416 |
+
arr = np.frombuffer(raw_bytes, dtype=np.uint8)
|
| 417 |
img_bgr = cv2.imdecode(arr, cv2.IMREAD_COLOR)
|
| 418 |
+
if img_bgr is not None:
|
| 419 |
+
st.session_state.captured_image_bgr = img_bgr
|
| 420 |
+
st.success("Image saved. Now click **Analyze Image** below.")
|
| 421 |
+
else:
|
| 422 |
+
st.warning("Could not decode the image. Please recapture/upload.")
|
| 423 |
else:
|
| 424 |
st.warning("No image provided.")
|
| 425 |
|
|
|
|
| 493 |
|
| 494 |
# Run analysis only when the explicit Analyze buttons are pressed
|
| 495 |
if mode == "Image" and 'analyze_image' in locals() and analyze_image:
|
| 496 |
+
# If user forgot "Save Image", try auto-buffer
|
| 497 |
+
if st.session_state.captured_image_bgr is None and st.session_state.img_bytes_buffer is not None:
|
| 498 |
+
arr = np.frombuffer(st.session_state.img_bytes_buffer, dtype=np.uint8)
|
| 499 |
+
img_bgr = cv2.imdecode(arr, cv2.IMREAD_COLOR)
|
| 500 |
+
if img_bgr is not None:
|
| 501 |
+
st.session_state.captured_image_bgr = img_bgr
|
| 502 |
+
|
| 503 |
html, frame_rgb_blurred = analyze_face(st.session_state.captured_image_bgr)
|
| 504 |
if frame_rgb_blurred is not None:
|
| 505 |
preview_placeholder.image(frame_rgb_blurred, caption="Blurred Image", use_container_width=True)
|