yolov8 分割任務切塊推理庫 patched_yolo_infer

阳光天气發表於2024-05-23

這個Python庫簡化了類似SAHI的推理,例如分割任務,從而能夠檢測影像中的小物件。它同時滿足物件檢測和例項分割任務,支援廣泛的Ultralytics模型。

該庫還為所有模型的推理結果視覺化提供了流暢的定製,包括標準方法(直接網路執行)和獨特的基於補丁的變體。

模型支援:該庫提供對多個超解析深度學習模型的支援,如YOLOv8、YOLOv8-seg、YOLOv9、YOLOv29-seg、FastSAM和RTDETR。使用者可以從預先訓練的選項中進行選擇,也可以使用自定義訓練的模型來最好地滿足他們的任務要求。
https://github.com/Koldim2001/YOLO-Patch-Based-Inference
程式碼測試:

點選檢視程式碼
import cv2
from patched_yolo_infer import MakeCropsDetectThem, CombineDetections
from patched_yolo_infer import visualize_results
# Load the image
img_path = r'D:\gzj\pic\see\0510\a001.jpg'
img = cv2.imread(img_path)

element_crops = MakeCropsDetectThem(
    image=img,
   # model_path=r"E:\03_PythonProjects\granule_server_cngr_2\y_models\primary_b.pt",
    model_path=r"E:\03_PythonProjects\granule_server_cngr_2\y_models\dakeli_weifen_suiqiu.pt",
    conf=0.7,
    segment=True,
    shape_x=640,
    shape_y=640,
    overlap_x=50,
    overlap_y=50,
    # conf=0.3,
    iou=0.7,

    resize_initial_size=True,
)
result = CombineDetections(element_crops, nms_threshold=0.05, match_metric='IOS')

# Final Results:
img=result.image
confidences=result.filtered_confidences
print("confidencs:",confidences)
boxes=result.filtered_boxes
masks=result.filtered_masks

classes_ids=result.filtered_classes_id
print("classes_ids",classes_ids)
classes_names=result.filtered_classes_names
print("classes_names",classes_names)
# Visualizing the results using the visualize_results function
visualize_results(
    img=result.image,
    confidences=result.filtered_confidences,
    boxes=result.filtered_boxes,
    masks=result.filtered_masks,
    classes_ids=result.filtered_classes_id,
    classes_names=result.filtered_classes_names,
    show_confidences=True,
    show_boxes=False,
    segment=True,
    show_class=True,
    font_scale=0.8,
)

相關文章