This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
sample.py
68 lines (52 loc) · 2.15 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import cv2
import os
from DeepForest import config
from DeepForest import onthefly_generator
from DeepForest import preprocess
from DeepForest import Lidar
from DeepForest import utils
import pyfor
import warnings
import sys
if not sys.warnoptions:
warnings.simplefilter("ignore")
def sample(n=50):
"""
Grab n random images from across the site
"""
#Load config
DeepForest_config = config.load_config()
#Read in data
data = preprocess.load_data(data_dir=DeepForest_config['training_csvs'], res=0.1, lidar_path=DeepForest_config["lidar_path"])
#Create windows
windows = preprocess.create_windows(data, DeepForest_config,base_dir = DeepForest_config["evaluation_tile_dir"])
selected_windows = windows[["tile","window"]].drop_duplicates().sample(n=n)
generator = onthefly_generator.OnTheFlyGenerator(data=data, windowdf=selected_windows, DeepForest_config = DeepForest_config)
folder_dir = os.path.join("data",DeepForest_config["evaluation_site"],"samples")
if not os.path.exists(folder_dir):
os.mkdir(folder_dir)
for i in range(generator.size()):
#Load image - done for side effects, allow to skip bad tiles.
try:
three_channel = generator.load_image(i)
except:
continue
#load lidar
generator.load_lidar_tile()
generator.clip_las()
if generator.clipped_las == None:
continue
#name RGB
tilename = os.path.splitext(generator.image_data[i]["tile"])[0]
tilename = tilename + "_" + str(generator.image_data[i]["window"]) + ".tif"
filename = os.path.join(folder_dir, tilename)
#Write
cv2.imwrite(filename, generator.image)
#name .laz
tilename = os.path.splitext(generator.image_data[i]["tile"])[0]
tilename = tilename + "_" + str(generator.image_data[i]["window"]) + ".laz"
filename = os.path.join(folder_dir, tilename)
#Write .laz
generator.clipped_las.write(filename)
if __name__ == "__main__":
sample(n=50)