-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_from_blob.py
81 lines (65 loc) · 2.73 KB
/
copy_from_blob.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
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import shutil
import re
from pathlib import Path
from omegaconf import DictConfig
def parse(filename):
# Parse MD_Row-3_1655826853.JPG
state, row, timestamp = re.findall("(MD|NC|TX)_Row-(\d+)_(\d+)", filename)[0]
ext = filename.split(".")[-1]
return state, int(row), int(timestamp), ext
def rename(state, row, pot, timestamp, ext):
return f"{state}_{row}_{pot}_{timestamp}.0.{ext}"
def main(cfg: DictConfig) -> None:
batch_id = cfg.general.batch_id
developed_home = Path(cfg.data.batchdir)
developed_home.mkdir(exist_ok=False) # Ensure that the ID has not already been processed
# Copy the developed images
src = Path(cfg.blob_storage.developeddir, batch_id, "images")
dst = Path(developed_home, "images")
shutil.copytree(src, dst)
if cfg.autosfm.autosfm_config.use_masking:
# Copy the masks
src = Path(cfg.blob_storage.developeddir, batch_id, "masks")
dst = Path(developed_home, "masks")
shutil.copytree(src, dst)
if cfg.data.rename:
files = os.listdir(dst)
rowmap = dict()
for file in files:
state, row, timestamp, ext = parse(file)
row_files = rowmap.get(row, [])
row_files.append((file, state, timestamp, ext))
rowmap[row] = row_files
# Sort
for row, row_files in rowmap.items():
# Sort according to timestamp
row_files.sort(key=lambda x: x[2])
# Rename
for idx, file in enumerate(row_files):
org_file, state, timestamp, ext = file
renamed_file = rename(state, row, idx+1, timestamp, ext)
_src = Path(dst, org_file)
_dst = Path(dst, renamed_file)
shutil.move(_src, _dst)
# Copy the Ground Control Points
# Are copied during autosfm. TODO: Remove the copy code
# src = Path(cfg.blob_storage.uploaddir, batch_id, "GroundControlPoints.csv")
# dst = Path(cfg.data.uploaddir, batch_id)
# dst.mkdir(exist_ok=False)
# dst = Path(dst, "GroundControlPoints.csv")
# shutil.copy(src, dst)
# Copy the detection model if not present
local_model_path = Path(cfg.detect.model_path)
if not local_model_path.exists():
blob_model_path = Path(cfg.blob_storage.modeldir, "plant_detector", cfg.detect.model_filename)
dst = Path(local_model_path)
shutil.copy(blob_model_path, dst)
# Copy the shapefiles
location = batch_id.split("_")[0]
shapefile_path = Path(cfg.data.utilsdir, location, "shapefiles")
if not shapefile_path.exists():
src = Path(cfg.blob_storage.utilsdir, location, "shapefiles")
assert src.exists()
dst = shapefile_path
shutil.copytree(src, dst)