Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,81 +11,44 @@ import cv2
|
|
| 11 |
# Set page configuration
|
| 12 |
st.set_page_config(page_title="Traffic Violation Detection", layout="wide")
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
if option == "Image":
|
| 20 |
-
st.header("🖼️ Upload and Process Image")
|
| 21 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 22 |
-
if uploaded_file:
|
| 23 |
-
image =
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
with st.spinner("Processing..."):
|
| 27 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
|
| 28 |
-
image.save(tmp.name)
|
| 29 |
-
frame = cv2.imread(tmp.name)
|
| 30 |
-
processed = process_image(tmp.name, "alfont_com_arial-1.ttf")
|
| 31 |
-
if processed is not None:
|
| 32 |
-
st.image(processed, caption='Processed Image.', use_column_width=True)
|
| 33 |
-
# Save processed image to temporary file for download
|
| 34 |
-
_, img_encoded = cv2.imencode('.jpg', processed)
|
| 35 |
-
st.download_button(
|
| 36 |
-
label="📥 Download Image",
|
| 37 |
-
data=img_encoded.tobytes(),
|
| 38 |
-
file_name="processed_image.jpg",
|
| 39 |
-
mime="image/jpeg"
|
| 40 |
-
)
|
| 41 |
-
else:
|
| 42 |
-
st.error("Failed to process the image.")
|
| 43 |
|
| 44 |
elif option == "Video":
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
if
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
video_path = os.path.join("videos", selected_video)
|
| 52 |
-
st.video(video_path)
|
| 53 |
if st.button("Process Video"):
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
st.video(processed_path)
|
| 59 |
-
with open(processed_path, "rb") as file:
|
| 60 |
-
st.download_button(
|
| 61 |
-
label="📥 Download Processed Video",
|
| 62 |
-
data=file,
|
| 63 |
-
file_name="processed_video.mp4",
|
| 64 |
-
mime="video/mp4"
|
| 65 |
-
)
|
| 66 |
-
else:
|
| 67 |
-
st.error("Failed to process the video.")
|
| 68 |
|
| 69 |
elif option == "Live Camera":
|
| 70 |
-
st.
|
| 71 |
-
st.
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
processed_img = process_frame(img, self.font_path)
|
| 83 |
-
return processed_img
|
| 84 |
-
|
| 85 |
-
webrtc_ctx = webrtc_streamer(
|
| 86 |
-
key="live-camera",
|
| 87 |
-
rtc_configuration=RTC_CONFIGURATION,
|
| 88 |
-
video_transformer_factory=VideoTransformer,
|
| 89 |
-
media_stream_constraints={"video": True, "audio": False},
|
| 90 |
-
async_transform=True
|
| 91 |
-
)
|
|
|
|
| 11 |
# Set page configuration
|
| 12 |
st.set_page_config(page_title="Traffic Violation Detection", layout="wide")
|
| 13 |
|
| 14 |
+
# Streamlit app layout
|
| 15 |
+
st.title("Motorbike Violation Detection")
|
| 16 |
|
| 17 |
+
option = st.selectbox(
|
| 18 |
+
"Choose Input Type",
|
| 19 |
+
("Image", "Video", "Live Camera")
|
| 20 |
+
)
|
| 21 |
|
| 22 |
if option == "Image":
|
|
|
|
| 23 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 24 |
+
if uploaded_file is not None:
|
| 25 |
+
image = cv2.imread(uploaded_file)
|
| 26 |
+
process_image(image)
|
| 27 |
+
st.download_button("Download Result", data=image, file_name="result_image.jpg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
elif option == "Video":
|
| 30 |
+
# Dynamically load video files from 'videos/' folder
|
| 31 |
+
video_folder = 'videos/'
|
| 32 |
+
video_files = [f for f in os.listdir(video_folder) if f.endswith(('.mp4', '.avi'))]
|
| 33 |
+
|
| 34 |
+
if video_files:
|
| 35 |
+
video_file = st.selectbox("Select a video", video_files)
|
|
|
|
|
|
|
| 36 |
if st.button("Process Video"):
|
| 37 |
+
process_video(os.path.join(video_folder, video_file))
|
| 38 |
+
st.download_button("Download Result", data=video_file, file_name="result_video.mp4")
|
| 39 |
+
else:
|
| 40 |
+
st.warning("No videos found in the 'videos/' folder.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
elif option == "Live Camera":
|
| 43 |
+
st.write("Starting live camera...")
|
| 44 |
+
run = st.checkbox('Run Camera')
|
| 45 |
+
camera = cv2.VideoCapture(0)
|
| 46 |
+
FRAME_WINDOW = st.image([])
|
| 47 |
+
|
| 48 |
+
while run:
|
| 49 |
+
ret, frame = camera.read()
|
| 50 |
+
if ret:
|
| 51 |
+
process_frame(frame)
|
| 52 |
+
FRAME_WINDOW.image(frame)
|
| 53 |
+
|
| 54 |
+
camera.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|