|
import numpy |
|
import pandas as pd |
|
import sahi.predict |
|
import sahi.utils |
|
from PIL import Image |
|
|
|
TEMP_DIR = "temp" |
|
|
|
|
|
def sahi_yolov8m_inference( |
|
image, |
|
detection_model, |
|
slice_height, |
|
slice_width, |
|
overlap_height_ratio, |
|
overlap_width_ratio, |
|
image_size, |
|
): |
|
|
|
detection_model.image_size = image_size |
|
prediction_result = 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, |
|
) |
|
visual_result = sahi.utils.cv.visualize_object_predictions( |
|
image=numpy.array(image), |
|
object_prediction_list=prediction_result.object_prediction_list, |
|
rect_th=3, |
|
text_size=2 |
|
) |
|
output_visual = Image.fromarray(visual_result["image"]) |
|
|
|
|
|
coco_annotations = prediction_result.to_coco_annotations() |
|
|
|
output_df = pd.DataFrame( |
|
{'category': ['ball-valve', 'butterfly-valve', 'centrifugal-pump', 'check-valve', 'gate-valve'], |
|
'count': [0, 0, 0, 0, 0] |
|
} |
|
) |
|
|
|
coco_df = pd.DataFrame( |
|
[(item['category_name'], round(item['score'], 2)) for item in coco_annotations], |
|
columns=['category', 'score'] |
|
) |
|
|
|
category_counts = coco_df['category'].value_counts().reset_index() |
|
category_counts.columns = ['category', 'count'] |
|
|
|
output_df['count'] = output_df['category'].map(category_counts.set_index('category')['count']).fillna(0).astype(int) |
|
|
|
return output_visual,coco_df,output_df |