A-A+

python 图片识别工具源码 (微软API需密钥)

2024年03月28日 14:41 精品软件 暂无评论 共3284字 (阅读151 views次)

python 图片识别工具源码 (微软API需密钥)

import tkinter as tk
from tkinter import filedialog, scrolledtext
from azure.ai.vision.imageanalysis import ImageAnalysisClient
from azure.ai.vision.imageanalysis.models import VisualFeatures
from azure.core.credentials import AzureKeyCredential
import threading


class ImageTranslatorApp:
    def __init__(self, master):
        self.master = master
        self.master.title("图片识别工具(需联网)")

        self.select_button = tk.Button(master, text="选择图片", command=self.select_image)
        self.select_button.grid(row=0, column=0, padx=5, pady=5)

        self.selected_image_label = tk.Label(master, text="")
        self.selected_image_label.grid(row=1, column=0, columnspan=2, padx=5, pady=5)

        self.translate_button = tk.Button(master, text="识别", command=self.translate_image)
        self.translate_button.grid(row=0, column=1, padx=5, pady=5)

        self.read_var = tk.BooleanVar(value=True)
        self.read_checkbutton = tk.Checkbutton(master, text="图片内容识别", variable=self.read_var)
        self.read_checkbutton.grid(row=2, column=1, padx=5, pady=2, sticky=tk.W)

        self.caption_var = tk.BooleanVar(value=True)
        self.caption_checkbutton = tk.Checkbutton(master, text="智能分析图片", variable=self.caption_var)
        self.caption_checkbutton.grid(row=2, column=0, padx=5, pady=2, sticky=tk.W)

        self.tags_var = tk.BooleanVar()
        self.tags_checkbutton = tk.Checkbutton(master, text="图片标签", variable=self.tags_var)
        self.tags_checkbutton.grid(row=3, column=0, padx=5, pady=2, sticky=tk.W)

        self.objects_var = tk.BooleanVar()
        self.objects_checkbutton = tk.Checkbutton(master, text="对象坐标分析", variable=self.objects_var)
        self.objects_checkbutton.grid(row=3, column=1, padx=5, pady=2, sticky=tk.W)

        self.dense_captions_var = tk.BooleanVar()
        self.dense_captions_checkbutton = tk.Checkbutton(master, text="详细对象坐标分析",
                                                         variable=self.dense_captions_var)
        self.dense_captions_checkbutton.grid(row=4, column=0, padx=5, pady=2, sticky=tk.W)

        self.smart_crops_var = tk.BooleanVar()
        self.smart_crops_checkbutton = tk.Checkbutton(master, text="智能裁剪分析", variable=self.smart_crops_var)
        self.smart_crops_checkbutton.grid(row=4, column=1, padx=5, pady=2, sticky=tk.W)

        self.people_var = tk.BooleanVar()
        self.people_checkbutton = tk.Checkbutton(master, text="人\物坐标分析", variable=self.people_var)
        self.people_checkbutton.grid(row=5, column=0, padx=5, pady=2, sticky=tk.W)

        self.textbox = scrolledtext.ScrolledText(master, height=18, width=60)
        self.textbox.grid(row=6, column=0, columnspan=2, padx=5, pady=5)
        self.client = ImageAnalysisClient(endpoint="https://这里是接口.cognitiveservices.azure.com/",
                                          credential=AzureKeyCredential("这里是密钥"))

    def select_image(self):
        file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.gif;*.bmp")])
        if file_path:
            self.selected_image = file_path
            self.selected_image_label.config(text="已选择图片:" + self.selected_image)

    def translate_image(self):
        if hasattr(self, 'selected_image'):
            self.textbox.delete(1.0, tk.END)
            self.textbox.insert(tk.END, f"识别中,请稍等……")

            # Get visual features from the checkbuttons
            selected_features = [
                VisualFeatures.CAPTION if self.caption_var.get() else None,
                VisualFeatures.READ if self.read_var.get() else None,
                VisualFeatures.TAGS if self.tags_var.get() else None,
                VisualFeatures.OBJECTS if self.objects_var.get() else None,
                VisualFeatures.DENSE_CAPTIONS if self.dense_captions_var.get() else None,
                VisualFeatures.SMART_CROPS if self.smart_crops_var.get() else None,
                VisualFeatures.PEOPLE if self.people_var.get() else None
            ]

            # Remove None values
            selected_features = [feature for feature in selected_features if feature is not None]

            if len(selected_features) <= 0:
                self.textbox.delete(1.0, tk.END)
                self.textbox.insert(tk.END, "必须勾选一个选项进行识别。")
            else:
                threading.Thread(target=self.analyze_image, args=(selected_features,)).start()

        else:
            self.textbox.delete(1.0, tk.END)
            self.textbox.insert(tk.END, "请先选择一张图片。")

    def analyze_image(self, selected_features):
        try:
            with open(self.selected_image, 'rb') as image_file:
                image_data = image_file.read()

            result = self.client.analyze(
                image_data=image_data,
                visual_features=selected_features,
                gender_neutral_caption=False,  # Optional (default is False)
                # language="zh-Hans"
            )

            # Process result and update GUI
            self.process_result(result)

        except Exception as e:
            self.textbox.delete(1.0, tk.END)
            self.textbox.insert(tk.END, f"出现错误:{e}")

    def process_result(self, result):
        pic_orc_result = ""
        if result.caption is not None:
            pic_orc_result = f"【智能分析图片:】\n'{result.caption.text}', 准确值:{result.caption.confidence:.4f}" + "\n\n"
        if result.read is not None:
            pic_orc_result = pic_orc_result + "【图片内容识别:】\n"
            for line in result.read.blocks[0].lines:
                pic_orc_result = pic_orc_result + (f"{line.text}\n")
                # for word in line.words:
                #     print(
                #         f"     Word: '{word.text}', Bounding polygon {word.bounding_polygon}, Confidence {word.confidence:.4f}")
            pic_orc_result = pic_orc_result + "\n\n"
        if result.tags is not None:
            pic_orc_result = pic_orc_result + "【图片标签:】\n"
            for tag in result.tags.list:
                pic_orc_result = pic_orc_result + (f"{tag.name}\n")
            pic_orc_result = pic_orc_result + "\n\n"
        if result.objects is not None:
            pic_orc_result = pic_orc_result + "【对象坐标分析:】\n"
            for object in result.objects.list:
                pic_orc_result = pic_orc_result + (f"'{object.tags[0].name}', {object.bounding_box}\n")
            pic_orc_result = pic_orc_result + "\n\n"
        if result.dense_captions is not None:
            pic_orc_result = pic_orc_result + "【详细对象坐标分析:】\n"
            for caption in result.dense_captions.list:
                pic_orc_result = pic_orc_result + (f"'{caption.text}', {caption.bounding_box}\n")
            pic_orc_result = pic_orc_result + "\n\n"
        if result.smart_crops is not None:
            pic_orc_result = pic_orc_result + "【裁剪坐标及宽高比建议:】\n"
            for smart_crop in result.smart_crops.list:
                pic_orc_result = pic_orc_result + (
                    f"Aspect ratio {smart_crop.aspect_ratio}: Smart crop {smart_crop.bounding_box}\n")
            pic_orc_result = pic_orc_result + "\n\n"
        if result.people is not None:
            pic_orc_result = pic_orc_result + "【人\物坐标分析:】\n"
            for person in result.people.list:
                pic_orc_result = pic_orc_result + (f"{person.bounding_box}\n")
            pic_orc_result = pic_orc_result + "\n\n"
        if pic_orc_result == "":
            self.textbox.delete(1.0, tk.END)
            self.textbox.insert(tk.END, "未识别出任何内容,无法识别图像。")
        else:
            self.textbox.delete(1.0, tk.END)
            self.textbox.insert(tk.END, pic_orc_result)


def main():
    root = tk.Tk()
    app = ImageTranslatorApp(root)
    root.mainloop()


if __name__ == "__main__":
    main()

布施恩德可便相知重

微信扫一扫打赏

支付宝扫一扫打赏

×

给我留言