Skip to content

Commit

Permalink
dealing with whitespaces in class_name
Browse files Browse the repository at this point in the history
  • Loading branch information
Cartucho committed Apr 13, 2018
1 parent 23db6a7 commit 2dacd95
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ In the [extra](https://github.com/Cartucho/mAP/tree/master/extra) folder you can
```
<class_name> <left> <top> <right> <bottom>
```
, where `<class_name>` must have no whitespaces between words.
- E.g. "image_1.txt":
```
tvmonitor 2 10 173 238
Expand All @@ -113,7 +112,6 @@ In the [extra](https://github.com/Cartucho/mAP/tree/master/extra) folder you can
```
<class_name> <confidence> <left> <top> <right> <bottom>
```
, where `<class_name>` must have no whitespaces between words.
- E.g. "image_1.txt":
```
tvmonitor 0.471781 0 13 174 244
Expand Down
5 changes: 3 additions & 2 deletions extra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ In the case you have the `--ground-truth` or `--predicted` files in the right fo

## Rename a specific class of objects

1) Run the `rename_class.py` script and specify the **class** and **new class name** as argument, e.g.
`python rename_class.py chair furniture`
1) Run the `rename_class.py` script and specify the `--current-class-name` and `--new-class-name` as arguments, e.g.

`python rename_class.py --current-class-name Picture Frame --new-class-name PictureFrame`
28 changes: 16 additions & 12 deletions extra/rename_class.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import sys
import os
import glob
import argparse

if len(sys.argv) != 3:
print("Error: wrong format.\nUsage: python rename_class.py [class] [new_class_name]")
sys.exit(0)
parser = argparse.ArgumentParser()
# argparse current class name to a list (since it can contain more than one word, e.g."dining table")
parser.add_argument('-c', '--current-class-name', nargs='+', type=str, help="current class name e.g.:\"dining table\".", required=True)
# new class name (should be a single string without any spaces, e.g. "diningtable")
parser.add_argument('-n', '--new-class-name', type=str, help="new class name.", required=True)
args = parser.parse_args()

old_class_name = sys.argv[1]
new_class_name = sys.argv[2]
current_class_name = " ".join(args.current_class_name) # join current name to single string
new_class_name = args.new_class_name


def query_yes_no(question, default="yes"):
Expand Down Expand Up @@ -43,7 +47,7 @@ def query_yes_no(question, default="yes"):
"(or 'y' or 'n').\n")


def rename_class(old_class_name, new_class_name):
def rename_class(current_class_name, new_class_name):
# get list of txt files
file_list = glob.glob('*.txt')
file_list.sort()
Expand All @@ -58,10 +62,10 @@ def rename_class(old_class_name, new_class_name):
new_content = []
# go through each line of eache file
for line in content:
class_name = line.split()[0]
if class_name == old_class_name:
#class_name = line.split()[0]
if current_class_name in line:
class_found = True
line = line.replace(old_class_name, new_class_name)
line = line.replace(current_class_name, new_class_name)
new_content.append(line)
if class_found:
# rewrite file
Expand All @@ -71,16 +75,16 @@ def rename_class(old_class_name, new_class_name):

y_n_message = ("Are you sure you want "
"to rename the class "
"\"" + old_class_name + "\" "
"\"" + current_class_name + "\" "
"into \"" + new_class_name + "\"?"
)

if query_yes_no(y_n_message):
print(" Ground-Truth folder:")
os.chdir("../ground-truth")
rename_class(old_class_name, new_class_name)
rename_class(current_class_name, new_class_name)
print(" Done!")
print(" Predicted folder:")
os.chdir("../predicted")
rename_class(old_class_name, new_class_name)
rename_class(current_class_name, new_class_name)
print(" Done!")
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ def draw_plot_func(dictionary, n_classes, window_title, plot_title, y_label, out
error_msg = "Error: File " + txt_file + " in the wrong format.\n"
error_msg += " Expected: <class_name> <left> <top> <right> <bottom>\n"
error_msg += " Received: " + line
error_msg += "\n\nIf you have a <class_name> with spaces between words you should remove them\n"
error_msg += "by running the script \"rename_class.py\" in the \"extra/\" folder."
error(error_msg)
# check if class is in the ignore list, if yes skip
if class_name in args.ignore:
Expand Down

0 comments on commit 2dacd95

Please sign in to comment.