forked from supmine/robocup2022-cv-yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyolov5.py
219 lines (182 loc) · 7.32 KB
/
yolov5.py
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# implement class of yolov5
import torch
import json
import os
from pathlib import Path
import socket
import sys
import cv2
import numpy as np
from models.common import DetectMultiBackend
from utils.augmentations import letterbox
from utils.general import non_max_suppression, scale_coords
from utils.plots import Annotator, colors
from utils.torch_utils import select_device, time_sync
import matplotlib.pyplot as plt
from custom_socket import CustomSocket
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # YOLOv5 root directory
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd()))
# path
CONFIG_PATH = 'config/'
WEIGHTS_PATH = 'weight_object.pt'
NAMES_PATH = CONFIG_PATH + 'coco.names'
DEVICE = 0
CFG_PATH = CONFIG_PATH + 'yolor_p6.cfg'
IMAGE_SIZE = 640
class ObjectDetection:
def __init__(self,
weights=WEIGHTS_PATH,
data=ROOT / 'data/coco128.yaml', # dataset
device=DEVICE,
half=False,
hide_labels=False,
hide_conf=False,
):
self.device = select_device(device)
model = DetectMultiBackend(weights, device=self.device, dnn=False, data=data, fp16=half)
stride, names, pt = model.stride, model.names, model.pt
self.model = model
self.hide_labels = hide_labels
self.hide_conf = hide_conf
self.names = names
self.img_size = IMAGE_SIZE
self.stride = stride
self.pt = pt
def load_classes(self, path):
# Loads *.names file at 'path'
with open(path, 'r') as f:
names = f.read().split('\n')
# filter removes empty strings (such as last line)
return list(filter(None, names))
def detect(self, input_image):
print('orignal shape', input_image.shape)
bbox_list = []
im = self.preprocess(input_image)
print(im.shape)
print("recieving image with shape {}".format(im.shape))
dt, seen = [0.0, 0.0, 0.0], 0
t1 = time_sync()
im = torch.from_numpy(im).to(self.device)
im = im.half() if self.model.fp16 else im.float() # uint8 to fp16/32
im /= 255 # 0 - 255 to 0.0 - 1.0
if len(im.shape) == 3:
im = im[None] # expand for batch dim
t2 = time_sync()
dt[0] += t2 - t1
# Inference
print("Inferencing ...")
pred = self.model(im)
t3 = time_sync()
dt[1] += t3 - t2
# NMS
pred = non_max_suppression(pred, conf_thres=0.25, iou_thres=0.45,
classes=None, agnostic=False, max_det=1000)
dt[2] += time_sync() - t3
# Process predictions
for i, det in enumerate(pred): # per image
annotator = Annotator(input_image, line_width=3, example=str(self.names))
# s += '%gx%g ' % im.shape[2:]
if det is not None and len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(
im.shape[2:], det[:, :4], input_image.shape).round()
# Print results
# for c in det[:, -1].unique():
# n = (det[:, -1] == c).sum() # detections per class
# s += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string
# Write results
for *xyxy, conf, cls in reversed(det):
c = int(cls) # integer class
label = None if self.hide_labels else (
self.names[c] if self.hide_conf else f'{self.names[c]} {conf:.2f}')
annotator.box_label(xyxy, label, color=colors(c, True))
return input_image
def preprocess(self, img):
img = letterbox(img, new_shape=self.img_size, stride=self.stride, auto=self.pt)[0]
img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
img = np.ascontiguousarray(img)
return img
def get_bbox(self, input_image):
print('orignal shape', input_image.shape)
bbox_list = []
im = self.preprocess(input_image)
print(im.shape)
print("recieving image with shape {}".format(im.shape))
dt, seen = [0.0, 0.0, 0.0], 0
t1 = time_sync()
im = torch.from_numpy(im).to(self.device)
im = im.half() if self.model.fp16 else im.float() # uint8 to fp16/32
im /= 255 # 0 - 255 to 0.0 - 1.0
if len(im.shape) == 3:
im = im[None] # expand for batch dim
t2 = time_sync()
dt[0] += t2 - t1
# Inferences
print("Inferencing ...")
with torch.no_grad():
pred = self.model(im)
t3 = time_sync()
dt[1] += t3 - t2
# NMS
pred = non_max_suppression(pred, conf_thres=0.25, iou_thres=0.45,
classes=None, agnostic=False, max_det=1000)
dt[2] += time_sync() - t3
# Process predictions
for i, det in enumerate(pred): # per image
# s += '%gx%g ' % im.shape[2:]
if det is not None and len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(
im.shape[2:], det[:, :4], input_image.shape).round()
# Print results
# for c in det[:, -1].unique():
# n = (det[:, -1] == c).sum() # detections per class
# s += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string
for *xyxy, conf, cls in det:
temp = []
for ts in xyxy:
temp.append(ts.item())
bbox = list(np.array(temp).astype(int))
bbox.append(self.names[int(cls)])
bbox_list.append(bbox)
return bbox_list
def main():
# HOST = socket.gethostname()
HOST = socket.gethostname()
PORT = 10001
server = CustomSocket(HOST, PORT)
server.startServer()
while True :
conn, addr = server.sock.accept()
print("Client connected from",addr)
OD = ObjectDetection()
results = []
bbox_list = []
result = None
x, y, w, h = 0, 0, 0, 0
name = ""
res = {}
while True :
try :
data = server.recvMsg(conn)
img = np.frombuffer(data,dtype=np.uint8).reshape(720,1280,3)
results = OD.get_bbox(img)
bbox_list = []
for result in results :
x, y, w, h = [int(e) for e in result[:4]]
name = result[-1]
bbox_list.append((x,y,w,h,name))
res = {"n" : len(results), "bbox_list" : bbox_list}
print("send")
server.sendMsg(conn,json.dumps(res))
except Exception as e :
print(e)
print("Connection Closed")
del OD, results, bbox_list, result, x, y, w, h, name, res
torch.cuda.empty_cache()
break
if __name__ == '__main__':
main()