Spaces:
Sleeping
Sleeping
Update app.py
Browse filesadded Progress bar, ETA and multiple images in Inference tool
app.py
CHANGED
|
@@ -17,73 +17,86 @@ def yolo_inference_tool():
|
|
| 17 |
Single-model, single-image inference subpage (example).
|
| 18 |
"""
|
| 19 |
st.header("YOLO Model Inference Tool")
|
| 20 |
-
st.write("Upload
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
model_file = st.file_uploader("Upload YOLO model (.pt)", type=["pt"], key="inference_model")
|
| 24 |
-
|
| 25 |
if st.button("Submit (Single-Model Inference)"):
|
| 26 |
-
if not
|
| 27 |
-
st.error("Please upload
|
| 28 |
return
|
| 29 |
|
| 30 |
-
# Save
|
| 31 |
-
image_path = save_uploaded_file(image_file)
|
| 32 |
model_path = save_uploaded_file(model_file)
|
| 33 |
-
|
| 34 |
-
# Load image
|
| 35 |
-
try:
|
| 36 |
-
image = Image.open(image_file).convert("RGB")
|
| 37 |
-
except Exception as e:
|
| 38 |
-
st.error(f"Error reading image: {e}")
|
| 39 |
-
return
|
| 40 |
-
|
| 41 |
-
st.subheader("Image Details")
|
| 42 |
-
st.write(f"**Image Size:** {image.size[0]} x {image.size[1]}")
|
| 43 |
-
st.write(f"**File Type:** {image_file.type}")
|
| 44 |
-
|
| 45 |
-
# Load model
|
| 46 |
try:
|
| 47 |
model = YOLO(model_path)
|
| 48 |
except Exception as e:
|
| 49 |
st.error(f"Error loading model: {e}")
|
| 50 |
return
|
| 51 |
|
| 52 |
-
|
| 53 |
-
st.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
st.error(f"Inference error: {e}")
|
| 58 |
-
return
|
| 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 |
def yolo_model_comparison_tool():
|
| 89 |
"""
|
|
|
|
| 17 |
Single-model, single-image inference subpage (example).
|
| 18 |
"""
|
| 19 |
st.header("YOLO Model Inference Tool")
|
| 20 |
+
st.write("Upload one or more images and a YOLO model (.pt) file to run inference and view detailed results.")
|
| 21 |
+
|
| 22 |
+
# Allow multiple images upload
|
| 23 |
+
images = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"], key="inference_images", accept_multiple_files=True)
|
| 24 |
model_file = st.file_uploader("Upload YOLO model (.pt)", type=["pt"], key="inference_model")
|
| 25 |
+
|
| 26 |
if st.button("Submit (Single-Model Inference)"):
|
| 27 |
+
if not images or not model_file:
|
| 28 |
+
st.error("Please upload at least one image and a model.")
|
| 29 |
return
|
| 30 |
|
| 31 |
+
# Save and load the model file
|
|
|
|
| 32 |
model_path = save_uploaded_file(model_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
model = YOLO(model_path)
|
| 35 |
except Exception as e:
|
| 36 |
st.error(f"Error loading model: {e}")
|
| 37 |
return
|
| 38 |
|
| 39 |
+
total_images = len(images)
|
| 40 |
+
progress_bar = st.progress(0)
|
| 41 |
+
eta_placeholder = st.empty()
|
| 42 |
+
start_time = time.time()
|
| 43 |
+
steps_done = 0
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Dictionaries to store inference results and metrics
|
| 46 |
+
image_results = {}
|
| 47 |
+
metrics = []
|
| 48 |
+
|
| 49 |
+
for img_file in images:
|
| 50 |
+
steps_done += 1
|
| 51 |
+
fraction_done = steps_done / total_images
|
| 52 |
+
progress_bar.progress(fraction_done)
|
| 53 |
+
|
| 54 |
+
elapsed_time = time.time() - start_time
|
| 55 |
+
time_per_step = elapsed_time / steps_done
|
| 56 |
+
remaining_steps = total_images - steps_done
|
| 57 |
+
eta_seconds = remaining_steps * time_per_step
|
| 58 |
+
eta_placeholder.info(f"Progress: {fraction_done:.1%}. ETA: ~{eta_seconds:.1f} s")
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
pil_img = Image.open(img_file).convert("RGB")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
st.error(f"Error reading image {img_file.name}: {e}")
|
| 64 |
+
continue
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
result = model(np.array(pil_img))
|
| 68 |
+
except Exception as e:
|
| 69 |
+
st.error(f"Inference error on image {img_file.name}: {e}")
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
r = result[0]
|
| 73 |
+
image_results[img_file.name] = r
|
| 74 |
+
|
| 75 |
+
# Collect basic metrics if available (e.g., inference time and detections)
|
| 76 |
+
inference_time = r.speed.get('inference', None) if isinstance(r.speed, dict) else None
|
| 77 |
+
detection_count = len(r.boxes) if r.boxes is not None else 0
|
| 78 |
+
metrics.append({
|
| 79 |
+
"Image": img_file.name,
|
| 80 |
+
"Inference Time (ms)": inference_time if inference_time is not None else "N/A",
|
| 81 |
+
"Detections": detection_count
|
| 82 |
+
})
|
| 83 |
+
|
| 84 |
+
eta_placeholder.empty()
|
| 85 |
+
|
| 86 |
+
# Display per-image metrics if collected
|
| 87 |
+
st.subheader("Inference Metrics")
|
| 88 |
+
if metrics:
|
| 89 |
+
df_metrics = pd.DataFrame(metrics)
|
| 90 |
+
st.dataframe(df_metrics, use_container_width=True)
|
| 91 |
+
|
| 92 |
+
# Display annotated images (using pil=True to ensure RGB output)
|
| 93 |
+
st.subheader("Annotated Images")
|
| 94 |
+
for img_name, r in image_results.items():
|
| 95 |
+
try:
|
| 96 |
+
annotated_img = r.plot(conf=True, boxes=True, labels=True, pil=True)
|
| 97 |
+
st.image(annotated_img, caption=img_name, use_container_width=True)
|
| 98 |
+
except Exception as e:
|
| 99 |
+
st.error(f"Error generating annotated image for {img_name}: {e}")
|
| 100 |
|
| 101 |
def yolo_model_comparison_tool():
|
| 102 |
"""
|