import gradio as gr from huggingface_hub import hf_hub_download from ultralytics import YOLO import cv2 import numpy as np import easyocr # Lade das Modell model_path = hf_hub_download(repo_id="foduucom/stockmarket-pattern-detection-yolov8", filename="model.pt") model = YOLO(model_path) # OCR für Preise reader = easyocr.Reader(['en']) def analyze_image(image, prompt): # Konvertiere PIL-Bild zu OpenCV-Format image_np = np.array(image) image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR) # Führe Objekterkennung durch results = model.predict(source=image_np, save=False) # Extrahiere Kerzen detections = [] for result in results: for box in result.boxes: label = result.names[int(box.cls)] confidence = float(box.conf) xmin, ymin, xmax, ymax = box.xyxy[0].tolist() # Extrahiere Farbe candle_roi = image_cv[int(ymin):int(ymax), int(xmin):int(xmax)] 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 Opening/Close-Preise (aus Achsen, anpassen an Chart) price_text = reader.readtext(image_cv[int(ymin):int(ymax), int(xmin):int(xmax)], detail=0) prices = ' '.join(price_text) if price_text else "No price detected" detections.append({ "pattern": label, "confidence": confidence, "color": color_rgb, "prices": 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) # Begrenze auf die letzten 8 Kerzen if "last 8 candles" in prompt.lower() or "letzte 8 kerzen" in prompt.lower(): detections = detections[:8] return detections iface = gr.Interface( fn=analyze_image, inputs=[ gr.Image(type="pil", label="Upload TradingView Screenshot"), gr.Textbox(label="Prompt", placeholder="Enter your prompt, e.g., 'List last 8 candles with their colors'") ], outputs="json", title="Stock Chart Analysis", description="Upload a screenshot and provide a prompt to analyze candlesticks." ) iface.launch()