Repository navigation
Expand file tree
/
Copy pathpitchDetection.py
More file actions
202 lines (156 loc) · 6.67 KB
/
Copy pathpitchDetection.py
File metadata and controls
202 lines (156 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#pitchDetection.py
import os
import cv2
import torch
import numpy as np
from numpy import random
from models.experimental import attempt_load
from utils.dataloaders import letterbox
from utils.general import check_img_size, check_requirements, non_max_suppression, scale_boxes
from utils.plots import plot_one_box
from utils.torch_utils import select_device
WEIGHTS = os.getcwd() + '/models/' + 'best2.pt'
IMG_SIZE = 640
DEVICE = ''
AUGMENT = False
CONF_THRES = 0.5
IOU_THRES = 0.45
CLASSES = None
AGNOSTIC_NMS = False
# stave_list가 들어올 경우 result_list에서
def detect(image):
weights, imgsz = WEIGHTS, IMG_SIZE
source = image
result_list=[] # 인식 결과를 담은 list 생성
# Initialize
device = select_device('cpu') # 일단 CPU defalut로
# device = select_device(DEVICE)
half = device.type != 'cpu' # half precision only supported on CUDA
# print('device:', device)
# Load model
model = attempt_load(weights)
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(imgsz, s=stride) # check img_size
if half:
model.half() # to FP16
# Get names and colors
names = model.module.names if hasattr(model, 'module') else model.names
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
# Run inference
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
# Load image
img0 = image # BGR
assert img0 is not None, 'Image Not Found ' + source
# Padded resize
img = letterbox(img0, imgsz, stride=stride)[0]
# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
pred = model(img, augment=AUGMENT)[0]
# print('pred shape:', pred.shape)
# Apply NMS
pred = non_max_suppression(pred, CONF_THRES, IOU_THRES, classes=CLASSES, agnostic=AGNOSTIC_NMS)
# Process detections
det = pred[0]
# print('det shape:', det.shape)
s = ''
s += '%gx%g ' % img.shape[2:] # print string
if len(det):
# Rescale boxes from img_size to img0 size
det[:, :4] = scale_boxes(img.shape[2:], det[:, :4], img0.shape).round()
# Print results
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
# Write results
sorted_det = sorted(det, key=lambda x: (x[0] + x[2]) / 2) # x 중점을 기준으로 정렬
for *xyxy, conf, cls in sorted_det:
label = f'{names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, img0, label=label, color=colors[int(cls)], line_thickness=3)
# 추가: bounding box의 중점 좌표와 확률(label 이름 포함) 출력
x_center = xyxy[0]
y_center = (xyxy[1] + xyxy[3]) / 2
x_center, y_center = round(float(x_center), 2), round(float(y_center), 2) # 텐서를 숫자로 변환 및 라운딩
# print(f'Box Center: ({x_center}, {y_center}), Confidence: {conf:.2f}, Class: {names[int(cls)]}') # 인식 결과를 x 좌표 순서대로 정렬
result_list.append([y_center, names[int(cls)], x_center])
# print(f'Inferencing and Processing Done.')
#print(result_list)
# Stream results
# print(s)
return result_list
def detect1(image):
imgsz = 1280
source = image
weights = os.getcwd() + '/models/' + 'masking.pt'
result_list=[] # 인식 결과를 담은 list 생성
# Initialize
device = select_device('cpu') # 일단 CPU defalut로
# device = select_device(DEVICE)
half = device.type != 'cpu' # half precision only supported on CUDA
# print('device:', device)
# Load model
model = attempt_load(weights)
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(imgsz, s=stride) # check img_size
if half:
model.half() # to FP16
# Get names and colors
names = model.module.names if hasattr(model, 'module') else model.names
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
# Run inference
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
# Load image
img0 = image # BGR
assert img0 is not None, 'Image Not Found ' + source
# Padded resize
img = letterbox(img0, imgsz, stride=stride)[0]
# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
pred = model(img, augment=AUGMENT)[0]
# print('pred shape:', pred.shape)
# Apply NMS
pred = non_max_suppression(pred, 0.4, IOU_THRES, classes=[9, 20], agnostic=AGNOSTIC_NMS)
# Process detections
det = pred[0]
# print('det shape:', det.shape)
s = ''
s += '%gx%g ' % img.shape[2:] # print string
if len(det):
# Rescale boxes from img_size to img0 size
det[:, :4] = scale_boxes(img.shape[2:], det[:, :4], img0.shape).round()
# Print results
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
# Write results
sorted_det = sorted(det, key=lambda y: (y[0] + y[2]) / 2) # x 중점을 기준으로 정렬
for *xyxy, conf, cls in sorted_det:
label = f'{names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, img0, label=label, color=colors[int(cls)], line_thickness=3)
# 추가: bounding box의 중점 좌표와 확률(label 이름 포함) 출력
x_center = xyxy[0]
y_center = (xyxy[1] + xyxy[3]) / 2
x_center, y_center = round(float(x_center), 2), round(float(y_center), 2) # 텐서를 숫자로 변환 및 라운딩
# print(f'Box Center: ({x_center}, {y_center}), Confidence: {conf:.2f}, Class: {names[int(cls)]}') # 인식 결과를 x 좌표 순서대로 정렬
result_list.append([[y_center, names[int(cls)], 0]])
# print(f'Inferencing and Processing Done.')
#print(result_list)
# Stream results
# print(s)
result_list.sort(key=lambda x: x[0]) # Y축 기준 정렬
return result_list