import numpy import sahi.predict import sahi.utils from PIL import Image TEMP_DIR = "temp" def sahi_mmdet_inference( image, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2, image_size=640, postprocess_type="GREEDYNMM", postprocess_match_metric="IOS", postprocess_match_threshold=0.5, postprocess_class_agnostic=False, ): # standard inference detection_model.image_size = image_size prediction_result_1 = sahi.predict.get_prediction( image=image, detection_model=detection_model ) visual_result_1 = sahi.utils.cv.visualize_object_predictions( image=numpy.array(image), object_prediction_list=prediction_result_1.object_prediction_list, ) output_1 = Image.fromarray(visual_result_1["image"]) # sliced inference prediction_result_2 = sahi.predict.get_sliced_prediction( image=image, detection_model=detection_model, slice_height=slice_height, slice_width=slice_width, overlap_height_ratio=overlap_height_ratio, overlap_width_ratio=overlap_width_ratio, postprocess_type=postprocess_type, postprocess_match_metric=postprocess_match_metric, postprocess_match_threshold=postprocess_match_threshold, postprocess_class_agnostic=postprocess_class_agnostic, ) visual_result_2 = sahi.utils.cv.visualize_object_predictions( image=numpy.array(image), object_prediction_list=prediction_result_2.object_prediction_list, ) output_2 = Image.fromarray(visual_result_2["image"]) return output_1, output_2