File size: 3,281 Bytes
c1b6e10 0835b33 c1b6e10 c7ca595 c1b6e10 c7ca595 c1b6e10 c7ca595 c1b6e10 c7ca595 c1b6e10 c7ca595 c1b6e10 c7ca595 d0e76dd c7ca595 d0e76dd c7ca595 d0e76dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import gradio as gr
import cv2
import numpy as np
from ultralytics import YOLO
import easyocr
# Lade YOLOv8-Modell
model = YOLO("yolov8n.pt", weights_only=False) # Nano-Modell, weights_only=False für einfaches Laden
# Lade EasyOCR
reader = easyocr.Reader(['en'], gpu=False) # Englisch, CPU für Free Tier
def analyze_image(image, prompt):
# Konvertiere PIL-Bild zu numpy-Format
image_np = np.array(image)
image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
# Allgemeine Bildbeschreibung
if "what do you see" in prompt.lower() or "was siehst du" in prompt.lower():
return "Ein Candlestick-Chart mit grünen und roten Kerzen, Preisen auf der y-Achse und weißem Hintergrund."
# Kerzen-Analyse
elif "last 8 candles" in prompt.lower() or "letzte 8 kerzen" in prompt.lower():
# YOLOv8 für Kerzen-Erkennung
results = model.predict(source=image_np, conf=0.3, iou=0.5)
detections = []
for r in results:
boxes = r.boxes.xyxy.cpu().numpy()
for box in boxes:
# Filter für Kerzen (schmal und hoch)
xmin, ymin, xmax, ymax = map(int, box)
if (ymax - ymin) / (xmax - xmin) > 2: # Verhältnis für Kerzen
candle_roi = image_cv[ymin:ymax, xmin:xmax]
if candle_roi.size == 0:
continue
mean_color = np.mean(candle_roi, axis=(0, 1)).astype(int)
color_rgb = f"RGB({mean_color[2]},{mean_color[1]},{mean_color[0]})"
# OCR für Preise
price_roi = image_cv[max(0, ymin-200):min(image_np.shape[0], ymax+200),
max(0, xmin-200):min(image_np.shape[1], xmax+200)]
ocr_results = reader.readtext(price_roi, detail=0)
prices = " ".join(ocr_results) if ocr_results else "Kein Preis erkannt"
detections.append({
"Kerze": f"Farbe: {color_rgb}, Preise: {prices}",
"x_center": (xmin + xmax) / 2
})
# Sortiere nach x-Position (rechts nach links = neueste Kerzen)
detections = sorted(detections, key=lambda x: x["x_center"], reverse=True)[:8]
if not detections:
return "Keine Kerzen erkannt. Bitte lade ein klares Bild mit sichtbaren Kerzen und Preisen hoch."
# Textantwort erstellen
response = "Letzte 8 Kerzen:\n"
for i, detection in enumerate(detections, 1):
response += f"Kerze {i}: {detection['Kerze']}\n"
return response
else:
return "Nicht unterstützter Prompt. Verwende 'Was siehst du auf dem Bild?' oder 'List last 8 candles with their colors'."
# Erstelle Gradio-Schnittstelle
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?' oder 'List last 8 candles with their colors'")
],
outputs="text",
title="Einfache Chart-Analyse mit YOLOv8",
description="Lade einen TradingView-Screenshot hoch, um Kerzen zu analysieren oder eine Beschreibung zu erhalten."
)
iface.launch() |