Advertisement
LovelessIsma

objectdetect.py

Nov 6th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. def detectObject(self, img, detectCallback, noDetectCallback=None):
  2.         kernel = np.ones((2,2),np.uint8)
  3.  
  4.         widthAlert = np.size(img, 1)
  5.         heightAlert = np.size(img, 0)
  6.         yAlert = int((heightAlert / 2)) + 100
  7.  
  8.  
  9.         lower = [0, 0 ,0]
  10.         upper = [122, 122, 122]
  11.         lower = np.array(lower, dtype="uint8")
  12.         upper = np.array(upper, dtype="uint8")
  13.  
  14.         mask = cv2.inRange(img, lower, upper)
  15.         output = cv2.bitwise_and(img, img, mask=mask)
  16.  
  17.         dilation = cv2.dilate(mask, kernel, iterations=3)
  18.         #closing = cv2.morphologyEx(dilation, cv2.MORPH_GRADIENT, kernel)
  19.         closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
  20.         edge = cv2.Canny(closing, 175, 175)
  21.  
  22.         contours, hierarchy = cv2.findContours(edge, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  23.  
  24.         threshold_area = 50
  25.         centers = []
  26.  
  27.         if len(contours) !=0:
  28.             for x in contours:
  29.                 #find the area of each contour
  30.                 area = cv2.contourArea(x)
  31.                 #find the center of each contour
  32.                 moments = cv2.moments(x)
  33.                 #weed out the contours that are less than our threshold
  34.                 if area > threshold_area:
  35.                     (x,y,w,h) = cv2.boundingRect(x)
  36.  
  37.                     centerX = int((x+x+w)/2)
  38.                     centerY = int((y+y+h)/2)
  39.  
  40.                     cv2.circle(img,(centerX, centerY), 7, (255, 255, 255), -1)
  41.  
  42.                     # Verify if the center of the circle is inside the countours
  43.                     isInside = False
  44.                     for cnt in self._contours:
  45.                         dist = cv2.pointPolygonTest(cnt,(centerX, centerY),True)
  46.                         if dist > 0:
  47.                             isInside = True
  48.                             break
  49.  
  50.                     if isInside:
  51.                         logging.info("DETECT")
  52.                         detectCallback()
  53.                         #cv2.putText(img, "DENTRO", (centerX, centerY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255),2)
  54.                     else:
  55.                         logging.info("NO DETECT")
  56.                         if noDetectCallback != None:
  57.                             noDetectCallback()
  58.                         #cv2.putText(img, "FUERA", (centerX, centerY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255),2)
  59.  
  60.         #return cv2.addWeighted(img_overlay,0.5,img,0.5,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement