Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForObjectDetection, AutoImageProcessor
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Lade das Modell und den Image Processor
|
| 8 |
+
model = AutoModelForObjectDetection.from_pretrained("facebook/dinov3-convnext-small-pretrain-lvd1689m")
|
| 9 |
+
image_processor = AutoImageProcessor.from_pretrained("facebook/dinov3-convnext-small-pretrain-lvd1689m")
|
| 10 |
+
|
| 11 |
+
def analyze_image(image, prompt):
|
| 12 |
+
# Konvertiere PIL-Bild zu OpenCV-Format
|
| 13 |
+
image_np = np.array(image)
|
| 14 |
+
image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
| 15 |
+
|
| 16 |
+
# Vorbereitung für DINOv3
|
| 17 |
+
inputs = image_processor(images=image_np, return_tensors="pt")
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
outputs = model(**inputs)
|
| 20 |
+
|
| 21 |
+
# Extrahiere Bounding-Boxes und Labels
|
| 22 |
+
detections = []
|
| 23 |
+
for score, label, box in zip(outputs.logits.softmax(-1).max(-1)[0], outputs.logits.softmax(-1).max(-1)[1], outputs.pred_boxes):
|
| 24 |
+
if score > 0.5: # Confidence-Schwellenwert
|
| 25 |
+
# Konvertiere Box-Koordinaten
|
| 26 |
+
box = box * torch.tensor([image_np.shape[1], image_np.shape[0], image_np.shape[1], image_np.shape[0]])
|
| 27 |
+
xmin, ymin, xmax, ymax = box.int().tolist()
|
| 28 |
+
|
| 29 |
+
# Schneide die Kerze für Farbanalyse
|
| 30 |
+
candle_roi = image_cv[ymin:ymax, xmin:xmax]
|
| 31 |
+
if candle_roi.size == 0: # Vermeide leere ROIs
|
| 32 |
+
continue
|
| 33 |
+
mean_color = np.mean(candle_roi, axis=(0, 1)).astype(int)
|
| 34 |
+
color_rgb = f"RGB({mean_color[2]},{mean_color[1]},{mean_color[0]})"
|
| 35 |
+
|
| 36 |
+
# Generische Labels, da DINOv3 nicht candlestick-spezifisch ist
|
| 37 |
+
detections.append({
|
| 38 |
+
"pattern": f"Candlestick_{label.item()}", # Placeholder
|
| 39 |
+
"confidence": score.item(),
|
| 40 |
+
"color": color_rgb,
|
| 41 |
+
"x_center": (xmin + xmax) / 2,
|
| 42 |
+
"prompt_used": prompt
|
| 43 |
+
})
|
| 44 |
+
|
| 45 |
+
# Sortiere nach x-Position (rechts nach links = neueste Kerzen)
|
| 46 |
+
detections = sorted(detections, key=lambda x: x["x_center"], reverse=True)
|
| 47 |
+
|
| 48 |
+
# Begrenze auf die letzten 10 Kerzen, wenn im Prompt gefordert
|
| 49 |
+
if "last 10 candles" in prompt.lower():
|
| 50 |
+
detections = detections[:10]
|
| 51 |
+
|
| 52 |
+
return detections
|
| 53 |
+
|
| 54 |
+
# Erstelle Gradio-Schnittstelle
|
| 55 |
+
iface = gr.Interface(
|
| 56 |
+
fn=analyze_image,
|
| 57 |
+
inputs=[
|
| 58 |
+
gr.Image(type="pil", label="Upload TradingView Screenshot"),
|
| 59 |
+
gr.Textbox(label="Prompt", placeholder="Enter your prompt, e.g., 'List last 10 candles with their colors'")
|
| 60 |
+
],
|
| 61 |
+
outputs="json",
|
| 62 |
+
title="Candlestick Pattern Detection with DINOv3",
|
| 63 |
+
description="Upload a TradingView screenshot and provide a prompt to detect candlestick patterns and colors."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
iface.launch()
|