Update app.py
Browse files
app.py
CHANGED
|
@@ -1,151 +1,238 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
from
|
|
|
|
| 4 |
import cv2
|
| 5 |
import tempfile
|
| 6 |
import time # For simulating processing time
|
|
|
|
| 7 |
from object_detection import detectObjects
|
| 8 |
from object_detection import detectVideo
|
| 9 |
from object_detection_count import detectObjectsAndCount
|
| 10 |
from pose_analysis import process_gif
|
| 11 |
from traffic_sign_detection import detectTrafficObjects
|
| 12 |
|
|
|
|
| 13 |
# Constants
|
|
|
|
| 14 |
MAX_FILE_SIZE_MB = 250
|
| 15 |
TABS = ["Object Detection", "Pose Analysis", "Object Counting", "Traffic Sign Detection"]
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def check_file_size(file):
|
| 19 |
file.seek(0, os.SEEK_END)
|
| 20 |
file_size = file.tell() / (1024 * 1024) # Convert to MB
|
| 21 |
file.seek(0) # Reset file pointer
|
| 22 |
return file_size
|
| 23 |
|
| 24 |
-
|
| 25 |
-
def
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return img, "image"
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
progress_placeholder.empty()
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Process Image
|
| 52 |
-
if uploaded_file.name.endswith((".jpg", ".png", ".jpeg")): # Image file
|
| 53 |
-
#img = Image.open(uploaded_file)
|
| 54 |
-
progress_placeholder.empty() # Clear the "Processing..." message
|
| 55 |
-
img, count = detectObjectsAndCount(uploaded_file.name, confidence_score, class_type)
|
| 56 |
return img, "image"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
elif tab_name ==
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
elif tab_name == 'Traffic Sign Detection':
|
| 77 |
-
if uploaded_file.name.endswith((".jpg", ".png", ".jpeg")): # Image file
|
| 78 |
-
#img = Image.open(uploaded_file)
|
| 79 |
-
progress_placeholder.empty() # Clear the "Processing..." message
|
| 80 |
-
img = detectTrafficObjects(uploaded_file.name, confidence_score)
|
| 81 |
return img, "image"
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
st.title("AI Video/Image Analysis Platform")
|
| 85 |
st.write("Upload an image or video and choose a tab for analysis.")
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
for i, tab_name in enumerate(TABS):
|
| 93 |
-
with
|
| 94 |
st.header(tab_name)
|
| 95 |
|
| 96 |
-
# File uploader
|
| 97 |
uploaded_file = st.file_uploader(
|
| 98 |
-
"Upload an Image/Video",
|
|
|
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
-
# Check file size
|
| 102 |
if uploaded_file:
|
| 103 |
file_size = check_file_size(uploaded_file)
|
| 104 |
if file_size > MAX_FILE_SIZE_MB:
|
| 105 |
-
st.error(
|
| 106 |
-
|
| 107 |
-
st.success(f"Uploaded file: {uploaded_file.name} ({file_size:.2f} MB)")
|
| 108 |
-
with open(f"{uploaded_file.name}", "wb") as f:
|
| 109 |
-
f.write(uploaded_file.read())
|
| 110 |
-
|
| 111 |
-
# Confidence score input
|
| 112 |
-
confidence_score = st.number_input(
|
| 113 |
-
"Adjust Confidence Score",
|
| 114 |
-
min_value=0.0,
|
| 115 |
-
max_value=1.0,
|
| 116 |
-
value=0.5,
|
| 117 |
-
step=0.01,
|
| 118 |
-
help="Set the confidence score threshold for the analysis (default: 0.5).",
|
| 119 |
-
key=f"confidence_{tab_name}",
|
| 120 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
tab_name,
|
| 140 |
-
confidence_score,
|
| 141 |
-
progress_placeholder,
|
| 142 |
-
class_type, # Pass class_type to the processing function
|
| 143 |
-
)
|
| 144 |
-
if result_type == "video":
|
| 145 |
-
if result:
|
| 146 |
-
st.success(f"{tab_name} completed successfully!")
|
| 147 |
-
st.video(result)
|
| 148 |
-
if result_type == "image":
|
| 149 |
-
#if result:
|
| 150 |
-
st.success(f"{tab_name} completed successfully!")
|
| 151 |
-
st.image(result, caption=f"{tab_name} Result", use_column_width=True)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from PIL import Image
|
| 5 |
import cv2
|
| 6 |
import tempfile
|
| 7 |
import time # For simulating processing time
|
| 8 |
+
|
| 9 |
from object_detection import detectObjects
|
| 10 |
from object_detection import detectVideo
|
| 11 |
from object_detection_count import detectObjectsAndCount
|
| 12 |
from pose_analysis import process_gif
|
| 13 |
from traffic_sign_detection import detectTrafficObjects
|
| 14 |
|
| 15 |
+
# -----------------------------
|
| 16 |
# Constants
|
| 17 |
+
# -----------------------------
|
| 18 |
MAX_FILE_SIZE_MB = 250
|
| 19 |
TABS = ["Object Detection", "Pose Analysis", "Object Counting", "Traffic Sign Detection"]
|
| 20 |
|
| 21 |
+
ASSETS_DIR = Path("assets")
|
| 22 |
+
|
| 23 |
+
TASK_TO_ASSET_SUBDIR = {
|
| 24 |
+
"Object Detection": "object_detection",
|
| 25 |
+
"Pose Analysis": "pose_analysis",
|
| 26 |
+
"Object Counting": "object_counting",
|
| 27 |
+
"Traffic Sign Detection": "traffic_sign_detection",
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
IMAGE_EXTS = {".jpg", ".jpeg", ".png"}
|
| 31 |
+
VIDEO_EXTS = {".mp4", ".mov", ".avi", ".gif"}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# -----------------------------
|
| 35 |
+
# Helpers
|
| 36 |
+
# -----------------------------
|
| 37 |
def check_file_size(file):
|
| 38 |
file.seek(0, os.SEEK_END)
|
| 39 |
file_size = file.tell() / (1024 * 1024) # Convert to MB
|
| 40 |
file.seek(0) # Reset file pointer
|
| 41 |
return file_size
|
| 42 |
|
| 43 |
+
|
| 44 |
+
def save_uploaded_file_to_temp(uploaded_file) -> str:
|
| 45 |
+
"""
|
| 46 |
+
Saves uploaded file to a temp folder and returns the absolute path.
|
| 47 |
+
This avoids filename collisions and issues with Streamlit reruns.
|
| 48 |
+
"""
|
| 49 |
+
tmp_dir = Path(tempfile.gettempdir())
|
| 50 |
+
# Make name safer (optional) - keep it simple
|
| 51 |
+
safe_name = uploaded_file.name.replace("/", "_").replace("\\", "_")
|
| 52 |
+
save_path = tmp_dir / safe_name
|
| 53 |
+
with open(save_path, "wb") as f:
|
| 54 |
+
f.write(uploaded_file.getbuffer())
|
| 55 |
+
return str(save_path)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def list_demo_files(task_name: str, limit: int = 6):
|
| 59 |
+
subdir = TASK_TO_ASSET_SUBDIR.get(task_name)
|
| 60 |
+
if not subdir:
|
| 61 |
+
return []
|
| 62 |
+
folder = ASSETS_DIR / subdir
|
| 63 |
+
if not folder.exists():
|
| 64 |
+
return []
|
| 65 |
+
files = [
|
| 66 |
+
p
|
| 67 |
+
for p in folder.iterdir()
|
| 68 |
+
if p.is_file() and p.suffix.lower() in (IMAGE_EXTS | VIDEO_EXTS)
|
| 69 |
+
]
|
| 70 |
+
files.sort(key=lambda p: p.name.lower())
|
| 71 |
+
return files[:limit]
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def render_media(path: Path):
|
| 75 |
+
ext = path.suffix.lower()
|
| 76 |
+
if ext in IMAGE_EXTS:
|
| 77 |
+
st.image(str(path), caption=path.name, use_container_width=True)
|
| 78 |
+
elif ext in VIDEO_EXTS:
|
| 79 |
+
st.video(str(path))
|
| 80 |
+
st.caption(path.name)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def render_showcase(tasks, per_task_limit=6):
|
| 84 |
+
st.subheader("Example outputs (what to expect)")
|
| 85 |
+
st.write(
|
| 86 |
+
"These are pre-generated results (detected/segmented/pose analyzed/traffic signs) so you can see the expected output before uploading."
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
for task in tasks:
|
| 90 |
+
st.markdown(f"### {task}")
|
| 91 |
+
demo_files = list_demo_files(task, limit=per_task_limit)
|
| 92 |
+
|
| 93 |
+
if not demo_files:
|
| 94 |
+
st.info(
|
| 95 |
+
f"No demo files found for **{task}**. Add images/videos under: "
|
| 96 |
+
f"`{ASSETS_DIR / TASK_TO_ASSET_SUBDIR[task]}`"
|
| 97 |
+
)
|
| 98 |
+
continue
|
| 99 |
+
|
| 100 |
+
cols = st.columns(3)
|
| 101 |
+
for idx, p in enumerate(demo_files):
|
| 102 |
+
with cols[idx % 3]:
|
| 103 |
+
render_media(p)
|
| 104 |
+
|
| 105 |
+
st.divider()
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def process_file(file_path, tab_name, confidence_score, progress_placeholder, class_type):
|
| 109 |
+
progress_placeholder.info(
|
| 110 |
+
f"Processing... Please wait. (Confidence Score: {confidence_score})"
|
| 111 |
+
)
|
| 112 |
+
time.sleep(1) # small UX delay
|
| 113 |
+
|
| 114 |
+
if tab_name == "Object Detection":
|
| 115 |
+
if file_path.lower().endswith((".jpg", ".png", ".jpeg")):
|
| 116 |
+
progress_placeholder.empty()
|
| 117 |
+
img = detectObjects(file_path, confidence_score)
|
| 118 |
return img, "image"
|
| 119 |
+
|
| 120 |
+
elif file_path.lower().endswith((".mp4", ".avi", ".mov", ".gif")):
|
| 121 |
+
progress_placeholder.empty()
|
| 122 |
+
out_video_path = detectVideo(file_path, confidence_score)
|
| 123 |
+
return out_video_path, "video"
|
| 124 |
+
|
| 125 |
+
progress_placeholder.empty()
|
| 126 |
+
st.error("Unsupported file format! Please upload an image or video.")
|
| 127 |
+
return None, None
|
| 128 |
+
|
| 129 |
+
elif tab_name == "Object Counting":
|
| 130 |
+
if file_path.lower().endswith((".jpg", ".png", ".jpeg")):
|
| 131 |
+
progress_placeholder.empty()
|
| 132 |
+
img, count = detectObjectsAndCount(file_path, confidence_score, class_type)
|
| 133 |
+
# Show count in UI
|
| 134 |
+
st.info(f"Count for class '{class_type}': {count}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
return img, "image"
|
| 136 |
+
|
| 137 |
+
elif file_path.lower().endswith((".mp4", ".avi", ".mov", ".gif")):
|
| 138 |
+
progress_placeholder.empty()
|
| 139 |
+
out_video_path = detectVideo(file_path, confidence_score)
|
| 140 |
+
return out_video_path, "video"
|
| 141 |
+
|
| 142 |
+
progress_placeholder.empty()
|
| 143 |
+
st.error("Unsupported file format! Please upload an image or video.")
|
| 144 |
+
return None, None
|
| 145 |
+
|
| 146 |
+
elif tab_name == "Pose Analysis":
|
| 147 |
+
progress_placeholder.empty()
|
| 148 |
+
out_video_path = process_gif(file_path, confidence_score)
|
| 149 |
+
return out_video_path, "video"
|
| 150 |
+
|
| 151 |
+
elif tab_name == "Traffic Sign Detection":
|
| 152 |
+
if file_path.lower().endswith((".jpg", ".png", ".jpeg")):
|
| 153 |
+
progress_placeholder.empty()
|
| 154 |
+
img = detectTrafficObjects(file_path, confidence_score)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
return img, "image"
|
| 156 |
|
| 157 |
+
progress_placeholder.empty()
|
| 158 |
+
st.error("Unsupported file format! Please upload an image.")
|
| 159 |
+
return None, None
|
| 160 |
+
|
| 161 |
+
st.error("Unknown tab selection.")
|
| 162 |
+
return None, None
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
# -----------------------------
|
| 166 |
+
# Streamlit Layout
|
| 167 |
+
# -----------------------------
|
| 168 |
+
st.set_page_config(page_title="AI Video/Image Analysis Platform", layout="wide")
|
| 169 |
+
|
| 170 |
st.title("AI Video/Image Analysis Platform")
|
| 171 |
st.write("Upload an image or video and choose a tab for analysis.")
|
| 172 |
|
| 173 |
+
# Showcase section (NO expander)
|
| 174 |
+
render_showcase(TABS, per_task_limit=6)
|
| 175 |
|
| 176 |
+
# Tabs for different functionalities
|
| 177 |
+
tabs = st.tabs(TABS)
|
| 178 |
|
| 179 |
for i, tab_name in enumerate(TABS):
|
| 180 |
+
with tabs[i]:
|
| 181 |
st.header(tab_name)
|
| 182 |
|
|
|
|
| 183 |
uploaded_file = st.file_uploader(
|
| 184 |
+
"Upload an Image/Video",
|
| 185 |
+
type=["jpg", "jpeg", "png", "gif", "mp4", "avi", "mov"],
|
| 186 |
+
key=f"uploader_{tab_name}",
|
| 187 |
)
|
| 188 |
|
|
|
|
| 189 |
if uploaded_file:
|
| 190 |
file_size = check_file_size(uploaded_file)
|
| 191 |
if file_size > MAX_FILE_SIZE_MB:
|
| 192 |
+
st.error(
|
| 193 |
+
f"File size exceeds {MAX_FILE_SIZE_MB} MB. Please upload a smaller file."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
)
|
| 195 |
+
continue
|
| 196 |
+
|
| 197 |
+
st.success(f"Uploaded file: {uploaded_file.name} ({file_size:.2f} MB)")
|
| 198 |
+
|
| 199 |
+
# Save to temp and use the full path for downstream processing
|
| 200 |
+
file_path = save_uploaded_file_to_temp(uploaded_file)
|
| 201 |
|
| 202 |
+
confidence_score = st.number_input(
|
| 203 |
+
"Adjust Confidence Score",
|
| 204 |
+
min_value=0.0,
|
| 205 |
+
max_value=1.0,
|
| 206 |
+
value=0.5,
|
| 207 |
+
step=0.01,
|
| 208 |
+
help="Set the confidence score threshold for the analysis (default: 0.5).",
|
| 209 |
+
key=f"confidence_{tab_name}",
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
class_type = None
|
| 213 |
+
if tab_name == "Object Counting":
|
| 214 |
+
class_type = st.text_input(
|
| 215 |
+
"Enter Class Type",
|
| 216 |
+
value="car",
|
| 217 |
+
help="Specify the class type to count (e.g., 'car', 'person').",
|
| 218 |
+
key=f"class_type_{tab_name}",
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
if st.button(f"Process {tab_name}", key=f"process_{tab_name}"):
|
| 222 |
+
progress_placeholder = st.empty()
|
| 223 |
+
with st.spinner("Processing... Please wait."):
|
| 224 |
+
result, result_type = process_file(
|
| 225 |
+
file_path,
|
| 226 |
+
tab_name,
|
| 227 |
+
confidence_score,
|
| 228 |
+
progress_placeholder,
|
| 229 |
+
class_type,
|
| 230 |
)
|
| 231 |
|
| 232 |
+
if result_type == "video" and result:
|
| 233 |
+
st.success(f"{tab_name} completed successfully!")
|
| 234 |
+
st.video(result)
|
| 235 |
+
|
| 236 |
+
if result_type == "image" and result is not None:
|
| 237 |
+
st.success(f"{tab_name} completed successfully!")
|
| 238 |
+
st.image(result, caption=f"{tab_name} Result", use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|