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
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 detectObjects
|
| 12 |
+
|
| 13 |
+
# Constants
|
| 14 |
+
MAX_FILE_SIZE_MB = 250
|
| 15 |
+
TABS = ["Object Detection", "Pose Analysis", "Object Counting", "Traffic Sign Detection"]
|
| 16 |
+
|
| 17 |
+
# Helper function to check file size
|
| 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 |
+
# Placeholder function for processing logic
|
| 25 |
+
def process_file(uploaded_file, tab_name, confidence_score, progress_placeholder, class_type):
|
| 26 |
+
progress_placeholder.info(f"Processing... Please wait. (Confidence Score: {confidence_score})")
|
| 27 |
+
time.sleep(2) # Simulate processing delay
|
| 28 |
+
if tab_name == 'Object Detection':
|
| 29 |
+
# Process Image
|
| 30 |
+
if uploaded_file.name.endswith((".jpg", ".png", ".jpeg")): # Image file
|
| 31 |
+
#img = Image.open(uploaded_file)
|
| 32 |
+
progress_placeholder.empty() # Clear the "Processing..." message
|
| 33 |
+
img = detectObjects(uploaded_file.name, confidence_score)
|
| 34 |
+
return img, "image"
|
| 35 |
+
|
| 36 |
+
# Process Video
|
| 37 |
+
elif uploaded_file.name.endswith((".mp4", ".avi", ".mov", ".gif")): # Video file
|
| 38 |
+
#temp_video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 39 |
+
#with open(temp_video_path, "wb") as f:
|
| 40 |
+
# f.write(uploaded_file.read())
|
| 41 |
+
progress_placeholder.empty() # Clear the "Processing..." message
|
| 42 |
+
temp_video_path = detectVideo(uploaded_file.name, confidence_score)
|
| 43 |
+
return temp_video_path, "video"
|
| 44 |
+
|
| 45 |
+
# Unsupported file type
|
| 46 |
+
else:
|
| 47 |
+
progress_placeholder.empty() # Clear the "Processing..." message
|
| 48 |
+
st.error("Unsupported file format! Please upload an image or video.")
|
| 49 |
+
return None, None
|
| 50 |
+
elif tab_name == 'Object Counting':
|
| 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 |
+
# Process Video
|
| 59 |
+
elif uploaded_file.name.endswith((".mp4", ".avi", ".mov", ".gif")): # Video file
|
| 60 |
+
#temp_video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 61 |
+
#with open(temp_video_path, "wb") as f:
|
| 62 |
+
# f.write(uploaded_file.read())
|
| 63 |
+
progress_placeholder.empty() # Clear the "Processing..." message
|
| 64 |
+
temp_video_path = detectVideo(uploaded_file.name, confidence_score)
|
| 65 |
+
return temp_video_path, "video"
|
| 66 |
+
|
| 67 |
+
# Unsupported file type
|
| 68 |
+
else:
|
| 69 |
+
progress_placeholder.empty() # Clear the "Processing..." message
|
| 70 |
+
st.error("Unsupported file format! Please upload an image or video.")
|
| 71 |
+
return None, None
|
| 72 |
+
elif tab_name == 'Pose Analysis':
|
| 73 |
+
progress_placeholder.empty() # Clear the "Processing..." message
|
| 74 |
+
temp_video_path = process_gif(uploaded_file.name, confidence_score)
|
| 75 |
+
return temp_video_path, "video"
|
| 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 = detectObjects(uploaded_file.name, confidence_score)
|
| 81 |
+
return img, "image"
|
| 82 |
+
|
| 83 |
+
# Streamlit app layout
|
| 84 |
+
st.title("AI Video/Image Analysis Platform")
|
| 85 |
+
st.write("Upload an image or video and choose a tab for analysis.")
|
| 86 |
+
|
| 87 |
+
# Tabs for different functionalities
|
| 88 |
+
tab = st.tabs(TABS)
|
| 89 |
+
|
| 90 |
+
uploaded_file = None
|
| 91 |
+
|
| 92 |
+
for i, tab_name in enumerate(TABS):
|
| 93 |
+
with tab[i]:
|
| 94 |
+
st.header(tab_name)
|
| 95 |
+
|
| 96 |
+
# File uploader
|
| 97 |
+
uploaded_file = st.file_uploader(
|
| 98 |
+
"Upload an Image/Video", type=["jpg", "jpeg", "png", "gif", "mp4", "avi", "mov"], key=tab_name
|
| 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(f"File size exceeds {MAX_FILE_SIZE_MB} MB. Please upload a smaller file.")
|
| 106 |
+
else:
|
| 107 |
+
st.success(f"Uploaded file: {uploaded_file.name} ({file_size:.2f} MB)")
|
| 108 |
+
|
| 109 |
+
# Confidence score input
|
| 110 |
+
confidence_score = st.number_input(
|
| 111 |
+
"Adjust Confidence Score",
|
| 112 |
+
min_value=0.0,
|
| 113 |
+
max_value=1.0,
|
| 114 |
+
value=0.5,
|
| 115 |
+
step=0.01,
|
| 116 |
+
help="Set the confidence score threshold for the analysis (default: 0.5).",
|
| 117 |
+
key=f"confidence_{tab_name}",
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Additional input for "Object Counting" tab
|
| 121 |
+
class_type = None
|
| 122 |
+
if tab_name == "Object Counting":
|
| 123 |
+
class_type = st.text_input(
|
| 124 |
+
"Enter Class Type",
|
| 125 |
+
value="car", # Default value, adjust as needed
|
| 126 |
+
help="Specify the class type to count (e.g., 'car', 'person').",
|
| 127 |
+
key=f"class_type_{tab_name}",
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
# Process file when button is clicked
|
| 131 |
+
if st.button(f"Process {tab_name}", key=f"process_{tab_name}"):
|
| 132 |
+
# Placeholder for the processing message
|
| 133 |
+
progress_placeholder = st.empty()
|
| 134 |
+
with st.spinner("Processing... Please wait."):
|
| 135 |
+
result, result_type = process_file(
|
| 136 |
+
uploaded_file,
|
| 137 |
+
tab_name,
|
| 138 |
+
confidence_score,
|
| 139 |
+
progress_placeholder,
|
| 140 |
+
class_type, # Pass class_type to the processing function
|
| 141 |
+
)
|
| 142 |
+
if result_type == "video":
|
| 143 |
+
if result:
|
| 144 |
+
st.success(f"{tab_name} completed successfully!")
|
| 145 |
+
st.video(result)
|
| 146 |
+
if result_type == "image":
|
| 147 |
+
#if result:
|
| 148 |
+
st.success(f"{tab_name} completed successfully!")
|
| 149 |
+
st.image(result, caption=f"{tab_name} Result", use_column_width=True)
|