点击下方“深度学习爱好者”,选择加p"星标"或“置顶”
# 导入必要的库
from transformers import pipeline
# 从Hugging Face模型中心加载特定检查点
checkpoint = “google/owlv2-base-patch16-ensemble”
detector = pipeline(model=checkpoint, task=”zero-shot-object-detection”)
# 导入图像处理库
import skimage
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
# 加载并显示图像
image = Image.open(‘/content/image2.jpg’)
plt.imshow(image)
plt.axis(‘off’)
plt.show()
image = Image.fromarray(np.uint8(image)).convert(“RGB”)
# 定义候选标签并在图像上运行检测器
predictions = detector(
image,
candidate_labels=[“fanta”, “cokacola”, “bottle”, “egg”, “bowl”, “donut”, “milk”, “jar”, “curd”, “pickle”, “refrigerator”, “fruits”, “vegetables”, “bread”,”yogurt”],
)
predictions
[{'score': 0.4910733997821808,
'label': 'bottle',
'box': {'xmin': 419, 'ymin': 1825, 'xmax': 574, 'ymax': 2116}},
{'score': 0.45601949095726013,
'label': 'bottle',
'box': {'xmin': 1502, 'ymin': 795, 'xmax': 1668, 'ymax': 1220}},
{'score': 0.4522128999233246,
'label': 'bottle',
'box': {'xmin': 294, 'ymin': 1714, 'xmax': 479, 'ymax': 1924}},
{'score': 0.4485340714454651,
'label': 'milk',
'box': {'xmin': 545, 'ymin': 811, 'xmax': 770, 'ymax': 1201}},
{'score': 0.44276902079582214,
'label': 'bottle',
'box': {'xmin': 1537, 'ymin': 958, 'xmax': 1681, 'ymax': 1219}},
{'score': 0.4287840723991394,
'label': 'bottle',
'box': {'xmin': 264, 'ymin': 1726, 'xmax': 459, 'ymax': 2104}},
{'score': 0.41883620619773865,
'label': 'bottle',
'box': {'xmin': 547, 'ymin': 632, 'xmax': 773, 'ymax': 1203}},
{'score': 0.15758953988552094,
'label': 'jar',
'box': {'xmin': 1141, 'ymin': 1628, 'xmax': 1259, 'ymax': 1883}},
{'score': 0.15696804225444794,
'label': 'egg',
'box': {'xmin': 296, 'ymin': 1034, 'xmax': 557, 'ymax': 1131}},
{'score': 0.15674084424972534,
'label': 'egg',
'box': {'xmin': 292, 'ymin': 1109, 'xmax': 552, 'ymax': 1212}},
{'score': 0.1565699428319931,
'label': 'coke',
'box': {'xmin': 294, 'ymin': 1714, 'xmax': 479, 'ymax': 1924}},
{'score': 0.15651869773864746,
'label': 'milk',
'box': {'xmin': 417, 'ymin': 1324, 'xmax': 635, 'ymax': 1450}}]
from PIL import ImageDraw
draw = ImageDraw.Draw(image)
for prediction in predictions:
box = prediction[“box”]
label = prediction[“label”]
score = prediction[“score”]
ymin, xmax, ymax = box.values()
ymin, xmax, ymax), outline=”red”, width=1)
ymin), f”{label}: {round(score,2)}”, fill=”white”)
image
# 提取检测到的物体的函数
def get_detected_objects(predictions):
detected_objects = [pred[“label”] for pred in predictions]
return detected_objects
# 打印检测到的物体列表
detected_objects = get_detected_objects(predictions)
print(“Detected Objects:”, detected_objects)
Detected Objects:
# 使用额外的标签再次运行检测器
predictions = detector(
image,
candidate_labels=[“fanta”, “cokacola”, “pepsi”, “mountain dew”, “sprite”, “pepper”, “sangria”, “vitamin water”, “beer”],
)
from PIL import ImageDraw
draw = ImageDraw.Draw(image)
for prediction in predictions:
box = prediction[“box”]
label = prediction[“label”]
score = prediction[“score”]
ymin, xmax, ymax = box.values()
ymin, xmax, ymax), outline=”red”, width=1)
ymin), f”{label}: {round(score,2)}”, fill=”white”)
image
下载1:Pytoch常用函数手册 在「深度学习爱好者」公众号后台回复:Pytorch常用函数手册,即可下载全网第一份Pytorch常用函数手册,涵盖Tensors介绍、基础函数介绍、数据处理函数、优化函数、CUDA编程、多线程处理等十四章章内容。 下载2:Python视觉实战项目52讲 在「小白学视觉」公众号后台回复:Python视觉实战项目,即可下载包括图像分割、口罩检测、车道线检测、车辆计数、添加眼线、车牌识别、字符识别、情绪检测、文本内容提取、面部识别等31个视觉实战项目,助力快速学校计算机视觉。 交流群 欢迎加入公众号读者群一起和同行交流,目前有SLAM、三维视觉驶、计算摄影、检测、分割、识别、医学影像、GAN、算法竞赛等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~