def save_json(self,out_dir): img_name = os.path.split(self.image_path)[-1] data = {} data["textBBs"]=[] self.polygons_mutex.acquire() poly_copy = self.polygons[:] for j,poly in enumerate(poly_copy): pt_data = {} pts = [] for i in range(len(poly.pt_coords)): pts.append([int(poly.pt_coords[i][0]),int(poly.pt_coords[i][1])]) ''' if self.scale_factor!=None: for i in range(len(poly.pt_coords)): pts.append([int(poly.pt_coords[i][0]/self.scale_factor),int(poly.pt_coords[i][1]/self.scale_factor)]) else: for i in range(len(poly.pt_coords)): pts.append([int(poly.pt_coords[i][0]),int(poly.pt_coords[i][1])]) ''' pt_data["poly_points"] = pts pt_data["id"] = str(j) pt_data["type"] = poly.poly_type data["textBBs"].append(pt_data) self.polygons_mutex.release() if not os.path.exists(out_dir): os.makedirs(out_dir) json_file_path = os.path.join(out_dir,'.'.join(img_name.split('.')[:-1])+'.json') print ("Saving json to",json_file_path) debug (2,"No of polygons drawn: "+str(len(data["textBBs"]))) with open(json_file_path,'w') as fl: json.dump(data,fl,indent=4)
def add_poly(self, pts): if self.scale_factor == None: self.bbs.append(pts) self.polygons.append(Polygon(self.root,self.canvas,bb,radius=RADIUS)) else: pts_copy = pts[:] for i in range(len(pts)): for j in range(2): pts_copy[i][j] = pts[i][j] * self.scale_factor self.bbs.append(pts_copy) self.polygons.append(Polygon(self.root,self.canvas,pts_copy,radius=RADIUS)) self.poly_type.append(None) self.polygons[-1].poly_type = None debug (3,"Polygon added, total polygons: "+ str(len(self.polygons)))
def draw_bbs(self,bbs): self.polygons_mutex.acquire() for bb in bbs: if self.scale_factor == None: self.polygons.append(Polygon(self.root,self.canvas,bb,radius=RADIUS)) continue for i in range(len(bb)): for j in range(2): bb[i][j] = bb[i][j] * self.scale_factor self.polygons.append(Polygon(self.root,self.canvas,bb,radius=RADIUS)) for i,pt in enumerate(self.poly_type): self.polygons[i].poly_type = pt self.polygons_mutex.release() debug (3,"Total Polygons drawn:"+str(len(self.polygons)))
def load_json(self): debug (2,"Loading json from:"+self.json_path) try: fl = open(self.json_path,'r') data = json.load(fl) boxes = data["textBBs"] for i,item in enumerate(boxes): debug (2, str(item)) pts = item["poly_points"] for i in range(len(pts)): pts[i][0],pts[i][1] = int(pts[i][0]),int(pts[i][1]) self.bbs.append(pts) self.poly_type.append(item["type"]) debug (2, str(self.poly_type)) fl.close() except: fl = open(self.json_path,'w+') data = {"textBBs":[]} json.dump(data,fl,indent=4) fl.close() debug(3, "Total BBs in json:"+str(len(self.bbs)))