Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,15 +44,6 @@ val_transform = transforms.Compose([
|
|
| 44 |
[0.229, 0.224, 0.225])
|
| 45 |
])
|
| 46 |
|
| 47 |
-
# ----------------------------
|
| 48 |
-
# Font helper
|
| 49 |
-
# ----------------------------
|
| 50 |
-
def _get_font(size):
|
| 51 |
-
try:
|
| 52 |
-
return ImageFont.truetype("DejaVuSans-Bold.ttf", size=size)
|
| 53 |
-
except Exception:
|
| 54 |
-
return ImageFont.load_default()
|
| 55 |
-
|
| 56 |
# ----------------------------
|
| 57 |
# Predict
|
| 58 |
# ----------------------------
|
|
@@ -71,23 +62,17 @@ def predict_image(img_path: str):
|
|
| 71 |
conf = float(top_prob.item()) * 100.0
|
| 72 |
predicted_breed = breeds[int(top_idx.item())]
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
font = _get_font(max(20, int(annotated.width * 0.05)))
|
| 78 |
text = f"{predicted_breed} ({conf:.2f}%)"
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
draw.rectangle([0, 0, text_w + 20, text_h + 20], fill="black")
|
| 83 |
-
draw.text((10, 10), text, fill="white", font=font)
|
| 84 |
-
|
| 85 |
-
# save annotated image
|
| 86 |
-
safe_breed = "".join(c if c.isalnum() or c in ('_', '-') else "_" for c in predicted_breed)
|
| 87 |
-
annotated_name = f"{safe_breed}_{conf:.2f}pct_{stem}.png"
|
| 88 |
-
annotated.save(annotated_name)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
df = pd.DataFrame([{
|
| 92 |
"breed": predicted_breed,
|
| 93 |
"confidence_percent": f"{conf:.2f}%",
|
|
@@ -96,32 +81,24 @@ def predict_image(img_path: str):
|
|
| 96 |
csv_name = f"{stem}_prediction.csv"
|
| 97 |
df.to_csv(csv_name, index=False)
|
| 98 |
|
| 99 |
-
return predicted_breed,
|
|
|
|
| 100 |
|
| 101 |
# ----------------------------
|
| 102 |
# UI
|
| 103 |
# ----------------------------
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
with gr.Row():
|
| 117 |
-
img_download = gr.File(label="β¬οΈ Download Image", type="filepath")
|
| 118 |
-
csv_download = gr.File(label="β¬οΈ Download CSV", type="filepath")
|
| 119 |
-
|
| 120 |
-
detect_btn.click(
|
| 121 |
-
fn=predict_image,
|
| 122 |
-
inputs=img_input,
|
| 123 |
-
outputs=[breed_output, confidence_output, annotated_preview, img_download, csv_download],
|
| 124 |
-
)
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
demo.launch()
|
|
|
|
| 44 |
[0.229, 0.224, 0.225])
|
| 45 |
])
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# ----------------------------
|
| 48 |
# Predict
|
| 49 |
# ----------------------------
|
|
|
|
| 62 |
conf = float(top_prob.item()) * 100.0
|
| 63 |
predicted_breed = breeds[int(top_idx.item())]
|
| 64 |
|
| 65 |
+
# Draw text directly on image
|
| 66 |
+
draw = ImageDraw.Draw(img)
|
| 67 |
+
font = ImageFont.load_default()
|
|
|
|
| 68 |
text = f"{predicted_breed} ({conf:.2f}%)"
|
| 69 |
+
draw.rectangle([0, 0, img.width, 30], fill="black")
|
| 70 |
+
draw.text((10, 5), text, fill="white", font=font)
|
| 71 |
|
| 72 |
+
annotated_name = f"{predicted_breed}_{conf:.2f}pct_{stem}.png"
|
| 73 |
+
img.save(annotated_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# CSV output
|
| 76 |
df = pd.DataFrame([{
|
| 77 |
"breed": predicted_breed,
|
| 78 |
"confidence_percent": f"{conf:.2f}%",
|
|
|
|
| 81 |
csv_name = f"{stem}_prediction.csv"
|
| 82 |
df.to_csv(csv_name, index=False)
|
| 83 |
|
| 84 |
+
return predicted_breed, f"{conf:.2f}%", annotated_name, csv_name
|
| 85 |
+
|
| 86 |
|
| 87 |
# ----------------------------
|
| 88 |
# UI
|
| 89 |
# ----------------------------
|
| 90 |
+
demo = gr.Interface(
|
| 91 |
+
fn=predict_image,
|
| 92 |
+
inputs=gr.Image(type="filepath", label="π€ Upload Cattle/Buffalo Image"),
|
| 93 |
+
outputs=[
|
| 94 |
+
gr.Textbox(label="Predicted Breed"),
|
| 95 |
+
gr.Textbox(label="Confidence (%)"),
|
| 96 |
+
gr.File(label="β¬οΈ Download Annotated Image", type="filepath"),
|
| 97 |
+
gr.File(label="β¬οΈ Download Prediction CSV", type="filepath"),
|
| 98 |
+
],
|
| 99 |
+
title="π GoVed AI β Indian Cattle/Buffalo Breed Detection",
|
| 100 |
+
description="Upload an image β Get predicted breed, confidence, annotated image, and CSV download."
|
| 101 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
demo.launch()
|