更新料带目标检测,判断料带到位逻辑
This commit is contained in:
93
ailai_pc/chose_ROI.py
Normal file
93
ailai_pc/chose_ROI.py
Normal file
@ -0,0 +1,93 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
# 全局变量
|
||||
drawing = False # 是否正在绘制
|
||||
ix, iy = -1, -1 # 起始点
|
||||
roi_list = [] # 存储多个 ROI 坐标 [(x, y, w, h), ...]
|
||||
image_path = "1.jpg" # <<< 修改为你自己的图像路径
|
||||
save_dir = "./roi_111/1.txt" # 保存坐标的目录
|
||||
|
||||
# 创建保存目录
|
||||
os.makedirs(save_dir, exist_ok=True)
|
||||
|
||||
def draw_rectangle(event, x, y, flags, param):
|
||||
global ix, iy, drawing, img_copy, roi_list
|
||||
|
||||
if event == cv2.EVENT_LBUTTONDOWN:
|
||||
drawing = True
|
||||
ix, iy = x, y
|
||||
|
||||
elif event == cv2.EVENT_MOUSEMOVE:
|
||||
if drawing:
|
||||
# 每次移动都恢复原始图像,重新画矩形
|
||||
img_copy = img.copy()
|
||||
cv2.rectangle(img_copy, (ix, iy), (x, y), (0, 255, 0), 2)
|
||||
cv2.imshow("Select ROI", img_copy)
|
||||
|
||||
elif event == cv2.EVENT_LBUTTONUP:
|
||||
drawing = False
|
||||
w = x - ix
|
||||
h = y - iy
|
||||
if w != 0 and h != 0:
|
||||
# 确保宽高为正
|
||||
x_start = min(ix, x)
|
||||
y_start = min(iy, y)
|
||||
w = abs(w)
|
||||
h = abs(h)
|
||||
cv2.rectangle(img_copy, (x_start, y_start), (x_start + w, y_start + h), (0, 255, 0), 2)
|
||||
cv2.imshow("Select ROI", img_copy)
|
||||
# 添加到列表
|
||||
roi_list.append((x_start, y_start, w, h))
|
||||
print(f"已选择 ROI: (x={x_start}, y={y_start}, w={w}, h={h})")
|
||||
|
||||
# 保存坐标到 .txt 文件的函数
|
||||
def save_rois_to_txt(rois, filepath):
|
||||
with open(filepath, 'w') as file:
|
||||
for roi in rois:
|
||||
# 将每个 ROI 转换为字符串并写入文件,每行一个 ROI
|
||||
line = ','.join(map(str, roi)) + '\n'
|
||||
file.write(line)
|
||||
print(f"💾 ROI 坐标已保存至: {filepath}")
|
||||
|
||||
def select_roi(image_path):
|
||||
global img, img_copy
|
||||
|
||||
img = cv2.imread(image_path)
|
||||
if img is None:
|
||||
print(f"❌ 无法读取图像: {image_path}")
|
||||
return
|
||||
|
||||
img_copy = img.copy()
|
||||
cv2.namedWindow("Select ROI")
|
||||
cv2.setMouseCallback("Select ROI", draw_rectangle)
|
||||
|
||||
print("📌 使用鼠标左键拖拽选择 ROI")
|
||||
print("✅ 选择完成后按 's' 键保存坐标")
|
||||
print("⏭️ 按 'n' 键跳过/下一步(可自定义)")
|
||||
print("🚪 按 'q' 键退出")
|
||||
|
||||
while True:
|
||||
cv2.imshow("Select ROI", img_copy)
|
||||
key = cv2.waitKey(1) & 0xFF
|
||||
|
||||
if key == ord('s'):
|
||||
# 保存坐标
|
||||
base_name = os.path.splitext(os.path.basename(image_path))[0]
|
||||
save_path = os.path.join(save_dir, f"{base_name}_rois1.txt") # 修改了扩展名为 .txt
|
||||
save_rois_to_txt(roi_list, save_path) # 使用新的保存函数
|
||||
|
||||
elif key == ord('n'):
|
||||
print("⏭️ 跳到下一张图片(此处可扩展)")
|
||||
break
|
||||
|
||||
elif key == ord('q'):
|
||||
print("👋 退出程序")
|
||||
cv2.destroyAllWindows()
|
||||
return
|
||||
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
if __name__ == "__main__":
|
||||
select_roi(image_path)
|
||||
65
ailai_pc/divid_conf—box.py
Normal file
65
ailai_pc/divid_conf—box.py
Normal file
@ -0,0 +1,65 @@
|
||||
import cv2
|
||||
import os
|
||||
import shutil
|
||||
from ultralytics import YOLO
|
||||
|
||||
# ====================== 配置 ======================
|
||||
MODEL_PATH = 'point.pt'
|
||||
IMAGE_SOURCE_DIR = '/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/ailaipoint'
|
||||
|
||||
OUTPUT_ROOT = '/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/ailaipoint/train_split'
|
||||
OUTPUT_DIR_0 = os.path.join(OUTPUT_ROOT, '0') # 无目标 / conf=0
|
||||
OUTPUT_DIR_1 = os.path.join(OUTPUT_ROOT, '1') # 0 < conf < 0.5
|
||||
OUTPUT_DIR_2 = os.path.join(OUTPUT_ROOT, '2') # conf >= 0.5
|
||||
|
||||
for d in [OUTPUT_DIR_0, OUTPUT_DIR_1, OUTPUT_DIR_2]:
|
||||
os.makedirs(d, exist_ok=True)
|
||||
|
||||
IMG_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.webp'}
|
||||
|
||||
# ====================== 主程序 ======================
|
||||
if __name__ == "__main__":
|
||||
print("🚀 bbox 置信度分桶(移动原图,含无目标图像)")
|
||||
|
||||
model = YOLO(MODEL_PATH)
|
||||
|
||||
image_files = [
|
||||
f for f in os.listdir(IMAGE_SOURCE_DIR)
|
||||
if os.path.splitext(f.lower())[1] in IMG_EXTENSIONS
|
||||
]
|
||||
|
||||
print(f"📸 找到图片 {len(image_files)} 张")
|
||||
|
||||
for img_name in image_files:
|
||||
src_path = os.path.join(IMAGE_SOURCE_DIR, img_name)
|
||||
|
||||
img = cv2.imread(src_path)
|
||||
if img is None:
|
||||
continue
|
||||
|
||||
results = model(img, verbose=False)
|
||||
|
||||
# ====================== 关键修复点 ======================
|
||||
if not results or results[0].boxes is None or len(results[0].boxes.conf) == 0:
|
||||
# 没有任何检测框 → 当作 conf = 0
|
||||
bbox_conf = 0.0
|
||||
else:
|
||||
# 有检测框 → 取第一个(或最大 conf)
|
||||
bbox_conf = float(results[0].boxes.conf[0].cpu().item())
|
||||
|
||||
# ====================== 分桶 ======================
|
||||
if bbox_conf == 0:
|
||||
dst_dir = OUTPUT_DIR_0
|
||||
elif bbox_conf < 0.5:
|
||||
dst_dir = OUTPUT_DIR_1
|
||||
else:
|
||||
dst_dir = OUTPUT_DIR_2
|
||||
|
||||
dst_path = os.path.join(dst_dir, img_name)
|
||||
|
||||
# ====================== 移动文件 ======================
|
||||
shutil.move(src_path, dst_path)
|
||||
|
||||
print(f"{img_name} -> conf={bbox_conf:.3f} -> {os.path.basename(dst_dir)}")
|
||||
|
||||
print("✅ 完成(含无目标图片)")
|
||||
@ -6,8 +6,8 @@ from ultralytics import YOLO
|
||||
# ====================== 用户配置 ======================
|
||||
#MODEL_PATH = '/home/hx/yolo/ultralytics_yolo11-main/runs/train/exp_ailai2/weights/best.pt'
|
||||
MODEL_PATH = 'point.pt'
|
||||
IMAGE_SOURCE_DIR = '/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/20251212' # 验证集图片目录
|
||||
LABEL_DIR = '/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/20251212' # 标签目录(与图片同名 .txt)
|
||||
IMAGE_SOURCE_DIR = '/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/20251214' # 验证集图片目录
|
||||
LABEL_DIR = '/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/1/20251214' # 标签目录(与图片同名 .txt)
|
||||
OUTPUT_DIR = './output_images'
|
||||
|
||||
|
||||
|
||||
0
ailai_pc/roi_111/1.txt/1_rois1.txt
Normal file
0
ailai_pc/roi_111/1.txt/1_rois1.txt
Normal file
Reference in New Issue
Block a user