Gemini 可以進(jìn)行目標(biāo)檢測(cè)了!
我們對(duì)Gemini印象深刻的多模態(tài)能力已經(jīng)很熟悉了,特別是在涉及圖像數(shù)據(jù)推理時(shí)——無(wú)論是涉及圖像描述、OCR、分類,還是識(shí)別圖像中的特定內(nèi)容。與其開(kāi)放模型對(duì)應(yīng)物PaliGemma不同,Gemini模型并沒(méi)有明確針對(duì)目標(biāo)檢測(cè)任務(wù)進(jìn)行訓(xùn)練。這一事實(shí)促使我進(jìn)行一些實(shí)驗(yàn)并撰寫這篇博客。
PaliGemma鏈接:https://ai.google.dev/gemma/docs/paligemma
注意:在這里,當(dāng)我們談?wù)撃繕?biāo)檢測(cè)時(shí),我們指的是通過(guò)繪制邊界框來(lái)識(shí)別和定位對(duì)象,就像YOLO、DETR、EfficientDet、Florence-2和PaliGemma等模型所做的那樣。
先決條件
我們只需要Gemini的API密鑰——?jiǎng)e無(wú)他物。我假設(shè)你已經(jīng)熟悉Gemini API。如果你不熟悉,請(qǐng)查看這篇博客,了解如何在Google AI Studio上創(chuàng)建你的Gemini API密鑰。打開(kāi)倉(cāng)庫(kù)中的Colab筆記本:https://github.com/NSTiwari/Object-Detection-using-Gemini
步驟1:安裝必要的庫(kù)和依賴項(xiàng)
# Install Generative AI SDK.
!pip install -q -U google-generativeai
# Import libraries
from google.colab import userdata
import google.generativeai as genai
import re
from PIL import Image
import cv2
import numpy as np
步驟2:配置API密鑰和模型
你可以選擇Gemini 1.5 Flash或Gemini 1.5 Pro,隨你喜歡。
API_KEY = userdata.get('gemini')
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel(model_name='gemini-1.5-pro')
步驟3:傳遞輸入圖像和文本提示
使文本提示清晰簡(jiǎn)單,使用示例。在這種情況下,我們要求Gemini提供如下格式的邊界框坐標(biāo):[ymin, xmin, ymax, xmax, object_name]。
input_image = "image.jpg" # @param {type : 'string'}
img = Image.open(input_image)
response = model.generate_content([
img,
(
"Return bounding boxes for all objects in the image in the following format as"
" a list. \n [ymin, xmin, ymax, xmax, object_name]. If there are more than one object, return separate lists for each object"
),
])
result = response.text
步驟4:解析模型響應(yīng)
def parse_bounding_box(response):
bounding_boxes = re.findall(r'\[(\d+,\s*\d+,\s*\d+,\s*\d+,\s*[\w\s]+)\]', response)
# Convert each group into a list of integers and labels.
parsed_boxes = []
for box in bounding_boxes:
parts = box.split(',')
numbers = list(map(int, parts[:-1]))
label = parts[-1].strip()
parsed_boxes.append((numbers, label))
# Return the list of bounding boxes with their labels.
return parsed_boxes
bounding_box = parse_bounding_box(result)
步驟5:繪制邊界框
模型提供的邊界框坐標(biāo)必須通過(guò)將圖像的高和寬除以1000來(lái)歸一化。
label_colors = {}
def draw_bounding_boxes(image, bounding_boxes_with_labels):
if image.mode != 'RGB':
image = image.convert('RGB')
image = np.array(image)
for bounding_box, label in bounding_boxes_with_labels:
# Normalize the bounding box coordinates.
width, height = image.shape[1], image.shape[0]
ymin, xmin, ymax, xmax = bounding_box
x1 = int(xmin / 1000 * width)
y1 = int(ymin / 1000 * height)
x2 = int(xmax / 1000 * width)
y2 = int(ymax / 1000 * height)
if label not in label_colors:
color = np.random.randint(0, 256, (3,)).tolist()
label_colors[label] = color
else:
color = label_colors[label]
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.5
font_thickness = 1
box_thickness = 2
text_size = cv2.getTextSize(label, font, font_scale, font_thickness)[0]
text_bg_x1 = x1
text_bg_y1 = y1 - text_size[1] - 5
text_bg_x2 = x1 + text_size[0] + 8
text_bg_y2 = y1
cv2.rectangle(image, (text_bg_x1, text_bg_y1), (text_bg_x2, text_bg_y2), color, -1)
cv2.putText(image, label, (x1 + 2, y1 - 5), font, font_scale, (255, 255, 255), font_thickness)
cv2.rectangle(image, (x1, y1), (x2, y2), color, box_thickness)
image = Image.fromarray(image)
return image
output = draw_bounding_boxes(img, bounding_box)
讓我們從一個(gè)簡(jiǎn)單的例子開(kāi)始:
目標(biāo):?jiǎn)蝹€(gè)對(duì)象的圖像
提示:以列表格式返回圖像中人物的邊界框。[ymin, xmin, ymax, xmax, object_name]。
好的開(kāi)始,現(xiàn)在讓我們嘗試多個(gè)對(duì)象。
目標(biāo):多個(gè)對(duì)象的圖像
提示:以列表格式返回圖像中所有對(duì)象的邊界框。[ymin, xmin, ymax, xmax, object_name]。如果有一個(gè)以上的對(duì)象,請(qǐng)為每個(gè)對(duì)象返回單獨(dú)的列表。
狗和自行車
一點(diǎn)也不差。它準(zhǔn)確地檢測(cè)到了對(duì)象,但這些都是常見(jiàn)的對(duì)象,對(duì)吧?讓我們進(jìn)一步挑戰(zhàn)Gemini。我有一張著名的畫作“Ram Darbar”的圖片,來(lái)自《羅摩衍那》。讓我們看看Gemini是否能識(shí)別和檢測(cè)畫中的所有角色。
提示:這是《羅摩衍那》中“Ram Darbar”的一幅畫。以列表格式返回圖像中所有角色的邊界框。[ymin, xmin, ymax, xmax, character_name]。
《羅摩衍那》中的Ram Darbar畫作
我印象深刻的是,它不僅繪制了邊界框,還準(zhǔn)確地識(shí)別了每個(gè)角色,尤其是當(dāng)我特別要求他們的名字時(shí)。是時(shí)候測(cè)試一些非傳統(tǒng)圖像了。我畫了阿爾伯特·愛(ài)因斯坦(抱歉,這是我能做的最好的了)。讓我們?cè)囈辉嚒?/p>
目標(biāo):一幅繪畫的圖片
提示:以列表格式返回圖像中著名人物的名稱和邊界框。[ymin, xmin, ymax, xmax, object_name]。
作者畫的阿爾伯特·愛(ài)因斯坦
在對(duì)不同圖像進(jìn)行一系列測(cè)試后:從識(shí)別人和物體到識(shí)別繪畫和繪畫中的人物,并準(zhǔn)確地用邊界框定位它們,Gemini確實(shí)滿足了我對(duì)目標(biāo)檢測(cè)的期望。我個(gè)人不會(huì)將Gemini與專門設(shè)計(jì)用于目標(biāo)檢測(cè)的模型進(jìn)行比較,因?yàn)樗膬?yōu)勢(shì)在于不同的領(lǐng)域。然而,這個(gè)實(shí)驗(yàn)滿足了我的好奇心:它能夠很好地處理檢測(cè)任務(wù),并且能夠檢測(cè)幾乎所有的物體。