File size: 3,981 Bytes
eaa4e30
15d5948
4a7ddd0
15d5948
 
 
4a7ddd0
15d5948
4a7ddd0
15d5948
4a7ddd0
15d5948
 
 
4a7ddd0
15d5948
 
 
26f78a4
15d5948
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26f78a4
15d5948
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
import streamlit as st
from processor import process_video, process_image
import os
from PIL import Image
import tempfile
import cv2

st.set_page_config(page_title="Traffic Violation Detection", layout="wide")

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

# Sidebar for selection
st.sidebar.title("Choose an Option")
option = st.sidebar.radio("Select the processing type:", ("Image", "Video", "Live Camera"))

if option == "Image":
    st.header("πŸ–ΌοΈ Image Processing")
    uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
    
    if uploaded_file is not None:
        # Save the uploaded image to a temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_image:
            temp_image.write(uploaded_file.read())
            temp_image_path = temp_image.name

        # Display the uploaded image
        st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True)

        # Process the image
        if st.button("Process Image"):
            with st.spinner("Processing..."):
                font_path = "fonts/alfont_com_arial-1.ttf"  # Update the path as needed
                processed_image = process_image(temp_image_path, font_path)
                if processed_image is not None:
                    # Convert the processed image to RGB
                    processed_image_rgb = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
                    st.image(processed_image_rgb, caption='Processed Image.', use_column_width=True)

                    # Save processed image to a temporary file
                    result_image = Image.fromarray(processed_image_rgb)
                    with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
                        result_image.save(tmp.name)
                        tmp_path = tmp.name

                    # Download button
                    with open(tmp_path, "rb") as file:
                        btn = st.download_button(
                            label="πŸ“₯ Download Processed Image",
                            data=file,
                            file_name="processed_image.jpg",
                            mime="image/jpeg"
                        )
                else:
                    st.error("Failed to process the image.")

elif option == "Video":
    st.header("πŸŽ₯ Video Processing")
    video_files = [f for f in os.listdir("videos") if f.endswith(('.mp4', '.avi', '.mov'))]
    
    if not video_files:
        st.warning("No predefined videos found in the 'videos/' directory.")
    else:
        selected_video = st.selectbox("Select a video to process:", video_files)
        video_path = os.path.join("videos", selected_video)

        st.video(video_path)

        if st.button("Process Video"):
            with st.spinner("Processing..."):
                font_path = "fonts/alfont_com_arial-1.ttf"  # Update the path as needed
                processed_video_path = process_video(video_path, font_path)
                if processed_video_path and os.path.exists(processed_video_path):
                    st.success("Video processed successfully!")
                    st.video(processed_video_path)

                    # Provide download button
                    with open(processed_video_path, "rb") as file:
                        btn = st.download_button(
                            label="πŸ“₯ Download Processed Video",
                            data=file,
                            file_name="processed_video.mp4",
                            mime="video/mp4"
                        )
                else:
                    st.error("Failed to process the video.")

elif option == "Live Camera":
    st.header("πŸ“· Live Camera Processing")
    st.warning("Live camera processing is currently not supported in this app due to Streamlit limitations.")
    st.info("Consider running the live camera processing separately using your existing script.")