roqaia123 commited on
Commit
a7cd855
·
verified ·
1 Parent(s): a6b2e02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -45
app.py CHANGED
@@ -1,60 +1,91 @@
1
  import gradio as gr
2
  import os
3
  import json
4
- from PIL import Image, ImageDraw, ImageFont, ImageColor
5
  import google.generativeai as genai
6
 
7
- # --- 1. SETUP API KEY ---
8
- # We use the key you provided.
9
- # (Note: For long-term safety, use Settings > Secrets, but this works for now!)
10
  api_key = "AIzaSyABaidsygD73gbaSlMHlkrhhiDT8NDzrjE"
11
-
12
  genai.configure(api_key=api_key)
13
 
14
- # --- 2. CONFIGURATION ---
15
- generation_config = {
16
- "temperature": 0.5,
17
- "response_mime_type": "application/json"
18
- }
19
-
20
- system_instructions = """
21
- Return bounding boxes as a JSON array.
22
- Format: [{"box_2d": [ymin, xmin, ymax, xmax], "label": "string"}]
23
- Coordinate scale: 0-1000.
24
- Do not use markdown.
25
- """
26
-
27
- model = genai.GenerativeModel(
28
- model_name='gemini-1.5-flash',
29
- system_instruction=system_instructions
30
- )
31
-
32
- # --- 3. HELPER FUNCTIONS ---
33
- def parse_json(json_output):
34
- """Safely parse JSON even if the model adds text."""
35
- try:
36
- # Clean potential markdown
37
- json_output = json_output.replace("```json", "").replace("```", "")
38
- return json.loads(json_output)
39
- except Exception as e:
40
- print(f"JSON Parsing Error: {e}")
41
- return []
42
 
 
43
  def plot_bounding_boxes(im, boxes):
44
- """Draws boxes on the image."""
 
45
  im = im.copy()
46
- width, height = im.size
47
  draw = ImageDraw.Draw(im)
 
48
 
49
- # Colors to cycle through
50
- colors = ['red', 'green', 'blue', 'yellow', 'cyan', 'magenta']
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  try:
53
- # Load a font (fallback to default if arial is missing)
54
- try:
55
- font = ImageFont.truetype("arial.ttf", 20)
56
- except OSError:
57
- font = ImageFont.load_default()
58
-
59
- for i, box in enumerate(boxes):
60
- color = colors[i % len(colors)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import os
3
  import json
4
+ from PIL import Image, ImageDraw, ImageFont
5
  import google.generativeai as genai
6
 
7
+ # --- 1. SETUP ---
8
+ # using the key you provided
 
9
  api_key = "AIzaSyABaidsygD73gbaSlMHlkrhhiDT8NDzrjE"
 
10
  genai.configure(api_key=api_key)
11
 
12
+ model = genai.GenerativeModel('gemini-1.5-flash')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ # --- 2. LOGIC ---
15
  def plot_bounding_boxes(im, boxes):
16
+ """Draws bounding boxes on the image."""
17
+ # Create a copy so we don't ruin the original
18
  im = im.copy()
 
19
  draw = ImageDraw.Draw(im)
20
+ width, height = im.size
21
 
22
+ # Simple list of colors
23
+ colors = ['red', 'green', 'blue', 'yellow', 'cyan']
24
 
25
+ for i, box in enumerate(boxes):
26
+ color = colors[i % len(colors)]
27
+
28
+ # Get the numbers (default to 0 if missing)
29
+ ymin, xmin, ymax, xmax = box.get("box_2d", [0,0,0,0])
30
+ label = box.get("label", "Object")
31
+
32
+ # Convert 0-1000 scale to pixels
33
+ left = int((xmin / 1000) * width)
34
+ top = int((ymin / 1000) * height)
35
+ right = int((xmax / 1000) * width)
36
+ bottom = int((ymax / 1000) * height)
37
+
38
+ # Draw the box
39
+ draw.rectangle([left, top, right, bottom], outline=color, width=4)
40
+
41
+ # Draw the text
42
+ # (We keep it simple to avoid font errors)
43
+ draw.text((left, top), label, fill=color)
44
+
45
+ return im
46
+
47
+ def detect(image):
48
+ if image is None:
49
+ return None, "Please upload an image."
50
+
51
+ # The prompt we send to Gemini
52
+ prompt = """
53
+ Detect objects in this image.
54
+ Return a JSON Array.
55
+ Format: [{"box_2d": [ymin, xmin, ymax, xmax], "label": "name"}]
56
+ """
57
+
58
  try:
59
+ # 1. Ask Gemini
60
+ response = model.generate_content([prompt, image])
61
+ text_data = response.text
62
+
63
+ # 2. Clean the text (remove markdown ```json ... ```)
64
+ text_data = text_data.replace("```json", "").replace("```", "")
65
+
66
+ # 3. Convert text to list
67
+ boxes = json.loads(text_data)
68
+
69
+ # 4. Draw boxes
70
+ result_image = plot_bounding_boxes(image, boxes)
71
+
72
+ return result_image, str(boxes)
73
+
74
+ except Exception as e:
75
+ return image, f"Error: {str(e)}"
76
+
77
+ # --- 3. UI ---
78
+ with gr.Blocks() as demo:
79
+ gr.Markdown("# 🔍 Gemini Object Detector")
80
+
81
+ with gr.Row():
82
+ inp = gr.Image(type="pil", label="Input Image")
83
+ out = gr.Image(type="pil", label="Result")
84
+
85
+ debug = gr.Textbox(label="Debug Information")
86
+
87
+ btn = gr.Button("Detect Objects")
88
+ btn.click(detect, inp, [out, debug])
89
+
90
+ if __name__ == "__main__":
91
+ demo.launch()