-
Notifications
You must be signed in to change notification settings - Fork 42
/
CaptureMarkHelper.py
206 lines (182 loc) · 7.79 KB
/
CaptureMarkHelper.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
# 标点截取工具 Author By Hanmin 2022.01
# 请参考使用文档使用本工具
import cv2, tkinter, os
import tkinter.simpledialog
import ADBHelper
# 修改以下参数来运行
# 原图缩放比例,用于展示在窗口里
scale = 0.5
# 截图保存路径,以/结束
save_file_path = "./img/"
# py变量字典文件
pos_img_dict = "./testDict.py"
# 动作类型 1=截图 2=标点 3=标线(取起终点组成向量) 4=标记区域
action = 4
# 图片来源替换输入你的did
ADBHelper.screenCapture("did", "screen.png")
img_file = "./screen.png"
# ===================================================
# 以下部分可以不改动
def isVarExist(varName):
if os.path.exists(pos_img_dict):
with open(pos_img_dict, 'r', encoding='utf-8') as f:
str = f.read()
if varName in str:
return True
else:
return False
else:
return False
# type=动作类型 1=截图 2=标点 3=标线(取起终点组成向量) 4=标记区域
def createVar(varName, value, type):
with open(pos_img_dict, 'a+', encoding='utf-8') as f:
if type == 1:
f.write(varName + " = \"" + value + "\"\n")
elif type == 2:
f.write(varName + " = " + str(value) + "\n")
elif type == 3:
f.write(varName + " = " + str(value) + "\n")
elif type == 4:
f.write(varName + " = " + str(value) + "\n")
def draw_Rect(event, x, y, flags, param):
global drawing, startPos, stopPos
if event == cv2.EVENT_LBUTTONDOWN: # 响应鼠标按下
drawing = True
startPos = (x, y)
elif event == cv2.EVENT_MOUSEMOVE: # 响应鼠标移动
if drawing == True:
img = img_source.copy()
cv2.rectangle(img, startPos, (x, y), (0, 255, 0), 2)
cv2.imshow('image', img)
elif event == cv2.EVENT_LBUTTONUP: # 响应鼠标松开
drawing = False
stopPos = (x, y)
elif event == cv2.EVENT_RBUTTONUP:
if startPos == (0, 0) and stopPos == (0, 0):
return
x0, y0 = startPos
x1, y1 = stopPos
cropped = img_source[y0:y1, x0:x1] # 裁剪坐标为[y0:y1, x0:x1]
res = tkinter.simpledialog.askstring(title="输入", prompt="请输入图片变量名:(存储路径为" + save_file_path + ")",
initialvalue="")
if res is not None:
if isVarExist(res):
tkinter.simpledialog.messagebox.showerror("错误", "该变量名已存在,请更换一个或手动去文件中删除!")
else:
cv2.imwrite(save_file_path + res + ".png", cropped)
createVar(res, save_file_path + res + ".png", 1)
tkinter.simpledialog.messagebox.showinfo("提示", "创建完成!")
elif event == cv2.EVENT_MBUTTONUP:
if startPos == (0, 0) and stopPos == (0, 0):
return
x0, y0 = startPos
x1, y1 = stopPos
cropped = img_source[y0:y1, x0:x1] # 裁剪坐标为[y0:y1, x0:x1]
cv2.imshow('cropImage', cropped)
cv2.waitKey(0)
def draw_Point(event, x, y, flags, param):
global drawing, startPos, stopPos
if event == cv2.EVENT_LBUTTONDOWN: # 响应鼠标按下
drawing = True
startPos = (x, y)
img = img_source.copy()
cv2.circle(img, startPos, 2, (0, 255, 0), 2)
cv2.putText(img, "Point:" + str(startPos), (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 255, 0), 3)
print("Point:" + str(startPos))
cv2.imshow('image', img)
elif event == cv2.EVENT_RBUTTONUP:
if startPos == (0, 0):
return
res = tkinter.simpledialog.askstring(title="输入", prompt="请输入坐标 " + str(startPos) + " 变量名:", initialvalue="")
if res is not None:
if isVarExist(res):
tkinter.simpledialog.messagebox.showerror("错误", "该变量名已存在,请更换一个或手动去文件中删除!")
else:
createVar(res, startPos, 2)
tkinter.simpledialog.messagebox.showinfo("提示", "创建完成!")
def draw_Line(event, x, y, flags, param):
global drawing, startPos, stopPos
if event == cv2.EVENT_LBUTTONDOWN: # 响应鼠标按下
drawing = True
startPos = (x, y)
elif event == cv2.EVENT_MOUSEMOVE: # 响应鼠标移动
if drawing == True:
img = img_source.copy()
cv2.line(img, startPos, (x, y), (0, 255, 0), 2)
cv2.imshow('image', img)
elif event == cv2.EVENT_LBUTTONUP: # 响应鼠标松开
drawing = False
stopPos = (x, y)
print("startPoint:" + str(startPos) + " stopPoint:" + str(stopPos))
elif event == cv2.EVENT_RBUTTONUP:
if startPos == (0, 0) and stopPos == (0, 0):
return
res = tkinter.simpledialog.askstring(title="输入", prompt="请输入开始坐标 " + str(startPos) + " 到结束坐标 " + str(
stopPos) + " 组成向量的变量名:", initialvalue="")
if res is not None:
if isVarExist(res):
tkinter.simpledialog.messagebox.showerror("错误", "该变量名已存在,请更换一个或手动去文件中删除!")
else:
createVar(res, (startPos, stopPos), 3)
tkinter.simpledialog.messagebox.showinfo("提示", "创建完成!")
def draw_Rect_Pos(event, x, y, flags, param):
global drawing, startPos, stopPos
if event == cv2.EVENT_LBUTTONDOWN: # 响应鼠标按下
drawing = True
startPos = (x, y)
elif event == cv2.EVENT_MOUSEMOVE: # 响应鼠标移动
if drawing == True:
img = img_source.copy()
cv2.rectangle(img, startPos, (x, y), (0, 255, 0), 2)
cv2.imshow('image', img)
elif event == cv2.EVENT_LBUTTONUP: # 响应鼠标松开
drawing = False
stopPos = (x, y)
print("startPoint:" + str(startPos) + " stopPoint:" + str(stopPos))
elif event == cv2.EVENT_RBUTTONUP:
if startPos == (0, 0) and stopPos == (0, 0):
return
x0, y0 = startPos
x1, y1 = stopPos
res = tkinter.simpledialog.askstring(title="输入", prompt="请输入矩形范围变量名:",
initialvalue="")
if res is not None:
if isVarExist(res):
tkinter.simpledialog.messagebox.showerror("错误", "该变量名已存在,请更换一个或手动去文件中删除!")
else:
createVar(res, (startPos, stopPos), 4)
tkinter.simpledialog.messagebox.showinfo("提示", "创建完成!")
elif event == cv2.EVENT_MBUTTONUP:
if startPos == (0, 0) and stopPos == (0, 0):
return
x0, y0 = startPos
x1, y1 = stopPos
cropped = img_source[y0:y1, x0:x1] # 裁剪坐标为[y0:y1, x0:x1]
cv2.imshow('cropImage', cropped)
cv2.waitKey(0)
drawing = False
startPos = (0, 0)
stopPos = (0, 0)
img_source = cv2.imread(img_file)
img = img_source.copy()
root = tkinter.Tk()
root.title('dialog')
root.resizable(0, 0)
root.withdraw()
h_src, w_src, tongdao = img.shape
w = int(w_src * scale)
h = int(h_src * scale)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.resizeWindow("image", w, h)
if action == 1:
cv2.setMouseCallback('image', draw_Rect)
elif action == 2:
cv2.setMouseCallback('image', draw_Point)
elif action == 3:
cv2.setMouseCallback('image', draw_Line)
elif action == 4:
cv2.setMouseCallback('image', draw_Rect_Pos)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
root.destroy()