parent
05ecb74903
commit
dbc376afd9
@ -0,0 +1,115 @@
|
|||||||
|
package com.zhehekeji.web.controller;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
public class BatchImageCropper {
|
||||||
|
public static void moveRandomFiles(String sourceDir, String targetDir, int numFiles) throws Exception {
|
||||||
|
File sourceFolder = new File(sourceDir);
|
||||||
|
File[] files = sourceFolder.listFiles((dir, name) -> new File(dir, name).isFile());
|
||||||
|
|
||||||
|
if (files == null || files.length == 0) {
|
||||||
|
System.out.println("源文件夹中没有文件可供移动。");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<File> fileList = Arrays.asList(files);
|
||||||
|
Collections.shuffle(fileList); // 随机打乱文件列表
|
||||||
|
|
||||||
|
int moveCount = Math.min(numFiles, fileList.size());
|
||||||
|
for (int i = 0; i < moveCount; i++) {
|
||||||
|
File fileToMove = fileList.get(i);
|
||||||
|
Path targetPath = Paths.get(targetDir, fileToMove.getName());
|
||||||
|
Files.move(fileToMove.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
// txt 文件名
|
||||||
|
String txtFileName = fileToMove.getPath().replace("images","labels").replace(".BMP", ".txt");
|
||||||
|
String txtPath = fileToMove.getPath().replace("train","val").replace("images","labels").replace(".BMP", ".txt");
|
||||||
|
Files.move(Paths.get(txtFileName), Paths.get(txtPath), StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
System.out.println("Moved: " + fileToMove.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main2(String[] args) {
|
||||||
|
try {
|
||||||
|
String sourceDir = "D:\\PycharmProjects\\yolo\\gjg\\train\\images";
|
||||||
|
String targetDir = "D:\\PycharmProjects\\yolo\\gjg\\val\\images";
|
||||||
|
int numFiles = 30;
|
||||||
|
|
||||||
|
moveRandomFiles(sourceDir, targetDir, numFiles);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
// 图片文件夹路径
|
||||||
|
String imagesFolderPath = "D:\\WeChet\\WeChat Files\\wxid_ttkf0xgmyihv22\\FileStorage\\File\\2025-05\\新建文件夹\\献礼";
|
||||||
|
// 标注文件夹路径
|
||||||
|
String labelsFolderPath = "D:\\WeChet\\WeChat Files\\wxid_ttkf0xgmyihv22\\FileStorage\\File\\2025-05\\新建文件夹\\献礼\\labels";
|
||||||
|
// 裁剪区域的坐标和尺寸
|
||||||
|
int cropX = 950, cropY = 356, cropWidth = 1680, cropHeight = 1680;
|
||||||
|
|
||||||
|
// 获取所有图片文件
|
||||||
|
File imagesFolder = new File(imagesFolderPath);
|
||||||
|
File[] imageFiles = imagesFolder.listFiles((dir, name) -> name.endsWith(".BMP"));
|
||||||
|
|
||||||
|
// 对每张图片进行处理
|
||||||
|
for (File imageFile : imageFiles) {
|
||||||
|
// 读取图片
|
||||||
|
BufferedImage image = ImageIO.read(imageFile);
|
||||||
|
|
||||||
|
// 裁剪图片
|
||||||
|
// BufferedImage croppedImage = image.getSubimage(cropX, cropY, cropWidth, cropHeight);
|
||||||
|
// // 直接覆盖原图片
|
||||||
|
// ImageIO.write(croppedImage, "BMP", imageFile);
|
||||||
|
|
||||||
|
// 处理标注文件
|
||||||
|
String labelFilePath = labelsFolderPath + "/" + imageFile.getName().replaceAll("\\.BMP$", ".txt");
|
||||||
|
File labelFile = new File(labelFilePath);
|
||||||
|
if (labelFile.exists()) {
|
||||||
|
List<String> newLabels = adjustYoloLabels(labelFilePath, cropX, cropY, cropWidth, cropHeight,6);
|
||||||
|
// 直接覆盖原标注文件
|
||||||
|
BufferedWriter writer = new BufferedWriter(new FileWriter(labelFilePath));
|
||||||
|
for (String line : newLabels) {
|
||||||
|
writer.write(line);
|
||||||
|
writer.newLine();
|
||||||
|
}
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改YOLO标注坐标
|
||||||
|
public static List<String> adjustYoloLabels(String labelFilePath, int cropX, int cropY, int cropWidth, int cropHeight,int classId) throws IOException {
|
||||||
|
List<String> newLabels = new ArrayList<>();
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(labelFilePath));
|
||||||
|
String line;
|
||||||
|
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
String[] parts = line.split(" ");
|
||||||
|
double centerX = Double.parseDouble(parts[1]);
|
||||||
|
double centerY = Double.parseDouble(parts[2]);
|
||||||
|
double width = Double.parseDouble(parts[3]);
|
||||||
|
double height = Double.parseDouble(parts[4]);
|
||||||
|
|
||||||
|
// 计算裁剪后的相对坐标
|
||||||
|
centerX = (centerX * 3072 - cropX) / cropWidth;
|
||||||
|
centerY = (centerY * 2048 - cropY) / cropHeight;
|
||||||
|
width = width * cropWidth / cropWidth;
|
||||||
|
height = height * cropHeight / cropHeight;
|
||||||
|
|
||||||
|
// 更新YOLO标注格式
|
||||||
|
newLabels.add(classId + " " + centerX + " " + centerY + " " + width + " " + height);
|
||||||
|
}
|
||||||
|
|
||||||
|
reader.close();
|
||||||
|
return newLabels;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue