def simplify(multi_geometry, single_geometry, length, max_vertices): '''Function that takes in a polygon and returns a simplified version taking the convex hull of the region.''' # Convert to GeoPandas Series and perform convex hull operation polygons = gpd.GeoSeries(multi_geometry) polygons_hull = polygons.convex_hull.item() # Format into CMR queryable polygon complex_hull_cmr_poly = format_polygon(polygons_hull) ####################### # Map the reduction of vertices to a tanh function reduction = int(max_vertices * np.tanh((1/max_vertices) * length)) # Initial simplification points = list(single_geometry.exterior.coords) simplifier = vw.Simplifier(points) visval_poly_points = simplifier.simplify(number=reduction) visval_poly = Polygon(visval_poly_points) visval_cmr_poly = format_polygon(visval_poly) ###################### bbox = str(polygons_hull.bounds) cmr_bbox = re.sub(r'[()\s+]', '', bbox) return complex_hull_cmr_poly, visval_cmr_poly, cmr_bbox
def simplify(self, thresh=None, nb_points=None): simplifier = vw.Simplifier(self.array) self.simplified = simplifier.simplify(threshold=thresh) if len(self.simplified) <= 2: self.simplified = None if nb_points is not None: if len(self.array) <= nb_points: self.simplified = self.array.copy() elif self.simplified is None or len(self.simplified) <= nb_points: self.simplified = simplifier.simplify(number=nb_points) else: pass # Checking the geometry and attempting to resolve it with shapely self.simplified = resolve_geometry(self.simplified) # poly = geometry.Polygon(self.simplified) # if not poly.is_valid: # poly = poly.buffer(0) # try: # x, y = poly.exterior.coords.xy # except AttributeError: # # We have a MultiPolygon! We just select the biggest one. # poly = list(poly) # poly = sorted(poly, key=lambda a: a.area) # poly = poly[0] # x, y = poly.exterior.coords.xy # # poly = list(zip(x, y)) # poly = [list(a) for a in poly] # self.simplified = np.array(poly) print(len(self.simplified))
def process_json(path): print(path) with open(path, 'r', encoding='utf-8') as f: data = json.loads(f.read()) out = [] for feature in data['features']: if not feature['geometry']['coordinates']: continue #print(feature) admin2_code = feature['id'] try: ja, en = id_to_city_names[admin2_code] except KeyError: import traceback traceback.print_exc() continue coordinates_out = [] for i_coords in feature['geometry']['coordinates']: simplifier = vw.Simplifier(i_coords) simplified = ([list(i) for i in simplifier.simplify(ratio=0.02)] # or #[list(i) for i in simplifier.simplify(number=4)] ) if len(simplified) < 3: # Probably can ignore continue coordinates_out.append(simplified) out.append({ "type": "Feature", "geometry": { "type": "Polygon", "coordinates": coordinates_out }, "properties": { 'admin2_code': admin2_code, 'ja': ja, 'en': en } }) return out
def runtest(img,model,dataset,filename,name): import json # image=img image=cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # print(image.shape,"image shape1") image,_,scale,scale1,padding,_=utils.resize_image(image,min_dim=256, max_dim=1024) # print(scale, " ", scale1) results = model.detect([image], verbose=1) # print("iii") ax = get_ax(1) # print("iiii") r = results[0] # print(image.shape,"image shape before contours") ccc,contours=visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'], ax=ax, title="Predictions",show_bbox=False,show_mask=True) # print(len(contours)) arr = np.array(contours) # print(arr[0][0][0][0],"shape") # print(contours[0][0],"contours") # print(contours[0]) cls=r['class_ids'] classes = ['Background','Hole(Virtual)','Hole(Physical)','Character Line Segment', 'Physical Degradation','Page Boundary','Character Component','Picture', 'Decorator','Library Marker','Boundary Line'] strt=""" { "_via_settings": { "ui": { "annotation_editor_height": 30, "annotation_editor_fontsize": 0.6000000000000001, "leftsidebar_width": 18, "image_grid": { "img_height": 80, "rshape_fill": "none", "rshape_fill_opacity": 0.3, "rshape_stroke": "yellow", "rshape_stroke_width": 2, "show_region_shape": true, "show_image_policy": "all" }, "image": { "region_label": "region_id", "region_label_font": "10px Sans" } }, "core": { "buffer_size": 18, "filepath": {}, "default_filepath": "" }, "project": { "name": "corrected_3" } }, "_via_img_metadata": { "": { "filename": \""""+str(filename)+"""\", "size": -1, "regions": [ """ end=""" ], "file_attributes": {} } }, "_via_attributes": { "region": { "Spatial Annotation": { "type": "dropdown", "description": "", "options": { "Hole(Virtual)": "", "Hole(Physical)": "", "Character Line Segment": "", "Boundary Line": "", "Physical Degradation": "", "Page Boundary": "", "Character Component": "", "Picture": "", "Decorator": "", "Library Marker": "" }, "default_options": {} }, "Comments": { "type": "text", "description": "", "default_value": "" } }, "file": {} }, "file": \""""+str(name)+"""\" } """ rgns="" # print(scale,"scale") # print(scale1,"scale1") arr1 = np.zeros(11) count1 = np.zeros(11) avg1 = np.zeros(11) # print("intial") # print(arr1,"arr") # print(count1,"count") ROOT_DIR = os.path.abspath("./Instance-segmentation-master") perimeter_path = ROOT_DIR+"/main/doc/" loaded_avg = np.load(perimeter_path+'perimeter.npy') # arr = arr # count = count for i in range(len(cls)): if i!=(-1): try: k = np.array(contours[i][0]) except: #error_out = """{ #"error": ["1"] #}""" #error_out = {} #error_out['error'] = 1 return [] # d = k[0][0] # print(type(d),"d") # print(scale,"scale") # k=(k *(1/scale)) k=k.astype(int) arr1=np.array(arr1) # count=count # print(k[0][0]) # k[:,0] = np.rint(k[:,0] * (1/ scale)) # k[:,1] = np.rint(k[:,1] * (1/ scale1)) # d = k[0][0] # print(type(d),"d") k= np.array(k) # print(k.shape,"kshape") # print(k," ith k",i) ln=len(contours[i][0]) mid = int(ln/2) # k1=k[0:mid,:] # k2=k[mid:ln-1,:] k1=k[0:ln-1,:] # print(k1,"ith k1",i) # print(k1.shape,"k1shape") # print(k2,"ith k2",i) # print(k2.shape,"k2shape") # from rdp import rdp # from simplification.cutil import simplify_coords, simplify_coordsvw # from polysimplify import VWSimplifier import visvalingamwyatt as vw # simplifier = vw.Simplifier(points) # Simplify by percentage of points to keep # simplifier.simplify(ratio=0.5) simplifier1 = vw.Simplifier(k1) # simplifier.simplify(ratio=0.5) n1=int(0.020*k1.shape[0]) # if(n1<4): # n1=4 # perimeter perimeter = Polygon(k1).length # print(perimeter,"pppppppppppp") sub_loaded = np.absolute(loaded_avg - perimeter) index_min = np.argmin(sub_loaded) # print(sub_loaded ,"subloaded") # print(index_min,"index_min") # if it is BG if(index_min)==0: n1=int(0.020*k1.shape[0]) #if it is H-V if(index_min)==1: n1=int(0.040*k1.shape[0]) #if it is H if(index_min)==2: n1=int(0.040*k1.shape[0]) # cls if(index_min)==3: n1=int(0.020*k1.shape[0]) # pd if(index_min)==4: n1=int(0.030*k1.shape[0]) # pb if(index_min)==5: n1=int(0.010*k1.shape[0]) # cc if(index_min)==6: n1=int(0.040*k1.shape[0]) # p if(index_min)==7: n1=int(0.040*k1.shape[0]) # d if(index_min)==8: n1=int(0.040*k1.shape[0]) # lm if(index_min)==9: n1=int(0.040*k1.shape[0]) # bl if(index_min)==10: n1=int(0.020*k1.shape[0]) if n1<4: n1=4 # print(n1,"n1") # n2=int(0.025*k2.shape[0]) # print(n2,"n2") rdpk1=np.array(simplifier1.simplify(number=n1)) # simplifier2 = vw.Simplifier(k2) # simplifier.simplify(ratio=0.5) # rdpk2=np.array(simplifier2.simplify(number=n2)) # rdpk1= rdp(k1,epsilon=1) # rdpk2= rdp(k2,epsilon=1) # print(rdpk1,"ith rdpk1",i) # print(rdpk1.shape,"rdpk1shape") # print(rdpk2,"ith rdpk2",i) # print(rdpk2.shape,"rdpk2shape") # final= np.concatenate((rdpk1, rdpk2), axis=0) final=rdpk1 # print(final.shape[0],"finalshape") # print(final[0][0],"final") i = int(i) finallist = np.array(final).tolist() # perimeter # perimeter = Polygon(final).length # # sub_loaded = abs.absolute(loaded_avg - perimeter) # print(perimeter,"perimeter of i - ", i) # print(cls[i]) arr1[cls[i]] += perimeter count1[cls[i]] += 1 avg1[cls[i]] = (arr1[cls[i]]/count1[cls[i]]) # print(arr1,"arr after i - ",i) # print(count1,"count after i - ",i) # print(avg1,"avg after i",i) # storingarray(arr,count) length=final.shape[0] str1="" str2="" for j in range(length): str1+=str(int((final[j][0]-padding[0][0])*(1/scale))) str1+="," str1+='\n' for j in range(length): str2+=str(int((final[j][1]-padding[1][0])*(1/scale))) # g=0 str2+="," str2+='\n' str1=str1[:-2] str2=str2[:-2] rg="""{ "shape_attributes": { "name": "polygon", "all_points_x": [ """+ str2+"""], "all_points_y": ["""+str1+ """] }, "region_attributes": { "Spatial Annotation":\""""+str(classes[cls[i]])+"""\", "Comments": "" }, "timestamp": { "StartingTime": 6016533, "EndTime": 6035060 } }""" if(i!=len(cls)-1): rg+="," rgns+=rg # if(i!=len(cls)-1): # rg+="," # rgns+=rg print("done") # for updating the perimeter values # print(np.load('perimeter.npy'),"old") # loaded_avg = np.load('perimeter.npy') new_avg= np.zeros(11) for i in range(11): if((int(count1[i])!=0) and (int(loaded_avg[i])!=0)): new_avg[i]= (loaded_avg[i] + avg1[i]) new_avg[i] = new_avg[i] / 2 else: new_avg[i] = loaded_avg[i] + avg1[i] np.save(perimeter_path+'perimeter.npy', new_avg) # print(np.load('perimeter.npy'),"new") #end of updating the perimeter values # k=np.array(contours) # print(k,"k") # print(k.shape[0],"shape1") # for i in range(len(cls)): # str1="" # str2="" # ln=len(contours[i][0]) # print(ln,"lrn") # for j in range(ln): # if(j%20==0): # str1+=str(contours[i][0][j][0]-padding[0][0]) # str1+=',' # str1+='\n' # for j in range(ln): # if(j%20==0): # str2+=str(contours[i][0][j][1]-padding[1][0]) # str2+=',' # str2+='\n' # str1=str1[:-2] # str2=str2[:-2] # rg="""{ # "shape_attributes": { # "name": "polygon", # "all_points_x": [ """+ str2+"""], # "all_points_y": ["""+str1+ """] # }, # "region_attributes": { # "Spatial Annotation":\""""+str(classes[cls[i]])+"""\", # "Comments": "" # }, # "timestamp": { # "StartingTime": 6016533, # "EndTime": 6035060 # } # }""" # if(i!=len(cls)-1): # rg+="," # rgns+=rg with open (perimeter_path+'save.json','w') as f: f.write(strt) f.write(rgns) f.write(end) h, w = image.shape[:2] image=image[padding[0][0]:h-padding[0][1],padding[1][0]:w-padding[1][1]] plt.savefig(OUTPUTPATH,bbox_inches='tight') # print(arr) # print(count) #final_output = strt + rgns + end final_output = '{"output": [' + rgns + '], "file": "' + str(name) + '", "url": "' + str(filename) + '"}' #print(final_output) return final_output
def runtest(img,model,dataset): import json # image=img image=cv2.cvtColor(img,cv2.COLOR_BGR2RGB) print(image.shape,"image shape1") image,_,scale,scale1,padding,_=utils.resize_image(image,min_dim=256, max_dim=1024) print(scale, " ", scale1) results = model.detect([image], verbose=1) ax = get_ax(1) r = results[0] print(image.shape,"image shape before contours") ccc,contours=visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'], ax=ax, title="Predictions",show_bbox=False,show_mask=True) print(len(contours)) arr = np.array(contours) print(arr[0][0][0][0],"shape") # print(contours[0][0],"contours") # print(contours[0]) cls=r['class_ids'] classes = ['Background','Hole(Virtual)','Hole(Physical)','Character Line Segment', 'Physical Degradation','Page Boundary','Character Component','Picture', 'Decorator','Library Marker','Boundary Line'] strt=""" { "_via_settings": { "ui": { "annotation_editor_height": 30, "annotation_editor_fontsize": 0.6000000000000001, "leftsidebar_width": 18, "image_grid": { "img_height": 80, "rshape_fill": "none", "rshape_fill_opacity": 0.3, "rshape_stroke": "yellow", "rshape_stroke_width": 2, "show_region_shape": true, "show_image_policy": "all" }, "image": { "region_label": "region_id", "region_label_font": "10px Sans" } }, "core": { "buffer_size": 18, "filepath": {}, "default_filepath": "" }, "project": { "name": "corrected_3" } }, "_via_img_metadata": { "": { "filename": \""""+str(filename)+"""\", "size": -1, "regions": [ """ end=""" ], "file_attributes": {} } }, "_via_attributes": { "region": { "Spatial Annotation": { "type": "dropdown", "description": "", "options": { "Hole(Virtual)": "", "Hole(Physical)": "", "Character Line Segment": "", "Boundary Line": "", "Physical Degradation": "", "Page Boundary": "", "Character Component": "", "Picture": "", "Decorator": "", "Library Marker": "" }, "default_options": {} }, "Comments": { "type": "text", "description": "", "default_value": "" } }, "file": {} } } """ rgns="" print(scale,"scale") print(scale1,"scale1") for i in range(len(cls)): if i!=(-1): k = np.array(contours[i][0]) # d = k[0][0] # print(type(d),"d") # print(scale,"scale") # k=(k *(1/scale)) k=k.astype(int) # print(k[0][0]) # k[:,0] = np.rint(k[:,0] * (1/ scale)) # k[:,1] = np.rint(k[:,1] * (1/ scale1)) # d = k[0][0] # print(type(d),"d") k= np.array(k) print(k.shape,"kshape") # print(k," ith k",i) ln=len(contours[i][0]) mid = int(ln/2) # k1=k[0:mid,:] # k2=k[mid:ln-1,:] k1=k[0:ln-1,:] # print(k1,"ith k1",i) # print(k1.shape,"k1shape") # print(k2,"ith k2",i) # print(k2.shape,"k2shape") from rdp import rdp # from simplification.cutil import simplify_coords, simplify_coordsvw # from polysimplify import VWSimplifier import visvalingamwyatt as vw # simplifier = vw.Simplifier(points) # Simplify by percentage of points to keep # simplifier.simplify(ratio=0.5) simplifier1 = vw.Simplifier(k1) # simplifier.simplify(ratio=0.5) n1=int(0.020*k1.shape[0]) # print(n1,"n1") # n2=int(0.025*k2.shape[0]) # print(n2,"n2") rdpk1=np.array(simplifier1.simplify(number=n1)) # simplifier2 = vw.Simplifier(k2) # simplifier.simplify(ratio=0.5) # rdpk2=np.array(simplifier2.simplify(number=n2)) # rdpk1= rdp(k1,epsilon=1) # rdpk2= rdp(k2,epsilon=1) # print(rdpk1,"ith rdpk1",i) # print(rdpk1.shape,"rdpk1shape") # print(rdpk2,"ith rdpk2",i) # print(rdpk2.shape,"rdpk2shape") # final= np.concatenate((rdpk1, rdpk2), axis=0) final=rdpk1 # print(final.shape[0],"finalshape") print(final[0][0],"final") length=final.shape[0] str1="" str2="" for j in range(length): str1+=str(int((final[j][0]-padding[0][0])*(1/scale))) str1+="," str1+='\n' for j in range(length): str2+=str(int((final[j][1]-padding[1][0])*(1/scale))) # g=0 str2+="," str2+='\n' str1=str1[:-2] str2=str2[:-2] rg="""{ "shape_attributes": { "name": "polygon", "all_points_x": [ """+ str2+"""], "all_points_y": ["""+str1+ """] }, "region_attributes": { "Spatial Annotation":\""""+str(classes[cls[i]])+"""\", "Comments": "" }, "timestamp": { "StartingTime": 6016533, "EndTime": 6035060 } }""" if(i!=len(cls)-1): rg+="," rgns+=rg # if(i!=len(cls)-1): # rg+="," # rgns+=rg # k=np.array(contours) # print(k,"k") # print(k.shape[0],"shape1") # for i in range(len(cls)): # str1="" # str2="" # ln=len(contours[i][0]) # print(ln,"lrn") # for j in range(ln): # if(j%20==0): # str1+=str(contours[i][0][j][0]-padding[0][0]) # str1+=',' # str1+='\n' # for j in range(ln): # if(j%20==0): # str2+=str(contours[i][0][j][1]-padding[1][0]) # str2+=',' # str2+='\n' # str1=str1[:-2] # str2=str2[:-2] # rg="""{ # "shape_attributes": { # "name": "polygon", # "all_points_x": [ """+ str2+"""], # "all_points_y": ["""+str1+ """] # }, # "region_attributes": { # "Spatial Annotation":\""""+str(classes[cls[i]])+"""\", # "Comments": "" # }, # "timestamp": { # "StartingTime": 6016533, # "EndTime": 6035060 # } # }""" # if(i!=len(cls)-1): # rg+="," # rgns+=rg with open ('save.json','w') as f: f.write(strt) f.write(rgns) f.write(end) h, w = image.shape[:2] image=image[padding[0][0]:h-padding[0][1],padding[1][0]:w-padding[1][1]] plt.savefig(OUTPUTPATH,bbox_inches='tight')
def function(year, args=args): extensions = ['nbk', 'gk'] language = ['de', 'en', 'fr'] file_type = ['gif'] origin_paths = [] url = "https://www.slf.ch/fileadmin/user_upload/import/lwdarchiv/public" urls = [] # for y in years: y = year for ext in extensions: for lan in language: for f_type in file_type: origin = os.path.join( *[args.maps_directory, y, ext, lan, f_type]) #,"*."+f_type]) if (Path(origin).exists()): origin_paths.append( os.path.join(*[origin, "*." + f_type])) urls.append("/".join([url, y, ext, lan, f_type])) for i, origin in enumerate(origin_paths): for file_map in glob.glob(origin): basename = os.path.basename(file_map) filename = '{}.json'.format( os.path.splitext(os.path.basename(file_map))[0]) destination = os.path.join(args.out_path, filename) file_url = urls[i] + "/" + basename if Path(destination).exists() and not args.f: print('Skip {} because {} already exists'.format( file_map, destination)) continue img = Image.open(file_map) img = img.convert('RGB') img_arr = np.array(img) img_no_gray = remove_grey(img_arr) color_map = build_color_map(img_no_gray, [white] + color_scale) img_projected = replace_color(img_no_gray, color_map) # load mask of this size try: binary_mask, landmarks_pix = open_mask(*img_arr.shape[:2]) except FileNotFoundError: print('Missing mask "{}x{}.gif" for file "{}"'.format( *img_arr.shape[:2], file_map), file=sys.stderr) continue # keep useful colors regions_only = keep_colors(img_projected, color_scale) # clip the binary mask to remove color key regions_only[~binary_mask] = 255 regions_only = Image.fromarray(regions_only).convert('RGB') smoothed = regions_only.filter(ImageFilter.MedianFilter(7)) pix = np.array(list(map(numpify, landmarks_pix.values()))) coord = np.array(list(map(numpify, landmarks_pix.keys()))) # add 1 bias raw pix_ext = np.vstack([np.ones((1, pix.shape[0])), pix.T]) coord_ext = np.vstack([np.ones((1, pix.shape[0])), coord.T]) T = np.linalg.lstsq(pix_ext.T, coord_ext.T)[0] def transform_pix2map(points): """n x 2 array""" points_ext = np.hstack( [np.ones((points.shape[0], 1)), points]) points_map = points_ext.dot(T) return points_map[:, 1:] geo_json = {"type": "FeatureCollection", "features": []} for danger_level, color in enumerate( [green, yellow, orange, red]): for contour in color_contours(smoothed, color): contour_right = contour.copy() contour_right[:, 0] = contour[:, 1] contour_right[:, 1] = contour[:, 0] contour_right = transform_pix2map(contour_right) simplifier = vw.Simplifier(contour_right) contour_right = simplifier.simplify( threshold=SMOOTHING_THRESHOLD) geo_json['features'].append({ "type": "Feature", "properties": { "date": ".".join([ basename[6:8], basename[4:6], basename[0:4] ]), "danger_level": danger_level + 1, "url": file_url }, "geometry": { "type": "Polygon", "coordinates": [list(reversed(contour_right.tolist()))] } }) with open(destination, 'w') as f: print('{} -> {}'.format(file_map, destination)) json.dump(geo_json, f)
def wyatt(self, points, number_of_wp): simplifier = vw.Simplifier(points[:,3:].astype(np.float)) simplified_path = simplifier.simplify(number_of_wp) return simplified_path