Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Gemini_API_Key="AIzaSyDi3ZnyT06gbVpRAtgk49yGNj4gxcMPLdA"
|
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
from PIL import Image, ImageDraw, ImageFont, ImageColor
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
api_key = os.getenv("Gemini_API_Key")
|
| 10 |
+
genai.configure(api_key=api_key)
|
| 11 |
+
|
| 12 |
+
SYSTEM_INSTRUCTION = """
|
| 13 |
+
Return bounding boxes as a JSON array with labels. Never return masks or code fencing. Limit to 25 objects.
|
| 14 |
+
If an object is present multiple times, name them according to their unique characteristic.
|
| 15 |
+
Use the format: [{"box_2d": [ymin, xmin, ymax, xmax], "label": "object_name"}]
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
model = genai.GenerativeModel(
|
| 19 |
+
model_name='gemini-2.5-flash',
|
| 20 |
+
system_instruction=SYSTEM_INSTRUCTION
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
def parse_response(text):
|
| 24 |
+
"""Cleans markdown formatting if the model includes it."""
|
| 25 |
+
clean = text.replace("json", "").replace("", "").strip()
|
| 26 |
+
return clean
|
| 27 |
+
|
| 28 |
+
def draw_boxes(image, boxes_json):
|
| 29 |
+
"""Draws boxes on the image using normalized coordinates (0-1000)."""
|
| 30 |
+
img = image.copy()
|
| 31 |
+
draw = ImageDraw.Draw(img)
|
| 32 |
+
width, height = img.size
|
| 33 |
+
colors = ["red", "green", "blue", "yellow", "magenta", "cyan", "orange"]
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
data = json.loads(boxes_json)
|
| 37 |
+
for i, item in enumerate(data):
|
| 38 |
+
color = colors[i % len(colors)]
|
| 39 |
+
# Gemini order: [ymin, xmin, ymax, xmax]
|
| 40 |
+
ymin, xmin, ymax, xmax = item["box_2d"]
|
| 41 |
+
|
| 42 |
+
left = (xmin / 1000) * width
|
| 43 |
+
top = (ymin / 1000) * height
|
| 44 |
+
right = (xmax / 1000) * width
|
| 45 |
+
bottom = (ymax / 1000) * height
|
| 46 |
+
|
| 47 |
+
draw.rectangle([left, top, right, bottom], outline=color, width=5)
|
| 48 |
+
draw.text((left + 5, top + 5), item.get("label", "Object"), fill=color)
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error drawing boxes: {e}")
|
| 51 |
+
|
| 52 |
+
return img
|
| 53 |
+
|
| 54 |
+
def process_image(input_img, user_instruction):
|
| 55 |
+
if input_img is None:
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
if not user_instruction.strip():
|
| 59 |
+
user_instruction = "Identify and label the objects in the image."
|
| 60 |
+
|
| 61 |
+
# Combine user instruction with JSON formatting requirement
|
| 62 |
+
full_prompt = f"{user_instruction} Return ONLY the JSON array of bounding boxes."
|
| 63 |
+
|
| 64 |
+
response = model.generate_content([full_prompt, input_img])
|
| 65 |
+
json_data = parse_response(response.text)
|
| 66 |
+
|
| 67 |
+
output_img = draw_boxes(input_img, json_data)
|
| 68 |
+
return output_img
|
| 69 |
+
|
| 70 |
+
demo = gr.Interface(
|
| 71 |
+
fn=process_image,
|
| 72 |
+
inputs=[
|
| 73 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 74 |
+
gr.Textbox(
|
| 75 |
+
label="What should I find?",
|
| 76 |
+
placeholder="e.g., 'Find all the red apples' or 'Detect the cars and pedestrians'",
|
| 77 |
+
lines=2
|
| 78 |
+
)
|
| 79 |
+
],
|
| 80 |
+
outputs=gr.Image(type="pil", label="Image with Bounding Boxes"),
|
| 81 |
+
title="Dynamic Gemini Object Detector",
|
| 82 |
+
description="Upload an image and give specific instructions on what to detect."
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
demo.launch()
|