|
|
|
import os |
|
import mss |
|
import cv2 |
|
import numpy as np |
|
import time |
|
import glob |
|
from ultralytics import YOLO |
|
from openpyxl import Workbook |
|
|
|
|
|
save_path = "./" |
|
screenshots_path = os.path.join(save_path, "screenshots") |
|
detect_path = os.path.join(save_path, "runs/detect/") |
|
|
|
os.makedirs(save_path, exist_ok=True) |
|
os.makedirs(screenshots_path, exist_ok=True) |
|
|
|
|
|
classes = ['Head and shoulders bottom', 'Head and shoulders top', 'M_Head', 'StockLine', 'Triangle', 'W_Bottom'] |
|
|
|
|
|
model_path = "model.pt" |
|
if not os.path.exists(model_path): |
|
raise FileNotFoundError(f"Model file not found: {model_path}") |
|
model = YOLO(model_path) |
|
|
|
|
|
monitor = {"top": 0, "left": 683, "width": 683, "height": 768} |
|
|
|
|
|
excel_file = os.path.join(save_path, "classification_results.xlsx") |
|
wb = Workbook() |
|
ws = wb.active |
|
ws.append(["Timestamp", "Predicted Image Path", "Label"]) |
|
|
|
|
|
video_path = "./video/annotated_video.mp4" |
|
fourcc = cv2.VideoWriter_fourcc(*"mp4v") |
|
fps = 0.5 |
|
video_writer = None |
|
|
|
|
|
with mss.mss() as sct: |
|
start_time = time.time() |
|
last_capture_time = start_time |
|
frame_count = 0 |
|
|
|
while True: |
|
|
|
sct_img = sct.grab(monitor) |
|
img = np.array(sct_img) |
|
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) |
|
|
|
|
|
current_time = time.time() |
|
if current_time - last_capture_time >= 60: |
|
|
|
timestamp = time.strftime("%Y-%m-%d %H:%M:%S") |
|
image_name = f"predicted_images_{timestamp}_{frame_count}.png" |
|
image_path = os.path.join(screenshots_path, image_name) |
|
cv2.imwrite(image_path, img) |
|
|
|
|
|
results = model(image_path, save=True) |
|
predict_path = results[0].save_dir if results else None |
|
|
|
|
|
if predict_path and os.path.exists(predict_path): |
|
annotated_images = sorted(glob.glob(os.path.join(predict_path, "*.jpg")), key=os.path.getmtime, reverse=True) |
|
final_image_path = annotated_images[0] if annotated_images else image_path |
|
else: |
|
final_image_path = image_path |
|
|
|
|
|
if results and results[0].boxes: |
|
class_indices = results[0].boxes.cls.tolist() |
|
predicted_label = classes[int(class_indices[0])] |
|
else: |
|
predicted_label = "No pattern detected" |
|
|
|
|
|
ws.append([timestamp, final_image_path, predicted_label]) |
|
|
|
|
|
annotated_img = cv2.imread(final_image_path) |
|
if annotated_img is not None: |
|
|
|
font = cv2.FONT_HERSHEY_SIMPLEX |
|
cv2.putText(annotated_img, f"{timestamp}", (10, 30), font, 0.7, (0, 255, 0), 2, cv2.LINE_AA) |
|
cv2.putText(annotated_img, f"{predicted_label}", (10, 60), font, 0.7, (0, 255, 255), 2, cv2.LINE_AA) |
|
|
|
|
|
if video_writer is None: |
|
height, width, layers = annotated_img.shape |
|
video_writer = cv2.VideoWriter(video_path, fourcc, fps, (width, height)) |
|
|
|
video_writer.write(annotated_img) |
|
|
|
print(f"Frame {frame_count}: {final_image_path} -> {predicted_label}") |
|
frame_count += 1 |
|
|
|
|
|
last_capture_time = current_time |
|
|
|
|
|
wb.save(excel_file) |
|
|
|
|
|
cv2.imshow("Screen Capture", img) |
|
|
|
|
|
if cv2.waitKey(1) & 0xFF == ord('q'): |
|
break |
|
|
|
|
|
if video_writer is not None: |
|
video_writer.release() |
|
print(f"Video saved at {video_path}") |
|
|
|
|
|
for file in os.scandir(screenshots_path): |
|
os.remove(file.path) |
|
os.rmdir(screenshots_path) |
|
|
|
print(f"Results saved to {excel_file}") |
|
|
|
|
|
cv2.destroyAllWindows() |