SICHQONCHA YORDAMIDA TASVIRGA TAVSIF BERISH TEXNOLOGIYALARI PYTHON DASTURI
Pythonda
Microsoft Azure Computer Vision API kabi oddiy tasvirni qayta ishlash va tavsif kutubxonasidan foydalanaylik.
Kutubxonalarni o'rnatish: Sizda kutubxonalar pillow(tasvirni qayta ishlash uchun) va requests(Azure Computer Vision API ga so'rov yuborish uchun) o'rnatilganligiga ishonch hosil qiling. Siz ularni buyruqlar yordamida o'rnatishingiz mumkin:
pip install pillow
pip install requests
Namunaviy dastur:
import tkinter as tk
from PIL import Image, ImageDraw
import requests
from io import BytesIO
class DrawingApp:
def __init__(self, root):
self.root = root
self.root.title("Drawing and Description App")
self.canvas = tk.Canvas(root, bg="white", width=400, height=400)
self.canvas.pack()
self.image = Image.new("RGB", (400, 400), "white")
self.draw = ImageDraw.Draw(self.image)
self.canvas.bind("", self.paint)
self.description_text = tk.Text(root, height=5, width=50)
self.description_text.pack()
self.generate_button = tk.Button(root, text="Generate Description", command=self.generate_description)
self.generate_button.pack()
def paint(self, event):
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
self.canvas.create_oval(x1, y1, x2, y2, fill="black", width=2)
self.draw.line([x1, y1, x2, y2], fill="black", width=2)
def generate_description(self):
# Chizilgan rasmni saqlang
image_path = "drawing.png"
self.image.save(image_path)
# Tasvirni tahlil qilish uchun Azure Computer Vision API-ga yuboring
subscription_key = 'YOUR_AZURE_SUBSCRIPTION_KEY'
endpoint = 'YOUR_AZURE_ENDPOINT'
analyze_url = f"{endpoint}/vision/v3.1/analyze"
headers = {
'Ocp-Apim-Subscription-Key': subscription_key,
'Content-Type': 'application/octet-stream',
}
with open(image_path, 'rb') as image_file:
response = requests.post(analyze_url, headers=headers, files={'file': image_file})
# Biz tahlil natijasini olamiz va tavsifni matn maydonida ko'rsatamiz
result = response.json()
description = ', '.join([tag['name'] for tag in result['description']['tags']])
self.description_text.delete(1.0, tk.END)
self.description_text.insert(tk.END, description)
if __name__ == "__main__":
root = tk.Tk()
app = DrawingApp(root)
root.mainloop()
Ushbu misol tasvirni tahlil qilish va aniqlangan teglar asosida tavsif yaratish uchun Azure Computer Vision API-dan foydalanadi. Azure hisob ma'lumotlaringiz bilan 'YOUR_AZURE_SUBSCRIPTION_KEY' almashtiring. 'YOUR_AZURE_ENDPOINT'
HTML+CSS+JawaSript
HTML va CSS:
Image Description on Hover
|