增加cvat反向上传
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
import cv2
|
||||
from ultralytics import YOLO
|
||||
@ -6,15 +7,15 @@ from ultralytics import YOLO
|
||||
# ---------------------------
|
||||
# 配置路径(请按需修改)
|
||||
# ---------------------------
|
||||
MODEL_PATH = "gaiban.pt" # 你的二分类模型
|
||||
INPUT_FOLDER = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/12.2" # 输入图像文件夹
|
||||
OUTPUT_ROOT = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/12.2.2" # 输出根目录(会生成 合格/不合格 子文件夹)
|
||||
MODEL_PATH = "xialiao.pt" # 你的二分类模型
|
||||
INPUT_FOLDER = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/ready" # 输入图像文件夹
|
||||
OUTPUT_ROOT = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/ready/result" # 输出根目录
|
||||
|
||||
# 类别映射(必须与训练时的 data.yaml 顺序一致)
|
||||
CLASS_NAMES = {0: "不合格", 1: "合格"}
|
||||
|
||||
# ---------------------------
|
||||
# 批量推理函数
|
||||
# 批量推理函数(移动原图)
|
||||
# ---------------------------
|
||||
def batch_classify(model_path, input_folder, output_root):
|
||||
# 加载模型
|
||||
@ -36,27 +37,32 @@ def batch_classify(model_path, input_folder, output_root):
|
||||
if img_path.suffix.lower() not in IMG_EXTS:
|
||||
continue
|
||||
|
||||
# 读取图像
|
||||
# 读取图像(用于推理)
|
||||
img = cv2.imread(str(img_path))
|
||||
if img is None:
|
||||
print(f"❌ 无法读取: {img_path}")
|
||||
print(f"❌ 无法读取图像(可能已损坏或被占用): {img_path}")
|
||||
continue
|
||||
|
||||
# 推理(整图)
|
||||
# 推理(整图分类)
|
||||
results = model(img)
|
||||
probs = results[0].probs.data.cpu().numpy()
|
||||
pred_class_id = int(probs.argmax())
|
||||
pred_label = CLASS_NAMES[pred_class_id]
|
||||
confidence = float(probs[pred_class_id])
|
||||
|
||||
# 保存原图到对应文件夹
|
||||
# ⚠️ 关键修改:移动原图(不是复制)
|
||||
dst = output_root / pred_label / img_path.name
|
||||
cv2.imwrite(str(dst), img)
|
||||
try:
|
||||
shutil.move(str(img_path), str(dst))
|
||||
except Exception as e:
|
||||
print(f"❌ 移动失败 {img_path} → {dst}: {e}")
|
||||
continue
|
||||
|
||||
print(f"✅ {img_path.name} → {pred_label} ({confidence:.2f})")
|
||||
processed += 1
|
||||
|
||||
print(f"\n🎉 共处理 {processed} 张图像,结果已保存至: {output_root}")
|
||||
print(f"\n🎉 共处理并移动 {processed} 张图像,结果已保存至: {output_root}")
|
||||
|
||||
|
||||
# ---------------------------
|
||||
# 运行入口
|
||||
|
||||
Reference in New Issue
Block a user