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