博客
关于我
Gemini 可以进行目标检测了!
阅读量:797 次
发布时间:2023-04-05

本文共 3551 字,大约阅读时间需要 11 分钟。

我们对Gemini的多模态能力已经有了深刻的认识,尤其是在处理图像数据推理时,无论是图像描述、OCR、分类还是特定内容识别。与其开放模型对应的PaliGemma不同,Gemini并未明确针对目标检测任务进行训练。这一特点促使我进行了一系列实验,并撰写了本博客。

PaliGemma链接

https://ai.google.dev/gemma/docs/paligemma

先决条件

仅需Gemini的API密钥,即可开始。如果你尚未熟悉Gemini API,可以参考以下仓库中的Colab笔记本:https://github.com/NSTiwari/Object-Detection-using-Gemini

安装必要库

# 安装生成式AI SDK!pip install -q -U google-generativeai

配置API密钥和模型

API_KEY = userdata.get('gemini')genai.configure(api_key=API_KEY)model = genai.GenerativeModel(model_name='gemini-1.5-pro')

输入图像和文本提示

input_image = "image.jpg"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. [ymin, xmin, ymax, xmax, object_name]. If there are more than one object, return separate lists for each object"    )])result = response.text

解析模型响应

def parse_bounding_box(response):    bounding_boxes = re.findall(r'\[(\d+,\s*\d+,\s*\d+,\s*\d+,\s*[\w\s]+)\]', response)    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 parsed_boxesbounding_box = parse_bounding_box(result)

绘制边界框

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:        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)    return Image.fromarray(image)output = draw_bounding_boxes(img, bounding_box)

实验结果

让我们从一个简单的例子开始:

目标:单个对象的图像

prompt = (    "Return bounding boxes for all objects in the image in the following format as "    "a list. [ymin, xmin, ymax, xmax, object_name]. If there is one object, return the list.")

多个对象的图像

prompt = (    "Return bounding boxes for all objects in the image in the following format as "    "a list. [ymin, xmin, ymax, xmax, object_name]. If there are multiple objects, return separate lists for each object.")

《罗摩衍那》中的Ram Darbar画作

prompt = (    "This is a painting of 'Ram Darbar' from the Ramayana series. Return bounding boxes for all characters in the image in the following format as "    "a list. [ymin, xmin, ymax, xmax, character_name]. If there are multiple characters, return separate lists for each character.")

作者绘制的阿尔伯特·爱因斯坦

prompt = (    "Return bounding boxes for all famous people in the image in the following format as "    "a list. [ymin, xmin, ymax, xmax, object_name].")

通过一系列测试,Gemini在识别人物和物体方面表现出色,并能准确地用边界框定位绘画中的人物。虽然Gemini并非专门设计用于目标检测的模型,但其多模态能力在处理检测任务方面表现出色。

转载地址:http://ixrfk.baihongyu.com/

你可能感兴趣的文章
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>