PYTHON   59
CircleWatcher
Guest on 1st February 2023 01:31:53 AM


  1. from cozmo_fsm import *
  2.  
  3. class CircleWatcher(StateMachineProgram):
  4.  
  5.     def user_image(self,image,gray):
  6.         ret, thresh = cv2.threshold(gray, 50, 255, 0)
  7.         #stuff, contours, hierarchy = \
  8.         contours, hierarchy = \
  9.             cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  10.         # Each index entry: (index, area, solidity)
  11.         indices = [(i,
  12.                     cv2.contourArea(contours[i]),
  13.                     float(cv2.contourArea(contours[i])) /
  14.                     max(1,cv2.contourArea(cv2.convexHull(contours[i]))))
  15.                    for i in range(len(contours))]
  16.         indices.sort(key=lambda x: x[1])
  17.         indices.reverse()
  18.         self.indices = indices
  19.         self.contours = contours
  20.  
  21.     def user_annotate(self,annotated_image):
  22.         scale = self.annotated_scale_factor
  23.         for c in self.indices:
  24.             if c[1] < 500:  # too small
  25.                 break
  26.             if c[2] < 0.8:  # not solid enough
  27.                 continue
  28.             i = c[0]
  29.             cnt = scale * self.contours[i]
  30.             ellipse = cv2.fitEllipse(cnt)
  31.             cv2.drawContours(annotated_image, [cnt], 0, (255, 255, 255), 3)
  32.             cv2.ellipse(annotated_image, ellipse, (255,0,255), 1)
  33.         return annotated_image

Raw Paste

Login or Register to edit or fork this paste. It's free.