Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Chart Pattern Detection API — YOLOv8 on HuggingFace Spaces"""
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from ultralyticsplus import YOLO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
model = YOLO("foduucom/stockmarket-pattern-detection-yolov8")
|
| 8 |
+
model.overrides["conf"] = 0.25
|
| 9 |
+
model.overrides["iou"] = 0.45
|
| 10 |
+
|
| 11 |
+
def detect_patterns(image):
|
| 12 |
+
if image is None:
|
| 13 |
+
return json.dumps({"patterns": [], "error": "No image"})
|
| 14 |
+
results = model.predict(image)
|
| 15 |
+
patterns = []
|
| 16 |
+
for r in results:
|
| 17 |
+
boxes = r.boxes
|
| 18 |
+
for i in range(len(boxes)):
|
| 19 |
+
box = boxes[i]
|
| 20 |
+
cls_id = int(box.cls[0])
|
| 21 |
+
conf = float(box.conf[0])
|
| 22 |
+
xyxy = box.xyxy[0].tolist()
|
| 23 |
+
label = r.names[cls_id]
|
| 24 |
+
patterns.append({
|
| 25 |
+
"label": label,
|
| 26 |
+
"confidence": round(conf, 3),
|
| 27 |
+
"bbox": [round(x, 1) for x in xyxy],
|
| 28 |
+
})
|
| 29 |
+
patterns.sort(key=lambda p: p["confidence"], reverse=True)
|
| 30 |
+
return json.dumps({"patterns": patterns, "count": len(patterns)})
|
| 31 |
+
|
| 32 |
+
demo = gr.Interface(
|
| 33 |
+
fn=detect_patterns,
|
| 34 |
+
inputs=gr.Image(type="pil", label="Chart Image"),
|
| 35 |
+
outputs=gr.Textbox(label="Detected Patterns (JSON)"),
|
| 36 |
+
title="Chart Pattern Detection — YOLOv8",
|
| 37 |
+
description="Detects: Head & Shoulders (Top/Bottom), Double Top/Bottom, Triangles, Trend Lines",
|
| 38 |
+
)
|
| 39 |
+
demo.launch()
|