利用Python實現(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***************' # 注冊網(wǎng)址:https://www.clarifai.com/ # 網(wǎng)址注冊后,拿授權(quán)密鑰,每月1000次免費 def analyze_image(image_path): # 使用 Clarifai API 進行圖像分析 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("錯誤", "圖像分析失敗,原因: " + str(e)) return if post_model_outputs_response.status.code != status_code_pb2.SUCCESS: messagebox.showerror("錯誤", "模型輸出提交失敗,狀態(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實現(xiàn)給圖像添加標(biāo)簽的文章就介紹到這了,更多相關(guān)Python圖像添加標(biāo)簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python六種基本數(shù)據(jù)類型及常用函數(shù)展示
這篇文章主要為大家介紹了python六種基本數(shù)據(jù)類型及常用函數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11python+selenium實現(xiàn)自動化百度搜索關(guān)鍵詞
在本篇文章里我們給大家分享了一篇關(guān)于python+selenium實現(xiàn)自動化百度搜索關(guān)鍵詞的實例文章,需要的朋友們可以跟著操作下。2019-06-06Python中 CSV格式清洗與轉(zhuǎn)換的實例代碼
這篇文章主要介紹了Python123 CSV格式清洗與轉(zhuǎn)換的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08OpenCV連通域數(shù)量統(tǒng)計學(xué)習(xí)示例
這篇文章主要為大家介紹了OpenCV連通域數(shù)量統(tǒng)計示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06Python中的命名元組簡單而強大的數(shù)據(jù)結(jié)構(gòu)示例詳解
namedtuple是Python中一個非常有用的數(shù)據(jù)結(jié)構(gòu),它提供了一種簡單的方式創(chuàng)建具有固定字段的輕量級對象,通過使用namedtuple,可以提高代碼的可讀性和可維護性,避免了使用類定義對象的復(fù)雜性,這篇文章主要介紹了Python中的命名元組簡單而強大的數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2024-05-05對Python中內(nèi)置異常層次結(jié)構(gòu)詳解
今天小編就為大家分享一篇對Python中內(nèi)置異常層次結(jié)構(gòu)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10