def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['Greyscale PNG - source image to binarize'][0]['resource_path']) image_prelim = load_image(inputs['Onebit PNG - preliminary binarization of the image'][0]['resource_path']) image_result = image_source.gatos_background(image_prelim, settings['Region size']) for i in range(len(outputs['Greyscale PNG - background estimation image'])): image_result.save_PNG(outputs['Greyscale PNG - background estimation image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['Source image'][0]['resource_path']) image_mask = load_image(inputs['Mask image'][0]['resource_path']) image_result = image_source.xor_image(image_mask) for i in range(len(outputs['Source image with mask applied'])): image_result.save_PNG(outputs['Source image with mask applied'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['Source image'][0]['resource_path']) image_mask = load_image(inputs['Mask image'][0]['resource_path']) image_result = image_source.and_image(image_mask) for i in range(len(outputs['Source image with mask applied'])): image_result.save_PNG( outputs['Source image with mask applied'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): if '@polygon_outer_points' not in settings: task_image = load_image(inputs['PNG image'][0]['resource_path']) polygon_outer_points = '[]' if 'Polygons' in inputs: with open(inputs['Polygons'][0]['resource_path'], 'r') as myfile: polygon_outer_points = myfile.read().replace('\n', '') settings_update = { '@image_width': task_image.ncols, '@polygon_outer_points': polygon_outer_points } return self.WAITING_FOR_INPUT(settings_update) else: polygon_outer_points = settings['@polygon_outer_points'] # If image output port, write image if 'PNG image (masked)' in outputs: task_image = load_image( inputs['PNG image'][0]['resource_path']) mask_img = Image.new('L', (task_image.ncols, task_image.nrows), color='white') mask_drawer = ImageDraw.Draw(mask_img) for polygon in polygon_outer_points: flattened_poly = [j for i in polygon for j in i] mask_drawer.polygon(flattened_poly, outline='black', fill='black') del mask_drawer task_image_rgb = task_image.to_rgb( ) # Because gamera masking doesn't work on onebit or grey16 images. segment_mask = from_pil(mask_img).to_onebit() result_image_rgb = task_image_rgb.mask(segment_mask) result_image = ensure_pixel_type( result_image_rgb, outputs['PNG image (masked)'][0]['resource_type']) result_image.save_PNG( outputs['PNG image (masked)'][0]['resource_path']) # If polygons image output port, write poly file if 'Polygons' in outputs: poly_string = str(polygon_outer_points) f = open(outputs['Polygons'][0]['resource_path'], 'w') f.write(poly_string) f.close()
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['Greyscale PNG image'][0]['resource_path']) image_result = image_source.bernsen_threshold(settings['Storage format'], settings['Region size'], settings['Contrast limit'], settings['Doubt to black']) for i in range(len(outputs['Onebit PNG image'])): image_result.save_PNG(outputs['Onebit PNG image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['RGB PNG image'][0]['resource_path']) image_result = image_source.djvu_threshold(settings['Smoothness'], settings['Maximum block size'], settings['Minimum block size'], settings['Block factor']) for i in range(len(outputs['Onebit PNG image'])): image_result.save_PNG(outputs['Onebit PNG image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_result = load_image(inputs['Onebit PNG image'][0]['resource_path']) processed_image = image_result.dilate() for i in range(len(outputs['Onebit PNG dilated image'])): processed_image.save_PNG(outputs['Onebit PNG dilated image'][i]['resource_path']) return True
def preconfigure_settings(self, page_url, settings): init_gamera() task_image = load_image(page_url) miyao_settings = settings.copy() del miyao_settings['image_width'] return {'image_width': task_image.ncols}
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['Onebit PNG image'][0]['resource_path']) image_result = image_source.stablePathDetection(settings['Trimming'], settings['Deletion'], settings['Staff fixing']) for i in range(len(outputs['RGB PNG image'])): image_result.save_PNG(outputs['RGB PNG image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_source = load_image( inputs['Greyscale PNG - source image to binarize'][0] ['resource_path']) image_prelim = load_image( inputs['Onebit PNG - preliminary binarization of the image'][0] ['resource_path']) image_result = image_source.gatos_background(image_prelim, settings['Region size']) for i in range( len(outputs['Greyscale PNG - background estimation image'])): image_result.save_PNG( outputs['Greyscale PNG - background estimation image'][i] ['resource_path']) return True
def OnItemSelected(self, e): filename = self.GetFilePath() if filename == '': return wx.BeginBusyCursor() try: filename = filename.encode('utf8') except Exception: pass try: try: image = core.load_image(filename) except Exception, e: gui_util.message( "Loading image %s failed. \n\nThe error was:\n%s" % (filename, str(e))) return finally: wx.EndBusyCursor() width, height = self.image_display.id.GetSize() scale = max( float(width) / float(image.width), (float(height) / float(image.height))) self.image_display.id.set_image(image, weak=0) self.image_display.id.scale(scale)
def do_tests(filename): image = core.load_image(filename) glyphs = image.cc_analysis() g = make_spanning_tree(glyphs) g_orig = g.copy() c = make_subtrees_stddev(g, 1.5) gamera_xml.glyphs_to_xml( c, os.path.abspath( os.path.dirname(filename) + "cluster_1_5_" + os.path.basename(filename))) g = g_orig.copy() c = make_subtrees_stddev(g, 2.0) gamera_xml.glyphs_to_xml( c, os.path.abspath( os.path.dirname(filename) + "cluster_2_0_" + os.path.basename(filename))) g = g_orig.copy() c = make_subtrees_stddev(g, 2.5) gamera_xml.glyphs_to_xml( c, os.path.abspath( os.path.dirname(filename) + "cluster_2_5_" + os.path.basename(filename))) g = g_orig.copy() c = make_subtrees_stddev(g, 3.0) gamera_xml.glyphs_to_xml( c, os.path.abspath( os.path.dirname(filename) + "cluster_3_0_" + os.path.basename(filename)))
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['PNG image'][0]['resource_path']) image_result = image_source.to_greyscale() for i in range(len(outputs['Greyscale PNG image'])): image_result.save_PNG(outputs['Greyscale PNG image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['PNG image'][0]['resource_path']) image_source.invert() for i in range(len(outputs['PNG image'])): image_source.save_PNG(outputs['PNG image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['Greyscale PNG image'][0]['resource_path']) image_result = image_source.niblack_threshold(settings['Region size'], settings['Sensitivity'], settings['Lower bound'], settings['Upper bound']) for i in range(len(outputs['Onebit PNG image'])): image_result.save_PNG(outputs['Onebit PNG image'][i]['resource_path']) return True
def run_task(self, result_id, runjob_id, *args, **kwargs): """Note: Even if you decide to override the entire run method, make sure you load and save images with gamera methods, and not PIL methods. Otherwise the loaded/saved images often don't have the proper pixel types, and becomes unsuitable for use in a workflow with other gamera-based jobs.""" runjob = RunJob.objects.get(pk=runjob_id) if runjob.needs_input: if runjob.status == RunJobStatus.RUN_ONCE_WAITING: self.retry(args=[result_id, runjob_id], *args, countdown=10, **kwargs) else: # This is the first time the job is running. taskutil.set_running(runjob) page_url = taskutil.get_page_url(runjob, result_id) settings = taskutil.get_settings(runjob) updates = self.preconfigure_settings(page_url, settings) taskutil.apply_updates(runjob, updates) taskutil.set_run_once_waiting(runjob) self.retry(args=[result_id, runjob_id], *args, countdown=10, **kwargs) else: taskutil.set_running(runjob) page_url = taskutil.get_page_url(runjob, result_id) settings = taskutil.get_settings(runjob) init_gamera() task_image = load_image(page_url) result_image = self.process_image(task_image, settings) result = taskutil.save_result_as_png(result_image, runjob) return str(result.uuid)
def test_interactive_classifier(): # We assume the XML reading/writing itself is fine (given # test_xml), but we should test the wrappers in classify anyway image = load_image("data/testline.png") ccs = image.cc_analysis() classifier = knn.kNNInteractive([], features=featureset) assert classifier.is_interactive() assert len(classifier.get_glyphs()) == 0 classifier.from_xml_filename("data/testline.xml") assert len(classifier.get_glyphs()) == 66 _test_classification(classifier, ccs) _test_training(classifier, ccs) length = len(classifier.get_glyphs()) # subtract len(group_parts) because to_xml_filename() does # not save "_group._part" group_parts = [x for x in classifier.get_glyphs() if x.get_main_id().startswith("_group._part")] length = length - len(group_parts) classifier.to_xml_filename("tmp/testline_classifier.xml") classifier.from_xml_filename("tmp/testline_classifier.xml") assert len(classifier.get_glyphs()) == length classifier.merge_from_xml_filename("data/testline.xml") assert len(classifier.get_glyphs()) == length + 66 classifier.clear_glyphs() assert len(classifier.get_glyphs()) == 0 classifier.from_xml_filename("data/testline.xml") assert len(classifier.get_glyphs()) == 66
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['Onebit PNG image'][0]['resource_path']) image_result = image_source.removeStaves() for i in range(len(outputs['Onebit PNG image (staves removed)'])): image_result.save_PNG(outputs['Onebit PNG image (staves removed)'][i]['resource_path']) return True
def test_textline_reading_order(): from gamera.core import load_image, init_gamera init_gamera() from gamera.plugins.pagesegmentation import textline_reading_order correct_orders = { "data/reading_order_2.png": [(42, 40, 1462, 114), (42, 158, 683, 232), (42, 276, 683, 350), (42, 418, 683, 492), (42, 560, 683, 633), (822, 158, 1462, 633), (42, 701, 1462, 775), (42, 843, 494, 917), (562, 843, 1132, 917), (42, 985, 683, 1059), (822, 985, 1132, 1059), (1200, 843, 1462, 1059)], "data/reading_order.png": [(51, 56, 1471, 130), (51, 174, 691, 248), (51, 292, 691, 366), (51, 434, 691, 508), (51, 576, 691, 649), (830, 174, 1471, 508), (830, 576, 1471, 649), (51, 717, 1471, 791), (51, 859, 691, 933), (51, 1001, 691, 1075), (830, 859, 1471, 933), (830, 1001, 1471, 1075)] } for file, correct in list(correct_orders.items()): img = load_image(file) ccs = img.cc_analysis() ro = textline_reading_order(ccs) result = [(a.ul_x, a.ul_y, a.lr_x, a.lr_y) for a in ro] assert result == correct del ro del ccs del img
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['Greyscale PNG image'][0]['resource_path']) image_result = image_source.otsu_threshold(settings['Storage format']) for i in range(len(outputs['Onebit PNG image'])): image_result.save_PNG(outputs['Onebit PNG image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_result = load_image(inputs['Onebit PNG image'][0]['resource_path']) image_result.despeckle(settings['Connected component size']) for i in range(len(outputs['Onebit PNG despeckled image'])): image_result.save_PNG(outputs['Onebit PNG despeckled image'][i]['resource_path']) return True
def get_generic_images(): images = {} for i in [ONEBIT, RGB, GREYSCALE, GREY16]: pixel_type_name = util.get_pixel_type_name(i) images[i] = core.load_image(os.path.join( paths.test, pixel_type_name + "_generic.tiff")) for i in (FLOAT, COMPLEX): image = core.load_image(os.path.join( paths.test, "GreyScale_generic.tiff")) if i == FLOAT: images[i] = _image_conversion.to_float(image) elif i == COMPLEX: try: images[i] = _image_conversion.to_complex(image) except: pass return images
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['PNG image'][0]['resource_path']) image_result = image_source.to_rgb() image_result.save_PNG(outputs['RGB PNG image'][0]['resource_path']) for i in range(len(outputs['RGB PNG image'])): image_result.save_PNG(outputs['RGB PNG image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): base = load_image(inputs['Base Layer'][0]['resource_path']) dirty = load_image(inputs['Dirty Layer'][0]['resource_path']) kwargs = { 'despeckle_size': 500, 'density': 0.3, } dlr = DirtyLayerRepairman(**kwargs) image = dlr.run(base, dirty) outfile_path = outputs['Repaired Base Layer'][0]['resource_path'] image.save_PNG(outfile_path) return True
def get_generic_images(): images = {} for i in [ONEBIT, RGB, GREYSCALE, GREY16]: pixel_type_name = util.get_pixel_type_name(i) images[i] = core.load_image( os.path.join(paths.test, pixel_type_name + "_generic.tiff")) for i in (FLOAT, COMPLEX): image = core.load_image( os.path.join(paths.test, "GreyScale_generic.tiff")) if i == FLOAT: images[i] = _image_conversion.to_float(image) elif i == COMPLEX: try: images[i] = _image_conversion.to_complex(image) except Exception: pass return images
def run_my_task(self, inputs, settings, outputs): image_result = load_image( inputs['Onebit PNG image'][0]['resource_path']) processed_image = image_result.dilate() for i in range(len(outputs['Onebit PNG dilated image'])): processed_image.save_PNG( outputs['Onebit PNG dilated image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_source = load_image( inputs['Onebit PNG image'][0]['resource_path']) image_result = image_source.removeStaves() for i in range(len(outputs['Onebit PNG image (staves removed)'])): image_result.save_PNG(outputs['Onebit PNG image (staves removed)'] [i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_source = load_image( inputs['Greyscale PNG image'][0]['resource_path']) image_result = image_source.brink_threshold() for i in range(len(outputs['Onebit PNG image'])): image_result.save_PNG( outputs['Onebit PNG image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_result = load_image( inputs['Onebit PNG image'][0]['resource_path']) image_result.despeckle(settings['Connected component size']) for i in range(len(outputs['Onebit PNG despeckled image'])): image_result.save_PNG( outputs['Onebit PNG despeckled image'][i]['resource_path']) return True
def __doc_example1__(images): from gamera.core import load_image from gamera.plugins import _image_conversion import os path = os.path.join("src","images","a_square.png") image = load_image(path) cimage = _image_conversion.to_complex(image) dfted = cimage.dft() return [image, dfted]
def run_my_task(self, inputs, settings, outputs): image_source = load_image(inputs['RGB PNG image'][0]['resource_path']) image_result = image_source.djvu_threshold( settings['Smoothness'], settings['Maximum block size'], settings['Minimum block size'], settings['Block factor']) for i in range(len(outputs['Onebit PNG image'])): image_result.save_PNG( outputs['Onebit PNG image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_source = load_image( inputs['Greyscale PNG image'][0]['resource_path']) image_result = image_source.tsai_moment_preserving_threshold( settings['Storage format']) for i in range(len(outputs['Onebit PNG image'])): image_result.save_PNG( outputs['Onebit PNG image'][i]['resource_path']) return True
def run_my_task(self, inputs, settings, outputs): image_source = load_image( inputs['Onebit PNG image'][0]['resource_path']) image_result = image_source.stablePathDetection( settings['Trimming'], settings['Deletion'], settings['Staff fixing']) for i in range(len(outputs['RGB PNG image'])): image_result.save_PNG(outputs['RGB PNG image'][i]['resource_path']) return True
def __doc_example1__(images): from gamera.core import load_image from gamera.plugins import _image_conversion import os path = os.path.join("src","images","a_square.png") image = load_image(path) cimage = _image_conversion.to_complex(image) dfted = cimage.dft() poled = dfted.log_polar() logged = poled.log_scale_pixels() return [image, dfted, poled, logged]
def __doc_example1__(images): from gamera.core import load_image from gamera.plugins import _image_conversion import os path = os.path.join("src","images","a_square.png") image = load_image(path) cimage = _image_conversion.to_complex(image) cimage2 = cimage.pad_to_power_of_2() dfted = cimage.dft() moved = dfted.move_dc_to_center() return [image, dfted, moved]
def get(self, thresh_method): #retrieve all relevant info t_value = self.get_argument("thresh_value", None) img_url_value = self.get_argument("img_url", None) gam.init_gamera() simple_thresh_obj = threshold.threshold() output_img = simple_thresh_obj(gam.load_image(img_url_value),int(t_value)) gam.save_image(output_img,"./static/resultimages/AntA_1520_1520.1_D.Mu_01_006_simpletresh" + t_value +".tiff") self.write(thresh_method)
def run_my_task(self, inputs, settings, outputs): image_source = load_image( inputs['Greyscale PNG image'][0]['resource_path']) image_result = image_source.bernsen_threshold( settings['Storage format'], settings['Region size'], settings['Contrast limit'], settings['Doubt to black']) for i in range(len(outputs['Onebit PNG image'])): image_result.save_PNG( outputs['Onebit PNG image'][i]['resource_path']) return True
def evaluate_alignment(manuscript, ind, eval_difficult=False, json_dict=None): fname = '{}_{}'.format(manuscript, ind) gt_xml = ET.parse('./ground-truth-alignments/{}_gt.xml'.format(fname)) gt_boxes = [] els = list(gt_xml.getroot()) for el in els: if not el.tag == 'object': continue diff = int(el.find('difficult').text) name = el.find('name').text bb = el.find('bndbox') ul = int(bb.find('xmin').text), int(bb.find('ymin').text) lr = int(bb.find('xmax').text), int(bb.find('ymax').text) gt_boxes.append({ 'syl': name, 'difficult': diff, 'ul': ul, 'lr': lr }) if json_dict: align_boxes = json_dict['syl_boxes'] else: with open('./out_json/{}.json'.format(fname), 'r') as j: align_boxes = json.load(j)['syl_boxes'] raw_image = gc.load_image('./png/' + fname + '_text.png') image, _, _ = preproc.preprocess_images(raw_image, correct_rotation=False) score = {} area_score = {} for box in gt_boxes: if box['difficult'] and not eval_difficult: continue same_syl_boxes = [x for x in align_boxes if x['syl'] in box['syl'] or box['syl'] in x['syl'] ] if not same_syl_boxes: score[box['syl']] = 0 area_score[box['syl']] = 0 continue ints = [intersect(box, x) for x in same_syl_boxes] if not any(ints): score[box['syl']] = 0 area_score[box['syl']] = 0 continue best_box = same_syl_boxes[ints.index(max(ints))] score[box['syl']] = IOU(box, best_box) area_score[box['syl']] = black_area_IOU(box, best_box, image) return (np.mean(score.values()), np.mean(area_score.values()))
def run_my_task(self, inputs, settings, outputs): fn_kwargs = { 'num_lines': settings['Number of lines per staff'], 'crossing_symbols': 'all', 'resolution': 3.0, 'debug': 0 } original_image = load_image(inputs['One-bit PNG image - original'][0]['resource_path']) staff_finder = MusicStaves_rl_roach_tatem(original_image) staff_finder.remove_staves(**fn_kwargs) staff_finder.image.save_PNG(outputs['One-bit PNG image - staff lines removed'][0]['resource_path'])
def run_my_task(self, inputs, settings, outputs): image_source = load_image( inputs['Greyscale PNG image'][0]['resource_path']) image_result = image_source.niblack_threshold(settings['Region size'], settings['Sensitivity'], settings['Lower bound'], settings['Upper bound']) for i in range(len(outputs['Onebit PNG image'])): image_result.save_PNG( outputs['Onebit PNG image'][i]['resource_path']) return True
def run_task(self, result_id, runjob_id, *args, **kwargs): runjob = RunJob.objects.get(pk=runjob_id) taskutil.set_runjob_status(runjob, RunJobStatus.RUNNING) # fall through to retrying if we're waiting for input if runjob.needs_input: runjob = taskutil.set_runjob_status(runjob, RunJobStatus.WAITING_FOR_INPUT) self.retry(args=[result_id, runjob_id], *args, countdown=10, **kwargs) if runjob.status == RunJobStatus.WAITING_FOR_INPUT: runjob = taskutil.set_runjob_status(runjob, RunJobStatus.RUNNING) if result_id is None: # this is the first job in a run page = runjob.page.compat_file_path else: # we take the page image we want to operate on from the previous result object result = Result.objects.get(pk=result_id) page = result.result.path new_result = Result(run_job=runjob) taskutil.save_instance(new_result) result_save_path = new_result.result_path settings = {} for s in runjob.job_settings: setting_name = "_".join(s['name'].split(" ")) setting_value = argconvert.convert_to_arg_type( s['type'], s['default']) settings[setting_name] = setting_value init_gamera() task_image = load_image(page) tdir = tempfile.mkdtemp() task_function = self.name.split(".")[-1] result_image = getattr(task_image, task_function)(**settings) result_file = "{0}.png".format(str(uuid.uuid4())) result_image.save_image(os.path.join(tdir, result_file)) f = open(os.path.join(tdir, result_file)) taskutil.save_file_field(new_result.result, os.path.join(result_save_path, result_file), File(f)) f.close() shutil.rmtree(tdir) return str(new_result.uuid)
def find_staves(num_lines=0, scanlines=20, blackness=0.8, tolerance=-1): #both 0's can be parameterized, first one is staffline_height and second is staffspace_height, both default 0 staff_finder = musicstaves.StaffFinder_miyao(gam.load_image("static/images/testim.tiff"), 0, 0) staff_finder.find_staves(num_lines, scanlines, blackness, tolerance) poly_list = staff_finder.get_polygon() poly_json_list = __create_polygon_json_dict(poly_list) encoded = json.dumps(poly_json_list) output_file_name = "static/json_in/imdata.json" with open(output_file_name, "w") as f: f.write(encoded)
def run_my_task(self, inputs, settings, outputs): task_image = load_image( inputs['Greyscale or one-bit PNG image'][0]['resource_path']) staff_finder = StaffFinder_miyao(task_image, 0, 0) fn_kwargs = { 'num_lines': settings['Number of lines'], 'scanlines': settings['Scan lines'], 'blackness': settings['Blackness'], 'tolerance': settings['Tolerance'], } staff_finder.find_staves(**fn_kwargs) poly_list = staff_finder.get_polygon() poly_list = fix_poly_point_list(poly_list, staff_finder.staffspace_height) poly_json_list = create_polygon_outer_points_json_dict(poly_list) # If poly output, save it if 'Polygons (Miyao results)' in outputs: poly_string = str(poly_json_list) f = open(outputs['Polygons (Miyao results)'][0]['resource_path'], 'w') f.write(poly_string) f.close() # If image output, save it if 'PNG image (Miyao results used as mask)' in outputs: mask_img = Image.new('L', (task_image.ncols, task_image.nrows), color='white') mask_drawer = ImageDraw.Draw(mask_img) for polygon in poly_json_list: flattened_poly = [j for i in polygon for j in i] mask_drawer.polygon(flattened_poly, outline='black', fill='black') del mask_drawer task_image_rgb = task_image.to_rgb( ) #Because gamera masking doesn't work on onebit or grey16 images. segment_mask = from_pil(mask_img).to_onebit() result_image_rgb = task_image_rgb.mask(segment_mask) result_image = ensure_pixel_type( result_image_rgb, outputs['PNG image (Miyao results used as mask)'][0] ['resource_type']) for i in range( len(outputs['PNG image (Miyao results used as mask)'])): result_image.save_PNG( outputs['PNG image (Miyao results used as mask)'][i] ['resource_path'])
def generate_generic_pngs(self): print("Copying over generic images") for pixel_type in ALL: if pixel_type in (FLOAT, COMPLEX): pixel_type_name = "GreyScale" else: pixel_type_name = util.get_pixel_type_name(pixel_type) image = core.load_image( os.path.join(paths.test, pixel_type_name + "_generic.tiff")) print(" " + image.pixel_type_name) _png_support.save_PNG( image, os.path.join(self.output_images_path, "%s_generic.png" % (pixel_type_name)))
def run_my_task(self, inputs, settings, outputs): if '@polygon_outer_points' not in settings: task_image = load_image(inputs['PNG image'][0]['resource_path']) polygon_outer_points = '[]' if 'Polygons' in inputs: with open(inputs['Polygons'][0]['resource_path'], 'r') as myfile: polygon_outer_points = myfile.read().replace('\n', '') settings_update = {'@image_width': task_image.ncols, '@polygon_outer_points': polygon_outer_points} return self.WAITING_FOR_INPUT(settings_update) else: polygon_outer_points = settings['@polygon_outer_points'] # If image output port, write image if 'PNG image (masked)' in outputs: task_image = load_image(inputs['PNG image'][0]['resource_path']) mask_img = Image.new('L', (task_image.ncols, task_image.nrows), color='white') mask_drawer = ImageDraw.Draw(mask_img) for polygon in polygon_outer_points: flattened_poly = [j for i in polygon for j in i] mask_drawer.polygon(flattened_poly, outline='black', fill='black') del mask_drawer task_image_rgb = task_image.to_rgb() # Because gamera masking doesn't work on onebit or grey16 images. segment_mask = from_pil(mask_img).to_onebit() result_image_rgb = task_image_rgb.mask(segment_mask) result_image = ensure_pixel_type(result_image_rgb, outputs['PNG image (masked)'][0]['resource_type']) result_image.save_PNG(outputs['PNG image (masked)'][0]['resource_path']) # If polygons image output port, write poly file if 'Polygons' in outputs: poly_string = str(polygon_outer_points) f = open(outputs['Polygons'][0]['resource_path'], 'w') f.write(poly_string) f.close()
def generate_generic_pngs(self): print "Copying over generic images" for pixel_type in ALL: if pixel_type in (FLOAT, COMPLEX): pixel_type_name = "GreyScale" else: pixel_type_name = util.get_pixel_type_name(pixel_type) image = core.load_image( os.path.join(paths.test, pixel_type_name + "_generic.tiff")) print " " + image.pixel_type_name _png_support.save_PNG( image, os.path.join(self.output_images_path, "%s_generic.png" % (pixel_type_name)))
def run_my_task(self, inputs, settings, outputs): transcript = align.read_file(inputs['Transcript'][0]['resource_path']) raw_image = gc.load_image(inputs['Text Layer'][0]['resource_path']) syl_boxes, _, lines_peak_locs = align.process(raw_image, transcript, wkdir_name='test') # test_string = str(syl_boxes) outfile_path = outputs['JSON'][0]['resource_path'] with open(outfile_path, 'w') as file: json.dump(align.to_JSON_dict(syl_boxes, lines_peak_locs), file) return True
def do_tests(filename): image = core.load_image(filename) glyphs = image.cc_analysis() g = make_spanning_tree(glyphs) g_orig = g.copy() c = make_subtrees_stddev(g, 1.5) gamera_xml.glyphs_to_xml(c, os.path.abspath(os.path.dirname(filename) + "cluster_1_5_" + os.path.basename(filename))) g = g_orig.copy() c = make_subtrees_stddev(g, 2.0) gamera_xml.glyphs_to_xml(c, os.path.abspath(os.path.dirname(filename) + "cluster_2_0_" + os.path.basename(filename))) g = g_orig.copy() c = make_subtrees_stddev(g, 2.5) gamera_xml.glyphs_to_xml(c, os.path.abspath(os.path.dirname(filename) + "cluster_2_5_" + os.path.basename(filename))) g = g_orig.copy() c = make_subtrees_stddev(g, 3.0) gamera_xml.glyphs_to_xml(c, os.path.abspath(os.path.dirname(filename) + "cluster_3_0_" + os.path.basename(filename)))
def preconfigure_settings(self, page_url, settings): init_gamera() task_image = load_image(page_url) ranked_page = task_image.rank(9, 9, 0) miyao_settings = settings.copy() del miyao_settings['polygon_outer_points'], miyao_settings['image_width'] staff_finder = StaffFinder_miyao(ranked_page, 0, 0) staff_finder.find_staves(**miyao_settings) poly_list = staff_finder.get_polygon() poly_list = fix_poly_point_list(poly_list, staff_finder.staffspace_height) poly_json_list = create_polygon_outer_points_json_dict(poly_list) return {'polygon_outer_points': poly_json_list, 'image_width': task_image.ncols}
def test_noninteractive_classifier(): # We assume the XML reading/writing itself is fine (given # test_xml), but we should test the wrappers in classify anyway image = load_image("data/testline.png") ccs = image.cc_analysis() database = gamera_xml.glyphs_from_xml("data/testline.xml") classifier = knn.kNNNonInteractive(database, features=featureset, normalize=False) assert not classifier.is_interactive() assert len(classifier.get_glyphs()) == 66 _test_classification(classifier, ccs) classifier.serialize("tmp/serialized.knn") classifier.clear_glyphs() assert len(classifier.get_glyphs()) == 0 classifier.unserialize("tmp/serialized.knn")
def post(self): """ Saves original uploaded file, then file as png. """ try: fulln = self.request.files["image"][0]["filename"] filen, ext = os.path.splitext(fulln) # WHICH EXTENSIONS TO ACCEPT??? HANDLE HERE??? f = open("static/img/{0}".format(fulln), "w") f.write(self.request.files["image"][0]['body']) f.close() # convert img to greyscale first (requirement of most thresholding functions) image = gam.load_image("static/img/{0}".format(fulln)) image.save_image("static/img/{0}_original{1}".format(filen, ext)) image = image.to_greyscale() image.save_PNG("static/img/{0}.png".format(filen)) self.redirect("/binarize?filen={0}.png".format(filen)) except KeyError: self.redirect("/")
def OnItemSelected(self, e): filename = self.GetFilePath() if filename == '': return wx.BeginBusyCursor() try: try: image = core.load_image(filename) except Exception, e: gui_util.message("Loading image %s failed. \n\nThe error was:\n%s" % (filename, str(e))) return finally: wx.EndBusyCursor() width, height = self.image_display.id.GetSize() scale = max(float(width) / float(image.width), (float(height) / float(image.height))) self.image_display.id.set_image(image, weak=0) self.image_display.id.scale(scale)
def get(self): if not self.is_xhr(): # not an ajax request - show binarize page filen = self.get_argument("filen") self.render("templates/binarize.html", filen=filen) else: # ajax request - get parameters and re-binarize data = tornado.escape.json_decode(self.get_argument("data")) path = os.path.dirname(data["filen"]) filen, ext = os.path.splitext(os.path.basename(data["filen"])) loadn = "{0}/{1}{2}".format(path, filen, ext) newn = "{0}/{1}_{2}{3}".format(path, filen, randint(1, 1000000), ext) # check if file already binarized & saved if not os.path.exists(newn): image = gam.load_image(loadn) binarized = BinarizationHandler.binarize(self, image, **data) binarized.save_PNG(newn) logging.info("binarized image %s to %s" % (filen, newn)) self.write(newn)
def try_params(params): gts = [{ 'manuscript': 'salzinnes', 'folio': '020v', 'text_func': pcc.filename_to_text_func('./csv/123723_Salzinnes.csv', './csv/mapping.csv'), 'ocr_model': './models/salzinnes_model-00054500.pyrnn.gz' }, { 'manuscript': 'salzinnes', 'folio': 25, 'text_func': pcc.filename_to_text_func('./csv/123723_Salzinnes.csv', './csv/mapping.csv'), 'ocr_model': './models/salzinnes_model-00054500.pyrnn.gz' }, { 'manuscript': 'stgall390', 'folio': '023', 'text_func': pcc.filename_to_text_func('./csv/stgall390_123717.csv'), 'ocr_model': './models/stgall2-00017000.pyrnn.gz' }] results = [] for x in gts: f_ind, transcript = x['text_func'](x['folio']) manuscript = x['manuscript'] fname = '{}_{}'.format(manuscript, f_ind) ocr_model = x['ocr_model'] ocr_pickle = './pik/{}_boxes.pickle'.format(fname) text_layer_fname = './png/{}_text.png'.format(fname) raw_image = gc.load_image('./png/' + fname + '_text.png') result = atocr.process(raw_image, transcript, ocr_model, seq_align_params=params, existing_ocr_pickle=ocr_pickle) syl_boxes, image, lines_peak_locs, all_chars = result json_dict = atocr.to_JSON_dict(syl_boxes, lines_peak_locs) res = evaluate_alignment(manuscript, f_ind, eval_difficult=False, json_dict=json_dict) with open('./pik/{}_boxes.pickle'.format(fname), 'wb') as f: pickle.dump(all_chars, f, -1) results.append(res[1]) return np.mean(results)
def process_image(self, segmented_image_path, xml_filepath, settings, page_order): segmented_image = load_image(segmented_image_path) rank_image = segmented_image.rank(9, 9, 0) try: aomr_obj = AomrObject(rank_image, discard_size=settings['discard_size'], lines_per_staff=4, staff_finder=0, staff_removal=0, binarization=0) glyphs = gamera.gamera_xml.glyphs_from_xml(xml_filepath) recognized_glyphs = aomr_obj.run(glyphs) data = json.loads(recognized_glyphs) mei_file = AomrMeiOutput(data, str(segmented_image_path), str(page_order)) except AomrUnableToFindStavesError as e: #if something goes wrong, this will create an empty mei file (instead of crashing) print e mei_file = AomrMeiOutput({}, segmented_image_path, str(page_order)) return mei_file.md