|
|
import gradio as gr |
|
|
import cv2 |
|
|
import numpy as np |
|
|
from ultralytics import YOLO |
|
|
from ultralytics.nn.tasks import DetectionModel |
|
|
import torch.serialization |
|
|
|
|
|
|
|
|
torch.serialization.add_safe_globals([DetectionModel]) |
|
|
|
|
|
|
|
|
model = YOLO("yolov8n.pt") |
|
|
|
|
|
def analyze_image(image, prompt): |
|
|
|
|
|
if "what do you see" not in prompt.lower() and "was siehst du" not in prompt.lower(): |
|
|
return "Nicht unterstützter Prompt. Verwende 'Was siehst du auf dem Bild?'" |
|
|
|
|
|
|
|
|
image_np = np.array(image) |
|
|
image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR) |
|
|
|
|
|
|
|
|
results = model.predict(source=image_np, conf=0.3, iou=0.5) |
|
|
objects = [] |
|
|
|
|
|
for r in results: |
|
|
boxes = r.boxes.xyxy.cpu().numpy() |
|
|
labels = r.boxes.cls.cpu().numpy() |
|
|
class_names = model.names |
|
|
for box, label in zip(boxes, labels): |
|
|
class_name = class_names[int(label)] |
|
|
objects.append(class_name) |
|
|
|
|
|
|
|
|
if not objects: |
|
|
return "Keine Objekte erkannt. Es sieht aus wie ein Chart oder ein anderes Bild ohne klar erkennbare Objekte." |
|
|
|
|
|
object_count = {} |
|
|
for obj in objects: |
|
|
object_count[obj] = object_count.get(obj, 0) + 1 |
|
|
|
|
|
response = "Ich sehe folgende Objekte im Bild:\n" |
|
|
for obj, count in object_count.items(): |
|
|
response += f"- {obj} ({count}x)\n" |
|
|
|
|
|
return response |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=analyze_image, |
|
|
inputs=[ |
|
|
gr.Image(type="pil", label="Bild hochladen"), |
|
|
gr.Textbox(label="Prompt", placeholder="z. B. 'Was siehst du auf dem Bild?'") |
|
|
], |
|
|
outputs="text", |
|
|
title="Einfache Bildanalyse mit YOLOv8", |
|
|
description="Lade ein Bild hoch und frage 'Was siehst du auf dem Bild?' für eine Beschreibung." |
|
|
) |
|
|
|
|
|
iface.launch() |