Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
+
from PIL import Image
|
3 |
+
import re
|
4 |
+
import cv2
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
def parse_bounding_box(response):
|
8 |
+
bounding_boxes = re.findall(r'\[(\d+,\s*\d+,\s*\d+,\s*\d+,\s*[\w\s]+)\]', response)
|
9 |
+
|
10 |
+
# Convert each group into a list of integers and labels.
|
11 |
+
parsed_boxes = []
|
12 |
+
for box in bounding_boxes:
|
13 |
+
parts = box.split(',')
|
14 |
+
numbers = list(map(int, parts[:-1]))
|
15 |
+
label = parts[-1].strip()
|
16 |
+
parsed_boxes.append((numbers, label))
|
17 |
+
|
18 |
+
# Return the list of bounding boxes with their labels.
|
19 |
+
return parsed_boxes
|
20 |
+
|
21 |
+
# Draw bounding boxes with labels.
|
22 |
+
def draw_bounding_boxes(image, bounding_boxes_with_labels):
|
23 |
+
label_colors = {}
|
24 |
+
if image.mode != 'RGB':
|
25 |
+
image = image.convert('RGB')
|
26 |
+
|
27 |
+
image = np.array(image)
|
28 |
+
|
29 |
+
for bounding_box, label in bounding_boxes_with_labels:
|
30 |
+
# Normalize the bounding box coordinates
|
31 |
+
width, height = image.shape[1], image.shape[0]
|
32 |
+
ymin, xmin, ymax, xmax = bounding_box
|
33 |
+
x1 = int(xmin / 1000 * width)
|
34 |
+
y1 = int(ymin / 1000 * height)
|
35 |
+
x2 = int(xmax / 1000 * width)
|
36 |
+
y2 = int(ymax / 1000 * height)
|
37 |
+
|
38 |
+
if label not in label_colors:
|
39 |
+
color = np.random.randint(0, 256, (3,)).tolist()
|
40 |
+
label_colors[label] = color
|
41 |
+
else:
|
42 |
+
color = label_colors[label]
|
43 |
+
|
44 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
45 |
+
font_scale = 1
|
46 |
+
font_thickness = 2
|
47 |
+
box_thickness = 2
|
48 |
+
text_size = cv2.getTextSize(label, font, font_scale, font_thickness)[0]
|
49 |
+
|
50 |
+
text_bg_x1 = x1
|
51 |
+
text_bg_y1 = y1 - text_size[1] - 5
|
52 |
+
text_bg_x2 = x1 + text_size[0] + 8
|
53 |
+
text_bg_y2 = y1
|
54 |
+
|
55 |
+
cv2.rectangle(image, (text_bg_x1, text_bg_y1), (text_bg_x2, text_bg_y2), color, -1)
|
56 |
+
cv2.putText(image, label, (x1 + 2, y1 - 5), font, font_scale, (255, 255, 255), font_thickness)
|
57 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), color, box_thickness)
|
58 |
+
|
59 |
+
image = Image.fromarray(image)
|
60 |
+
return image
|
61 |
+
|
62 |
+
def detect_objects(api_key, prompt, input_image):
|
63 |
+
genai.configure(api_key=api_key)
|
64 |
+
|
65 |
+
img = Image.open(input_image)
|
66 |
+
|
67 |
+
model = genai.GenerativeModel(model_name='gemini-1.5-pro')
|
68 |
+
|
69 |
+
response = model.generate_content([
|
70 |
+
img,
|
71 |
+
(
|
72 |
+
f"Return bounding boxes for {prompt} in the image in the following format as"
|
73 |
+
" a list. \n [ymin, xmin, ymax, xmax, object_name]. "
|
74 |
+
),
|
75 |
+
])
|
76 |
+
|
77 |
+
result = response.text
|
78 |
+
result = result[result.find('-'):].strip()
|
79 |
+
|
80 |
+
bounding_box = parse_bounding_box(result)
|
81 |
+
output = draw_bounding_boxes(img, bounding_box)
|
82 |
+
|
83 |
+
return output
|
84 |
+
|
85 |
+
# Gradio app
|
86 |
+
demo = gr.Interface(
|
87 |
+
fn=detect_objects,
|
88 |
+
inputs=[
|
89 |
+
gr.Textbox(label="Your Gemini API Key", type="password"),
|
90 |
+
gr.Textbox(label="Object(s) to detect", value="famous personality"),
|
91 |
+
gr.Image(type="filepath", label="Input Image")
|
92 |
+
],
|
93 |
+
outputs=gr.Image(type="pil", label="Detected Image"),
|
94 |
+
title="Object Detection using Gemini ✨",
|
95 |
+
description="Detect objects in images using the Gemini.",
|
96 |
+
allow_flagging="never"
|
97 |
+
)
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
demo.launch(debug=True)
|