LovelessIsma

previous detection

Nov 29th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 KB | None | 0 0
  1. class ObjectDetector:
  2.     def __init__(self, overlayFile):
  3.         self._overlayFile = overlayFile
  4.         self._contoursImage = None
  5.         self._contours = None
  6.  
  7.     def createContourOverlay(self):
  8.         kernel = np.ones((2,2),np.uint8)
  9.  
  10.         self._contoursImage = cv2.imread(self._overlayFile)
  11.  
  12.         lower = [1, 0 ,20]
  13.         upper = [60, 40, 200]
  14.         lower = np.array(lower, dtype="uint8")
  15.         upper = np.array(upper, dtype="uint8")
  16.  
  17.         mask = cv2.inRange(self._contoursImage, lower, upper)
  18.         output = cv2.bitwise_and(self._contoursImage, self._contoursImage, mask=mask)
  19.  
  20.         dilation = cv2.dilate(mask, kernel, iterations=3)
  21.         closing = cv2.morphologyEx(dilation, cv2.MORPH_GRADIENT, kernel)
  22.         closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
  23.         edges = cv2.Canny(self._contoursImage, 175, 175)
  24.         lines = cv2.HoughLinesP(edges, 1, np.pi/180, 20)
  25.         for line in lines:
  26.             x1,y1, x2, y2 = line[0]
  27.             cv2.line(self._contoursImage, (x1, y1) , (x2, y2), (0, 255, 0), 3)
  28.  
  29.         self._contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  30.  
  31.     def detectObject(self, img, detectCallback, noDetectCallback=None):
  32.         kernel = np.ones((2,2),np.uint8)
  33.  
  34.         widthAlert = np.size(img, 1)
  35.         heightAlert = np.size(img, 0)
  36.         yAlert = int((heightAlert / 2)) + 100
  37.  
  38.  
  39.         lower = [0, 0 ,0]
  40.         upper = [122, 122, 122]
  41.         lower = np.array(lower, dtype="uint8")
  42.         upper = np.array(upper, dtype="uint8")
  43.  
  44.         mask = cv2.inRange(img, lower, upper)
  45.         output = cv2.bitwise_and(img, img, mask=mask)
  46.  
  47.         dilation = cv2.dilate(mask, kernel, iterations=3)
  48.         #closing = cv2.morphologyEx(dilation, cv2.MORPH_GRADIENT, kernel)
  49.         closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
  50.         edge = cv2.Canny(closing, 175, 175)
  51.  
  52.         contours, hierarchy = cv2.findContours(edge, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  53.  
  54.         threshold_area = 50
  55.         centers = []
  56.  
  57.         if len(contours) !=0:
  58.             for x in contours:
  59.                 #find the area of each contour
  60.                 area = cv2.contourArea(x)
  61.                 #find the center of each contour
  62.                 moments = cv2.moments(x)
  63.                 #weed out the contours that are less than our threshold
  64.                 if area > threshold_area:
  65.                     (x,y,w,h) = cv2.boundingRect(x)
  66.  
  67.                     centerX = int((x+x+w)/2)
  68.                     centerY = int((y+y+h)/2)
  69.  
  70.                     cv2.circle(img,(centerX, centerY), 7, (255, 255, 255), -1)
  71.  
  72.                     # Verify if the center of the circle is inside the countours
  73.                     isInside = False
  74.                     for cnt in self._contours:
  75.                         dist = cv2.pointPolygonTest(cnt,(centerX, centerY),True)
  76.                         if dist > 0:
  77.                             isInside = True
  78.                             break
  79.  
  80.                     if isInside:
  81.                         logging.info("DETECT")
  82.                         detectCallback()
  83.                         #cv2.putText(img, "DENTRO", (centerX, centerY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255),2)
  84.                     else:
  85.                         logging.info("NO DETECT")
  86.                         if noDetectCallback != None:
  87.                             noDetectCallback()
  88.                         #cv2.putText(img, "FUERA", (centerX, centerY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255),2)
  89.  
  90.         #return cv2.addWeighted(img_overlay,0.5,img,0.5,0)
Advertisement
Add Comment
Please, Sign In to add comment