File size: 3,518 Bytes
eaa4e30
3a97db5
4a7ddd0
15d5948
 
3a97db5
15d5948
4a7ddd0
3a97db5
 
4a7ddd0
15d5948
4a7ddd0
3a97db5
 
4a7ddd0
15d5948
3a97db5
 
 
 
 
15d5948
 
3a97db5
 
 
 
 
 
 
 
 
 
 
 
 
15d5948
 
3a97db5
15d5948
 
3a97db5
15d5948
3a97db5
15d5948
 
 
3a97db5
 
 
 
 
 
 
 
 
 
 
 
 
 
15d5948
 
3a97db5
3889969
3a97db5
3889969
 
 
3a97db5
3889969
 
 
 
3a97db5
3889969
 
 
3a97db5
3889969
3a97db5
 
3889969
 
3a97db5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import streamlit as st
from processor import process_frame
import os
from PIL import Image
import tempfile
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer, RTCConfiguration
import cv2

# Set page config
st.set_page_config(page_title="Traffic Violation Detection", layout="wide")

st.title("🚦 Traffic Violation Detection App")

# Sidebar options
option = st.sidebar.radio("Select Option:", ("Image", "Video", "Live Camera"))

if option == "Image":
    st.header("πŸ–ΌοΈ Upload Image")
    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
    if uploaded_file:
        image = Image.open(uploaded_file)
        st.image(image, caption='Uploaded Image.', use_column_width=True)
        if st.button("Process Image"):
            with st.spinner("Processing..."):
                with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
                    image.save(tmp.name)
                    frame = cv2.imread(tmp.name)
                processed = process_frame(frame, "fonts/alfont_com_arial-1.ttf")
                st.image(processed, caption='Processed Image.', use_column_width=True)
                # Download button
                _, img_encoded = cv2.imencode('.jpg', processed)
                st.download_button(
                    label="Download Image",
                    data=img_encoded.tobytes(),
                    file_name="processed_image.jpg",
                    mime="image/jpeg"
                )

elif option == "Video":
    st.header("πŸŽ₯ Select Video")
    video_files = [f for f in os.listdir("videos") if f.endswith(('.mp4', '.avi', '.mov'))]
    if not video_files:
        st.warning("No videos found in the 'videos/' folder.")
    else:
        selected_video = st.selectbox("Choose a video:", video_files)
        video_path = os.path.join("videos", selected_video)
        st.video(video_path)
        if st.button("Process Video"):
            with st.spinner("Processing Video..."):
                processed_path = 'output_violation.mp4'  # Adjust as needed
                # Implement video processing logic here
                # For brevity, assume process_video returns the path
                # You can extend processor.py with a process_video function
                st.success("Video processed!")
                st.video(processed_path)
                with open(processed_path, "rb") as file:
                    st.download_button(
                        label="Download Video",
                        data=file,
                        file_name="processed_video.mp4",
                        mime="video/mp4"
                    )

elif option == "Live Camera":
    st.header("πŸ“· Live Camera Feed")

    RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})

    class VideoTransformer(VideoTransformerBase):
        def __init__(self):
            self.font_path = "fonts/alfont_com_arial-1.ttf"

        def transform(self, frame):
            img = frame.to_ndarray(format="bgr24")
            processed_img = process_frame(img, self.font_path)
            return processed_img

    webrtc_ctx = webrtc_streamer(
        key="live-camera",
        rtc_configuration=RTC_CONFIGURATION,
        video_transformer_factory=VideoTransformer,
        media_stream_constraints={"video": True, "audio": False},
        async_transform=True
    )

    st.info("Live processing is active. Detected violations will be annotated on the video feed.")