18 if pyjevois.pro:
import libjevoispro
as jevois
19 else:
import libjevois
as jevois
115 self.
fname =
"/jevois/modules/JeVois/PythonObject6D/images/reference.png"
126 self.
winh = (12 + 4.25) * 0.0254
137 cpf = pyjevois.share +
"/camera/calibration{}x{}.yaml".format(w, h)
138 fs = cv2.FileStorage(cpf, cv2.FILE_STORAGE_READ)
141 self.
distCoeffs = fs.getNode(
"distortion_coefficients").mat()
142 jevois.LINFO(
"Loaded camera calibration from {}".format(cpf))
144 jevois.LERROR(
"Failed to read camera parameters from file [{}] -- IGNORED".format(cpf))
145 self.
camMatrix = np.eye(3, 3, dtype=double)
146 self.
distCoeffs = np.zeros(5, 1, dtype=double)
150 def detect(self, imggray, outimg = None):
155 if not hasattr(self,
'detector'):
160 refimg = cv2.imread(self.
fname, 0)
164 refh, refw = refimg.shape
165 self.
refcorners = np.float32([ [ 0.0, 0.0 ], [ 0.0, refh ], [refw, refh ], [ refw, 0.0 ] ]).reshape(-1,1,2)
171 jevois.LINFO(
"Extracted {} keypoints and descriptors from {}".format(len(self.refkp), self.
fname))
174 kp, des = self.
detector.detectAndCompute(imggray,
None)
175 str =
"{} keypoints".format(len(kp))
178 if not hasattr(self,
'matcher'):
179 self.
matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck =
True)
183 matches = sorted(matches, key =
lambda x:x.distance)
184 str +=
", {} matches".format(len(matches))
189 if m.distance < self.
distth: lastidx += 1
191 matches = matches[0:lastidx]
192 str +=
", {} good".format(len(matches))
197 if len(matches) >= 10:
203 obj.append(self.refkp[m.trainIdx].pt)
204 scene.append(kp[m.queryIdx].pt)
207 hmg, mask = cv2.findHomography(np.array(obj), np.array(scene), cv2.RANSAC, 5.0)
210 u, s, v = np.linalg.svd(hmg, full_matrices =
False)
215 if s[-1] > 0.001
and s[0] / s[-1] > 100:
217 corners = cv2.perspectiveTransform(self.
refcorners, hmg)
218 wincorners = cv2.perspectiveTransform(self.
wincorners, hmg)
221 if outimg
is not None and outimg.valid():
222 if len(corners) == 4:
223 jevois.drawLine(outimg,
int(corners[0][0,0] + 0.5),
int(corners[0][0,1] + 0.5),
224 int(corners[1][0,0] + 0.5),
int(corners[1][0,1] + 0.5),
225 2, jevois.YUYV.LightPink)
226 jevois.drawLine(outimg,
int(corners[1][0,0] + 0.5),
int(corners[1][0,1] + 0.5),
227 int(corners[2][0,0] + 0.5),
int(corners[2][0,1] + 0.5),
228 2, jevois.YUYV.LightPink)
229 jevois.drawLine(outimg,
int(corners[2][0,0] + 0.5),
int(corners[2][0,1] + 0.5),
230 int(corners[3][0,0] + 0.5),
int(corners[3][0,1] + 0.5),
231 2, jevois.YUYV.LightPink)
232 jevois.drawLine(outimg,
int(corners[3][0,0] + 0.5),
int(corners[3][0,1] + 0.5),
233 int(corners[0][0,0] + 0.5),
int(corners[0][0,1] + 0.5),
234 2, jevois.YUYV.LightPink)
235 jevois.writeText(outimg, str, 3, h+4, jevois.YUYV.White, jevois.Font.Font6x10)
239 if len(wincorners) == 4: hlist.append(wincorners)
250 objPoints = np.array([ ( -self.
winw * 0.5, -self.
winh * 0.5, 0 ),
251 ( -self.
winw * 0.5, self.
winh * 0.5, 0 ),
252 ( self.
winw * 0.5, self.
winh * 0.5, 0 ),
253 ( self.
winw * 0.5, -self.
winh * 0.5, 0 ) ])
255 for detection
in hlist:
256 det = np.array(detection, dtype=np.float).reshape(4,2,1)
262 rvecs.append(np.array([ (0.0), (0.0), (0.0) ]))
263 tvecs.append(np.array([ (0.0), (0.0), (0.0) ]))
265 return (rvecs, tvecs)
275 angle = (axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]) ** 0.5
278 mag_sq = axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]
279 if (abs(1.0 - mag_sq) > 1e-12): axis = axis / (mag_sq ** 0.5)
282 i = axis * math.sin(theta)
283 q = (r, i[0], i[1], i[2])
285 jevois.sendSerial(
"D3 {} {} {} {} {} {} {} {} {} {} OBJ6D".
286 format(np.asscalar(tv[0]), np.asscalar(tv[1]), np.asscalar(tv[2]),
288 r, np.asscalar(i[0]), np.asscalar(i[1]), np.asscalar(i[2])))
299 empty = np.array([ (0.0), (0.0), (0.0) ])
305 if np.array_equal(rvecs[i], empty):
312 axisPoints = np.array([ (0.0, 0.0, 0.0), (hw, 0.0, 0.0), (0.0, hh, 0.0), (0.0, 0.0, dd) ])
313 imagePoints, jac = cv2.projectPoints(axisPoints, rvecs[i], tvecs[i], self.
camMatrix, self.
distCoeffs)
316 jevois.drawLine(outimg,
int(imagePoints[0][0,0] + 0.5),
int(imagePoints[0][0,1] + 0.5),
317 int(imagePoints[1][0,0] + 0.5),
int(imagePoints[1][0,1] + 0.5),
318 2, jevois.YUYV.MedPurple)
319 jevois.drawLine(outimg,
int(imagePoints[0][0,0] + 0.5),
int(imagePoints[0][0,1] + 0.5),
320 int(imagePoints[2][0,0] + 0.5),
int(imagePoints[2][0,1] + 0.5),
321 2, jevois.YUYV.MedGreen)
322 jevois.drawLine(outimg,
int(imagePoints[0][0,0] + 0.5),
int(imagePoints[0][0,1] + 0.5),
323 int(imagePoints[3][0,0] + 0.5),
int(imagePoints[3][0,1] + 0.5),
324 2, jevois.YUYV.MedGrey)
328 cubePoints = np.array([ (-hw, -hh, 0.0), (hw, -hh, 0.0), (hw, hh, 0.0), (-hw, hh, 0.0),
329 (-hw, -hh, -dd), (hw, -hh, -dd), (hw, hh, -dd), (-hw, hh, -dd) ])
330 cu, jac2 = cv2.projectPoints(cubePoints, rvecs[i], tvecs[i], self.
camMatrix, self.
distCoeffs)
336 jevois.drawLine(outimg,
int(cu[0][0,0]),
int(cu[0][0,1]),
int(cu[1][0,0]),
int(cu[1][0,1]),
337 1, jevois.YUYV.LightGreen)
338 jevois.drawLine(outimg,
int(cu[1][0,0]),
int(cu[1][0,1]),
int(cu[2][0,0]),
int(cu[2][0,1]),
339 1, jevois.YUYV.LightGreen)
340 jevois.drawLine(outimg,
int(cu[2][0,0]),
int(cu[2][0,1]),
int(cu[3][0,0]),
int(cu[3][0,1]),
341 1, jevois.YUYV.LightGreen)
342 jevois.drawLine(outimg,
int(cu[3][0,0]),
int(cu[3][0,1]),
int(cu[0][0,0]),
int(cu[0][0,1]),
343 1, jevois.YUYV.LightGreen)
344 jevois.drawLine(outimg,
int(cu[4][0,0]),
int(cu[4][0,1]),
int(cu[5][0,0]),
int(cu[5][0,1]),
345 1, jevois.YUYV.LightGreen)
346 jevois.drawLine(outimg,
int(cu[5][0,0]),
int(cu[5][0,1]),
int(cu[6][0,0]),
int(cu[6][0,1]),
347 1, jevois.YUYV.LightGreen)
348 jevois.drawLine(outimg,
int(cu[6][0,0]),
int(cu[6][0,1]),
int(cu[7][0,0]),
int(cu[7][0,1]),
349 1, jevois.YUYV.LightGreen)
350 jevois.drawLine(outimg,
int(cu[7][0,0]),
int(cu[7][0,1]),
int(cu[4][0,0]),
int(cu[4][0,1]),
351 1, jevois.YUYV.LightGreen)
352 jevois.drawLine(outimg,
int(cu[0][0,0]),
int(cu[0][0,1]),
int(cu[4][0,0]),
int(cu[4][0,1]),
353 1, jevois.YUYV.LightGreen)
354 jevois.drawLine(outimg,
int(cu[1][0,0]),
int(cu[1][0,1]),
int(cu[5][0,0]),
int(cu[5][0,1]),
355 1, jevois.YUYV.LightGreen)
356 jevois.drawLine(outimg,
int(cu[2][0,0]),
int(cu[2][0,1]),
int(cu[6][0,0]),
int(cu[6][0,1]),
357 1, jevois.YUYV.LightGreen)
358 jevois.drawLine(outimg,
int(cu[3][0,0]),
int(cu[3][0,1]),
int(cu[7][0,0]),
int(cu[7][0,1]),
359 1, jevois.YUYV.LightGreen)
369 imggray = inframe.getCvGRAY()
376 hlist = self.
detect(imggray)
396 inimg = inframe.get()
402 imggray = jevois.convertToCvGray(inimg)
406 outimg = outframe.get()
407 outimg.require(
"output", w, h + 22, jevois.V4L2_PIX_FMT_YUYV)
408 jevois.paste(inimg, outimg, 0, 0)
409 jevois.drawFilledRect(outimg, 0, h, outimg.width, outimg.height-h, jevois.YUYV.Black)
415 hlist = self.
detect(imggray, outimg)
430 fps = self.
timer.stop()
431 jevois.writeText(outimg, fps, 3, h-10, jevois.YUYV.White, jevois.Font.Font6x10)