ravens/tasks/defs_bags.py
def determine_task_stage(self, colormap=None, heightmap=None,
                             object_mask=None, visible_beads=None):
        """Get the task stage in a consistent manner among different policies.

        This should be easier as compared to the bag-items since we only handle
        two task stages (and we don't have drastically different action params).
        """
        if self.task_stage == 2:
            print('ON TASK STAGE 2, still here, should not normally happen...')
            return (True, None)

        # Hand-tuned, if too small the agent won't open the bag enough ...
        BUF = 0.025

        # Check object_mask for all IDs that correspond to the cable ring OF THE TARGET!
        cable_IDs = np.array(self.cable_bead_target_bag_IDs)
        bead_mask = np.isin(object_mask, test_elements=cable_IDs)

        # Threshold image to get 0s and 255s (255s=bead pixels) and find its contours.
        bead_mask = np.uint8(bead_mask * 255)
        contours, _ = cv2.findContours(bead_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        # If only a few beads are visible (or no contours detected) exit early.
        frac_visible = len(visible_beads) / len(self.cable_bead_target_bag_IDs)
        if len(contours) == 0 or frac_visible <= BEAD_THRESH:
            return (False, None)

        # Combine contours via concatenation (shape=(N,1,2)) and get the convex hull.
        allc = np.concatenate([c for c in contours])
        contours_list = [allc]
        hull_list = [cv2.convexHull(c) for c in contours_list]

        # Make an RGB image, then draw the filled-in area of all items in `hull_list`.
        hull = np.zeros((bead_mask.shape[0], bead_mask.shape[1], 3), dtype=np.uint8)
        cv2.drawContours(hull, hull_list, -1, (255,255,255), thickness=-1)
        hull = cv2.cvtColor(hull, cv2.COLOR_BGR2GRAY)

        # Following task.random_pose, use object_size to find placing points. Assumes
        # object sizes are same. We add a buffer since convex hulls inflate area.
        # TODO(daniel) this is where we should really be using rotation.
        object_size = self.single_block_size
        object_size = (object_size[0] + BUF, object_size[1] + BUF, object_size[2])
        max_size = np.sqrt(object_size[0]**2 + object_size[1]**2)
        erode_size = int(np.round(max_size / self.pixel_size))

        # Use cv2.erode to find place pixels on the hull, converted to grayscale.
        place_pixels = np.uint8(hull == 255)
        kernel = np.ones((erode_size, erode_size), np.uint8)
        place_pixels_eroded = cv2.erode(place_pixels, kernel)

        # On stage 1, if there exists any possible placing point, go to stage 2.
        assert self.task_stage == 1
        if np.sum(place_pixels_eroded) > 0:
            self.task_stage = 2
            #print('now on task stage 1 --> 2')
        return (True, place_pixels_eroded)
Get the task stage in a consistent manner among different policies.

        This should be easier as compared to the bag-items since we only handle
        two task stages (and we don't have drastically different action params).
        
Get the task stage in a consistent manner among different policies.
This should be easier as compared to the bag-items since we only handle
two task stages (and we don't have drastically different action params).
Get the task stage in a consistent manner among different policies . This should be easier as compared to the bag - items since we only handle two task stages ( and we don ' t have drastically different action params ) .
def determine_task_stage(self, colormap=None, heightmap=None,
                             object_mask=None, visible_beads=None):
        if self.task_stage == 2:
            print('ON TASK STAGE 2, still here, should not normally happen...')
            return (True, None)
        BUF = 0.025
        cable_IDs = np.array(self.cable_bead_target_bag_IDs)
        bead_mask = np.isin(object_mask, test_elements=cable_IDs)
        bead_mask = np.uint8(bead_mask * 255)
        contours, _ = cv2.findContours(bead_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        frac_visible = len(visible_beads) / len(self.cable_bead_target_bag_IDs)
        if len(contours) == 0 or frac_visible <= BEAD_THRESH:
            return (False, None)
        allc = np.concatenate([c for c in contours])
        contours_list = [allc]
        hull_list = [cv2.convexHull(c) for c in contours_list]
        hull = np.zeros((bead_mask.shape[0], bead_mask.shape[1], 3), dtype=np.uint8)
        cv2.drawContours(hull, hull_list, -1, (255,255,255), thickness=-1)
        hull = cv2.cvtColor(hull, cv2.COLOR_BGR2GRAY)
        object_size = self.single_block_size
        object_size = (object_size[0] + BUF, object_size[1] + BUF, object_size[2])
        max_size = np.sqrt(object_size[0]**2 + object_size[1]**2)
        erode_size = int(np.round(max_size / self.pixel_size))
        place_pixels = np.uint8(hull == 255)
        kernel = np.ones((erode_size, erode_size), np.uint8)
        place_pixels_eroded = cv2.erode(place_pixels, kernel)
        assert self.task_stage == 1
        if np.sum(place_pixels_eroded) > 0:
            self.task_stage = 2
        return (True, place_pixels_eroded)
def determine_task_stage ( self , colormap = None , heightmap = None , object_mask = None , visible_beads = None ) : if self . task_stage == 2 : print ( 'ON TASK STAGE 2, still here, should not normally happen...' ) return ( True , None ) BUF = 0.025 cable_IDs = np . array ( self . cable_bead_target_bag_IDs ) bead_mask = np . isin ( object_mask , test_elements = cable_IDs ) bead_mask = np . uint8 ( bead_mask * 255 ) contours , _ = cv2 . findContours ( bead_mask , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE ) frac_visible = len ( visible_beads ) / len ( self . cable_bead_target_bag_IDs ) if len ( contours ) == 0 or frac_visible <= BEAD_THRESH : return ( False , None ) allc = np . concatenate ( [ c for c in contours ] ) contours_list = [ allc ] hull_list = [ cv2 . convexHull ( c ) for c in contours_list ] hull = np . zeros ( ( bead_mask . shape [ 0 ] , bead_mask . shape [ 1 ] , 3 ) , dtype = np . uint8 ) cv2 . drawContours ( hull , hull_list , - 1 , ( 255 , 255 , 255 ) , thickness = - 1 ) hull = cv2 . cvtColor ( hull , cv2 . COLOR_BGR2GRAY ) object_size = self . single_block_size object_size = ( object_size [ 0 ] + BUF , object_size [ 1 ] + BUF , object_size [ 2 ] ) max_size = np . sqrt ( object_size [ 0 ] ** 2 + object_size [ 1 ] ** 2 ) erode_size = int ( np . round ( max_size / self . pixel_size ) ) place_pixels = np . uint8 ( hull == 255 ) kernel = np . ones ( ( erode_size , erode_size ) , np . uint8 ) place_pixels_eroded = cv2 . erode ( place_pixels , kernel ) assert self . task_stage == 1 if np . sum ( place_pixels_eroded ) > 0 : self . task_stage = 2 return ( True , place_pixels_eroded )
Get the task stage in a consistent manner among different policies.
Get the task stage in a consistent manner among different policies .
"""Get the task stage in a consistent manner among different policies.

        This should be easier as compared to the bag-items since we only handle
        two task stages (and we don't have drastically different action params).
        """ # Hand-tuned, if too small the agent won't open the bag enough ... # Check object_mask for all IDs that correspond to the cable ring OF THE TARGET! # Threshold image to get 0s and 255s (255s=bead pixels) and find its contours. # If only a few beads are visible (or no contours detected) exit early. # Combine contours via concatenation (shape=(N,1,2)) and get the convex hull. # Make an RGB image, then draw the filled-in area of all items in `hull_list`. # Following task.random_pose, use object_size to find placing points. Assumes # object sizes are same. We add a buffer since convex hulls inflate area. # TODO(daniel) this is where we should really be using rotation. # Use cv2.erode to find place pixels on the hull, converted to grayscale. # On stage 1, if there exists any possible placing point, go to stage 2. #print('now on task stage 1 --> 2')
self
colormap
heightmap
object_mask
visible_beads