doinglean commited on
Commit
b65ef32
·
verified ·
1 Parent(s): c7ca595

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -55
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", weights_only=False) # Nano-Modell, weights_only=False für einfaches Laden
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
- # Allgemeine Bildbeschreibung
19
- if "what do you see" in prompt.lower() or "was siehst du" in prompt.lower():
20
- return "Ein Candlestick-Chart mit grünen und roten Kerzen, Preisen auf der y-Achse und weißem Hintergrund."
21
-
22
- # Kerzen-Analyse
23
- elif "last 8 candles" in prompt.lower() or "letzte 8 kerzen" in prompt.lower():
24
- # YOLOv8 für Kerzen-Erkennung
25
- results = model.predict(source=image_np, conf=0.3, iou=0.5)
26
- detections = []
27
-
28
- for r in results:
29
- boxes = r.boxes.xyxy.cpu().numpy()
30
- for box in boxes:
31
- # Filter für Kerzen (schmal und hoch)
32
- xmin, ymin, xmax, ymax = map(int, box)
33
- if (ymax - ymin) / (xmax - xmin) > 2: # Verhältnis für Kerzen
34
- candle_roi = image_cv[ymin:ymax, xmin:xmax]
35
- if candle_roi.size == 0:
36
- continue
37
- mean_color = np.mean(candle_roi, axis=(0, 1)).astype(int)
38
- color_rgb = f"RGB({mean_color[2]},{mean_color[1]},{mean_color[0]})"
39
-
40
- # OCR für Preise
41
- price_roi = image_cv[max(0, ymin-200):min(image_np.shape[0], ymax+200),
42
- max(0, xmin-200):min(image_np.shape[1], xmax+200)]
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?' oder 'List last 8 candles with their colors'")
72
  ],
73
  outputs="text",
74
- title="Einfache Chart-Analyse mit YOLOv8",
75
- description="Lade einen TradingView-Screenshot hoch, um Kerzen zu analysieren oder eine Beschreibung zu erhalten."
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()