偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

使用零樣本目標(biāo)檢測(cè)識(shí)別物體 | 附代碼

開(kāi)發(fā) 人工智能
在這篇文章中,我們將探討如何使用Hugging Face的transformers庫(kù)來(lái)使用零樣本目標(biāo)檢測(cè)在冰箱圖像中識(shí)別物體

在這篇文章中,我們將探討如何使用Hugging Face的transformers庫(kù)來(lái)使用零樣本目標(biāo)檢測(cè)在冰箱圖像中識(shí)別物體。這種方法允許我們?cè)诓恍枰槍?duì)這些物體進(jìn)行特定預(yù)訓(xùn)練的情況下識(shí)別各種物品。

以下是如何工作的代碼的逐步指南。在這種情況下,我們使用Google的OWL-ViT模型,該模型非常適合目標(biāo)檢測(cè)任務(wù)。該模型作為管道加載,允許我們將其作為目標(biāo)檢測(cè)器使用,設(shè)置非常簡(jiǎn)單。

# 導(dǎo)入必要的庫(kù)
from transformers import pipeline

在這里,transformers庫(kù)用于目標(biāo)檢測(cè),利用Hugging Face的零樣本目標(biāo)檢測(cè)模型。零樣本模型是目標(biāo)檢測(cè)任務(wù)的強(qiáng)大工具,因?yàn)樗鼈儾恍枰獙?duì)每個(gè)對(duì)象的特定數(shù)據(jù)集進(jìn)行訓(xùn)練,而是能夠開(kāi)箱即用地理解各種對(duì)象的上下文。

# 從Hugging Face模型中心加載特定檢查點(diǎn)
checkpoint = “google/owlv2-base-patch16-ensemble”
detector = pipeline(model=checkpoint, task=”zero-shot-object-detection”)

加載和顯示圖像

# 導(dǎo)入圖像處理庫(kù)
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”)

在這里,我們使用廣泛用于Python圖像處理的PIL庫(kù)從指定路徑加載圖像(image2.jpg)。然后我們使用matplotlib顯示它。

檢測(cè)物體

模型已加載,圖像已準(zhǔn)備就緒,我們繼續(xù)進(jìn)行檢測(cè)。

# 定義候選標(biāo)簽并在圖像上運(yùn)行檢測(cè)器
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}}]

在零樣本檢測(cè)中,我們提供了一個(gè)候選標(biāo)簽列表,或在圖像中尋找的可能物品,例如常見(jiàn)的冰箱物品:“fanta”,“milk”,“yogurt”等。然后模型嘗試在圖像中定位這些物體,提供它們的邊界框和置信度分?jǐn)?shù)。

可視化檢測(cè)結(jié)果

為了可視化檢測(cè)到的物體,我們?cè)谒鼈冎車L制矩形框,并用檢測(cè)到的標(biāo)簽和置信度分?jǐn)?shù)標(biāo)記它們。

from PIL import ImageDraw
draw = ImageDraw.Draw(image)
for prediction in predictions:
    box = prediction[“box”]
    label = prediction[“l(fā)abel”]
    score = prediction[“score”]
    xmin, ymin, xmax, ymax = box.values()
    draw.rectangle((xmin, ymin, xmax, ymax), outline=”red”, width=1)
    draw.text((xmin, ymin), f”{label}: {round(score,2)}”, fill=”white”)
image

代碼創(chuàng)建了一個(gè)ImageDraw實(shí)例,允許我們?cè)趫D像上疊加矩形框和文本。對(duì)于每個(gè)檢測(cè)到的物體,我們提取其邊界框坐標(biāo)(xmin,ymin,xmax,ymax),標(biāo)簽和置信度分?jǐn)?shù)。在檢測(cè)到的物體周圍繪制矩形框,并將標(biāo)簽和分?jǐn)?shù)添加為文本。

提取檢測(cè)到的物體

get_detected_objects函數(shù)允許我們僅從預(yù)測(cè)中提取檢測(cè)到的物體的標(biāo)簽,以便更容易地訪問(wèn)物體名稱。

# 提取檢測(cè)到的物體的函數(shù)
def get_detected_objects(predictions):
    detected_objects = [pred[“l(fā)abel”] for pred in predictions]
    return detected_objects
# 打印檢測(cè)到的物體列表
detected_objects = get_detected_objects(predictions)
print(“Detected Objects:”, detected_objects)

輸出:

