aditya-sah commited on
Commit
459a033
Β·
verified Β·
1 Parent(s): fa4119e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -45
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
- # Annotate using PIL
75
- annotated = img.copy()
76
- draw = ImageDraw.Draw(annotated)
77
- font = _get_font(max(20, int(annotated.width * 0.05)))
78
  text = f"{predicted_breed} ({conf:.2f}%)"
 
 
79
 
80
- # background rectangle
81
- text_w, text_h = draw.textsize(text, font=font)
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
- # save CSV
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, round(conf, 2), annotated_name, annotated_name, csv_name
 
100
 
101
  # ----------------------------
102
  # UI
103
  # ----------------------------
104
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
105
- gr.Markdown("<h1 style='text-align:center;'>πŸ„ GoVed AI β€” Indian Cattle/Buffalo Breed Detection</h1>")
106
-
107
- with gr.Row():
108
- with gr.Column(scale=1):
109
- img_input = gr.Image(type="filepath", label="πŸ“€ Upload Image", height=340)
110
- detect_btn = gr.Button("πŸ” Detect Breed", variant="primary")
111
-
112
- with gr.Column(scale=1):
113
- breed_output = gr.Textbox(label="Predicted Breed", interactive=False)
114
- confidence_output = gr.Number(label="Confidence (%)", interactive=False, precision=2)
115
- annotated_preview = gr.Image(type="filepath", label="Annotated Preview", height=340)
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()