NSTiwari commited on
Commit
8e354ef
·
verified ·
1 Parent(s): 2aa47b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -100
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 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)
 
 
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)