This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
map_insert.py
219 lines (218 loc) · 9.53 KB
/
map_insert.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
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QImage
import sys
import time
import win32gui
import win32api
import win32print
import win32con
import re
import pickle
import os
from get_search_data import inputandfind
import delete_mark
import keyboard
import win32com.client
import pythoncom
import pic_locate
MIN_MATCH_COUNT = 10
surf = cv.xfeatures2d.SURF_create()
surf.setUpright(True)
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 1)
search_params = dict(checks = 1)
flann = cv.FlannBasedMatcher(index_params, search_params)
base_dir = os.path.dirname(os.path.abspath(__file__))
with open(r'{0}\cache\kplist.bin'.format(base_dir), 'rb') as f:
kplist=pickle.load(f)
kp2=[]
for kp in kplist:
kp2.append(cv.KeyPoint(kp['pt'][0],kp['pt'][1],kp['size'],kp['angle'],kp['octave'],kp['class_id']))
des2=np.load(r'{0}\cache\des2.npy'.format(base_dir))
hwnd = win32gui.FindWindow('UnityWndClass', None)
app = QApplication(sys.argv)
global lastDeleteTime
lastDeleteTime=0
def findplace(pname):
coorlist = []
with open(r"{0}\data\{1}.txt".format(base_dir,pname),mode='r',encoding='utf8') as f:
data=f.readlines()
for dt in data:
rs=re.search(r'([-0-9.]+).*?([-0-9.]+)',dt,flags=0)
a=(float(rs.group(1))*0.390*1.5+1426,float(rs.group(2))*0.3911*1.5+3015)
coorlist.append(a)
return coorlist
def clk(x,y):
time.sleep(0.3)
win32api.SetCursorPos((x, y))
time.sleep(0.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
time.sleep(0.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
def listname():
file_name = os.listdir(r'{0}\data'.format(base_dir))
print(file_name)
def cmpcenter(i):
return (i[0]-i[2])**2+(i[1]-i[3])**2
def GetDeletedList():
with open(r'{0}\cache\deletedlist.txt'.format(base_dir),'r',encoding='utf-8') as f:
deletedlist=[]
rawlist=f.readlines()
for l in rawlist:
rl=l.split(' ')
deletedlist.append([int(rl[0]),int(rl[1]),float(rl[2])])
return deletedlist
def oper(select_name,notecnt):
global lastDeleteTime
if time.time()-lastDeleteTime>300:
delete_mark.DeleteOutOfRefreshTime()
lastDeleteTime=time.time()
print(lastDeleteTime)
lst1=[]
with open(r'{0}\cache\cache_alreadymarked.bin'.format(base_dir), 'rb') as f:
if os.path.getsize(r'{0}\cache\cache_alreadymarked.bin'.format(base_dir)):
lst1=pickle.load(f)
screen = QApplication.primaryScreen()
pix=screen.grabWindow(hwnd).toImage().convertToFormat(QImage.Format.Format_RGBA8888)
width = pix.width()
height = pix.height()
rect = win32gui.GetWindowRect(hwnd)
hDC = win32gui.GetDC(0)
w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES)
pscale=w/win32api.GetSystemMetrics(0)
xf=int(rect[2]/pscale)-width
yf=int(rect[3]/pscale)-height
ptr = pix.bits()
ptr.setsize(height * width * 4)
img = np.frombuffer(ptr, np.uint8).reshape((height, width, 4))
img1=cv.cvtColor(img,cv.COLOR_RGB2GRAY)
kp1, des1 = surf.detectAndCompute(img1,None)
matches = flann.knnMatch(des1,des2,k=2)
good = []
for m,n in matches:
if m.distance < 0.7*n.distance:
good.append(m)
if len(good)>MIN_MATCH_COUNT:
src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
M, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC,5.0)
h,w= img1.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv.perspectiveTransform(pts,M)
xlength=dst[2][0][0]-dst[0][0][0]
ylength=dst[2][0][1]-dst[0][0][1]
scale=(xlength/img1.shape[1]+ylength/img1.shape[0])/2.0
#win32gui.ShowWindow(hwnd, win32con.SW_SHOW)
pythoncom.CoInitialize()
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(hwnd)
time.sleep(0.1)
pic_locate.OpenMap(hwnd)
for i in range(10):
if pic_locate.SetBtnON(hwnd)==0:break
time.sleep(0.2)
ltt=findplace(select_name[1])
lst=[]
for l in ltt:
lst.append([l[0],l[1],(dst[2][0][0]+dst[0][0][0])/2.0,(dst[2][0][1]+dst[0][0][1])/2.0])
lst.sort(key=cmpcenter)
denylist=findplace('deny1')
cnt=0
for ls in lst:
x1,y1=ls[:2]
flag=0
#判断范围
for l in lst1:
if (x1-l[0])**2+(y1-l[1])**2<2000*scale*scale and select_name[1]==l[2]:
flag=1
break
if flag==0:
for l in denylist:
if (x1-l[0])**2+(y1-l[1])**2<2500*scale*scale:
flag=1
break
b1=(x1-dst[0][0][0])/scale<350/1920.0*img1.shape[1] and (y1-dst[0][0][1])/scale<200/1080.0*img1.shape[0]
b2=(x1-dst[0][0][0])/scale>1347/1920.0*img1.shape[1] and (y1-dst[0][0][1])/scale<82/1080.0*img1.shape[0]
b3=(x1-dst[0][0][0])/scale<140/1920.0*img1.shape[1] and (y1-dst[0][0][1])/scale>960/1080.0*img1.shape[0]
b4=(x1-dst[0][0][0])/scale>1667/1920.0*img1.shape[1] and (y1-dst[0][0][1])/scale>967/1080.0*img1.shape[0]
if b1 or b2 or b3 or b4:
flag=1
b5=x1>dst[0][0][0] and x1<dst[2][0][0] and y1>dst[0][0][1] and y1<dst[2][0][1]
if b5 and flag==0 and cnt<notecnt:
lst1.append([ls[0],ls[1],select_name[1]])
cnt+=1
xps=int((x1-dst[0][0][0])/scale+xf)
yps=int((y1-dst[0][0][1])/scale+yf)
clk(xps,yps)
for i in range(20):
if pic_locate.ClickBtn (hwnd)==0:break
time.sleep(0.03)
for i in range(10):
if pic_locate.SetBtnOFF(hwnd)==0:break
time.sleep(0.1)
with open(r'{0}\cache\cache_alreadymarked.bin'.format(base_dir), 'wb') as f:
pickle.dump(lst1,f)
return
def notecnt(select_name):
flag=0
while flag==0:
cnt=input('请输入标点个数:\n')
cnt=int(cnt)
lst=findplace(select_name[1])
if cnt>len(lst) or cnt<= 0:
print('范围错误')
return -1
else:
flag=1
return cnt
def MarkMap(kp2,des2):
global lastDeleteTime
if time.time()-lastDeleteTime>300:
delete_mark.DeleteOutOfRefreshTime()
lastDeleteTime=time.time()
print('hello')
print(lastDeleteTime)
pythoncom.CoInitialize()
mainhwnd = win32gui.FindWindow('ConsoleWindowClass', None)
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(mainhwnd)
bdir=os.path.dirname(os.path.abspath(__file__))
with open(r'{0}\cache\cache_lstdata.bin'.format(bdir), 'rb') as f:
if os.path.getsize(r'{0}\cache\cache_lstdata.bin'.format(bdir)):
lstdata=pickle.load(f)
else:
lstdata=[0,0]
select_name=inputandfind()
while select_name[0]==0:
select_name=inputandfind()
if select_name[0]==2:
print(lstdata)
if lstdata[1]!=0:
oper(lstdata[0],lstdata[1])
else:
print('上次未输入')
else:
cct=notecnt(select_name)
if cct==-1:
return
oper(select_name,cct)
lstdata=[select_name,cct]
with open(r'{0}\cache\cache_lstdata.bin'.format(bdir), 'wb') as f:
pickle.dump(lstdata,f)
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(hwnd)
if __name__=='__main__':
pythoncom.CoInitialize()
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(hwnd)
keyboard.add_hotkey('ctrl+m',MarkMap,(kp2,des2))
keyboard.add_hotkey('ctrl+p',delete_mark.DeleteCenterMark,(kp2,des2))
keyboard.add_hotkey('ctrl+d',delete_mark.DeleteMouseMark,(kp2,des2))
keyboard.wait()