def load_style_template_images(style_num): return DB.load_template_images(**{'style_num': style_num})
def create_redlined_images(argsdict, style_num, rois_map_df): """ Given the rois map and style_num, access image and create 'redlined' images which have boxes drawn around contests, options, and targets, and text shown providing official names of contests and options. """ style_template_images = DB.load_template_images(style_num=style_num) rois_map_df = rois_map_df.replace(np.nan, 0) contest_roi_outline_width = 2 contest_text_x_os = 5 contest_text_y_os = -5 # offset from bottom of contest box contest_font_scale = 0.5 option_roi_outline_width = 1 #option_text_x_os = 80 #option_text_y_os = -5 # offset from bottom of option box option_font_scale = 0.5 checkbox_outline_width = 1 # integer required target_side = argsdict.get('target_side', 'left') rgb_images = [None, None] # first convert the (up to two) template images to RGB. for pg, grayscale_image in enumerate(style_template_images): rgb_images[pg] = cv2.cvtColor(grayscale_image, cv2.COLOR_GRAY2RGB) # consider only the records with the style_num indicated. Note that the # rois_map_df includes all styles processed so far. # note that 'style_num' field in the rois_map is a str. style_rois_map_df = rois_map_df.loc[rois_map_df['style_num'] == style_num] layout_params = get_layout_params(argsdict) target_w_os = round(layout_params['target_area_w'] / 2) target_h_os = round(layout_params['target_area_h'] / 2) for idx in range(len(style_rois_map_df.index)): style_rois_dict = style_rois_map_df.iloc[idx] roi = {} page0, roi['x'], roi['y'], roi['w'], roi['h'], \ roi['blk_x'], roi['blk_y'], roi['blk_w'], roi['blk_h'] \ = [int(x) for x in list(style_rois_dict['roi_coord_csv'].split(r','))] contest = style_rois_dict['contest'] option = style_rois_dict['option'] target_x = style_rois_dict['target_x'] target_y = style_rois_dict['target_y'] if bool(re.match(r'#', option)): # contest header, draw red box around countest header, write in contest name draw_one_box(rgb_images[page0], roi, color='red', line_width=contest_roi_outline_width) text_x = roi['x'] + contest_text_x_os text_y = roi['y'] + roi['h'] + contest_text_y_os draw_text(rgb_images[page0], text_x, text_y, text=contest, color='red', font_scale=contest_font_scale) else: # option rois draw_one_box(rgb_images[page0], roi, color='blue', line_width=option_roi_outline_width) draw_checkbox(rgb_images[page0], target_x, target_y, layout_params, line_width=checkbox_outline_width) ev_coord_str = str(style_rois_dict['ev_coord_str']) text = option if ev_coord_str: text += ' [' + ev_coord_str + ']' target_w_os = round(layout_params['target_area_w'] / 2) target_h_os = round(layout_params['target_area_h'] / 2) if target_side == 'left': text_x = target_x - target_w_os text_y = target_y - target_h_os - 2 ref = 'bl' else: text_x = target_x + target_w_os text_y = target_y - target_h_os - 2 ref = 'br' draw_text(rgb_images[page0], text_x, text_y, text=text, color='blue', font_scale=option_font_scale, ref=ref) DB.save_template_images(style_num=style_num, images=rgb_images, file_type='redlined')