This commit is contained in:
琉璃月光
2025-10-21 14:11:52 +08:00
parent 349449f2b7
commit df7c0730f5
363 changed files with 5386 additions and 578 deletions

BIN
yemian/resize/best.pt Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

View File

@ -0,0 +1 @@
0 0.317016 0.412804 0.274836 0.624093 0.466866 0.652477 0.748806 0.636707 0.729938 0.409651 0.482407 0.422264 0.436896 0.425418

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 KiB

View File

@ -0,0 +1 @@
0 0.314796 0.422264 0.268177 0.620940 0.462427 0.652477 0.756576 0.627247 0.732158 0.409651 0.480187 0.425418 0.432457 0.431725

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

View File

@ -0,0 +1 @@
0 0.315906 0.409651 0.274836 0.617786 0.466866 0.643014 0.751026 0.630400 0.733267 0.406497 0.490177 0.422264 0.434676 0.419111

View File

@ -0,0 +1,52 @@
import os
import cv2
# ----------------------------
# 配置
# ----------------------------
SOURCE_DIR = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/seg/resize_seg2" # 原始图片目录
TARGET_DIR = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/seg/resize_seg3" # 输出目录
TARGET_SIZE = 640 # resize 尺寸
SUBSETS = ["train", "val", "test"]
# ----------------------------
# 全局 ROI (x, y, w, h)
# ----------------------------
GLOBAL_ROI = [562, 798, 1287, 453]
# ----------------------------
# 主处理函数
# ----------------------------
def process_images():
x, y, w, h = GLOBAL_ROI
for subset in SUBSETS:
src_dir = os.path.join(SOURCE_DIR, subset)
tgt_dir = os.path.join(TARGET_DIR, subset)
os.makedirs(tgt_dir, exist_ok=True)
for file in os.listdir(src_dir):
if not (file.endswith(".jpg") or file.endswith(".png")):
continue
img_path = os.path.join(src_dir, file)
img = cv2.imread(img_path)
if img is None:
print(f"❌ 无法读取图片: {img_path}")
continue
h_img, w_img = img.shape[:2]
x1, y1 = max(0, x), max(0, y)
x2, y2 = min(w_img, x + w), min(h_img, y + h)
cropped = img[y1:y2, x1:x2]
if cropped.size == 0:
print(f"❌ 裁剪结果为空: {file}")
continue
resized = cv2.resize(cropped, (TARGET_SIZE, TARGET_SIZE))
tgt_path = os.path.join(tgt_dir, file)
cv2.imwrite(tgt_path, resized)
print(f"✅ 图片处理完成: {subset}/{file}")
if __name__ == "__main__":
process_images()

View File

@ -0,0 +1,93 @@
import os
# ----------------------------
# 配置
# ----------------------------
SOURCE_DIR = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/seg/resize_seg2" # 原始图片目录
TARGET_DIR = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/seg/resize_seg3" # 输出目录
SUBSETS = ["train", "val", "test"]
# 全局 ROI (x, y, w, h)
GLOBAL_ROI = [562, 798, 1287, 453]
# ----------------------------
# 分割标签处理函数
# ----------------------------
def adjust_seg_labels(label_path, crop_coords, orig_size):
x1, y1, x2, y2 = crop_coords
crop_w = x2 - x1
crop_h = y2 - y1
new_labels = []
if not os.path.exists(label_path):
print(f"⚠️ 标签源文件不存在: {label_path}")
return new_labels
with open(label_path, 'r') as f:
lines = f.readlines()
orig_w, orig_h = orig_size
for line in lines:
parts = line.strip().split()
if len(parts) < 3 or len(parts) % 2 == 0:
continue # 至少类别 + 一个点(x,y)
cls = parts[0]
coords = list(map(float, parts[1:]))
new_coords = []
for i in range(0, len(coords), 2):
x = coords[i] * orig_w
y = coords[i+1] * orig_h
x -= x1
y -= y1
x_new = x / crop_w
y_new = y / crop_h
new_coords.extend([x_new, y_new])
new_line = cls + " " + " ".join([f"{c:.6f}" for c in new_coords])
new_labels.append(new_line)
return new_labels
# ----------------------------
# 主处理函数
# ----------------------------
def process_seg_labels():
x, y, w, h = GLOBAL_ROI
fixed_roi = (x, y, x + w, y + h)
for subset in SUBSETS:
src_dir = os.path.join(SOURCE_DIR, subset)
tgt_dir = os.path.join(TARGET_DIR, subset)
os.makedirs(tgt_dir, exist_ok=True)
for file in os.listdir(src_dir):
if not (file.endswith(".txt")):
continue
label_path = os.path.join(src_dir, file)
# 读取原图尺寸
img_name = os.path.splitext(file)[0] + ".jpg"
img_path = os.path.join(src_dir, img_name)
if not os.path.exists(img_path):
print(f"❌ 无法读取图片以获取尺寸: {img_path}")
continue
import cv2
img = cv2.imread(img_path)
if img is None:
print(f"❌ 无法读取图片: {img_path}")
continue
h_img, w_img = img.shape[:2]
new_labels = adjust_seg_labels(label_path, fixed_roi, (w_img, h_img))
tgt_label_path = os.path.join(tgt_dir, file)
with open(tgt_label_path, 'w') as f:
f.write("\n".join(new_labels))
print(f"✅ 标签处理完成: {subset}/{file}, 条数 {len(new_labels)}")
if __name__ == "__main__":
process_seg_labels()

View File

@ -0,0 +1 @@
562, 798, 1287, 453

View File

@ -6,12 +6,12 @@ import numpy as np
from ultralytics import YOLO
from pathlib import Path
# ====================== 配置参数 ======================
MODEL_PATH = "/home/hx/yolo/ultralytics_yolo11-main/runs/train/seg_r/exp2/weights/best.pt"
# ====================== 配置参数 ======================3
MODEL_PATH = "best.pt"
#SOURCE_IMG_DIR = "/home/hx/yolo/output_masks" # 原始输入图像目录
SOURCE_IMG_DIR = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/f6" # 原始输入图像目录
SOURCE_IMG_DIR = "/home/hx/yolo/yemian/test_image" # 原始输入图像目录
OUTPUT_DIR = "/home/hx/yolo/output_masks2" # 推理输出根目录
ROI_COORDS_FILE = "./roi_coordinates/1_rois.txt" # 必须与训练时相同
ROI_COORDS_FILE = "./roi_coordinates/1_rois2.txt" # 必须与训练时相同
CONF_THRESHOLD = 0.25
IOU_THRESHOLD = 0.45
DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu"

Binary file not shown.

After

Width:  |  Height:  |  Size: 504 KiB

View File

@ -0,0 +1 @@
0 0.378906 0.684028 0.357701 0.750496 0.454241 0.759425 0.595982 0.754464 0.586496 0.683036 0.462054 0.687004 0.439174 0.687996

Binary file not shown.

After

Width:  |  Height:  |  Size: 501 KiB

View File

@ -0,0 +1 @@
0 0.377790 0.687004 0.354353 0.749504 0.452009 0.759425 0.599888 0.751488 0.587612 0.683036 0.460938 0.687996 0.436942 0.689980

Binary file not shown.

After

Width:  |  Height:  |  Size: 508 KiB

View File

@ -0,0 +1 @@
0 0.378348 0.683036 0.357701 0.748512 0.454241 0.756448 0.597098 0.752480 0.588170 0.682044 0.465960 0.687004 0.438058 0.686012