doinglean commited on
Commit
cb9b58d
·
verified ·
1 Parent(s): a3c8d18

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import hf_hub_download
3
+ from ultralytics import YOLO
4
+ import cv2
5
+ import numpy as np
6
+ import easyocr
7
+
8
+ # Lade das Modell
9
+ model_path = hf_hub_download(repo_id="foduucom/stockmarket-pattern-detection-yolov8", filename="model.pt")
10
+ model = YOLO(model_path)
11
+
12
+ # OCR für Preise
13
+ reader = easyocr.Reader(['en'])
14
+
15
+ def analyze_image(image, prompt):
16
+ # Konvertiere PIL-Bild zu OpenCV-Format
17
+ image_np = np.array(image)
18
+ image_cv = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
19
+
20
+ # Führe Objekterkennung durch
21
+ results = model.predict(source=image_np, save=False)
22
+
23
+ # Extrahiere Kerzen
24
+ detections = []
25
+ for result in results:
26
+ for box in result.boxes:
27
+ label = result.names[int(box.cls)]
28
+ confidence = float(box.conf)
29
+ xmin, ymin, xmax, ymax = box.xyxy[0].tolist()
30
+
31
+ # Extrahiere Farbe
32
+ candle_roi = image_cv[int(ymin):int(ymax), int(xmin):int(xmax)]
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
+ # OCR für Opening/Close-Preise (aus Achsen, anpassen an Chart)
37
+ price_text = reader.readtext(image_cv[int(ymin):int(ymax), int(xmin):int(xmax)], detail=0)
38
+ prices = ' '.join(price_text) if price_text else "No price detected"
39
+
40
+ detections.append({
41
+ "pattern": label,
42
+ "confidence": confidence,
43
+ "color": color_rgb,
44
+ "prices": prices,
45
+ "x_center": (xmin + xmax) / 2
46
+ })
47
+
48
+ # Sortiere nach x-Position (rechts nach links = neueste Kerzen)
49
+ detections = sorted(detections, key=lambda x: x["x_center"], reverse=True)
50
+
51
+ # Begrenze auf die letzten 8 Kerzen
52
+ if "last 8 candles" in prompt.lower() or "letzte 8 kerzen" in prompt.lower():
53
+ detections = detections[:8]
54
+
55
+ return detections
56
+
57
+ iface = gr.Interface(
58
+ fn=analyze_image,
59
+ inputs=[
60
+ gr.Image(type="pil", label="Upload TradingView Screenshot"),
61
+ gr.Textbox(label="Prompt", placeholder="Enter your prompt, e.g., 'List last 8 candles with their colors'")
62
+ ],
63
+ outputs="json",
64
+ title="Stock Chart Analysis",
65
+ description="Upload a screenshot and provide a prompt to analyze candlesticks."
66
+ )
67
+
68
+ iface.launch()