|
#!/bin/bash |
|
|
|
|
|
SCRIPT_DIR=$(cd $(dirname $0); pwd) |
|
LABELME_DIR=$SCRIPT_DIR/../export_annotated |
|
COCO_DIR=$SCRIPT_DIR/../export_coco |
|
|
|
|
|
mkdir -p $COCO_DIR/train |
|
mkdir -p $COCO_DIR/val |
|
mkdir -p $COCO_DIR/annotations |
|
|
|
|
|
rm -rf $COCO_DIR/train/* |
|
rm -rf $COCO_DIR/val/* |
|
rm -rf $COCO_DIR/annotations/* |
|
|
|
|
|
LABELME_FILES=$(find "$LABELME_DIR" -name "*.json") |
|
NUM_FILES=$(echo "$LABELME_FILES" | wc -l) |
|
VAL_COUNT=$(echo "$NUM_FILES * 0.2" | bc | awk '{print int($1+0.5)}') |
|
|
|
|
|
SHUFFLED_FILES=$(echo "$LABELME_FILES" | shuf) |
|
VAL_FILES=$(echo "$SHUFFLED_FILES" | head -n $VAL_COUNT) |
|
TRAIN_FILES=$(echo "$SHUFFLED_FILES" | tail -n +$(($VAL_COUNT + 1))) |
|
|
|
|
|
echo "$VAL_FILES" | while read -r val_file; do |
|
cp "$val_file" "$COCO_DIR/val/" |
|
|
|
|
|
image_file="${val_file%.json}.png" |
|
if [[ -f "$image_file" ]]; then |
|
cp "$image_file" "$COCO_DIR/val/" |
|
fi |
|
done |
|
|
|
|
|
echo "$TRAIN_FILES" | while read -r train_file; do |
|
cp "$train_file" "$COCO_DIR/train/" |
|
|
|
|
|
image_file="${train_file%.json}.png" |
|
if [[ -f "$image_file" ]]; then |
|
cp "$image_file" "$COCO_DIR/train/" |
|
fi |
|
done |
|
|
|
|
|
labelme2coco $COCO_DIR/train $COCO_DIR/annotations |
|
mv $COCO_DIR/annotations/dataset.json $COCO_DIR/annotations/train.json |
|
labelme2coco $COCO_DIR/val $COCO_DIR/annotations |
|
mv $COCO_DIR/annotations/dataset.json $COCO_DIR/annotations/val.json |
|
|
|
|
|
find $COCO_DIR/train -name "*.json" -type f -delete |
|
find $COCO_DIR/val -name "*.json" -type f -delete |
|
|
|
echo "Conversion to COCO format completed, and JSON files have been deleted from train and val directories." |
|
|