Detected Objects: [‘bottle’, ‘bottle’, ‘bottle’, ‘milk’, ‘bottle’, ‘bottle’, ‘bottle’, ‘coke’, ‘jar’, ‘milk’, ‘refrigerator’, ‘jar’, ‘jar’, ‘refrigerator’, ‘bottle’, ‘jar’, ‘yogurt’, ‘yogurt’, ‘refrigerator’, ‘bottle’, ‘jar’, ‘vegetables’, ‘bottle’, ‘jar’, ‘coke’, ‘jar’, ‘yogurt’, ‘coke’, ‘yogurt’, ‘milk’, ‘coke’, ‘egg’, ‘egg’, ‘bottle’, ‘vegetables’, ‘milk’, ‘coke’, ‘fruits’, ‘vegetables’, ‘milk’, ‘jar’, ‘jar’, ‘bottle’, ‘yogurt’, ‘refrigerator’, ‘milk’, ‘milk’, ‘coke’, ‘bottle’, ‘coke’, ‘egg’, ‘yogurt’, ‘bottle’, ‘milk’, ‘refrigerator’, ‘bottle’, ‘bottle’, ‘egg’, ‘bottle’, ‘milk’, ‘egg’, ‘bottle’, ‘milk’, ‘curd’, ‘coke’, ‘bowl’, ‘vegetables’, ‘milk’, ‘milk’, ‘coke’, ‘egg’, ‘bottle’, ‘curd’, ‘egg’, ‘egg’, ‘yogurt’, ‘egg’, ‘bottle’, ‘egg’, ‘jar’, ‘egg’, ‘egg’, ‘coke’, ‘milk’, ‘vegetables’, ‘curd’, ‘bottle’, ‘jar’, ‘egg’, ‘yogurt’, ‘milk’, ‘egg’, ‘fruits’, ‘yogurt’, ‘jar’, ‘milk’, ‘milk’, ‘curd’, ‘fruits’, ‘curd’, ‘yogurt’, ‘yogurt’, ‘yogurt’, ‘egg’, ‘coke’, ‘egg’, ‘refrigerator’, ‘cokacola’, ‘curd’, ‘jar’, ‘bottle’, ‘refrigerator’, ‘bottle’, ‘milk’, ‘milk’, ‘coke’, ‘curd’, ‘yogurt’, ‘fruits’, ‘yogurt’, ‘vegetables’, ‘yogurt’, ‘coke’, ‘cokacola’, ‘egg’, ‘milk’, ‘milk’, ‘egg’, ‘coke’, ‘coke’, ‘curd’, ‘cokacola’, ‘jar’, ‘jar’, ‘bottle’, ‘curd’, ‘coke’, ‘yogurt’, ‘curd’, ‘fruits’, ‘refrigerator’, ‘milk’, ‘fruits’, ‘cokacola’, ‘milk’, ‘cokacola’, ‘egg’, ‘yogurt’, ‘pickle’, ‘fruits’, ‘coke’, ‘pickle’, ‘egg’, ‘fruits’, ‘refrigerator’, ‘refrigerator’, ‘bottle’, ‘curd’, ‘egg’, ‘egg’, ‘bottle’, ‘refrigerator’, ‘egg’, ‘jar’, ‘jar’, ‘bottle’, ‘pickle’, ‘egg’, ‘jar’, ‘cokacola’, ‘yogurt’, ‘milk’, ‘curd’, ‘bottle’, ‘milk’, ‘milk’, ‘cokacola’, ‘bottle’]

這段代碼僅從預(yù)測(cè)中檢索標(biāo)簽,并打印檢測(cè)到的物體列表。

擴(kuò)展檢測(cè)標(biāo)簽

我們可以通過(guò)調(diào)整候選標(biāo)簽來(lái)執(zhí)行進(jìn)一步的檢測(cè),例如添加其他飲料或品牌。

# 使用額外的標(biāo)簽再次運(yùn)行檢測(cè)器
predictions = detector(
    image,
    candidate_labels=[“fanta”, “cokacola”, “pepsi”, “mountain dew”, “sprite”, “pepper”, “sangria”, “vitamin water”, “beer”],
)

通過(guò)這種方式,我們擴(kuò)展了候選標(biāo)簽列表,允許我們搜索冰箱中常見(jiàn)的其他物品和品牌。

from PIL import ImageDraw
draw = ImageDraw.Draw(image)
for prediction in predictions:
    box = prediction[“box”]
    label = prediction[“l(fā)abel”]
    score = prediction[“score”]
    xmin, ymin, xmax, ymax = box.values()
    draw.rectangle((xmin, ymin, xmax, ymax), outline=”red”, width=1)
    draw.text((xmin, ymin), f”{label}: {round(score,2)}”, fill=”white”)
image

圖像中檢測(cè)到的物體

結(jié)論

這個(gè)代碼示例展示了零樣本目標(biāo)檢測(cè)在動(dòng)態(tài)環(huán)境中識(shí)別物體的強(qiáng)大功能,比如冰箱內(nèi)部。通過(guò)指定自定義標(biāo)簽,你可以將檢測(cè)定制到廣泛的應(yīng)用中,而無(wú)需為每個(gè)特定任務(wù)重新訓(xùn)練模型。Hugging Face的transformers庫(kù)和像Google的OWL-ViT這樣的預(yù)訓(xùn)練模型,使得實(shí)施強(qiáng)大的目標(biāo)檢測(cè)變得非常簡(jiǎn)單,幾乎不需要設(shè)置。

責(zé)任編輯:趙寧寧 來(lái)源: 小白玩轉(zhuǎn)Python
相關(guān)推薦

2024-06-25 09:35:04

模型訓(xùn)練

2025-02-06 11:25:50

2025-02-07 15:20:00

AI數(shù)據(jù)訓(xùn)練

2025-02-08 11:12:34

ZAPS影像模型

2025-02-07 10:10:05

MusicMagus擴(kuò)散模型音樂(lè)編輯

2023-02-24 10:22:15

2023-03-06 16:17:13

2024-03-25 08:35:00

6DAI

2024-09-12 08:00:00

2024-10-28 17:17:32

2023-04-11 09:43:21

模型AI

2023-11-09 23:45:01

Pytorch目標(biāo)檢測(cè)

2023-07-12 09:00:00

自動(dòng)語(yǔ)音識(shí)別機(jī)器學(xué)習(xí)

2020-03-09 14:08:25

Python目標(biāo)檢測(cè)視覺(jué)識(shí)別

2018-11-27 11:58:34

Python人臉識(shí)別編程語(yǔ)言

2024-02-07 12:13:19

AI數(shù)據(jù)

2025-09-29 14:02:02

DeepMind模型AI

2023-02-03 16:31:02

模型

2021-10-25 09:06:29

模型人工智能計(jì)算
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)