def _calculate_roi_boundary(self): if not utils.postProcessRoi(self.data): self.logger.warning("_calculate_roi_boundary returned early because of a failure in"\ " utils.postProcessRoi\n") return False if not utils.getRoiContour(self.data): self.logger.error("_calculate_roi_boundary returned early because of a failure in"\ " utils.getRoiContour\n") return False validBoundaryPoints = utils.getRoiBoundaryPoints(self.data) if self.data.debugMode: self.data.debug_info.append({ 'description': 'output of roiDelineator, ouput of"\ " boxDetector, and the approximated roi boundary points all overlayed on the input image', 'image': visUtils.visualizeAllResults( self.data.input_image_np, self.data.roi_actualSize, self.data.bestBoundary, self.data.bucketScore, self.data.matInsideBoundary, self.data.matInsideScore, self.data.caseBoundary, self.data.caseScore, self.data.bucketLeftEdge, self.data.bucketRightEdge, self.data.bucketMidEdge, self.data.approximated_roi_boundary) }) return validBoundaryPoints
def _find_best_crop_boundary(self): utils.getBestBoundaryToCropWith(self.data) self.logger.debug("The best boundary to crop the image with was found to be\n%s\n\n",\ self.data.bestBoundary) if len(self.data.bestBoundary) != 4: self.logger.warning("Was unable to find the best boundary to crop the image with"\ " because NOT ALL of the objects required by config were detected.\n***Algo will"\ " early return***\n") return False return True
def _find_pixel_conversion_factor(self): boxDetectorPredictionValid = utils.getPixel2CmConversionFactor( self.data) self.logger.debug("_find_pixel_conversion_factor detected the width of the bucket in pixels to be: %s\n "\ "pixel2CM_conversion_factor was found to be: %s\nboxDetectorPredictionValid = %s\nbucketWidthPointsXCord = %s\n\n",\ self.data.pixel2CM_conversion_factor, self.data.bucketWidthPX, boxDetectorPredictionValid, self.data.bucketWidthPointsXCord) if not boxDetectorPredictionValid: self.logger.warning( "Detected bucket boundary NOT valid.\n***Algo will early return***\n" ) return False return True
def __init__(self, boxDetectorNetworkPath, roiDelineatorNetworkPath, debugMode=False): self.logger = logging.getLogger(__name__) self.data = FMDLData(debugMode) if self.data.debugMode: self.logger.setLevel(logging.DEBUG) try: self.boxDetector = BoxDetector(boxDetectorNetworkPath) except utils.NetworkLoadException: self.logger.exception("Could not load the specified boxDetector network,"\ " you provided:\n%s\n\n", boxDetectorNetworkPath) raise utils.NetworkLoadException(boxDetectorNetworkPath) try: self.roiDelineator = RoiDelineator(roiDelineatorNetworkPath) except utils.NetworkLoadException: self.logger.exception("Could not load the specified roiDelineator network,"\ " you provided:\n%s\n\n", roiDelineatorNetworkPath) raise utils.NetworkLoadException(roiDelineatorNetworkPath)
def _validate_results(self): roiValid = utils.validateApproximatedRoiBoundary(self.data) bucketBoxValid = utils.validateBucketBox(self.data) matInsideBoxValid = utils.validateMatInsideBox(self.data) bucketLinesValid = utils.validateBucketLines(self.data) returnedDictionaryIsSerializable = self._validate_returned_dictionary_serializability( ) self.logger.debug( "The approximated (reduced) roi boundary was valid = %s\n\n", roiValid) self.logger.debug("The bucketBox was valid = %s\n\n", bucketBoxValid) self.logger.debug("The matInsideBox was valid = %s\n\n", matInsideBoxValid) self.logger.debug("The bucketLines were valid = %s\n\n", bucketLinesValid) self.logger.debug("returnedDictionaryIsSerializable = %s\n\n", returnedDictionaryIsSerializable) if roiValid and bucketBoxValid and matInsideBoxValid and returnedDictionaryIsSerializable: self.data.valid = True self.logger.info( "Successfully calculated and verified final results.\n") return True else: self.data.clearOutputData() self.logger.info("Validation of final results failed. We are going to return empty values"\ " intead of what we calculated because garbage values cause trouble downstream.") return False
def _crop_input_image(self): imageCropSuccess = utils.cropImage(self.data) if (not imageCropSuccess) or len( self.data.input_image_np_cropped) == 0: self.logger.warning("Could not find appropriate bounding boxes to crop the image with."\ "\n***Algo will early return***\n") return False self.logger.info( "Was able to successfully crop the image using detected bounding boxes.\n" ) if self.data.debugMode: self.data.debug_info.append({'description': 'Input image cropped using the best"\ " detected boundary' ,'image':\ visUtils.encodeImageAsBase64(self.data.input_image_np_cropped)}) return imageCropSuccess
def _detect_roi_uncrop_image(self): validRoi = self.roiDelineator.inferOnSingleImage(self.data) imageUncropSuccess = utils.uncropImage(self.data) if self.data.debugMode: self.data.roi_roidDelineatorSize.dtype = 'uint8' self.data.debug_info.append({'description': 'ouput of roiDelineator network in original'\ ' (raw) size','image': visUtils.encodeImageAsBase64(cv2.cvtColor(\ self.data.roi_roidDelineatorSize*255, cv2.COLOR_GRAY2BGR))}) self.data.roi_actualSize.dtype = 'uint8' self.data.debug_info.append({'description': 'ouput of roiDelineator network resized '\ 'to the same scale as input image', 'image': visUtils.encodeImageAsBase64(cv2.cvtColor(\ self.data.roi_actualSize*255, cv2.COLOR_GRAY2BGR))}) return validRoi and imageUncropSuccess
def as_dict(self, debug=False): data_dict = { 'valid': self.valid, 'pixel2CM_conversion_factor': self.pixel2CM_conversion_factor, 'detected_bucketWidth_inPixels': self.bucketWidthPX, 'imageWidthPx': self.imageWidthPx, 'imageHeightPx': self.imageHeightPx, 'bucket_valid': self.bucketValid, 'matInside_valid': self.matInsideValid, 'effective_width_calculations_valid': self.effectiveWidthValid, 'approximated_roi_boundary': utils.getNumpyAsList(self.approximated_roi_boundary_2D, 'approximated_roi_boundary_2D'), 'approximated_bucket_box': utils.getNumpyAsList(self.bucketBox, 'bucketBox'), 'approximated_matInside_box': utils.getNumpyAsList(self.matInsideBox, 'matInsideBox'), 'approximated_bucket_left_line': utils.getNumpyAsList(self.bucketLeftLine, 'bucketLeftLine'), 'approximated_bucket_right_line': utils.getNumpyAsList(self.bucketRightLine, 'bucketRightLine'), 'approximated_bucket_mid_line': utils.getNumpyAsList(self.bucketMidLine, 'bucketMidLine'), 'bucketWidthPoints': utils.getNumpyAsList(self.bucketWidthPointsXCord, 'bucketWidthPointsXCord'), } if (debug): data_dict['debug'] = self.debug_info return data_dict
def load_image(self, input_image): self.is_valid_image, self.input_image_np, self.imageWidthPx, self.imageHeightPx =\ utils.loadBytearrayImageIntoNp(input_image)