-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
203 lines (163 loc) · 7.87 KB
/
main.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# ----------------------------------------------------------------#
"""
Author: Amine Hadj-Youcef
Email: hadjyoucef.amine@gmail.com
GitHub: https://github.com/amineHY/computervision-dashboard.git
"""
# ----------------------------------------------------------------#
import base64
import os
import pandas as pd
import requests
import streamlit as st
import src.functions as F
import logging
logging.info(__doc__)
# ----------------------------------------------------------------#
# python functions
# ----------------------------------------------------------------#
def main():
"""
This is the main function
"""
# ----------------------------------------------------------------#
logging.info("Get parameters from the GUI")
# ----------------------------------------------------------------#
guiParam = F.GUI().getGuiParameters()
# Define paths
paths = {
"path_database": "database/",
"received_data": "received_data/",
}
# Update the dictionary of parameters
guiParam.update(paths)
# Check if the selectedApp is not empty
if guiParam["selectedApp"] != "Empty":
# ----------------------------------------------------------------#
# Set the API URL
api_url_base = (
"http://localhost:8000/" # api_url_base = "http://inveesion-api:8000/"
)
# If the selectedApp is for images
# ----------------------------------------------------------------#
if guiParam["appType"] == "Image Application":
logging.info("Generate the image_path depending on data source")
image_path = F.DataManager(guiParam).get_image_path()
if st.button("Run"):
logging.info("RUN button is clicked")
with open(image_path, "rb") as filelike:
logging.info("Send POST request to computervision-api")
files = {"image": ("image", filelike, "image/jpeg")}
api_endpoint = "image-api/"
response = requests.request(
"POST",
api_url_base + api_endpoint,
params=guiParam,
files=files,
timeout=360,
)
logging.info("[computervision-api] Response : \t " + response.url)
if response.status_code == 200:
logging.info(
f"[computervision-api] Success: {response.status_code}"
)
st.markdown("## Results")
response_json = response.json()["response"]
values = list(response_json.values())
# Parse API response and extract data (image + csv)
img_overlay_path = paths["received_data"] + "img_overlay.png"
with open(img_overlay_path, "wb") as im_byte:
im_byte.write(base64.b64decode(values[0]))
csv_path = paths["received_data"] + "csv_analytics.csv"
with open(csv_path, "wb") as csv_byte:
csv_byte.write(base64.b64decode(values[1]))
# Display image with overlay
st.image(
open(img_overlay_path, "rb").read(),
channels="BGR",
use_column_width=True,
)
logging.info("Display link to download CSV file")
href = f'<a href="data:file/csv;base64,{values[1]}">Download CSV File</a> (right-click and save as <some_name>.csv)'
st.markdown(href, unsafe_allow_html=True)
# Display dataframe
df_silver = pd.read_csv(csv_path)
st.markdown("## Display Received Analytics from the API")
st.dataframe(df_silver)
else:
logging.info(
"\n[computervision-api] Failure: ", response.status_code
)
# If the selectedApp is for videos
# ----------------------------------------------------------------#
elif guiParam["appType"] == "Video Application":
logging.info("Generate the video_path depending on data source")
video_path = F.DataManager(guiParam).get_video_path()
if st.button("Run"):
logging.info("RUN button is clicked")
with open(video_path, "rb") as filelike:
logging.info("Send POST request to computervision-api")
files = {"video": ("video", filelike, "video/mp4")}
api_endpoint = "video-api/"
response = requests.request(
"POST",
api_url_base + api_endpoint,
params=guiParam,
files=files,
timeout=360,
)
logging.info(
"[computervision-api] Response : \n\t " + response.url
)
if response.status_code == 200:
logging.info(
f"[computervision-api] Success: {response.status_code}"
)
st.markdown("## Results")
response_json = response.json()["response"]
values = list(response_json.values())
# Parse API response and extract data (video + csv)
vid_overlay_path = paths["received_data"] + "vid_overlay.avi"
with open(vid_overlay_path, "wb") as vid_byte:
vid_byte.write(base64.b64decode(values[0]))
csv_path = paths["received_data"] + "csv_analytics.csv"
with open(csv_path, "wb") as csv_byte:
csv_byte.write(base64.b64decode(values[1]))
logging.info("Convert the overlay video to mp4 using FFmpeg")
# ----------------------------------------------------------------#
os.system(
"ffmpeg -y -i "
+ vid_overlay_path
+ " -vcodec libx264 "
+ vid_overlay_path[:-4]
+ ".mp4 && rm "
+ vid_overlay_path
)
logging.info("Display video with overlay")
with open(vid_overlay_path[:-4] + ".mp4", "rb") as f:
st.video(f.read())
logging.info("Display link to download CSV file")
href = f'<a href="data:file/csv;base64,{values[1]}">Download CSV File</a> (right-click and save as <some_name>.csv)'
st.markdown(href, unsafe_allow_html=True)
logging.info("Display analytics")
# ----------------------------------------------------------------#
if guiParam["selectedApp"] in [
"Object Detection",
"Face Detection",
]:
df_silver = pd.read_csv(csv_path)
df_gold = F.postprocessing_object_detection_df(df_silver)
st.markdown("## Display Received Analytics from the API")
st.dataframe(df_gold)
F.plot_analytics(df_gold)
else:
logging.info(
"\n[computervision-api] Failure: ", response.status_code
)
else:
st.warning("Please select an application from the sidebar menu")
# ----------------------------------------------------------------#
# main program
# ----------------------------------------------------------------#
if __name__ == "__main__":
main()