Update app.py
Browse files
app.py
CHANGED
|
@@ -2,77 +2,55 @@ import gradio as gr
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from ultralytics import YOLO
|
| 5 |
-
import easyocr
|
| 6 |
|
| 7 |
# Lade YOLOv8-Modell
|
| 8 |
-
model = YOLO("yolov8n.pt"
|
| 9 |
-
|
| 10 |
-
# Lade EasyOCR
|
| 11 |
-
reader = easyocr.Reader(['en'], gpu=False) # Englisch, CPU für Free Tier
|
| 12 |
|
| 13 |
def analyze_image(image, prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Konvertiere PIL-Bild zu numpy-Format
|
| 15 |
image_np = np.array(image)
|
| 16 |
image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
ocr_results = reader.readtext(price_roi, detail=0)
|
| 44 |
-
prices = " ".join(ocr_results) if ocr_results else "Kein Preis erkannt"
|
| 45 |
-
|
| 46 |
-
detections.append({
|
| 47 |
-
"Kerze": f"Farbe: {color_rgb}, Preise: {prices}",
|
| 48 |
-
"x_center": (xmin + xmax) / 2
|
| 49 |
-
})
|
| 50 |
-
|
| 51 |
-
# Sortiere nach x-Position (rechts nach links = neueste Kerzen)
|
| 52 |
-
detections = sorted(detections, key=lambda x: x["x_center"], reverse=True)[:8]
|
| 53 |
-
|
| 54 |
-
if not detections:
|
| 55 |
-
return "Keine Kerzen erkannt. Bitte lade ein klares Bild mit sichtbaren Kerzen und Preisen hoch."
|
| 56 |
-
|
| 57 |
-
# Textantwort erstellen
|
| 58 |
-
response = "Letzte 8 Kerzen:\n"
|
| 59 |
-
for i, detection in enumerate(detections, 1):
|
| 60 |
-
response += f"Kerze {i}: {detection['Kerze']}\n"
|
| 61 |
-
return response
|
| 62 |
-
|
| 63 |
-
else:
|
| 64 |
-
return "Nicht unterstützter Prompt. Verwende 'Was siehst du auf dem Bild?' oder 'List last 8 candles with their colors'."
|
| 65 |
|
| 66 |
# Erstelle Gradio-Schnittstelle
|
| 67 |
iface = gr.Interface(
|
| 68 |
fn=analyze_image,
|
| 69 |
inputs=[
|
| 70 |
gr.Image(type="pil", label="Bild hochladen"),
|
| 71 |
-
gr.Textbox(label="Prompt", placeholder="z. B. 'Was siehst du auf dem Bild?'
|
| 72 |
],
|
| 73 |
outputs="text",
|
| 74 |
-
title="Einfache
|
| 75 |
-
description="Lade
|
| 76 |
)
|
| 77 |
|
| 78 |
iface.launch()
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from ultralytics import YOLO
|
|
|
|
| 5 |
|
| 6 |
# Lade YOLOv8-Modell
|
| 7 |
+
model = YOLO("yolov8n.pt") # Nano-Modell, Standard-Laden
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def analyze_image(image, prompt):
|
| 10 |
+
# Prüfe Prompt
|
| 11 |
+
if "what do you see" not in prompt.lower() and "was siehst du" not in prompt.lower():
|
| 12 |
+
return "Nicht unterstützter Prompt. Verwende 'Was siehst du auf dem Bild?'"
|
| 13 |
+
|
| 14 |
# Konvertiere PIL-Bild zu numpy-Format
|
| 15 |
image_np = np.array(image)
|
| 16 |
image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
| 17 |
|
| 18 |
+
# YOLOv8 für Objekterkennung
|
| 19 |
+
results = model.predict(source=image_np, conf=0.3, iou=0.5)
|
| 20 |
+
objects = []
|
| 21 |
+
|
| 22 |
+
for r in results:
|
| 23 |
+
boxes = r.boxes.xyxy.cpu().numpy()
|
| 24 |
+
labels = r.boxes.cls.cpu().numpy()
|
| 25 |
+
class_names = model.names # Klassen-Namen aus dem Modell
|
| 26 |
+
for box, label in zip(boxes, labels):
|
| 27 |
+
class_name = class_names[int(label)]
|
| 28 |
+
objects.append(class_name)
|
| 29 |
+
|
| 30 |
+
# Erstelle Textbeschreibung
|
| 31 |
+
if not objects:
|
| 32 |
+
return "Keine Objekte erkannt. Es sieht aus wie ein Chart oder ein anderes Bild ohne klar erkennbare Objekte."
|
| 33 |
+
|
| 34 |
+
object_count = {}
|
| 35 |
+
for obj in objects:
|
| 36 |
+
object_count[obj] = object_count.get(obj, 0) + 1
|
| 37 |
+
|
| 38 |
+
response = "Ich sehe folgende Objekte im Bild:\n"
|
| 39 |
+
for obj, count in object_count.items():
|
| 40 |
+
response += f"- {obj} ({count}x)\n"
|
| 41 |
+
|
| 42 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Erstelle Gradio-Schnittstelle
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=analyze_image,
|
| 47 |
inputs=[
|
| 48 |
gr.Image(type="pil", label="Bild hochladen"),
|
| 49 |
+
gr.Textbox(label="Prompt", placeholder="z. B. 'Was siehst du auf dem Bild?'")
|
| 50 |
],
|
| 51 |
outputs="text",
|
| 52 |
+
title="Einfache Bildanalyse mit YOLOv8",
|
| 53 |
+
description="Lade ein Bild hoch und frage 'Was siehst du auf dem Bild?' für eine Beschreibung."
|
| 54 |
)
|
| 55 |
|
| 56 |
iface.launch()
|