欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 使用transformers调用owlv2实现开放目标检测

使用transformers调用owlv2实现开放目标检测

2024/10/25 3:41:27 来源:https://blog.csdn.net/qq_45270993/article/details/142661655  浏览:    关键词:使用transformers调用owlv2实现开放目标检测

目录

  • 安装
  • Demo

安装

pip install transformers

Demo

from PIL import Image, ImageDraw, ImageFont
import numpy as np
import torch
from transformers import AutoProcessor, Owlv2ForObjectDetection
from transformers.utils.constants import OPENAI_CLIP_MEAN, OPENAI_CLIP_STDprocessor = AutoProcessor.from_pretrained("/home/share3/mayunchuan/google/owlv2-large-patch14-ensemble")
model = Owlv2ForObjectDetection.from_pretrained("/home/share3/mayunchuan/google/owlv2-large-patch14-ensemble").cuda()image = Image.open('/home/mayunchuan/lavad/dataset/Thumos14_25fps/frames/video_test_0000293/004902.jpg')
# image = Image.open('/home/mayunchuan/lavad/dataset/Thumos14_25fps/frames/video_validation_0000990/001388.jpg')
# texts = [["a photo of a volleyball", "a photo of a man"]]
texts = [[" javelin"]]
inputs = processor(text=texts, images=image, return_tensors="pt")
inputs['input_ids'] = inputs['input_ids'].cuda()
inputs['attention_mask'] = inputs['attention_mask'].cuda()
inputs['pixel_values'] = inputs['pixel_values'].cuda()
# forward pass
with torch.no_grad():outputs = model(**inputs)# Note: boxes need to be visualized on the padded, unnormalized image
# hence we'll set the target image sizes (height, width) based on thatdef get_preprocessed_image(pixel_values):pixel_values = pixel_values.squeeze().cpu().numpy()unnormalized_image = (pixel_values * np.array(OPENAI_CLIP_STD)[:, None, None]) + np.array(OPENAI_CLIP_MEAN)[:, None, None]unnormalized_image = (unnormalized_image * 255).astype(np.uint8)unnormalized_image = np.moveaxis(unnormalized_image, 0, -1)unnormalized_image = Image.fromarray(unnormalized_image)return unnormalized_imageunnormalized_image = get_preprocessed_image(inputs.pixel_values)target_sizes = torch.Tensor([unnormalized_image.size[::-1]])
# Convert outputs (bounding boxes and class logits) to final bounding boxes and scores
results = processor.post_process_object_detection(outputs=outputs, threshold=0.2, target_sizes=target_sizes
)i = 0  # Retrieve predictions for the first image for the corresponding text queries
text = texts[i]
boxes, scores, labels = results[i]["boxes"], results[i]["scores"], results[i]["labels"]for box, score, label in zip(boxes, scores, labels):box = [round(i, 2) for i in box.tolist()]print(f"Detected {text[label]} with confidence {round(score.item(), 3)} at location {box}")# 绘制边界框
draw = ImageDraw.Draw(unnormalized_image)for score, label, box in zip(scores, labels, boxes):box = [round(i, 2) for i in box.tolist()]x, y, x2, y2 = tuple(box)draw.rectangle((x, y, x2, y2), outline="red", width=1)draw.text((x, y), text[label.item()], font_size=20, fill="black")# 保存标记好的图片
unnormalized_image.save("marked_image.jpg")

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com