Skip to content

Commit e8e2e13

Browse files
authored
Merge pull request #18 from Project-AgML/dev
Release 0.2.7
2 parents 60c2541 + 430395b commit e8e2e13

File tree

3 files changed

+64
-17
lines changed

3 files changed

+64
-17
lines changed

agml/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
__version__ = '0.2.6'
15+
__version__ = '0.2.7'
1616
__all__ = ['data', 'backend', 'viz']
1717

1818
# If AgML is being imported for the first time, then we need to setup

agml/_assets/public_datasources.json

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,13 @@
307307
]
308308
},
309309
"classes": {
310-
"0": "avocado",
311-
"1": "rockmelon",
312-
"2": "apple",
313-
"3": "orange",
314-
"4": "strawberry",
315-
"5": "mango",
316-
"6": "capsicum"
310+
"1": "avocado",
311+
"2": "rockmelon",
312+
"3": "apple",
313+
"4": "orange",
314+
"5": "strawberry",
315+
"6": "mango",
316+
"7": "capsicum"
317317
},
318318
"external_image_sources": []
319319
},
@@ -502,8 +502,7 @@
502502
},
503503
"classes": {
504504
"0": "Rice",
505-
"1": "Background",
506-
"2": "Weed"
505+
"1": "Weed"
507506
},
508507
"external_image_sources": []
509508
},

agml/_internal/preprocess.py

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,45 @@ def fruit_detection_worldwide(self, dataset_name):
174174
anno_data_all, label2id, output_json_file,
175175
output_img_path, general_info)
176176

177-
def apple_detection_usa(self, dataset_name):
177+
def apple_detection_usa(self, dataset_name, fix = False):
178+
# Just a quick fix to clip over-sized bounding boxes.
179+
if fix:
180+
# Load in the annotations.
181+
dataset_dir = os.path.join(self.data_original_dir, dataset_name)
182+
with open(os.path.join(dataset_dir, 'annotations.json'), 'r') as f:
183+
annotations = json.load(f)
184+
185+
# Get the images and all of their heights/widths.
186+
images = annotations['images']
187+
image_id_content_map = {}
188+
for image in images:
189+
image_id_content_map[image['id']] = (image['height'], image['width'])
190+
191+
# Load all of the annotations.
192+
new_annotations = []
193+
for a in annotations['annotations']:
194+
new_a = a.copy()
195+
height, width = image_id_content_map[a['image_id']]
196+
(x, y, w, h) = a['bbox']
197+
x1, y1, x2, y2 = x, y, x + w, y + h
198+
x1 = np.clip(x1, 0, width)
199+
x2 = np.clip(x2, 0, width)
200+
y1 = np.clip(y1, 0, height)
201+
y2 = np.clip(y2, 0, height)
202+
new_a['bbox'] = [int(i) for i in [x1, y1, x2 - x1, y2 - y1]]
203+
new_annotations.append(new_a)
204+
205+
# Save the annotations.
206+
annotations['annotations'] = new_annotations
207+
with open(os.path.join(dataset_dir, 'annotations.json'), 'w') as f:
208+
json.dump(annotations, f)
209+
return
210+
178211
# resize the dataset
179212
resize = 1.0
180213

181214
# Read public_datasources.json to get class information
182-
category_info = self.data_sources[dataset_name]['crop_types']
215+
category_info = self.data_sources[dataset_name]['classes']
183216
labels_str = []
184217
labels_ids = []
185218
for info in category_info:
@@ -197,18 +230,16 @@ def apple_detection_usa(self, dataset_name):
197230

198231
# do tasks along folders
199232
anno_data_all = []
200-
img_ids = []
201-
bbox_ids = []
202233
for folder in plant_folders:
203234
# Get image file and xml file
204235
full_path = os.path.join(obj_Detection_data,folder)
205236
all_files = get_file_list(full_path)
206237
anno_files = [x for x in all_files if "txt" in x]
207238
for anno_file in anno_files:
208239
anno_line = []
209-
anno_path = os.path.join(full_path,anno_file)
240+
anno_path = os.path.join(full_path, anno_file)
210241
# Opening annotation file
211-
anno_data = read_txt_file(anno_path,delimiter=',')
242+
anno_data = read_txt_file(anno_path, delimiter=',')[0]
212243

213244
for i, anno in enumerate(anno_data):
214245
new_anno = [os.path.join(dataset_dir, anno_data[i][0])]
@@ -510,7 +541,24 @@ def _annotation_preprocess_fn(annotation_path, out_path):
510541
annotation_preprocess_fn = _annotation_preprocess_fn
511542
)
512543

513-
def rice_seedling_segmentation(self, dataset_name):
544+
def rice_seedling_segmentation(self, dataset_name, fix = False):
545+
# Re-mapping labels to remove the `Background` class.
546+
if fix:
547+
data_dir = os.path.join(self.data_original_dir, dataset_name)
548+
annotations = sorted([
549+
os.path.join(data_dir, 'annotations', i)
550+
for i in os.listdir(os.path.join(data_dir, 'annotations'))])
551+
os.makedirs(os.path.join(data_dir, 'new_annotations'))
552+
553+
# Create the remap.
554+
for annotation in tqdm(annotations):
555+
a = cv2.imread(annotation)
556+
a[a == 2] = 0
557+
a[a == 3] = 2
558+
cv2.imwrite(os.path.join(
559+
data_dir, 'new_annotations', os.path.basename(annotation)), a)
560+
return
561+
514562
# Get all of the relevant data.
515563
data_dir = os.path.join(self.data_original_dir, dataset_name)
516564
images = sorted(glob.glob(os.path.join(data_dir, 'image_*.jpg')))

0 commit comments

Comments
 (0)