From 16c9a92c19dc92cbf0e5089f135efecf5955a668 Mon Sep 17 00:00:00 2001 From: David MacNaughton Date: Fri, 5 Nov 2021 11:08:43 +1000 Subject: [PATCH 1/5] classes-config Dependencies changed to fix Windows 10 problem. --- requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index db49ed2..8286b81 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -lxml==4.3.0 -numpy==1.16.0 +lxml==4.6.3 +numpy==1.21.2 opencv-contrib-python==3.4.9.33 tqdm==4.29.1 From ceaec85a0eaa3422b6431aac35f0d4d7f0f84303 Mon Sep 17 00:00:00 2001 From: David MacNaughton Date: Fri, 5 Nov 2021 11:13:57 +1000 Subject: [PATCH 2/5] classes-config Cleanup imports main.py. --- main/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/main/main.py b/main/main.py index 0b76409..beb7188 100755 --- a/main/main.py +++ b/main/main.py @@ -1,6 +1,5 @@ #!/bin/python import argparse -import glob import json import os import re From eb5dc11600a5dd84a4a5d0d855272db0e81ccca9 Mon Sep 17 00:00:00 2001 From: David MacNaughton Date: Fri, 5 Nov 2021 12:21:32 +1000 Subject: [PATCH 3/5] classes-config Unit test. --- tests/__init__.py | 0 tests/test_load_classes.py | 6 ++++++ 2 files changed, 6 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_load_classes.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_load_classes.py b/tests/test_load_classes.py new file mode 100644 index 0000000..9df76d7 --- /dev/null +++ b/tests/test_load_classes.py @@ -0,0 +1,6 @@ +from main.load_classes import get_class_list + + +def test_get_class_list(): + classes = get_class_list() + assert classes == ['person', 'billiard ball', 'donut'] From 9f68bf8900c362150743cf3f0e4593fea4d74963 Mon Sep 17 00:00:00 2001 From: David MacNaughton Date: Fri, 5 Nov 2021 11:46:36 +1000 Subject: [PATCH 4/5] classes-config Class list source file defined in config.ini... If no classes source file is defined in config.ini, CLASS_LIST will default to the classes defined in the example class_list.txt file provided in this repository. --- main/__init__.py | 0 main/config.ini | 3 +++ main/load_classes.py | 32 ++++++++++++++++++++++++++++++++ main/main.py | 14 +++----------- main/main_auto.py | 17 ++++++----------- 5 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 main/__init__.py create mode 100644 main/load_classes.py diff --git a/main/__init__.py b/main/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main/config.ini b/main/config.ini index fb305f9..a9b54ce 100644 --- a/main/config.ini +++ b/main/config.ini @@ -2,3 +2,6 @@ OBJECT_SCORE_THRESHOLD = 0.65 OBJECT_IDS = 1,2 CUDA_VISIBLE_DEVICES = '' + +[CLASSES] +MOST_RECENT_FILE = diff --git a/main/load_classes.py b/main/load_classes.py new file mode 100644 index 0000000..b66536a --- /dev/null +++ b/main/load_classes.py @@ -0,0 +1,32 @@ +import configparser + +from pathlib import Path + + +def non_blank_lines(file_object): + for l in file_object: + line = l.rstrip() + if line: + yield line + + +def get_class_list(): + """ + Uses the most recent classes source file defined in config.ini + otherwise defaults the example class_list.txt file provided in + this repository. + + """ + config = configparser.ConfigParser() + this_dir = Path(__file__).parent + config_path = this_dir / "config.ini" + config.read(str(config_path)) + most_recent_classes_file = config.get("CLASSES", "MOST_RECENT_FILE") + + if not most_recent_classes_file: + classes_src = str(this_dir / "class_list.txt") + else: + classes_src = most_recent_classes_file + + with open(classes_src) as f: + return list(non_blank_lines(file_object=f)) diff --git a/main/main.py b/main/main.py index beb7188..05da04c 100755 --- a/main/main.py +++ b/main/main.py @@ -11,7 +11,10 @@ from lxml import etree import xml.etree.cElementTree as ET +from load_classes import get_class_list + +CLASS_LIST = get_class_list() DELAY = 20 # keyboard delay (in milliseconds) WITH_QT = False try: @@ -717,13 +720,6 @@ def convert_video_to_images(video_path, n_frames, desired_img_format): return file_path, video_name_ext -def nonblank_lines(f): - for l in f: - line = l.rstrip() - if line: - yield line - - def get_annotation_paths(img_path, annotation_formats): annotation_paths = [] for ann_dir, ann_ext in annotation_formats.items(): @@ -1031,10 +1027,6 @@ def complement_bgr(color): elif '.xml' in ann_path: create_PASCAL_VOC_xml(ann_path, abs_path, folder_name, image_name, img_height, img_width, depth) - # load class list - with open('class_list.txt') as f: - CLASS_LIST = list(nonblank_lines(f)) - #print(CLASS_LIST) last_class_index = len(CLASS_LIST) - 1 # Make the class colors the same each session diff --git a/main/main_auto.py b/main/main_auto.py index b32e09f..b8be738 100755 --- a/main/main_auto.py +++ b/main/main_auto.py @@ -26,6 +26,12 @@ from lxml import etree import xml.etree.cElementTree as ET import sys + +from load_classes import get_class_list + + +CLASS_LIST = get_class_list() + sys.path.insert(0, "..") from object_detection.tf_object_detection import ObjectDetector import configparser @@ -313,13 +319,6 @@ def convert_video_to_images(video_path, n_frames, desired_img_format): return file_path, video_name_ext -def nonblank_lines(f): - for l in f: - line = l.rstrip() - if line: - yield line - - def get_annotation_paths(img_path, annotation_formats): annotation_paths = [] for ann_dir, ann_ext in annotation_formats.items(): @@ -695,10 +694,6 @@ def predict_next_frames(self,json_file_data,json_file_path): elif '.xml' in ann_path: create_PASCAL_VOC_xml(ann_path, abs_path, folder_name, image_name, img_height, img_width, depth) -# load class list -with open('class_list.txt') as f: - CLASS_LIST = list(nonblank_lines(f)) -#print(CLASS_LIST) last_class_index = len(CLASS_LIST) - 1 # Make the class colors the same each session From dd0f1ffd9ade10350f5b6c32c77352660e0962a1 Mon Sep 17 00:00:00 2001 From: David MacNaughton Date: Fri, 5 Nov 2021 12:32:10 +1000 Subject: [PATCH 5/5] classes-config Updated README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 92c5604..6d8333c 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,11 @@ Step by step: 1. Open the `main/` directory 2. Insert the input images and videos in the folder **input/** - 3. Insert the classes in the file **class_list.txt** (one class name per line) + 3. Edit config.in setting the full filepath to your class_list (WITHOUT quotation marks). E.g. + [CLASSES] + MOST_RECENT_FILE = /home/user1/Desktop/class_list.txt + + Alternatively, just edit the contents of the example file ./main/class_list.txt 4. Run the code: 5. You can find the annotations in the folder **output/**