-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyuVision.py
282 lines (258 loc) · 9.9 KB
/
yuVision.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
#! /usr/bin/python3
# -*- coding: utf-8 -*-
'''
| author:
| Belal HMEDAN,
| LIG lab/ Marvin Team,
| France, 2021.
| Raspberry vision handeling script.
'''
import paramiko
import numpy as np
import cv2
from time import time, sleep
from imageProcessor import imageProcessor
#=====================
# class communicator |
#=====================
class communicator():
def __init__(self, host, username, password, port=22):
"""
Class communicator: Read images remotely.
---
Parameters:
@param: host, string, host address 192.168.0.40 , www.something.org
@param: username, string, the username.
@param: password, string, the password.
@param: port, integer.
"""
self.host = host
self.username = username
self.password = password
self.port = port
self.ssh = paramiko.SSHClient() # <-- 0
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(hostname=host, username=username, password=password)
def getImage(self, localPath, remotePath='vision/image.jpg'):
"""
Function: getImage, to copy the image from the Raspberry pi to a localPath in PC.
---
Parameters:
@param: localPath, string, the path to the destination folder.
@param: remotePath, string, the path to the image on the Pi.
---
@return: None.
"""
sftp = self.ssh.open_sftp()
# remove the image
ssh_stdin, ssh_stdout, ssh_stderr = self.ssh.exec_command('sudo rm {}'.format(remotePath))
channel = ssh_stdout.channel
status = channel.recv_exit_status()
# read a new image
ssh_stdin, ssh_stdout, ssh_stderr = self.ssh.exec_command('sudo raspistill -o {} --nopreview --exposure sports --timeout 1'.format(remotePath))
channel = ssh_stdout.channel
status = channel.recv_exit_status()
sftp.get(remotePath, (localPath+'image.jpg'))
sftp.close()
#======================
# class visionHandler |
#======================
class visionHandler():
def __init__(self, path, pi_id=1, taskID='id_18', connection=True):
"""
Class visionHandler: Read and Process images remotely.
---
Parameters:
@param: pi_id, integer, raspberry pi module id: 1, 2, ..., 6.
@param: path, string, the path to save the image locally on PC.
@param: taskID, string, ArUco ID, ['id_18', 'id_25', 'id_101']
@param: connection, bool, to establish a connection with the Raspberry.
"""
self.pi_id = pi_id
self.path = path
if connection:
self.com = communicator('192.168.125.{}0'.format(pi_id), 'pi{}'.format(pi_id), '000000')
# self.proc = imageProcessor(path)
self.hand = False
self.taskID = taskID
self.errors = []
self.message = ""
self.solved = False
self.worldState = {
# First column
'p_10_04': 'g', 'p_11_04': 'g', 'p_12_04': 'g', 'p_13_04': 'g',
# Second column
'p_10_05': 'g', 'p_11_05': 'g', 'p_12_05': 'g', 'p_13_05': 'g',
# Third column
'p_10_06': 'g', 'p_11_06': 'g', 'p_12_06': 'g', 'p_13_06': 'g',
# Fourth column
'p_10_07': 'g', 'p_11_07': 'g', 'p_12_07': 'g', 'p_13_07': 'g',
# Swap
'p_07_06' : 'g', 'p_07_07' : 'g'
}
self.humanStock = {
'b_2x2': 1, 'b_2x4': 1, 'y_2x2': 1, 'y_2x4': 1
}
self.taskWorld = {
'id_18' : {
# First column
'p_10_04': 'b', 'p_11_04': 'y', 'p_12_04': 'y', 'p_13_04': 'b',
# Second column
'p_10_05': 'y', 'p_11_05': 'y', 'p_12_05': 'b', 'p_13_05': 'b',
# Third column
'p_10_06': 'b', 'p_11_06': 'y', 'p_12_06': 'b', 'p_13_06': 'y',
# Fourth column
'p_10_07': 'y', 'p_11_07': 'y', 'p_12_07': 'y', 'p_13_07': 'b',
# Swap
'p_07_06' : 'g', 'p_07_07' : 'g'
},
'id_25' : {
# First column
'p_10_04': 'y', 'p_11_04': 'b', 'p_12_04': 'b', 'p_13_04': 'y',
# Second column
'p_10_05': 'b', 'p_11_05': 'y', 'p_12_05': 'b', 'p_13_05': 'b',
# Third column
'p_10_06': 'b', 'p_11_06': 'y', 'p_12_06': 'y', 'p_13_06': 'y',
# Fourth column
'p_10_07': 'b', 'p_11_07': 'b', 'p_12_07': 'b', 'p_13_07': 'y',
# Swap
'p_07_06' : 'g', 'p_07_07' : 'g'
},
'id_101' : {
# First column
'p_10_04': 'y', 'p_11_04': 'b', 'p_12_04': 'y', 'p_13_04': 'y',
# Second column
'p_10_05': 'b', 'p_11_05': 'y', 'p_12_05': 'b', 'p_13_05': 'b',
# Third column
'p_10_06': 'y', 'p_11_06': 'y', 'p_12_06': 'y', 'p_13_06': 'b',
# Fourth column
'p_10_07': 'y', 'p_11_07': 'b', 'p_12_07': 'y', 'p_13_07': 'b',
# Swap
'p_07_06' : 'g', 'p_07_07' : 'g'
},
'id_21' : {
# First column
'p_10_04': 'y', 'p_11_04': 'b', 'p_12_04': 'y', 'p_13_04': 'y',
# Second column
'p_10_05': 'b', 'p_11_05': 'b', 'p_12_05': 'b', 'p_13_05': 'b',
# Third column
'p_10_06': 'b', 'p_11_06': 'b', 'p_12_06': 'y', 'p_13_06': 'b',
# Fourth column
'p_10_07': 'y', 'p_11_07': 'b', 'p_12_07': 'y', 'p_13_07': 'y',
# Swap
'p_07_06' : 'g', 'p_07_07' : 'g'
}
}
def compareWorld(self):
"""
Function: compareWorld, to compare the world with the task.
---
Parameters:
@param: None
---
@return: None
"""
groundTruth = self.taskWorld[self.taskID]
self.solved = True
self.message = ""
for point in groundTruth:
if not self.worldState[point] in ['g', groundTruth[point]] and not point in ['p_07_06', 'p_07_07']:
# signal to the GUI an error in position
self.errors.append(point)
# send a message to the operator.
self.message = "please pay attention, there is a wrong block!"
print(point, self.worldState[point], groundTruth[point])
if self.worldState[point] != groundTruth[point] and not point in ['p_07_06', 'p_07_07']:
# print(point, self.worldState[point], groundTruth[point])
self.solved = False
def updateState(self, verbose=False):
"""
Function: updateState, to update the real state.
---
Parameters:
@param: verbose, boolean, to print the output of the function.
---
@return: None
"""
# write the colors to the worldState dictionary
# self.proc = imageProcessor(path)
self.proc.stateAnalyzer(verbose=verbose)
if not self.proc.hand:
for key in self.proc.cellsState:
self.worldState[self.proc.cellsState[key][0]] = self.proc.cellsState[key][1]
for key in self.proc.swapState:
self.worldState[key] = self.proc.swapState[key]
for key in self.humanStock:
self.humanStock[key] = self.proc.humanStock[key]
else:
print("yuVis, Hand Detected!...")
self.proc.hand = False
sleep(5)
# capture an image.
self.com.getImage(self.path)
self.updateState()
# print("YuVis\n", self.worldState, self.proc.cellsState)
def captureWorld(self, verbose=False):
"""
Function: captureWorld, to capture an image of the workspace/swap and analyse the locations.
---
Parameters:
@param: verbose, boolean, to print the output of the function.
---
@return: None
"""
if self.solved:
print("yuVis, solved!")
return
self.com.getImage(self.path)
self.proc = imageProcessor(self.path)
hand = self.proc.handDetector()
while hand:
print("yuVis, Hand Detected!")
sleep(5)
# capture an image.
self.com.getImage(self.path) # <-- 1
self.proc = imageProcessor(self.path)
hand = self.proc.handDetector()
self.updateState(verbose=verbose)
self.compareWorld()
# def captureHand(self, verbose=False):
# """
# Function: captureHand, to capture an image of the workspace/swap and check the human hand presence.
# ---
# Parameters:
# @param: verbose, boolean, to print the output of the function.
# ---
# @return: hand, bool True if there is human hand, False if there is no human hand.
# """
# # # capture an image.
# # tic = time()
# # self.com.getImage(self.path) # <-- 2
# # toc = time()
# # if verbose:
# # print('time for getting the image is: ', round(toc-tic, 3))
# # proc = imageProcessor(self.path)
# detected = self.proc.handDetector()
# if(detected):
# self.hand = True
# # check if there is human hand.
# while(detected):
# # Process the image.
# print('detected status', detected)
# tic = time()
# self.com.getImage(self.path) # <-- 3
# toc = time()
# if verbose:
# print('time for getting the image is: ', round(toc-tic, 3))
# tic = toc
# detected = self.proc.handDetector()
# if verbose:
# print('Detected Hand status: ', detected)
# self.hand = False
#----------------------------------------------------------------------------------
# mypath = 'G:/Grenoble/Semester_4/Project_Codes/Problem_Domain/New_Domain_Problem/'
# vh = visionHandler(path=mypath)
# vh.captureWorld()
# # vh.captureHand(verbose=True)
# # print('out', vh.hand)
# print(vh.worldState, '\n', vh.humanStock, '\n', vh.humanAction)