File size: 1,453 Bytes
3aa6f4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/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."