Last active
April 6, 2022 11:09
-
-
Save gumdropsteve/34f4d8854972a2c2a92db0bf28007b34 to your computer and use it in GitHub Desktop.
Convert attribute columns from VGG Image Annotator CSV to lists, then to pandas DataFrame. Draw the bounding boxes on og images and save the new image.
This file contains 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
# folder where the image is already saved (without bounding boxes) | |
data_dir = 'media/stable_numbers/number_crops/' | |
# folder where the new image with bounding boxes should be saved | |
out_dir = 'media/copy_labeled_number_crops/' | |
# go through the unique file names (because files can have more than 1 bounding box) | |
for file in new_df.filename.unique(): | |
# load in the image and .loc[] the rows where it's referenced (each row represents 1 bounding box) | |
image = cv.imread(f'{data_dir}{file}') | |
box_rows = new_df.loc[new_df.filename == file] | |
# go through each row (each bounding box) | |
for index, row in box_rows.iterrows(): | |
# set the coordinates of this row's bounding box (+1 in each direction) | |
top_left = int(row['x']) - 1, int(row['y']) - 1 | |
bottom_right = int(top_left[0] + row['width'] + 2), int(top_left[1] + row['height'] + 2) | |
# put the bounding box on the image in green | |
cv.rectangle(image, top_left, bottom_right, color=(0, 255, 0), thickness=1) | |
# save the new image with bounding boxes to the output directory (same file name) | |
new = f'{out_dir}{file}' | |
cv.imwrite(new, image) | |
# diaplay a blown up version of the last image (optional) | |
img = Image.fromarray(image) | |
img.resize((img.size[0]*5, img.size[1]*5)) |
This file contains 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
a = pd.DataFrame(file_attributes_list) | |
b = pd.DataFrame(shape_attributes_list) | |
c = pd.DataFrame(region_attributes_list) | |
new_df = pd.concat([a, b, c], axis=1, ignore_index=False) | |
new_df.columns = [c for c in a.columns] + [c for c in b.columns] + [c for c in c.columns] | |
new_df['filename'] = df.filename | |
new_df['file_size'] = df.file_size | |
new_df['region_count'] = df.region_count | |
new_df['region_id'] = df.region_id | |
new_df['file_size'] = df.file_size | |
new_df |
This file contains 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
import pandas as pd | |
df = pd.read_csv('YOURCSVHERE') | |
file_attributes_list = [] | |
for attribute in df.file_attributes.values: | |
attribute = attribute.replace('{', '').replace('}', '').split(',') | |
this_row = {} | |
for a in attribute: | |
a = a.replace('"', '') | |
a = a.split(':') | |
try: | |
try: | |
this_row.update({str(a[0]) : int(a[1])}) | |
except: | |
this_row.update({str(a[0]) : str(a[1])}) | |
except: | |
this_row.update({str(a[0]) : ''}) | |
file_attributes_list.append(this_row) | |
shape_attributes_list = [] | |
for attribute in df.region_shape_attributes.values: | |
attribute = attribute.replace('{', '').replace('}', '').split(',') | |
this_row = {} | |
for a in attribute: | |
a = a.replace('"', '') | |
a = a.split(':') | |
try: | |
try: | |
this_row.update({str(a[0]) : int(a[1])}) | |
except: | |
this_row.update({str(a[0]) : str(a[1])}) | |
except: | |
this_row.update({str(a[0]) : ''}) | |
shape_attributes_list.append(this_row) | |
region_attributes_list = [] | |
for attribute in df.region_attributes.values: | |
attribute = attribute.replace('{', '').replace('}', '').split(',') | |
this_row = {} | |
for a in attribute: | |
a = a.replace('"', '') | |
a = a.split(':') | |
try: | |
try: | |
this_row.update({str(a[0]) : int(a[1])}) | |
except: | |
this_row.update({str(a[0]) : str(a[1])}) | |
except: | |
this_row.update({str(a[0]) : ''}) | |
region_attributes_list.append(this_row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment