Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,14 @@ import json
|
|
| 4 |
from PIL import Image, ImageDraw, ImageFont, ImageColor
|
| 5 |
import google.generativeai as genai
|
| 6 |
|
| 7 |
-
# --- 1. SETUP API KEY
|
| 8 |
-
#
|
|
|
|
| 9 |
api_key = "AIzaSyABaidsygD73gbaSlMHlkrhhiDT8NDzrjE"
|
| 10 |
|
| 11 |
genai.configure(api_key=api_key)
|
| 12 |
|
| 13 |
# --- 2. CONFIGURATION ---
|
| 14 |
-
# We use Gemini 1.5 Flash for fast object detection
|
| 15 |
generation_config = {
|
| 16 |
"temperature": 0.5,
|
| 17 |
"response_mime_type": "application/json"
|
|
@@ -33,7 +33,7 @@ model = genai.GenerativeModel(
|
|
| 33 |
def parse_json(json_output):
|
| 34 |
"""Safely parse JSON even if the model adds text."""
|
| 35 |
try:
|
| 36 |
-
#
|
| 37 |
json_output = json_output.replace("```json", "").replace("```", "")
|
| 38 |
return json.loads(json_output)
|
| 39 |
except Exception as e:
|
|
@@ -57,22 +57,4 @@ def plot_bounding_boxes(im, boxes):
|
|
| 57 |
font = ImageFont.load_default()
|
| 58 |
|
| 59 |
for i, box in enumerate(boxes):
|
| 60 |
-
color = colors[i % len(colors)]
|
| 61 |
-
|
| 62 |
-
# Get coords
|
| 63 |
-
ymin, xmin, ymax, xmax = box.get("box_2d", [0,0,0,0])
|
| 64 |
-
label = box.get("label", "Object")
|
| 65 |
-
|
| 66 |
-
# Convert 0-1000 scale to pixels
|
| 67 |
-
left = int((xmin / 1000) * width)
|
| 68 |
-
top = int((ymin / 1000) * height)
|
| 69 |
-
right = int((xmax / 1000) * width)
|
| 70 |
-
bottom = int((ymax / 1000) * height)
|
| 71 |
-
|
| 72 |
-
# Draw Box
|
| 73 |
-
draw.rectangle([left, top, right, bottom], outline=color, width=4)
|
| 74 |
-
|
| 75 |
-
# Draw Label with background
|
| 76 |
-
text_bbox = draw.textbbox((left, top), label, font=font)
|
| 77 |
-
draw.rectangle(text_bbox, fill=color)
|
| 78 |
-
draw.text((left, top), label, fill="white", font=font)
|
|
|
|
| 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"
|
|
|
|
| 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:
|
|
|
|
| 57 |
font = ImageFont.load_default()
|
| 58 |
|
| 59 |
for i, box in enumerate(boxes):
|
| 60 |
+
color = colors[i % len(colors)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|