-
Notifications
You must be signed in to change notification settings - Fork 0
/
YOLO.py
317 lines (258 loc) · 8.18 KB
/
YOLO.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# This code is used for WEB requests to return inference results
import base64
import threading
import time
import torch
import numpy as np
import os
import sys
from models.common import DetectMultiBackend
from utils.general import (Profile, check_img_size, cv2, non_max_suppression, scale_boxes)
from utils.torch_utils import select_device
from utils.augmentations import letterbox
from gevent.pywsgi import WSGIServer
from threading import Thread
from flask import Flask, request
import urllib.request
import mmap
import ctypes
import ctypes.wintypes
import win32api
import win32con
import win32gui
import win32ui
lock = threading.Lock()
jcqbh = "1"
weights = 'yolov5n.pt'
IMGSZ = [640, 640]
max_det = 1000
conf_thres = 0.1
iou_thres = 0.45
device_ = ''
dk = 7700
temp_device = ''
geshu = len(sys.argv)
if geshu < 6:
print('参数不足,启动失败,2秒后自动退出')
time.sleep(2)
sys.exit()
else:
jcqbh = sys.argv[1]
SIZE = sys.argv[2]
IMGSZ = [int(SIZE), int(SIZE)]
dk = int(sys.argv[3])
temp_device = sys.argv[4]
if temp_device == 'aidmlm':
device_ = ''
else:
device_ = temp_device
weights = sys.argv[5]
pid = os.getpid()
print('jcqbh', jcqbh)
print('weights', weights)
print('IMGSZ', IMGSZ)
def Window_Shot(hwnd):
if win32gui.IsWindow(hwnd) == False:
hwnd = win32gui.GetDesktopWindow()
MoniterDev = win32api.EnumDisplayMonitors(None, None)
w = MoniterDev[0][2][2]
h = MoniterDev[0][2][3]
else:
ret = win32gui.GetClientRect(hwnd)
w = ret[2]
h = ret[3]
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
saveDC.SelectObject(saveBitMap)
saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY)
signedIntsArray = saveBitMap.GetBitmapBits(True)
im_opencv = np.frombuffer(signedIntsArray, dtype='uint8')
im_opencv.shape = (h, w, 4)
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)
return im_opencv
def get_model():
start_time = time.time()
device1 = select_device(device_)
model1 = DetectMultiBackend(weights, device=device1)
stride1, names1, pt1 = model1.stride, model1.names, model1.pt
end_time = time.time()
print("模型加载完成,耗时", int((end_time-start_time)*1000), "毫秒")
print('')
return device1, model1, stride1, names1, pt1
device, model, stride, names, pt = get_model()
imgsz = check_img_size(IMGSZ, s=stride)
model.warmup(imgsz=(1, 3, *imgsz))
@torch.no_grad()
def yuce(img0):
if hasattr(img0, 'shape') == False:
return ''
lock.acquire()
seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
im0 = img0
if im0 is None:
return ''
im = letterbox(im0, imgsz, stride=stride, auto=pt)[0]
im = im.transpose((2, 0, 1))[::-1]
im = np.ascontiguousarray(im)
with dt[0]:
im = torch.from_numpy(im).to(model.device)
im = im.half() if model.fp16 else im.float()
im /= 255
if len(im.shape) == 3:
im = im[None]
with dt[1]:
pred = model(im, augment=False, visualize=False)
with dt[2]:
pred = non_max_suppression(pred, conf_thres, iou_thres, None, False, max_det=max_det)
det = pred[0]
xywh_list = ''
if len(det):
det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round()
for *xyxy, conf, cls in reversed(det):
c = int(cls)
x1 = int(xyxy[0])
y1 = int(xyxy[1])
x2 = int(xyxy[2])
y2 = int(xyxy[3])
w = abs(x2-x1)
h = abs(y2-y1)
zxd = float(conf)
xywh = str(c) + "," + str(x1) + "," + str(y1) + "," + str(w) + "," + str(h) + "," + str(zxd) + ',' + str(names[c]) + '|'
xywh_list = xywh_list + xywh
xywh_list = xywh_list.encode('utf-8')
lock.release()
return xywh_list
def bytesToMat(img):
np_arr = np.frombuffer(bytearray(img), dtype=np.uint8)
return cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
app = Flask(__name__)
@app.route('/pid', methods=['GET', 'POST'])
def YOLOv5_WEB_pid():
return str(pid)
@app.route('/pic', methods=['GET', 'POST'])
def YOLOv5_WEB_pic():
if request.data == b'':
return ''
try:
tp = request.get_data()
img = bytesToMat(tp)
jieguo = yuce(img)
return jieguo
except:
return ''
@app.route('/hwnd',methods=['GET', 'POST'])
def YOLOv5_WEB_hwnd():
try:
tp = request.get_data()
sj = tp.decode('utf-8', "ignore")
jb = int(float(sj))
img = Window_Shot(jb)
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
jieguo = yuce(img)
return jieguo
except:
return ''
@app.route('/base64', methods=['GET', 'POST'])
def YOLOv5_WEB_base64():
try:
tp = request.get_data()
tp_wb = tp.decode('utf-8', "ignore")
b_tp = base64.b64decode(tp_wb)
img = bytesToMat(b_tp)
jieguo = yuce(img)
return jieguo
except:
return ''
@app.route('/file',methods=['GET', 'POST'])
def YOLOv5_WEB_file():
try:
tp = request.get_data()
tp_file = tp.decode('utf-8', "ignore")
img = cv2.imdecode(np.fromfile(file=tp_file, dtype=np.uint8), -1)
jieguo = yuce(img)
return jieguo
except:
return ''
def qidong():
print('使用端口号' + str(dk) + '启动WEB服务器')
print('')
WSGIServer(('0.0.0.0', dk), app, log=None).serve_forever() # log=None
SendMessage = ctypes.windll.user32.SendMessageA
class COPYDATASTRUCT(ctypes.Structure):
_fields_ = [
('dwData', ctypes.wintypes.LPARAM),
('cbData', ctypes.wintypes.DWORD),
('lpData', ctypes.c_void_p)
]
PCOPYDATASTRUCT = ctypes.POINTER(COPYDATASTRUCT)
class Listener:
def __init__(self):
WindowName = "aidmlm.com" + jcqbh
message_map = {
win32con.WM_COPYDATA: self.OnCopyData
}
wc = win32gui.WNDCLASS()
wc.lpfnWndProc = message_map
wc.lpszClassName = WindowName
hinst = wc.hInstance = win32api.GetModuleHandle(None)
classAtom = win32gui.RegisterClass(wc)
self.hwnd = win32gui.CreateWindow(
classAtom,
"aidmlm.com" + jcqbh,
0,
0,
0,
win32con.CW_USEDEFAULT,
win32con.CW_USEDEFAULT,
0,
0,
hinst,
None
)
print('pid', pid)
print("hwnd", self.hwnd)
Thread(target=qidong).start()
def OnCopyData(self, hwnd, msg, wparam, lparam):
pCDS = ctypes.cast(lparam, PCOPYDATASTRUCT)
s = ctypes.string_at(pCDS.contents.lpData, pCDS.contents.cbData).decode() # "utf-8", "ignore"
cd = int(float(s))
if wparam == 1:
file_name = 'aidmlm.com' + jcqbh
shmem = mmap.mmap(0, cd, file_name, mmap.ACCESS_WRITE) # python读取共享内存
tp = shmem.read(cd) # 从共享内存读取共享数据
img = bytesToMat(tp)
jieguo = yuce(img)
# 将jieguo写到共享内存
jgcd = len(jieguo)
shmem.seek(0)
shmem.write(jieguo)
shmem.close() # 关闭映射
return jgcd
if wparam == 0:
return pid
if wparam == 2:
jb = cd
if win32gui.IsWindow(jb) == False:
return 11
# img = screen.grabWindow(jb).toImage()
# img = convertQImageToMat(img) # QImage转Mat格式
img = Window_Shot(jb)
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
jieguo = yuce(img)
file_name = 'aidmlm.com' + jcqbh
shmem = mmap.mmap(0, 51200, file_name, mmap.ACCESS_WRITE) # python读取共享内存
# 将jieguo写到共享内存
jgcd = len(jieguo)
shmem.seek(0)
shmem.write(jieguo)
shmem.close() # 关闭映射
return jgcd
return 10
l = Listener()
win32gui.PumpMessages()