18 if pyjevois.pro:
import libjevoispro
as jevois
19 else:
import libjevois
as jevois
87 self.
HSVmin = np.array([ 20, 50, 180], dtype=np.uint8)
88 self.
HSVmax = np.array([ 80, 255, 255], dtype=np.uint8)
111 cpf = pyjevois.share +
"/camera/calibration{}x{}.yaml".format(w, h)
112 fs = cv2.FileStorage(cpf, cv2.FILE_STORAGE_READ)
115 self.
distCoeffs = fs.getNode(
"distortion_coefficients").mat()
116 jevois.LINFO(
"Loaded camera calibration from {}".format(cpf))
118 jevois.LERROR(
"Failed to read camera parameters from file [{}] -- IGNORED".format(cpf))
119 self.
camMatrix = np.eye(3, 3, dtype=double)
120 self.
distCoeffs = np.zeros(5, 1, dtype=double)
126 h, w, chans = imgbgr.shape
129 imghsv = cv2.cvtColor(imgbgr, cv2.COLOR_BGR2HSV)
133 str =
"H={}-{} S={}-{} V={}-{} ".format(self.
HSVmin[0], self.
HSVmax[0], self.
HSVmin[1],
137 if not hasattr(self,
'erodeElement'):
146 contours, hierarchy = cv2.findContours(imgth, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
147 str +=
"N={} ".format(len(contours))
150 contours = sorted(contours, key = cv2.contourArea, reverse =
True)[:maxn]
158 if len(str2) > len(beststr2): beststr2 = str2
162 area = cv2.contourArea(c, oriented =
False)
165 rawhull = cv2.convexHull(c, clockwise =
True)
166 rawhullperi = cv2.arcLength(rawhull, closed =
True)
167 hull = cv2.approxPolyDP(rawhull, epsilon = self.
epsilon * rawhullperi * 3.0, closed =
True)
170 if (hull.shape != (4,1,2)):
continue
173 huarea = cv2.contourArea(hull, oriented =
False)
177 hufill = area / huarea * 100.0
182 peri = cv2.arcLength(c, closed =
True)
183 approx = cv2.approxPolyDP(c, epsilon = self.
epsilon * peri, closed =
True)
184 if len(approx) < 7
or len(approx) > 9:
continue
188 serr = 100.0 * cv2.matchShapes(c, approx, cv2.CONTOURS_MATCH_I1, 0.0)
189 if serr > self.
ethresh:
continue
201 if reject == 1:
continue
213 v10p23 = complex(hull[0][0,0] - hull[1][0,0] + hull[3][0,0] - hull[2][0,0],
214 hull[0][0,1] - hull[1][0,1] + hull[3][0,1] - hull[2][0,1])
215 len10p23 = abs(v10p23)
216 v03p12 = complex(hull[3][0,0] - hull[0][0,0] + hull[2][0,0] - hull[1][0,0],
217 hull[3][0,1] - hull[0][0,1] + hull[2][0,1] - hull[1][0,1])
218 len03p12 = abs(v03p12)
221 momC = cv2.moments(c)
222 momH = cv2.moments(hull)
223 vCH = complex(momH[
'm10'] / momH[
'm00'] - momC[
'm10'] / momC[
'm00'],
224 momH[
'm01'] / momH[
'm00'] - momC[
'm01'] / momC[
'm00'])
227 if len10p23 < 0.1
or len03p12 < 0.1
or lenCH < 0.1:
continue
230 good = (v10p23.real * vCH.real + v10p23.imag * vCH.imag) / (len10p23 * lenCH)
231 bad = (v03p12.real * vCH.real + v03p12.imag * vCH.imag) / (len03p12 * lenCH)
234 if vCH.imag >= -2.0:
continue
238 if bad > good: hull = np.roll(hull, shift = 1, axis = 0)
244 if len(str2) > len(beststr2): beststr2 = str2
247 if outimg
is not None and outimg.valid():
248 if (outimg.width == w * 2): jevois.pasteGreyToYUYV(imgth, outimg, w, 0)
249 jevois.writeText(outimg, str + beststr2, 3, h+1, jevois.YUYV.White, jevois.Font.Font6x10)
260 objPoints = np.array([ ( -self.
owm * 0.5, -self.
ohm * 0.5, 0 ),
261 ( -self.
owm * 0.5, self.
ohm * 0.5, 0 ),
262 ( self.
owm * 0.5, self.
ohm * 0.5, 0 ),
263 ( self.
owm * 0.5, -self.
ohm * 0.5, 0 ) ])
265 for detection
in hlist:
266 det = np.array(detection, dtype=np.float).reshape(4,2,1)
272 rvecs.append(np.array([ (0.0), (0.0), (0.0) ]))
273 tvecs.append(np.array([ (0.0), (0.0), (0.0) ]))
275 return (rvecs, tvecs)
285 angle = (axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]) ** 0.5
288 mag_sq = axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]
289 if (abs(1.0 - mag_sq) > 1e-12): axis = axis / (mag_sq ** 0.5)
292 i = axis * math.sin(theta)
293 q = (r, i[0], i[1], i[2])
295 jevois.sendSerial(
"D3 {} {} {} {} {} {} {} {} {} {} FIRST".
296 format(np.asscalar(tv[0]), np.asscalar(tv[1]), np.asscalar(tv[2]),
298 r, np.asscalar(i[0]), np.asscalar(i[1]), np.asscalar(i[2])))
309 empty = np.array([ (0.0), (0.0), (0.0) ])
313 if np.array_equal(rvecs[i], empty):
318 axisPoints = np.array([ (0.0, 0.0, 0.0), (hw, 0.0, 0.0), (0.0, hh, 0.0), (0.0, 0.0, dd) ])
319 imagePoints, jac = cv2.projectPoints(axisPoints, rvecs[i], tvecs[i], self.
camMatrix, self.
distCoeffs)
322 jevois.drawLine(outimg,
int(imagePoints[0][0,0] + 0.5),
int(imagePoints[0][0,1] + 0.5),
323 int(imagePoints[1][0,0] + 0.5),
int(imagePoints[1][0,1] + 0.5),
324 2, jevois.YUYV.MedPurple)
325 jevois.drawLine(outimg,
int(imagePoints[0][0,0] + 0.5),
int(imagePoints[0][0,1] + 0.5),
326 int(imagePoints[2][0,0] + 0.5),
int(imagePoints[2][0,1] + 0.5),
327 2, jevois.YUYV.MedGreen)
328 jevois.drawLine(outimg,
int(imagePoints[0][0,0] + 0.5),
int(imagePoints[0][0,1] + 0.5),
329 int(imagePoints[3][0,0] + 0.5),
int(imagePoints[3][0,1] + 0.5),
330 2, jevois.YUYV.MedGrey)
333 cubePoints = np.array([ (-hw, -hh, 0.0), (hw, -hh, 0.0), (hw, hh, 0.0), (-hw, hh, 0.0),
334 (-hw, -hh, dd), (hw, -hh, dd), (hw, hh, dd), (-hw, hh, dd) ])
335 cu, jac2 = cv2.projectPoints(cubePoints, rvecs[i], tvecs[i], self.
camMatrix, self.
distCoeffs)
341 jevois.drawLine(outimg,
int(cu[0][0,0]),
int(cu[0][0,1]),
int(cu[1][0,0]),
int(cu[1][0,1]),
342 1, jevois.YUYV.LightGreen)
343 jevois.drawLine(outimg,
int(cu[1][0,0]),
int(cu[1][0,1]),
int(cu[2][0,0]),
int(cu[2][0,1]),
344 1, jevois.YUYV.LightGreen)
345 jevois.drawLine(outimg,
int(cu[2][0,0]),
int(cu[2][0,1]),
int(cu[3][0,0]),
int(cu[3][0,1]),
346 1, jevois.YUYV.LightGreen)
347 jevois.drawLine(outimg,
int(cu[3][0,0]),
int(cu[3][0,1]),
int(cu[0][0,0]),
int(cu[0][0,1]),
348 1, jevois.YUYV.LightGreen)
349 jevois.drawLine(outimg,
int(cu[4][0,0]),
int(cu[4][0,1]),
int(cu[5][0,0]),
int(cu[5][0,1]),
350 1, jevois.YUYV.LightGreen)
351 jevois.drawLine(outimg,
int(cu[5][0,0]),
int(cu[5][0,1]),
int(cu[6][0,0]),
int(cu[6][0,1]),
352 1, jevois.YUYV.LightGreen)
353 jevois.drawLine(outimg,
int(cu[6][0,0]),
int(cu[6][0,1]),
int(cu[7][0,0]),
int(cu[7][0,1]),
354 1, jevois.YUYV.LightGreen)
355 jevois.drawLine(outimg,
int(cu[7][0,0]),
int(cu[7][0,1]),
int(cu[4][0,0]),
int(cu[4][0,1]),
356 1, jevois.YUYV.LightGreen)
357 jevois.drawLine(outimg,
int(cu[0][0,0]),
int(cu[0][0,1]),
int(cu[4][0,0]),
int(cu[4][0,1]),
358 1, jevois.YUYV.LightGreen)
359 jevois.drawLine(outimg,
int(cu[1][0,0]),
int(cu[1][0,1]),
int(cu[5][0,0]),
int(cu[5][0,1]),
360 1, jevois.YUYV.LightGreen)
361 jevois.drawLine(outimg,
int(cu[2][0,0]),
int(cu[2][0,1]),
int(cu[6][0,0]),
int(cu[6][0,1]),
362 1, jevois.YUYV.LightGreen)
363 jevois.drawLine(outimg,
int(cu[3][0,0]),
int(cu[3][0,1]),
int(cu[7][0,0]),
int(cu[7][0,1]),
364 1, jevois.YUYV.LightGreen)
372 imgbgr = inframe.getCvBGR()
373 h, w, chans = imgbgr.shape
379 hlist = self.
detect(imgbgr)
399 inimg = inframe.get()
405 imgbgr = jevois.convertToCvBGR(inimg)
406 h, w, chans = imgbgr.shape
409 outimg = outframe.get()
410 outimg.require(
"output", w * 2, h + 12, jevois.V4L2_PIX_FMT_YUYV)
411 jevois.paste(inimg, outimg, 0, 0)
412 jevois.drawFilledRect(outimg, 0, h, outimg.width, outimg.height-h, jevois.YUYV.Black)
418 hlist = self.
detect(imgbgr, outimg)
433 fps = self.
timer.stop()
434 jevois.writeText(outimg, fps, 3, h-10, jevois.YUYV.White, jevois.Font.Font6x10)