add tools

This commit is contained in:
liangyuxuan
2025-12-08 17:23:26 +08:00
parent efcd598565
commit e27acec63e
6 changed files with 207 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
{
"T": [
[
0.013635791720580838,
0.9992765703636767,
-0.03550212819481636,
-0.08645650535173793
],
[
-0.9998865276218899,
0.013399556063222661,
-0.006883587549256321,
0.036196137264974
],
[
-0.006402895000908794,
0.03559196285001416,
0.999345893630474,
0.014407883180676354
],
[
0.0,
0.0,
0.0,
1.0
]
]
}

39
tools/cap_video.py Normal file
View File

@@ -0,0 +1,39 @@
import cv2
# import numpy as np
import time
cap = cv2.VideoCapture(6)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) # 宽
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) # 高
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'MP4V') # 'XVID', 'MJPG', 'MP4V' 等
out = cv2.VideoWriter('video/output_720p.mp4', fourcc, 20.0, (frame_width, frame_height))
i = 0
time.sleep(1)
while True:
# 逐帧捕获
ret, frame = cap.read()
if not ret:
print("读取摄像头画面失败")
break
if i <= 5:
i = i+1
continue
cv2.imshow('camera', frame)
out.write(frame)
if cv2.waitKey(5) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()

31
tools/crop_from_video.py Normal file
View File

@@ -0,0 +1,31 @@
import cv2
import os
video_path = "video/video_5.mp4"
save_dir = "/home/lyx/Images/hivecore_box_datasets"
os.makedirs(save_dir, exist_ok=True)
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("无法打开视频")
exit()
frame_interval = 15
frame_count = 0
saved_count = 448
while True:
ret, frame = cap.read()
if not ret:
break
if frame_count % frame_interval == 0:
save_path = os.path.join(save_dir, f"frame_{saved_count:06d}.jpg")
cv2.imwrite(save_path, frame)
saved_count += 1
frame_count += 1
cap.release()
print(f"共保存 {saved_count} 张图像到 {save_dir}")

View File

@@ -0,0 +1,16 @@
import os
file_dir = "/home/lyx/Datasets/hivecore_box_datasets/"
with open(os.path.join(file_dir, "train.txt"), "w") as f:
for file in os.listdir(os.path.join(file_dir, 'images/train/')):
if file.endswith(".jpg"):
labels_dir = os.path.join(os.path.join('./images/train/', file))
f.write(labels_dir + "\n")
with open(os.path.join(file_dir, "val.txt"), "w") as f:
for file in os.listdir(os.path.join(file_dir, 'images/val/')):
if file.endswith(".jpg"):
labels_dir = os.path.join(os.path.join('./images/val/', file))
f.write(labels_dir + "\n")

View File

@@ -0,0 +1,37 @@
import os
import shutil
def check_dir(dirname):
if not os.path.exists(dirname):
os.makedirs(dirname)
file_dir = "/home/lyx/Datasets/hivecore_box_datasets/data"
imgs_dir = "/home/lyx/Datasets/hivecore_box_datasets/images"
labels_dir = "/home/lyx/Datasets/hivecore_box_datasets/labels"
check_dir(imgs_dir)
check_dir(os.path.join(imgs_dir, "train"))
check_dir(os.path.join(imgs_dir, "val"))
check_dir(labels_dir)
check_dir(os.path.join(labels_dir, "train"))
check_dir(os.path.join(labels_dir, "val"))
i = 0
for file in os.listdir(file_dir):
if file.endswith(".jpg"):
dot_index = file.rfind(".")
file_name = file[:dot_index]
label = file_name + '.txt'
if i % 10 == 9:
shutil.copy(os.path.join(file_dir, file), os.path.join(imgs_dir, "val",file))
shutil.copy(os.path.join(file_dir, label), os.path.join(labels_dir, "val", label))
else:
shutil.copy(os.path.join(file_dir, file), os.path.join(imgs_dir, "train", file))
shutil.copy(os.path.join(file_dir, label), os.path.join(labels_dir, "train", label))
i = i + 1

56
tools/json2yolo.py Normal file
View File

@@ -0,0 +1,56 @@
import json
import os
label_to_class_id = {
"box": 0, # 从0开始
# 其他类别...
}
def convert_labelme_json_to_yolo(json_file, output_dir):
try:
with open(json_file, 'r') as f:
labelme_data = json.load(f)
img_width = labelme_data["imageWidth"]
img_height = labelme_data["imageHeight"]
file_name = os.path.splitext(os.path.basename(json_file))[0]
txt_path = os.path.join(output_dir, f"{file_name}.txt")
with open(txt_path, 'w') as txt_file:
for shape in labelme_data['shapes']:
label = shape['label']
points = shape['points']
if not points:
continue
class_id = label_to_class_id.get(label)
if class_id is None:
print(f"Warning: 跳过未定义标签 '{label}'")
continue
# 检查多边形是否闭合
if points[0] != points[-1]:
points.append(points[0])
normalized = [(x / img_width, y / img_height) for x, y in points]
line = f"{class_id} " + " ".join(f"{x:.6f} {y:.6f}" for x, y in normalized)
txt_file.write(line + "\n")
except Exception as e:
print(f"处理文件 {json_file} 时出错: {str(e)}")
if __name__ == "__main__":
json_dir = "/home/lyx/Datasets/hivecore_box_datasets/data" # labelme标注存放的目录
output_dir = "/home/lyx/Datasets/hivecore_box_datasets/data" # 输出目录
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for json_file in os.listdir(json_dir):
if json_file.endswith(".json"):
json_path = os.path.join(json_dir, json_file)
convert_labelme_json_to_yolo(json_path, output_dir)