Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def detectObject(self, img, detectCallback, noDetectCallback=None):
- kernel = np.ones((2,2),np.uint8)
- widthAlert = np.size(img, 1)
- heightAlert = np.size(img, 0)
- yAlert = int((heightAlert / 2)) + 100
- lower = [0, 0 ,0]
- upper = [122, 122, 122]
- lower = np.array(lower, dtype="uint8")
- upper = np.array(upper, dtype="uint8")
- mask = cv2.inRange(img, lower, upper)
- output = cv2.bitwise_and(img, img, mask=mask)
- dilation = cv2.dilate(mask, kernel, iterations=3)
- #closing = cv2.morphologyEx(dilation, cv2.MORPH_GRADIENT, kernel)
- closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
- edge = cv2.Canny(closing, 175, 175)
- contours, hierarchy = cv2.findContours(edge, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- threshold_area = 50
- centers = []
- if len(contours) !=0:
- for x in contours:
- #find the area of each contour
- area = cv2.contourArea(x)
- #find the center of each contour
- moments = cv2.moments(x)
- #weed out the contours that are less than our threshold
- if area > threshold_area:
- (x,y,w,h) = cv2.boundingRect(x)
- centerX = int((x+x+w)/2)
- centerY = int((y+y+h)/2)
- cv2.circle(img,(centerX, centerY), 7, (255, 255, 255), -1)
- # Verify if the center of the circle is inside the countours
- isInside = False
- for cnt in self._contours:
- dist = cv2.pointPolygonTest(cnt,(centerX, centerY),True)
- if dist > 0:
- isInside = True
- break
- if isInside:
- logging.info("DETECT")
- detectCallback()
- #cv2.putText(img, "DENTRO", (centerX, centerY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255),2)
- else:
- logging.info("NO DETECT")
- if noDetectCallback != None:
- noDetectCallback()
- #cv2.putText(img, "FUERA", (centerX, centerY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255),2)
- #return cv2.addWeighted(img_overlay,0.5,img,0.5,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement