懒得推gitee,记录一下
import os import time import shutil os.getcwd() # arrange an instance segmentation model for test from sahi.utils.yolov8 import ( download_yolov8s_model, ) import cv2 from sahi import AutoDetectionModel from sahi.utils.cv import read_image from sahi.utils.file import download_from_url from sahi.predict import get_prediction, get_sliced_prediction, predict from IPython.display import Image detection_model = AutoDetectionModel.from_pretrained( model_type='yolov8', # YOLOv8模型的路径 model_path="/home/hxzh/PycharmProjects/SAHI_YOLOv8/best.pt", # YOLOv8模型的路径 confidence_threshold=0.3, # 设备类型。 # 如果您的计算机配备 NVIDIA GPU,则可以通过将 'device' 标志更改为'cuda:0'来启用 CUDA 加速;否则,将其保留为'cpu' device="cuda:0", # or 'cuda:0' ) start_time=time.time() images_path="/media/hxzh/D/Dataset/plean_test/0926" for i in os.listdir(images_path): image=cv2.imread(f"{images_path}/{i}")[:,:,::-1] # image=cv2.resize(image,(1000,700))[:,:,::-1] result = get_sliced_prediction( # "/media/hxzh/D/Dataset/plean_test/0926/DJI_20230919085432_0018_W.JPG", image, detection_model, slice_height = 1000, slice_width = 750, overlap_height_ratio = 0.2, overlap_width_ratio = 0.2 ) result.export_visuals(export_dir="demo_data/",hide_labels=True,hide_conf=True) os.rename("demo_data/prediction_visual.png",f"demo_data/{i}") print(time.time()-start_time) Image(f"demo_data/{i}")