本文共 3551 字,大约阅读时间需要 11 分钟。
我们对Gemini的多模态能力已经有了深刻的认识,尤其是在处理图像数据推理时,无论是图像描述、OCR、分类还是特定内容识别。与其开放模型对应的PaliGemma不同,Gemini并未明确针对目标检测任务进行训练。这一特点促使我进行了一系列实验,并撰写了本博客。
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_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.")
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/