-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.py
182 lines (133 loc) · 5.57 KB
/
task.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
from vision import *
from UR5e import *
from com import *
from Controller import *
from CPSBot import *
import numpy as np
import cv2
from time import sleep
def task_A():
"""
Foul assignments included in task_A:
1. To detect the screen, the icon and the cursor in the screen as well as their positions.
2. To control the UR5e robot arm to move the keyboard like human.
3. To double-click to launch Solidworks.
4. To grab and release the keyboard.
Created bt Dengfeng Yan, April 2nd, 2004.
"""
move_to_xyplane(0.20, -0.60, 0.2, -3.14165/2, 0, 0.)
move_to_xyplane(0.20, -0.60, 0.047, -3.14165/2, 0, 0.)
sleep(0.5)
# move_to_xyplane(0.20, -0.60, 0.047, -3.14165/2, 0, 0.)
bellow_close()
cap = cv2.VideoCapture(1)
ct = MyController()
sp = ScreenProcess()
at = ArrowTrack()
ip = IconProcess(3)
# tp = TextProcess()
# move_to_xyplane(0.334, -0.45, 0.05, -3.14165/2, 0, 0.)
index = 1
while True:
ret, frame = cap.read()
sp.detect_screen(frame)
if sp.is_screen == True:
sp.perspective_transform(frame)
screen_img = sp.result_img
at.detect_arrow(screen_img)
cursor_position = at.get_arrow_position(screen_img)
ip.detect_icon(screen_img)
icon_position = ip.get_icon_position(screen_img)
# tp.detect_text(screen_img)
# tp.text_judgement(3)
# text_position = np.array([0.5*(tp.start_point[0]+tp.end_point[0]),
# 0.5*(tp.start_point[1]+tp.end_point[1])])
if ct.is_inplace(icon_position, cursor_position) == False:
increase_x, increase_y = ct.adjust(cursor_position, icon_position)
increase_move(increase_x, increase_y, 0, 0)
print("cursor_position", index, cursor_position)
print("icon_position", index, icon_position)
elif ct.is_inplace(icon_position, cursor_position) == True:
print("Already here!")
sleep(2)
index_2()
sleep(3)
break
index = index + 1
cv2.imshow("screen", screen_img)
cv2.imshow("frame", frame)
if chr(cv2.waitKey(1)&255) == 'q': # 按 q 退出
# cap.terminate()
break
move_to_xyplane(0.20, -0.60, 0.2, -3.14165/2, 0, 0.)
move_to_xyplane(0.0, -0.50, 0.2, -3.14165/2, 0, 0.)
move_to_xyplane(0.0, -0.50, 0.047, -3.14165/2, 0, 0.)
sleep(2)
bellow_open()
sleep(3)
move_to_xyplane(0.0, -0.50, 0.2, -3.14165/2, 0, 0.)
def task_B():
"""
Foul assignments included in task_B:
1. To detect the screen, the target and the cursor in the screen as well as their positions.
2. To control the UR5e robot arm to move the keyboard like human.
3. To click when a target is detected.
4. To grab and release the keyboard.
Created bt Dengfeng Yan, April 9th, 2004.
"""
# move_to_xyplane(0.20, -0.60, 0.2, -3.14165/2, 0, 0.)
# move_to_xyplane(0.20, -0.60, 0.047, -3.14165/2, 0, 0.)
# sleep(0.5)
# move_to_xyplane(0.20, -0.60, 0.047, -3.14165/2, 0, 0.)
# bellow_close()
cap = cv2.VideoCapture(1)
ct = MyController()
sp = ScreenProcess()
at = ArrowTrack()
cps = CPSBot()
# tp = TextProcess()
index = 1
sleep(5)
index_1()
# move_to_xyplane(0.334, -0.45, 0.05, -3.14165/2, 0, 0.)
while True:
ret, frame = cap.read()
sp.detect_screen(frame)
cv2.imshow("frame", frame)
if sp.is_screen == True:
sp.perspective_transform(frame)
screen_img = sp.result_img
at.detect_arrow(screen_img)
cursor_position = at.get_arrow_position(screen_img)
target_position = cps.get_target_position(screen_img)
# tp.detect_text(screen_img)
# tp.text_judgement(3)
# text_position = np.array([0.5*(tp.start_point[0]+tp.end_point[0]),
# 0.5*(tp.start_point[1]+tp.end_point[1])])
cv2.imshow("screen", screen_img)
save_path = "{}/{:>03d}.jpg".format('fetch', index)
cv2.imwrite(save_path, screen_img)
if ct.is_inplace(target_position, cursor_position) == False:
increase_x, increase_y = ct.adjust(cursor_position, target_position)
increase_move(increase_x, increase_y, 0, 0)
print("cursor_position", index, cursor_position)
print("target_position", index, target_position)
elif ct.is_inplace(target_position, cursor_position) == True:
print("Already here!")
index_1()
sleep(1.3)
# break
index = index + 1
if chr(cv2.waitKey(1)&255) == 'q': # 按 q 退出
# cap.terminate()
break
# move_to_xyplane(0.20, -0.60, 0.2, -3.14165/2, 0, 0.)
# move_to_xyplane(0.0, -0.50, 0.2, -3.14165/2, 0, 0.)
# move_to_xyplane(0.0, -0.50, 0.047, -3.14165/2, 0, 0.)
# sleep(2)
# bellow_open()
# sleep(3)
# move_to_xyplane(0.0, -0.50, 0.2, -3.14165/2, 0, 0.)
if __name__ == "__main__":
task_B()
# ser.close()