def get_cone(cv_image): msg = CVMessage() msg.x = 0 msg.y = 0 msg.height = 0 hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV) lower_green = np.array([60, 50, 0], dtype=np.uint8) upper_green = np.array([100, 200, 200], dtype=np.uint8) mask = cv2.inRange(hsv, lower_green, upper_green).astype(np.uint8) kernel = np.ones((11,11), np.uint8) denoised = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) denoised = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) allContours, hierarchy = cv2.findContours(denoised, cv2.cv.CV_RETR_EXTERNAL, cv2.cv.CV_CHAIN_APPROX_SIMPLE) if len(allContours) == 0: if C.Debug: cv2.imshow('cone',cv_image) return None else: largestContour = allContours[0] largestArea = cv2.contourArea(largestContour) for i in range (1, len(allContours)): area = cv2.contourArea(allContours[i]) if area > largestArea: largestContour = allContours[i] largestArea = area x, y, w, h = cv2.boundingRect(largestContour) if C.Debug: cv2.rectangle(cv_image, (x, y), (x+w, y+h), (0, 255, 0), 4) cv2.imshow('cone',cv_image) if (w*h > 512): nRows_nCols = cv_image.shape msg.x, msg.y,msg.width, msg.height = util.scaled_rect_coords(x, y, w, h, nRows_nCols[0], nRows_nCols[1]) else: return None return msg;
cone = face = speech = None bridge = CvBridge() def sendCone(m): global cone cone.publish(m) def sendFace(m): global face face.publish(m) not_found_msg = CVMessage() not_found_msg.height = C.ITEM_NOT_FOUND # import cv2 # cv2.startWindowThread() # cv2.namedWindow("preview") def camera_callback(data): cv_image = bridge.imgmsg_to_cv2(data, "bgr8") # cv2.imshow('preview',cv_image) cone_msg = cv_cone.get_cone(cv_image.copy()) face_msg = cv_face.get_face(cv_image.copy()) if cone_msg: # print cone_msg