Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,17 +45,20 @@ if option == "Image":
|
|
| 45 |
st.error("Failed to process the image.")
|
| 46 |
|
| 47 |
elif option == "Video":
|
| 48 |
-
st.header("🎥
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
if st.button("Process Video"):
|
| 57 |
with st.spinner("Processing Video..."):
|
| 58 |
-
processed_path = process_video(
|
| 59 |
if processed_path and os.path.exists(processed_path):
|
| 60 |
st.success("Video processed successfully!")
|
| 61 |
st.video(processed_path)
|
|
@@ -69,6 +72,7 @@ elif option == "Video":
|
|
| 69 |
else:
|
| 70 |
st.error("Failed to process the video.")
|
| 71 |
|
|
|
|
| 72 |
elif option == "Live Camera":
|
| 73 |
st.header("📷 Live Camera Feed")
|
| 74 |
st.info("Live processing is active. Detected violations will be annotated on the video feed.")
|
|
@@ -81,14 +85,19 @@ elif option == "Live Camera":
|
|
| 81 |
self.font_path = "alfont_com_arial-1.ttf"
|
| 82 |
|
| 83 |
def transform(self, frame):
|
| 84 |
-
img = frame.to_ndarray(format="bgr24")
|
| 85 |
-
processed_img = process_frame(img, self.font_path)
|
| 86 |
return processed_img
|
| 87 |
|
|
|
|
| 88 |
webrtc_ctx = webrtc_streamer(
|
| 89 |
key="live-camera",
|
| 90 |
rtc_configuration=RTC_CONFIGURATION,
|
| 91 |
video_transformer_factory=VideoTransformer,
|
| 92 |
media_stream_constraints={"video": True, "audio": False},
|
| 93 |
async_transform=True
|
| 94 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
st.error("Failed to process the image.")
|
| 46 |
|
| 47 |
elif option == "Video":
|
| 48 |
+
st.header("🎥 Upload and Process Video")
|
| 49 |
+
uploaded_video = st.file_uploader("Choose a video...", type=["mp4", "avi", "mov"])
|
| 50 |
+
|
| 51 |
+
if uploaded_video is not None:
|
| 52 |
+
# Save the uploaded video to a temporary file
|
| 53 |
+
with open("temp_video.mp4", "wb") as f:
|
| 54 |
+
f.write(uploaded_video.read())
|
| 55 |
+
|
| 56 |
+
# Display the uploaded video
|
| 57 |
+
st.video("temp_video.mp4")
|
| 58 |
+
|
| 59 |
if st.button("Process Video"):
|
| 60 |
with st.spinner("Processing Video..."):
|
| 61 |
+
processed_path = process_video("temp_video.mp4", "alfont_com_arial-1.ttf")
|
| 62 |
if processed_path and os.path.exists(processed_path):
|
| 63 |
st.success("Video processed successfully!")
|
| 64 |
st.video(processed_path)
|
|
|
|
| 72 |
else:
|
| 73 |
st.error("Failed to process the video.")
|
| 74 |
|
| 75 |
+
|
| 76 |
elif option == "Live Camera":
|
| 77 |
st.header("📷 Live Camera Feed")
|
| 78 |
st.info("Live processing is active. Detected violations will be annotated on the video feed.")
|
|
|
|
| 85 |
self.font_path = "alfont_com_arial-1.ttf"
|
| 86 |
|
| 87 |
def transform(self, frame):
|
| 88 |
+
img = frame.to_ndarray(format="bgr24") # Convert the frame to a numpy array
|
| 89 |
+
processed_img = process_frame(img, self.font_path) # Process the image using your custom function
|
| 90 |
return processed_img
|
| 91 |
|
| 92 |
+
# Start the WebRTC streaming
|
| 93 |
webrtc_ctx = webrtc_streamer(
|
| 94 |
key="live-camera",
|
| 95 |
rtc_configuration=RTC_CONFIGURATION,
|
| 96 |
video_transformer_factory=VideoTransformer,
|
| 97 |
media_stream_constraints={"video": True, "audio": False},
|
| 98 |
async_transform=True
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# Optional: Provide a stop button to end the live stream
|
| 102 |
+
if webrtc_ctx.video_receiver:
|
| 103 |
+
st.button("Stop Live Camera", on_click=webrtc_ctx.stop_stream)
|