自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Gemini 可以進(jìn)行目標(biāo)檢測(cè)了!

開(kāi)發(fā)
如果你不熟悉Gemini API,請(qǐng)查看這篇文章,了解如何在Google AI Studio上創(chuàng)建你的Gemini API密鑰。

我們對(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è)幾乎所有的物體。

責(zé)任編輯:趙寧寧 來(lái)源: 小白玩轉(zhuǎn)Python
相關(guān)推薦

2024-11-29 16:10:31

2024-11-27 16:06:12

2025-02-10 16:00:00

OpenCVYOLOv8計(jì)算機(jī)視覺(jué)

2024-07-17 10:30:00

2024-08-01 09:00:00

目標(biāo)檢測(cè)端到端

2024-10-07 11:12:55

2024-09-18 05:15:00

OpenCV樹(shù)莓派目標(biāo)檢測(cè)

2024-06-28 09:20:29

2016-09-21 08:26:27

LinuxGoogleUbuntu

2019-08-01 12:47:26

目標(biāo)檢測(cè)計(jì)算機(jī)視覺(jué)CV

2017-10-02 16:13:47

深度學(xué)習(xí)目標(biāo)檢測(cè)計(jì)算機(jī)視覺(jué)

2024-07-17 09:32:19

2024-10-09 17:02:34

2025-02-19 10:14:42

2024-07-02 10:04:09

2024-06-13 11:53:40

2024-08-15 09:50:44

2017-10-14 21:24:33

TensorFlow目標(biāo)檢測(cè)模型

2021-10-08 09:46:42

深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)人工智能

2025-01-22 13:15:10

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)