File size: 4,227 Bytes
c640bc9
 
 
 
 
 
066c36c
c640bc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import requests
import streamlit as st
import pandas as pd
import uvicorn

from src.api.fast_api import app

LOG_DIR = "logs"
API_URL = "http://localhost:8000"

ipcam_server_status = False
detection_service_status = False


def call_api(endpoint):
    try:
        response = requests.post(f"{API_URL}/{endpoint}")
        return response.json()
    except Exception as e:
        return {"error": str(e)}


def start_stop_server(start_message, already_running_message, not_running_message):
    global ipcam_server_status, detection_service_status
    if not ipcam_server_status:
        result = call_api("start_ipcam_server")
        ipcam_server_status = True
        return {"message": start_message, "result": result}
    else:
        return {"message": already_running_message}


def start_ipcam_server():
    return start_stop_server("IP Cam Server started.", "IP Cam Server is already running.",
                             "IP Cam Server is not running.")["result"]


def stop_ipcam_server():
    global ipcam_server_status
    if ipcam_server_status:
        result = call_api("stop_ipcam_server")
        ipcam_server_status = False
        return {"message": "IP Cam Server stopped.", "result": result}
    else:
        return {"message": "IP Cam Server is not running."}


def start_detection_service():
    return start_stop_server("Detection Service started.", "Detection Service is already running.",
                             "Detection Service is not running.")["result"]


def stop_detection_service():
    global detection_service_status
    if detection_service_status:
        result = call_api("stop_detection_service")
        detection_service_status = False
        return {"message": "Detection Service stopped.", "result": result}
    else:
        return {"message": "Detection Service is not running."}


def extract_info_from_filename(filename):
    parts = os.path.splitext(filename)[0].split()

    date, time = parts[0], parts[1]

    return date, time, filename


def create_datatable(image_folder):
    st.subheader("Image DataTable")

    image_files = [file for file in os.listdir(image_folder) if
                   file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]

    data = {"Date": [], "Time": [], "ImageName": []}

    for img in image_files:
        date, time, image_name = extract_info_from_filename(img)
        data["Date"].append(date)
        data["Time"].append(time)
        data["ImageName"].append(image_name)

    df = pd.DataFrame(data)

    # Convert Date and Time columns to datetime for sorting
    df['DateTime'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])

    # Sort DataFrame by DateTime in descending order
    df = df.sort_values(by='DateTime', ascending=False).drop('DateTime', axis=1)

    st.table(df)


def main_streamlit():
    global ipcam_server_status, detection_service_status

    image_folder = "images/cam_images"
    st.sidebar.header("Weapon Detection and Location Sharing Alert Systems")

    container = st.sidebar.empty()

    selected_option = st.sidebar.selectbox('Select an option',
                                           ["IP Cam Service", "Detection Service"])

    if selected_option == "IP Cam Service":
        st.subheader("IP Cam Service")
        if st.button('Start Server'):
            result = start_ipcam_server()
            st.write(result)
        if st.button('Stop Server'):
            result = stop_ipcam_server()
            st.write(result)
        if st.button('Refresh Datatable'):
            create_datatable(image_folder)

    elif selected_option == "Detection Service":
        st.subheader("Detection Service")
        if st.button('Start Server'):
            result = start_detection_service()
            st.write(result)
        if st.button('Stop Server'):
            result = stop_detection_service()
            st.write(result)

    container.subheader("Service Status:")
    container.write(f"IP Cam Service: {'Running' if ipcam_server_status else 'Stopped'}")
    container.write(f"Detection Service: {'Running' if detection_service_status else 'Stopped'}")


if __name__ == '__main__':
    uvicorn.run(app, host="localhost", port=8000)
    main_streamlit()