<jupyter_start><jupyter_text>Load Metadata on images* Find out the coordinate locations for 3 images of interest* Draw the coordinates on the image* Show the images* Determine if would be in the crop<jupyter_code>SAGEMAKER_PATH = r'/home/ec2-user/SageMaker'
SPLIT_PATH = os.path.join(SAGEMAKER_PATH, 'classify-streetview', 'split-train-test')

df_split = pd.read_csv(os.path.join(SPLIT_PATH, 'data-split', 'final_combined_splits.csv'))
df_split.head()
img_id_list = ['10013', '11206', '1908']
df_split.loc[df_split['img_id'].isin(img_id_list)]
df_labels = pd.read_csv(os.path.join(SPLIT_PATH, 'restructure_single_labels.csv'))
df_labels.head()
image_filename_list = ['10013_135.jpg', '11206_135.jpg', '1908_135.jpg']
df_labels.loc[df_labels['filename'].isin(image_filename_list)]<jupyter_output><empty_output><jupyter_text>Assume that we take it if it's in the bounds of the crop* So do a comparison tree?<jupyter_code>COOR_STRING = StringIO("""
crop_number	corner_x	corner_y	size	x_crop_left	x_crop_right	y_crop_top	y_crop_bottom
1	0	200	220	0	220	200	420
2	140	200	220	140	360	200	420
3	280	200	220	280	500	200	420
4	420	200	220	420	640	200	420
5	0	340	300	0	300	340	640
6	170	340	300	170	470	340	640
7	340	340	300	340	640	340	640
""")
df_crop_coor = pd.read_csv(COOR_STRING, sep = '\t')
df_crop_coor
df_crop_coor.to_csv('crop_coor.csv', index = False)
df_crop_coor.dtypes

df_crop_coor['x_left'] = df_crop_coor['x_crop_left']
df_crop_coor['x_right'] = df_crop_coor['x_crop_right']
df_crop_coor['y_top'] = df_crop_coor['y_crop_bottom']
df_crop_coor['y_bottom'] = df_crop_coor['y_crop_bottom']
df_crop_coor.columns
def is_label_in_roi(x_pt, y_pt, x_left, x_right, y_top, y_bottom):
    # Where x_pt and y_pt are the coordinate location of x and y
    # x_left and x_right are the vertical bounds
    # y_top and y_bottom are the horizontal bounds of the square
    # Note that the point (0, 0) is in the top left corner
    
    # We'll just assume the point is positive 
    if (x_left < x_pt < x_right) and (y_top < y_pt < y_bottom):
        return True
    else:
        return False
def get_label_roi(x_pt, y_pt, list_coors):
    result_dict = {}
    
    for index, row in df_roi.iterrows():
        result_dict[df_roi['crop_number']] = is_label_in_roi(x_pt, y_pt, df_roi['x_left'], df_roi['x_right'], df_roi['y_top'], df_roi['y_bottom'])
        
    return result_dict

# This function errors out because we are comparing values<jupyter_output><empty_output><jupyter_text>Try to Reshape the DF to a list<jupyter_code>keep_cols = ['crop_number', 'x_crop_left',
       'x_crop_right', 'y_crop_top', 'y_crop_bottom']
list_coors = df_crop_coor[keep_cols].values.tolist()
list_coors
def get_label_roi(x_pt, y_pt, list_coors):
    # Where there's a list of lists
    # The sublists have the following order
    
    result_dict = {}
    
    for coor in list_coors:
        result_dict[coor[0]] = is_label_in_roi(x_pt, y_pt, coor[1], coor[2], coor[3], coor[4])
        
    return result_dict
%%time
get_label_roi(392, 433, list_coors) #1908
%%time
get_label_roi(141, 391, list_coors) #10013_135
%%time
get_label_roi(260, 463, list_coors) #10013_135<jupyter_output>CPU times: user 10 µs, sys: 1e+03 ns, total: 11 µs
Wall time: 14.8 µs<jupyter_text>Set up to run on all points<jupyter_code>list_dicts = []
list_dicts.append(get_label_roi(392, 433, list_coors)) #1908
list_dicts.append(get_label_roi(141, 391, list_coors)) #10013_135
list_dicts.append(get_label_roi(260, 463, list_coors)) #10013_135
list_dicts
df = pd.DataFrame(list_dicts)
df
df_pivot = df.unstack()
df_pivot
list_dicts = []
list_xy = [[392, 433], [141, 391], [260, 463]]
image_names = ['1908', '10013', '11206']

for xy, image_name in zip(list_xy, image_names):
    my_dict = get_label_roi(392, 433, list_coors)
    my_dict['img_id'] = image_name
    list_dicts.append(my_dict)
list_dicts

df = pd.DataFrame(list_dicts)
df = df.set_index('img_id')
df_pivot = df.unstack()
df_pivot
df_pivot.index
# Try to reorganize index
# https://jakevdp.github.io/PythonDataScienceHandbook/03.05-hierarchical-indexing.html<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1139517896274653, 'fraction_numerical': 0.07791575359142927, 'mean_word_length': 5.32, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 16, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '482679', 'license': 'MIT', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1713, 'n_words': 388, 'path': 'label-crops/2020-03-24-EDA-ROI-Size.ipynb', 'repo_name': 'w210-accessibility/classify-streetview', 'size': '4107'}