roboone_auto_dataset / modules /labelme2yolo.sh
nyxrobotics's picture
Add yolo training
3aa6f4a
#!/bin/bash
# Get script dir
SCRIPT_DIR=$(cd $(dirname $0); pwd)
LABELME_DIR=$SCRIPT_DIR/../export_annotated
YOLO_DIR=$SCRIPT_DIR/../export_yolo
# Create necessary directories
# mkdir -p $YOLO_DIR/train
# mkdir -p $YOLO_DIR/val
# Clean train and val directories by removing all files
# rm -rf $YOLO_DIR/train/*
# rm -rf $YOLO_DIR/val/*
# Determine the number of files for validation (20%)
LABELME_FILES=$(find "$LABELME_DIR" -name "*.json")
# Copy labelme files and their corresponding images
echo "$LABELME_FILES" | while read -r lableme_file; do
cp "$lableme_file" "$YOLO_DIR/"
# Copy the corresponding image file (assuming it has the same base name)
image_file="${lableme_file%.json}.png" # Change the extension to match your images
if [[ -f "$image_file" ]]; then
cp "$image_file" "$YOLO_DIR/"
fi
done
# Automatically generate LABEL_LIST by extracting labels from all JSON files
LABEL_LIST=$(find "$LABELME_DIR" -name "*.json" -exec jq -r '.shapes[].label' {} + | sort | uniq | paste -sd "," -)
echo "Detected labels: $LABEL_LIST"
# Convert Labelme JSON to YOLO format for train and val
cd $YOLO_DIR
labelme2yolo --output_format bbox --json_dir $YOLO_DIR --val_size 0.1 --test_size 0.1 $LABEL_LIST
# Remove temporary labelme files and their corresponding images
find $YOLO_DIR -maxdepth 1 -name "*.json" -exec rm -f {} +
find $YOLO_DIR -maxdepth 1 -name "*.png" -exec rm -f {} +
echo "YOLO dataset conversion completed."