修改
for image_file in image_files:
# 构建输入和输出文件的路径
input_path = os.path.join(input_folder, image_file)
output_path = os.path.join(output_folder, image_file)
#读取图像
img = cv2.imread(input_path)
image, ratio, dwdh = letterbox(img, auto=False)
image = image.transpose((2, 0, 1))[::-1]
image = np.expand_dims(image, 0)
image = np.ascontiguousarray(image)
im = torch.from_numpy(image).float()
im /= 255
print(im.shape)
# 将模型和输入数据移动到GPU上
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = torch.jit.load(r'E:\yolov5-master\runs\train\v5sapple\weights\last.pt').to(device)
im = im.to(device)
result = model(im)[0]
result = non_max_suppression(result, 0.5, 0.65)[0]
result[:, :4] = scale_coords(im.shape[2:], result[:, :4], img.shape)