Glas42 commited on
Commit
dd9d337
·
verified ·
1 Parent(s): f7863d0

Upload 2 files

Browse files
Files changed (2) hide show
  1. files/ExampleUsage.py +96 -0
  2. files/HowToUse.txt +2 -0
files/ExampleUsage.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ try:
2
+ import subprocess
3
+ except:
4
+ print('"subprocess" module is not available, exiting...')
5
+ exit()
6
+ try:
7
+ import sys
8
+ except:
9
+ print('"sys" module is not available, exiting...')
10
+ exit()
11
+ try:
12
+ import time
13
+ except:
14
+ print('"time" module is not available, exiting...')
15
+ exit()
16
+ try:
17
+ import os
18
+ except:
19
+ print('"os" module is not available, exiting...')
20
+ exit()
21
+ try:
22
+ import numpy as np
23
+ except:
24
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "numpy"])
25
+ import numpy as np
26
+ try:
27
+ import pyautogui
28
+ except:
29
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "pyautogui"])
30
+ import pyautogui
31
+ try:
32
+ import dxcam
33
+ except:
34
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "dxcam"])
35
+ import dxcam
36
+ try:
37
+ import torch
38
+ except:
39
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "torch"])
40
+ import torch
41
+ try:
42
+ import cv2
43
+ except:
44
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "opencv-python"])
45
+ import cv2
46
+ try:
47
+ import pathlib
48
+ except:
49
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "pathlib"])
50
+ import pathlib
51
+
52
+ temp = pathlib.PosixPath
53
+ pathlib.PosixPath = pathlib.WindowsPath
54
+
55
+ current_file_path = os.path.dirname(os.path.abspath(__file__))
56
+ model_path = os.path.join(current_file_path, 'best.pt')
57
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, force_reload=True)
58
+
59
+ screen_width, screen_height = pyautogui.size()
60
+ camera = dxcam.create(device_idx=0,output_color="RGB")
61
+
62
+ cv2.namedWindow('YOLOv5 Detection', cv2.WINDOW_NORMAL)
63
+ cv2.resizeWindow('YOLOv5 Detection', 960, 540)
64
+ cv2.setWindowProperty('YOLOv5 Detection', cv2.WND_PROP_TOPMOST, 1)
65
+
66
+ while True:
67
+ start_time = time.time()
68
+ frame = camera.grab(region=(0, 0, screen_width, screen_height))
69
+ if frame is None: continue
70
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
71
+
72
+ results = model(rgb_frame)
73
+
74
+ boxes = results.pandas().xyxy[0]
75
+
76
+ for _, box in boxes.iterrows():
77
+ label = box['name']
78
+ score = box['confidence']
79
+ x, y, w, h = int(box['xmin']), int(box['ymin']), int(box['xmax'] - box['xmin']), int(box['ymax'] - box['ymin'])
80
+
81
+ if label in ['map']:
82
+ cv2.rectangle(rgb_frame, (x, y), (x + w, y + h), (0, 255, 255), 3)
83
+ cv2.putText(rgb_frame, f"{score:.2f}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 255), 2, cv2.LINE_AA)
84
+ if label in ['arrow']:
85
+ cv2.rectangle(rgb_frame, (x, y), (x + w, y + h), (255, 0, 0), 3)
86
+ cv2.putText(rgb_frame, f"{score:.2f}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 0, 0), 2, cv2.LINE_AA)
87
+
88
+ fps = round(1 / (time.time() - start_time), 1)
89
+ cv2.putText(rgb_frame, f"fps: {fps}", (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2, cv2.LINE_AA)
90
+ cv2.imshow('YOLOv5 Detection', rgb_frame)
91
+ cv2.resizeWindow('YOLOv5 Detection', 854, 480)
92
+
93
+ if cv2.waitKey(1) & 0xFF == ord('q'):
94
+ break
95
+
96
+ cv2.destroyAllWindows()
files/HowToUse.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ The model contains 2 classes: "map" and "arrow"
2
+ You can run the ExampleUsage.py to test the model.