利用Python實(shí)現(xiàn)給圖像添加標(biāo)簽
一、需求
給指定的圖片添加標(biāo)簽
二、代碼
# !/usr/bin/env python
# coding: utf-8
import tkinter as tk
from tkinter import filedialog, messagebox
import os
import json
from google.protobuf.json_format import MessageToJson
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
# 授權(quán)密鑰
API_KEY = '5************************************'
USER_ID = '3*********h'
APP_ID = 'my***************'
# 注冊(cè)網(wǎng)址:https://www.clarifai.com/
# 網(wǎng)址注冊(cè)后,拿授權(quán)密鑰,每月1000次免費(fèi)
def analyze_image(image_path):
# 使用 Clarifai API 進(jìn)行圖像分析
results = []
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + API_KEY),)
userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)
with open(image_path, 'rb') as f:
file_bytes = f.read()
try:
print(f"開始分析圖像: {image_path}") # 添加調(diào)試語句
post_model_outputs_response = stub.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
user_app_id=userDataObject,
model_id='general-image-recognition',
inputs=[
resources_pb2.Input(
data=resources_pb2.Data(
image=resources_pb2.Image(
base64=file_bytes
)
)
)
]
),
metadata=metadata
)
print(f"圖像分析完成: {image_path}") # 添加調(diào)試語句
except Exception as e:
messagebox.showerror("錯(cuò)誤", "圖像分析失敗,原因: " + str(e))
return
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
messagebox.showerror("錯(cuò)誤", "模型輸出提交失敗,狀態(tài): " + post_model_outputs_response.status.description)
return
# 將獲取到的結(jié)果轉(zhuǎn)為 JSON
json_result = MessageToJson(post_model_outputs_response)
json_output = json.loads(json_result)
print(json_output)
# 從 JSON 結(jié)果中提取并顯示關(guān)鍵信息
for output in json_output["outputs"]:
for concept in output['data']['concepts']:
object_name = concept['name']
results.append(object_name)
return results
if __name__ == '__main__':
image_path = r"D:\Desktop\tp.png"
ret = analyze_image(image_path)三、圖片

四、結(jié)果
['interior design', 'luxury', 'contemporary', 'indoors', 'furniture', 'comfort', 'family', 'room', 'hotel', 'sofa', 'no person', 'minimalist', 'rug', 'chair', 'seat', 'lamp', 'coffee table', 'curtain', 'trading floor', 'mansion']
到此這篇關(guān)于利用Python實(shí)現(xiàn)給圖像添加標(biāo)簽的文章就介紹到這了,更多相關(guān)Python圖像添加標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python六種基本數(shù)據(jù)類型及常用函數(shù)展示
這篇文章主要為大家介紹了python六種基本數(shù)據(jù)類型及常用函數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11
python+selenium實(shí)現(xiàn)自動(dòng)化百度搜索關(guān)鍵詞
在本篇文章里我們給大家分享了一篇關(guān)于python+selenium實(shí)現(xiàn)自動(dòng)化百度搜索關(guān)鍵詞的實(shí)例文章,需要的朋友們可以跟著操作下。2019-06-06
Python中 CSV格式清洗與轉(zhuǎn)換的實(shí)例代碼
這篇文章主要介紹了Python123 CSV格式清洗與轉(zhuǎn)換的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
OpenCV連通域數(shù)量統(tǒng)計(jì)學(xué)習(xí)示例
這篇文章主要為大家介紹了OpenCV連通域數(shù)量統(tǒng)計(jì)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Python中的命名元組簡單而強(qiáng)大的數(shù)據(jù)結(jié)構(gòu)示例詳解
namedtuple是Python中一個(gè)非常有用的數(shù)據(jù)結(jié)構(gòu),它提供了一種簡單的方式創(chuàng)建具有固定字段的輕量級(jí)對(duì)象,通過使用namedtuple,可以提高代碼的可讀性和可維護(hù)性,避免了使用類定義對(duì)象的復(fù)雜性,這篇文章主要介紹了Python中的命名元組簡單而強(qiáng)大的數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2024-05-05
Python實(shí)現(xiàn)的企業(yè)粉絲抽獎(jiǎng)功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的企業(yè)粉絲抽獎(jiǎng)功能,涉及Python數(shù)值運(yùn)算與隨機(jī)數(shù)生成相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
對(duì)Python中內(nèi)置異常層次結(jié)構(gòu)詳解
今天小編就為大家分享一篇對(duì)Python中內(nèi)置異常層次結(jié)構(gòu)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10

