Last active
September 10, 2021 11:48
-
-
Save adujardin/174cd65fccf7e4c6ce4c49ab4b3ae464 to your computer and use it in GitHub Desktop.
Filter yolo coco class with class id remapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Assume coco folder from YOLO | |
| coco_original_path="/path/to/coco" | |
| coco_new_path="/path/to/coco_customclass" | |
| mkdir -p ${coco_new_path} | |
| cp -R ${coco_original_path}/annotations ${coco_new_path}/ | |
| cp -R ${coco_original_path}/common ${coco_new_path}/ | |
| cp -R ${coco_original_path}/*.txt ${coco_new_path}/ | |
| cp -R ${coco_original_path}/results ${coco_new_path}/ | |
| cp -R ${coco_original_path}/*API ${coco_new_path}/ | |
| ln -s ${coco_original_path}/images ${coco_new_path}/ # Images are not duplicated | |
| mkdir -p ${coco_new_path}/labels/train2014 | |
| mkdir -p ${coco_new_path}/labels/val2014 | |
| class_to_keep=( | |
| 0 # 0 person | |
| 2 # 1 car | |
| 8 # 2 boat | |
| ) | |
| path_=( | |
| "labels/train2014" | |
| "labels/val2014" | |
| ) | |
| # https://stackoverflow.com/a/15028821/7036639 | |
| get_index_fct(){ | |
| value=$1 | |
| for i in "${!class_to_keep[@]}"; do | |
| if [[ "${class_to_keep[$i]}" = "${value}" ]]; then | |
| echo "${i}"; | |
| fi | |
| done | |
| } | |
| update_label_fct(){ | |
| filepath=$1 | |
| filename=$(basename -- "$filepath") | |
| filepath_new="${coco_new_path}/${current_path}/${filename}" | |
| touch "${filepath_new}" | |
| echo "$filepath -> ${filepath_new}" | |
| while read -r line; do | |
| currClass=$(echo "${line}" | sed -r 's/^([^.]+).*$/\1/; s/^[^0-9]*([0-9]+).*$/\1/') | |
| if [[ "${class_to_keep[@]}" =~ "${currClass}" ]]; then | |
| # whatever you want to do when arr contains value #https://stackoverflow.com/a/15394738/7036639 | |
| newClass=$(get_index_fct ${currClass}) | |
| new_line=$(echo "${line}" | sed '0,/'$currClass'/s//'$newClass'/') | |
| echo "${new_line}" >> "${filepath_new}" | |
| #echo "$line -> $new_line" | |
| fi | |
| done < "${filepath}" | |
| } | |
| sed -i "s#${coco_original_path}#${coco_new_path}#g" ${coco_new_path}/*5k.txt | |
| for current_path in "${path_[@]}" ; do | |
| #https://unix.stackexchange.com/a/216466/217235 | |
| N=256 | |
| ( | |
| for filepath in "${coco_original_path}/${current_path}"/*.txt; do | |
| ((i=i%N)); ((i++==0)) && wait | |
| update_label_fct "$filepath" & | |
| done | |
| ) | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment