Update README.md
Browse files
README.md
CHANGED
@@ -34,7 +34,7 @@ pipeline_tag: object-detection
|
|
34 |
<img width="500" alt="foduucom/stockmarket-pattern-detection-yolov8" src="https://huggingface.co/foduucom/stockmarket-pattern-detection-yolov8/resolve/main/thumbnail.jpg">
|
35 |
</div>
|
36 |
|
37 |
-
# Model Card for YOLOv8s Stock Market Pattern Detection from Live Screen Capture
|
38 |
|
39 |
## Model Summary
|
40 |
|
@@ -84,15 +84,15 @@ Users should be aware of the model's limitations and potential biases. Testing a
|
|
84 |
|
85 |
## How to Get Started with the Model
|
86 |
|
87 |
-
To begin using the YOLOv8s Stock Market Pattern Detection model, install the necessary libraries:
|
88 |
```bash
|
89 |
-
pip install opencv-python==4.11.0.86 numpy==2.1.3
|
90 |
```
|
91 |
|
92 |
### Screen Capture and Pattern Detection Implementation
|
93 |
```python
|
94 |
import os
|
95 |
-
import mss
|
96 |
import cv2
|
97 |
import numpy as np
|
98 |
import time
|
@@ -100,11 +100,15 @@ import glob
|
|
100 |
from ultralytics import YOLO
|
101 |
from openpyxl import Workbook
|
102 |
|
103 |
-
#
|
104 |
home_dir = os.path.expanduser("~")
|
|
|
|
|
105 |
save_path = os.path.join(home_dir, "yolo_detection")
|
106 |
screenshots_path = os.path.join(save_path, "screenshots")
|
107 |
detect_path = os.path.join(save_path, "runs", "detect")
|
|
|
|
|
108 |
os.makedirs(screenshots_path, exist_ok=True)
|
109 |
os.makedirs(detect_path, exist_ok=True)
|
110 |
|
@@ -112,49 +116,96 @@ os.makedirs(detect_path, exist_ok=True)
|
|
112 |
classes = ['Head and shoulders bottom', 'Head and shoulders top', 'M_Head', 'StockLine', 'Triangle', 'W_Bottom']
|
113 |
|
114 |
# Load YOLOv8 model
|
115 |
-
|
|
|
|
|
|
|
116 |
|
117 |
# Define screen capture region
|
118 |
monitor = {"top": 0, "left": 683, "width": 683, "height": 768}
|
119 |
|
120 |
-
# Create Excel file
|
121 |
excel_file = os.path.join(save_path, "classification_results.xlsx")
|
122 |
wb = Workbook()
|
123 |
ws = wb.active
|
124 |
-
ws.append(["Timestamp", "Predicted Image Path", "Label"])
|
125 |
|
126 |
# Initialize video writer
|
127 |
video_path = os.path.join(save_path, "annotated_video.mp4")
|
128 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
129 |
-
fps = 0.5
|
130 |
video_writer = None
|
131 |
|
132 |
# Start capturing
|
133 |
with mss.mss() as sct:
|
134 |
start_time = time.time()
|
135 |
frame_count = 0
|
136 |
-
|
137 |
while time.time() - start_time < 60:
|
|
|
138 |
sct_img = sct.grab(monitor)
|
139 |
img = np.array(sct_img)
|
140 |
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
|
|
|
|
|
141 |
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
|
142 |
image_name = f"predicted_images_{timestamp}_{frame_count}.png"
|
143 |
image_path = os.path.join(screenshots_path, image_name)
|
144 |
cv2.imwrite(image_path, img)
|
145 |
-
|
|
|
146 |
results = model(image_path, save=True)
|
147 |
predict_path = results[0].save_dir if results else None
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
ws.append([timestamp, final_image_path, predicted_label])
|
153 |
-
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
frame_count += 1
|
156 |
time.sleep(5)
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
print(f"Results saved to {excel_file}")
|
159 |
```
|
160 |
|
@@ -164,7 +215,7 @@ For inquiries and contributions, please contact us at [email protected].
|
|
164 |
```bibtex
|
165 |
@ModelCard{
|
166 |
author = {Nehul Agrawal,
|
167 |
-
Pranjal Singh Thakur, Arjun Singh},
|
168 |
title = {YOLOv8s Stock Market Pattern Detection from Live Screen Capture},
|
169 |
year = {2023}
|
170 |
}
|
|
|
34 |
<img width="500" alt="foduucom/stockmarket-pattern-detection-yolov8" src="https://huggingface.co/foduucom/stockmarket-pattern-detection-yolov8/resolve/main/thumbnail.jpg">
|
35 |
</div>
|
36 |
|
37 |
+
# Model Card for YOLOv8s Stock Market Real Time Pattern Detection from Live Screen Capture
|
38 |
|
39 |
## Model Summary
|
40 |
|
|
|
84 |
|
85 |
## How to Get Started with the Model
|
86 |
|
87 |
+
To begin using the YOLOv8s Stock Market Real Time Pattern Detection model, install the necessary libraries:
|
88 |
```bash
|
89 |
+
pip install mss==10.0.0 opencv-python==4.11.0.86 numpy==2.1.3 ultralytics==8.3.94 openpyxl==3.1.5
|
90 |
```
|
91 |
|
92 |
### Screen Capture and Pattern Detection Implementation
|
93 |
```python
|
94 |
import os
|
95 |
+
import mss # type: ignore
|
96 |
import cv2
|
97 |
import numpy as np
|
98 |
import time
|
|
|
100 |
from ultralytics import YOLO
|
101 |
from openpyxl import Workbook
|
102 |
|
103 |
+
# Get the user's home directory
|
104 |
home_dir = os.path.expanduser("~")
|
105 |
+
|
106 |
+
# Define dynamic paths
|
107 |
save_path = os.path.join(home_dir, "yolo_detection")
|
108 |
screenshots_path = os.path.join(save_path, "screenshots")
|
109 |
detect_path = os.path.join(save_path, "runs", "detect")
|
110 |
+
|
111 |
+
# Ensure necessary directories exist
|
112 |
os.makedirs(screenshots_path, exist_ok=True)
|
113 |
os.makedirs(detect_path, exist_ok=True)
|
114 |
|
|
|
116 |
classes = ['Head and shoulders bottom', 'Head and shoulders top', 'M_Head', 'StockLine', 'Triangle', 'W_Bottom']
|
117 |
|
118 |
# Load YOLOv8 model
|
119 |
+
model_path = "foduucom/stockmarket-pattern-detection-yolov8"
|
120 |
+
if not os.path.exists(model_path):
|
121 |
+
raise FileNotFoundError(f"Model file not found: {model_path}")
|
122 |
+
model = YOLO(model_path)
|
123 |
|
124 |
# Define screen capture region
|
125 |
monitor = {"top": 0, "left": 683, "width": 683, "height": 768}
|
126 |
|
127 |
+
# Create an Excel file
|
128 |
excel_file = os.path.join(save_path, "classification_results.xlsx")
|
129 |
wb = Workbook()
|
130 |
ws = wb.active
|
131 |
+
ws.append(["Timestamp", "Predicted Image Path", "Label"]) # Headers
|
132 |
|
133 |
# Initialize video writer
|
134 |
video_path = os.path.join(save_path, "annotated_video.mp4")
|
135 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
136 |
+
fps = 0.5 # Adjust frames per second as needed
|
137 |
video_writer = None
|
138 |
|
139 |
# Start capturing
|
140 |
with mss.mss() as sct:
|
141 |
start_time = time.time()
|
142 |
frame_count = 0
|
143 |
+
|
144 |
while time.time() - start_time < 60:
|
145 |
+
# Capture the screen region
|
146 |
sct_img = sct.grab(monitor)
|
147 |
img = np.array(sct_img)
|
148 |
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
|
149 |
+
|
150 |
+
# Save the frame in the screenshots folder
|
151 |
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
|
152 |
image_name = f"predicted_images_{timestamp}_{frame_count}.png"
|
153 |
image_path = os.path.join(screenshots_path, image_name)
|
154 |
cv2.imwrite(image_path, img)
|
155 |
+
|
156 |
+
# Run YOLO model and get save directory
|
157 |
results = model(image_path, save=True)
|
158 |
predict_path = results[0].save_dir if results else None
|
159 |
+
|
160 |
+
# Find the latest annotated image inside predict_path
|
161 |
+
if predict_path and os.path.exists(predict_path):
|
162 |
+
annotated_images = sorted(glob.glob(os.path.join(predict_path, "*.jpg")), key=os.path.getmtime, reverse=True)
|
163 |
+
final_image_path = annotated_images[0] if annotated_images else image_path
|
164 |
+
else:
|
165 |
+
final_image_path = image_path # Fallback to original image
|
166 |
+
|
167 |
+
# Determine predicted label
|
168 |
+
if results and results[0].boxes:
|
169 |
+
class_indices = results[0].boxes.cls.tolist()
|
170 |
+
predicted_label = classes[int(class_indices[0])]
|
171 |
+
else:
|
172 |
+
predicted_label = "No pattern detected"
|
173 |
+
|
174 |
+
# Insert data into Excel (store path instead of image)
|
175 |
ws.append([timestamp, final_image_path, predicted_label])
|
176 |
+
|
177 |
+
# Read the image for video processing
|
178 |
+
annotated_img = cv2.imread(final_image_path)
|
179 |
+
if annotated_img is not None:
|
180 |
+
# Add timestamp and label text to the image
|
181 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
182 |
+
cv2.putText(annotated_img, f"{timestamp}", (10, 30), font, 0.7, (0, 255, 0), 2, cv2.LINE_AA)
|
183 |
+
cv2.putText(annotated_img, f"{predicted_label}", (10, 60), font, 0.7, (0, 255, 255), 2, cv2.LINE_AA)
|
184 |
+
|
185 |
+
# Initialize video writer if not already initialized
|
186 |
+
if video_writer is None:
|
187 |
+
height, width, layers = annotated_img.shape
|
188 |
+
video_writer = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
|
189 |
+
|
190 |
+
video_writer.write(annotated_img)
|
191 |
+
|
192 |
+
print(f"Frame {frame_count}: {final_image_path} -> {predicted_label}")
|
193 |
frame_count += 1
|
194 |
time.sleep(5)
|
195 |
+
|
196 |
+
# Save the Excel file
|
197 |
+
wb.save(excel_file)
|
198 |
+
|
199 |
+
# Release video writer
|
200 |
+
if video_writer is not None:
|
201 |
+
video_writer.release()
|
202 |
+
print(f"Video saved at {video_path}")
|
203 |
+
|
204 |
+
# Remove all files in screenshots directory
|
205 |
+
for file in os.scandir(screenshots_path):
|
206 |
+
os.remove(file.path)
|
207 |
+
os.rmdir(screenshots_path)
|
208 |
+
|
209 |
print(f"Results saved to {excel_file}")
|
210 |
```
|
211 |
|
|
|
215 |
```bibtex
|
216 |
@ModelCard{
|
217 |
author = {Nehul Agrawal,
|
218 |
+
Pranjal Singh Thakur, and Arjun Singh},
|
219 |
title = {YOLOv8s Stock Market Pattern Detection from Live Screen Capture},
|
220 |
year = {2023}
|
221 |
}
|