def get_DOM(pid_id, seg_files, saved_path, resolution): seg_dir = os.path.dirname(seg_files[0]) total_cnt = len(seg_files) start_time_all = time.perf_counter() while len(seg_files) > 0: seg_file = seg_files.pop() start_time = time.perf_counter() try: print("Process No.", pid_id, "is processing: ", total_cnt - len(seg_files), seg_file) panoId = os.path.basename(seg_file)[:-4] # pano1 = GSV_pano(panoId=panoId, crs_local=6487, saved_path=saved_path) new_name = os.path.join(saved_path, panoId + f"_DOM_{resolution:.2f}.tif") is_processed = os.path.exists(new_name) Links = pano1.jdata['Links'] for link in Links: temp_name = os.path.join(seg_dir, link['panoId'] + '.png') if temp_name in seg_files: try: seg_files.remove(temp_name) seg_files.append(temp_name) except: pass if is_processed: print("Skip: ", seg_file) continue pano1 = GSV_pano(panoId=panoId, crs_local=6487, saved_path=saved_path) # pano1 = GSV_pano(request_lon = lon, request_lat=lat, saved_path=r'J:\Research\StreetView\gsv_pano\test_results') pano1.set_segmentation_path(full_path=seg_file) DOM = pano1.get_DOM(width=40, height=40, resolution=resolution, zoom=4, img_type='segmentation', fill_clipped_seg=True) total_time = (time.perf_counter() - start_time_all) efficency = total_time / (total_cnt - len(seg_files)) time_remain = efficency * len(seg_files) print( f"Time spent (seconds): {time.perf_counter() - start_time:.1f}, time used: {delta_time(total_time)} , time remain: {delta_time(time_remain)} \n" ) # palette = Image.open(seg_file).getpalette() # palette = np.array(palette,dtype=np.uint8) # pil_img = PIL.Image.fromarray(DOM['DOM']) # pil_img.putpalette(palette) # self.assertEqual((800, 800, 3), DOM.shape) # pil_img.show() except Exception as e: print("Error :", e, seg_file) continue
def get_pano_jpgs(lon_lat_list=[], saved_path=os.getcwd()): if len(lon_lat_list) == 0: lon_lat_list = [(-94.8696048, 29.2582707)] while len(lon_lat_list) > 0: lon, lat = lon_lat_list.pop(0) pano = GSV_pano(request_lon=lon, request_lat=lat, saved_path=saved_path) pano.get_panorama(zoom=5)
def download_pano(latlon_list, saved_path): while len(latlon_list) > 0: lat, lon = latlon_list.pop(0) try: pano = GSV_pano(request_lat=lat, request_lon=lon, saved_path=saved_path) except Exception as e: print("Error in download_pano():", e)
def downoad_panoramas_from_json_list(json_file_list, saved_path, zoom=3): total_cnt = len(json_file_list) pre_dir = r'E:\USC_OneDrive\OneDrive - University of South Carolina\Research\sidewalk_wheelchair\DC_panoramas' start_time_all = time.perf_counter() while len(json_file_list) > 0: try: start_time = time.perf_counter() json_file = json_file_list.pop() basename = os.path.basename(json_file)[:-5] + "_4.jpg" new_name = os.path.join(saved_path, basename) if os.path.exists(new_name): print(f"{basename} exits, continue.") continue basename = os.path.basename(json_file)[:-5] + "_5.jpg" new_name = os.path.join(pre_dir, basename) if os.path.exists(new_name): print(f"{basename} exits, resample the current file.") img_pil = Image.open(new_name) w, h = img_pil.size w = int(w / 2) h = int(h / 2) img_pil = img_pil.resize((w, h)) zoom4_pano_name = os.path.join( saved_path, os.path.basename(json_file)[:-5] + "_4.jpg") img_pil.save(zoom4_pano_name) continue pano1 = GSV_pano(json_file=json_file, saved_path=saved_path) pano1.get_panorama(zoom=zoom) total_time = (time.perf_counter() - start_time_all) efficency = total_time / (total_cnt - len(json_file_list)) time_remain = efficency * len(json_file_list) print( f"Time spent (seconds): {time.perf_counter() - start_time:.1f}, time used: {utils.delta_time(total_time)} , time remain: {utils.delta_time(time_remain)} \n" ) except Exception as e: logging.error( "Error in downoad_panoramas_from_json_list(): %s, %s" % (e, json_file), exc_info=True) continue
def get_panorama(mp_list, saved_path, zoom=4): total = len(mp_list) saved_path = saved_path processed_cnt = 0 while len(mp_list) > 0: try: i, lon, lat = mp_list.pop(0) pano1 = GSV_pano(request_lon=lon, request_lat=lat, saved_path=saved_path) pano1.download_panorama(zoom=zoom) processed_cnt = total - len(mp_list) print( f"PID {os.getpid()} downloaded row # {i}, {lon}, {lat}, {pano1.panoId}. {processed_cnt} / {total}" ) except Exception as e: print(e) continue
def test_get_point_cloud(self): panoId_2019 = "BM1Qt23drK3-yMWxYfOfVg" panoId_2019 = "AZK1jDGIZC1zmuooSZCzEg" # Walker Ave to Franlin Elementary # lat, lon = 40.7093514, -74.2453146 # road bridge ramp tilt, z = 37 m # lat, lon = 40.780667, -73.9610365 pano1 = GSV_pano(panoId=panoId_2019, crs_local=6526) # pano1 = GSV_pano(request_lon=lon, request_lat=lat, crs_local=6526, saved_path=os.getcwd()) dm = pano1.get_depthmap(zoom=0, saved_path=os.getcwd()) # point_cloud = pano1.get_point_cloud(distance_threshole=200, zoom=0)['point_cloud'] point_cloud = pano1.get_ground_points(color=True, zoom=4) # point_cloud = pano1.get_DOM_points() print(point_cloud.shape) P = point_cloud v = pptk.viewer(P[:, :3]) v.set(point_size=0.001, show_axis=False, show_grid=False) # v.attributes(P[:, 4:7] / 255.0, P[:, 3], P[:, 8:11]/255.0, P[:, 7]) # v.attributes(P[:, 3:6] / 255.0) # for DOM points v.attributes(P[:, 4:7] / 255.0)
def test_get_depthmap(): saved_path = os.getcwd() print(Image.__version__) lon = -77.072465 lat = 38.985399 pano1 = GSV_pano(request_lon=lon, request_lat=lat, crs_local=6487, saved_path=saved_path) pano1.get_depthmap(zoom=0, saved_path=saved_path) pano1.get_DOM(resolution=0.1)
def draw_panorama_apex(panoIds, json_dir, saved_path, results, local_crs=6487): print("PID: ", os.getpid()) total_cnt = len(panoIds) print("total_cnt:", total_cnt) processed_cnt = 0 transformer = utils.epsg_transform(in_epsg=4326, out_epsg=local_crs) while len(panoIds) > 0: panoId = panoIds.pop() processed_cnt += 1 if len(results) % 100 == 0: print(f"Processed {len(results)} jsons.") json_file = os.path.join(json_dir, panoId + ".json") if os.path.exists(json_file): local_crs = 6487 # print(json_file) pano = GSV_pano(json_file=json_file) Links = pano.jdata["Links"] if len(Links) < 2: # print(f"Error in draw_panorama_apex(): {pano.panoId} has no 2 panoramas in Links.") continue try: json_file_0 = os.path.join(json_dir, Links[0]['panoId'] + ".json") json_file_1 = os.path.join(json_dir, Links[1]['panoId'] + ".json") # print("json_file_0:", json_file_0) pano_0 = GSV_pano(json_file=json_file_0) pano_1 = GSV_pano(json_file=json_file_1) # print("pano_1.panoId:", pano_1.panoId) if (pano_1.panoId == 0) or (pano_0.panoId == 0): # print("Error in Links:") continue # pano_1 = GSV_pano(panoId=Links[1]['panoId']) # print("Line 532") # print("Line 572") xy = transformer.transform(pano.lat, pano.lon) xy0 = transformer.transform(pano_0.lat, pano_0.lon) xy1 = transformer.transform(pano_1.lat, pano_1.lon) pts = np.array([xy0, xy, xy1]) # print("Line 577") # calculate angle a = (xy[1] - xy0[1], xy[0] - xy0[0]) a = np.array(a) b = (xy[1] - xy1[1], xy[0] - xy1[0]) b = np.array(b) angle = np.arccos(np.dot(a, b) / (LA.norm(a) * LA.norm(b))) angle_deg = np.degrees(angle) # print("Line 540") draw_fig = False results.append(f"{pano.panoId},{angle_deg:.2f}") if draw_fig: max_x = pts[:, 0].max() max_y = pts[:, 1].max() min_x = pts[:, 0].min() min_y = pts[:, 1].min() range_x = max_x - min_x range_y = max_y - min_y range_max = max(range_y, range_x) * 1.5 x_center = (max_x + min_x) / 2 y_center = (max_y + min_y) / 2 plt.axis('scaled') # plt.axis("off") plt.xlim(x_center - range_max / 2, x_center + range_max / 2) plt.ylim(y_center - range_max / 2, y_center + range_max / 2) plt.plot(pts[:, 0], pts[:, 1]) plt.scatter(pts[:, 0], pts[:, 1], marker='o', color='red') anno_texts = [pano_0.panoId, pano.panoId, pano_1.panoId] texts = [] for i, txt in enumerate(anno_texts): texts.append(plt.annotate(txt, pts[i], ha='center')) # plt.axis('square') adjust_text(texts, only_move={ 'points': 'y', 'texts': 'y' }, expand_text=(1.25, 1.3), expand_objects=(1.25, 1.4), expand_align=(1.25, 1.4), arrowprops=dict(arrowstyle="->", color='r', lw=0.5)) # print(f"angle: {angle_deg:.2f}") # plt.show() ax = plt.gca() # plt.title(f"{pano.panoId}: {angle_deg:.2f}") ax.xaxis.set_label_position('top') ax.set_title(f"{pano.panoId}: {angle_deg:.2f}", y=1.0, pad=-14) plt.savefig(os.path.join(saved_path, panoId + '.png')) except Exception as e: print(f"Error in draw_panorama_apex():", e, json_file) continue
def download_panoramas_from_seed_points(seed_points, pending_panoIds, saved_path, polygon, max_step=math.inf, json_file=True, pano_jpg=False, pano_zoom=0, depthmap=False): total_cnt = len(seed_points) verbose = True seed_points = seed_points[79:] while len(seed_points) > 1: print( f"Processed {total_cnt - len(seed_points)} / {total_cnt} seed points." ) seed_point = seed_points.pop() lon = seed_point[0] lat = seed_point[1] pano1 = GSV_pano(request_lon=lon, request_lat=lat) # DON'T save! if pano1.panoId == 0: # Find no panorama continue else: print(f"Found a panorama in seed point: {seed_point}") pending_panoIds.append(pano1.panoId) # add a panoId to pending_panoIds step = 0 # Divide pending_panoIds and seed_points when thinking. The purpose of seed_points is to initiate and infill the pending_panoIds while len(pending_panoIds) > 0: try: panoId = pending_panoIds.pop() json_name = os.path.join(saved_path, panoId + ".json") if os.path.exists( json_name ): # This panorama have been downloaded. Skip it. continue else: pano2 = GSV_pano(panoId=panoId) lon = pano2.lon lat = pano2.lat pt = Point(lon, lat) if pt.within(polygon) and (step < max_step): if not os.path.exists(json_name): with open(json_name, 'w') as f: json.dump(pano2.jdata, f) step += 1 links = pano2.jdata["Links"] for link in links: link_panoId = link['panoId'] if link_panoId in pending_panoIds: # move this adjacent panoId to the next. try: pending_panoIds.remove(link_panoId) except: print( f"Failed when removing {link_panoId} in pending_panoIds." ) pending_panoIds.append(link_panoId) if step % 100 == 0: print( f"Process (PID) {os.getpid()} has walked {step} steps. Pending panoIds: {len(pending_panoIds)}" ) else: continue except Exception as e: print("Error in download_panoramas_from_seed_point():", e) continue print(f"Downloaded {step} panoramas for this seed point.")
def quick_DOM(): seg_dir = r'D:\DC_segmented' pano_dir = r'E:\USC_OneDrive\OneDrive - University of South Carolina\Research\sidewalk_wheelchair\DC_panoramas' seg_files = glob.glob(os.path.join(seg_dir, "*.png")) pano_files = glob.glob(os.path.join(pano_dir, "*.jpg")) saved_path = r'F:\Research\sidewalk_wheelchair\DOMs' resolution = 0.05 img_w = 40 img_h = 40 zoom = 4 # pano_files.reverse() # pano_files = pano_files[:] for idx, pano_file in enumerate(pano_files[20000:]): try: print(f"Processing: {idx} / {len(pano_files)}, {pano_file}") panoId = os.path.basename(pano_file)[:-6] new_name = os.path.join(saved_path, f"{panoId}_DOM_{resolution:0.2f}.tif") if os.path.exists(new_name): print(f"Skip processed panoramas: {panoId}") continue img_zoom = int(pano_file[-5]) if img_zoom == 4: print("Skipe: ", pano_file) continue # if img_zoom == 5: # img_zoom5 = Image.open(pano_file) # pano_file = pano_file.replace("_5.jpg", "_4.jpg") timer_start = time.perf_counter() distance_threshole = img_w * 1.5 pano1 = GSV_pano(panoId=panoId, saved_path=pano_dir, crs_local=6487) pano1.get_depthmap(zoom=zoom) pano1.depthmap['ground_mask'] = np.where( pano1.depthmap['depthMap'] < distance_threshole, 1, 0) mask_h, mask_w = pano1.depthmap['ground_mask'].shape pano1.depthmap['ground_mask'][int(mask_h / 4 * 3):, :] = 0 P = pano1.get_ground_points(zoom=zoom, color=True, img_type="pano") # P = P[P[:, 3] < distance_threshole] P = P[P[:, 0] < img_w / 2] P = P[P[:, 0] > -img_w / 2] P = P[P[:, 1] < img_h / 2] P = P[P[:, 1] > -img_h / 2] timer_end = time.perf_counter() np_img, worldfile = pano1.points_to_DOM(P[:, 0], P[:, 1], P[:, 4:7], resolution=resolution) pano1.save_image(np_img, new_name, worldfile) print("Time spent (second):", timer_end - timer_start) except Exception as e: print("Error in quick_DOM():", pano_file, e)