Spaces:
Runtime error
Runtime error
fouadmahmoud283-ai commited on
Commit ·
ecf5f79
1
Parent(s): dfe1c5b
fixing camera n1235
Browse files299 unmodified lines30030130230330430516 unmodified lines322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364241 unmodified lines606607608609610611299 unmodified linescontainer = Nonestream = Nonecodec_name = Nonetry:container = av.open(output_path, mode="w")try:16 unmodified linesif frame_skip > 1 and frame_index % frame_skip != 0:continueimg_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)model.conf = conf_thresholdresults = model(img_rgb)detections = results.pandas().xyxy[0]relevant = detections[detections['name'].isin(WHEELCHAIR_CLASSES.values())]for name in relevant['name'].tolist():detection_counts[name] = detection_counts.get(name, 0) + 1rendered_frame = results.render()[0]if rendered_frame.shape[1] != width or rendered_frame.shape[0] != height:rendered_frame = cv2.resize(rendered_frame, (width, height))video_frame = av.VideoFrame.from_ndarray(rendered_frame, format="bgr24")for packet in stream.encode(video_frame):container.mux(packet)processed_frames += 1if total_frames > 0:progress.progress(min(processed_frames / total_frames, 1.0))for packet in stream.encode():container.mux(packet)except Exception:output_path = Nonefinally:progress.empty()cap.release()if container is not None:container.close()stats = {"total_frames": total_frames,"processed_frames": processed_frames,"detection_counts": detection_counts,"codec": codec_name}return output_path, stats241 unmodified linesstats = st.session_state.video_statsst.metric("🎞️ Total Frames", stats.get("total_frames", 0))st.metric("✅ Processed Frames", stats.get("processed_frames", 0))if stats.get("detection_counts"):counts_df = pd.DataFrame(299 unmodified lines30030130230330430530630730830931031131231331431531631731831932032132232332432532632732832916 unmodified lines346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399241 unmodified lines641642643644645646647648649650299 unmodified linescontainer = Nonestream = Nonecodec_name = Noneerror_message = Nonedef _process_frame(frame_bgr):nonlocal processed_framesimg_rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)model.conf = conf_thresholdresults = model(img_rgb)detections = results.pandas().xyxy[0]relevant = detections[detections['name'].isin(WHEELCHAIR_CLASSES.values())]for name in relevant['name'].tolist():detection_counts[name] = detection_counts.get(name, 0) + 1rendered_frame = results.render()[0]if rendered_frame.shape[1] != width or rendered_frame.shape[0] != height:rendered_frame = cv2.resize(rendered_frame, (width, height))processed_frames += 1if total_frames > 0:progress.progress(min(processed_frames / total_frames, 1.0))return rendered_frametry:container = av.open(output_path, mode="w")try:16 unmodified linesif frame_skip > 1 and frame_index % frame_skip != 0:continuerendered_frame = _process_frame(frame)video_frame = av.VideoFrame.from_ndarray(rendered_frame, format="bgr24")for packet in stream.encode(video_frame):container.mux(packet)for packet in stream.encode():container.mux(packet)except Exception as e:error_message = f"PyAV encode failed: {e}"finally:if container is not None:container.close()if error_message:try:fourcc = cv2.VideoWriter_fourcc(*"mp4v")writer = cv2.VideoWriter(output_path, fourcc, fps, (width, height))cap.set(cv2.CAP_PROP_POS_FRAMES, 0)frame_index = 0while True:ret, frame = cap.read()if not ret:breakframe_index += 1if frame_skip > 1 and frame_index % frame_skip != 0:continuerendered_frame = _process_frame(frame)writer.write(rendered_frame)writer.release()error_message = Nonecodec_name = "mp4v"except Exception as e:error_message = f"OpenCV encode failed: {e}"output_path = Noneprogress.empty()cap.release()stats = {"total_frames": total_frames,"processed_frames": processed_frames,"detection_counts": detection_counts,"codec": codec_name,"error": error_message}return output_path, stats241 unmodified linesstats = st.session_state.video_statsst.metric("🎞️ Total Frames", stats.get("total_frames", 0))st.metric("✅ Processed Frames", stats.get("processed_frames", 0))if stats.get("codec"):st.caption(f"Codec: {stats.get('codec')}")if stats.get("error"):st.error(stats.get("error"))if stats.get("detection_counts"):counts_df = pd.DataFrame(
src/streamlit_app.py+61-22