博客
关于我
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 InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>