|
|
import gradio as gr |
|
|
import cv2 |
|
|
import numpy as np |
|
|
from ultralytics import YOLO |
|
|
import easyocr |
|
|
|
|
|
|
|
|
model = YOLO("yolov8n.pt", weights_only=False) |
|
|
|
|
|
|
|
|
reader = easyocr.Reader(['en'], gpu=False) |
|
|
|
|
|
def analyze_image(image, prompt): |
|
|
|
|
|
image_np = np.array(image) |
|
|
image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR) |
|
|
|
|
|
|
|
|
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." |
|
|
|
|
|
|
|
|
elif "last 8 candles" in prompt.lower() or "letzte 8 kerzen" in prompt.lower(): |
|
|
|
|
|
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: |
|
|
|
|
|
xmin, ymin, xmax, ymax = map(int, box) |
|
|
if (ymax - ymin) / (xmax - xmin) > 2: |
|
|
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]})" |
|
|
|
|
|
|
|
|
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 |
|
|
}) |
|
|
|
|
|
|
|
|
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." |
|
|
|
|
|
|
|
|
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'." |
|
|
|
|
|
|
|
|
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() |