Skip to content

Commit 4e83805

Browse files
committed
restructured
1 parent 4762f64 commit 4e83805

File tree

8 files changed

+93
-57
lines changed

8 files changed

+93
-57
lines changed

lib/__init__.py

Whitespace-only changes.

helpers.py renamed to lib/helpers.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
import math
21
import cv2
2+
import math
33
import random
4+
import shutil
5+
import os.path
6+
import pathlib
47
import numpy as np
58

6-
import layers
7-
import loaders
9+
from lib import layers
10+
from lib import loaders
811

912
def mil(fp):
1013
return math.floor(fp*1000000)
1114

1215
def osm_at_tile(tx, ty, z):
13-
# returns a link to open iD editor at specified tile
16+
# prints a link to open iD editor at specified tile
1417
lat, lng = layers.wgs_at_tile(tx, ty, z)
1518
print(f"https://www.openstreetmap.org/edit#map={z}/{lat}/{lng}")
1619

20+
def cleandir(path):
21+
# removes a directory and creates it again
22+
target = pathlib.Path(path)
23+
if os.path.isdir(target):
24+
shutil.rmtree(target)
25+
target.mkdir(parents=True, exist_ok=True)
26+
return target
27+
28+
def outside(point, lefttop, rightbot):
29+
return point[0] < lefttop[0] \
30+
or point[1] < lefttop[1] \
31+
or point[0] >= rightbot[0] \
32+
or point[1] >= rightbot[1]
33+
1734
class MercatorPainter:
1835
# paints an area with dots and lines representing positive examples.
1936
# everything not painted over is supposed to be negative.

layers.py renamed to lib/layers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ def tilefile(self, x, y, z):
5353

5454
def xy_fromfile(self, path):
5555
f = path.name
56-
sx = f[1:7]
57-
sy = f[8:14]
56+
xpos = f.index('x')
57+
ypos = f.index('y')
58+
dpos = f.index('.')
59+
sx = f[xpos+1:ypos]
60+
sy = f[ypos+1:dpos]
5861
return (int(sx), int(sy))
5962

6063
def tileurl(self, x, y, z):
File renamed without changes.

make_expand.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import pathlib
1+
import cv2
2+
import random
23
import shutil
34
import os.path
4-
import random
5-
import cv2
6-
7-
import layers
8-
import loaders
9-
import helpers
105
import tarfile
116

7+
from lib import layers
8+
from lib import loaders
9+
from lib import helpers
10+
1211
# this is 256 for all current imagery providers
1312
TILESIZE = 256
1413

@@ -26,13 +25,6 @@
2625

2726
# validation images of each category
2827
VALID = 1000
29-
30-
def cleandir(path):
31-
target = pathlib.Path(path)
32-
if os.path.isdir(target):
33-
shutil.rmtree(target)
34-
target.mkdir(parents=True, exist_ok=True)
35-
return target
3628

3729
if __name__ == "__main__":
3830
# box = (27.4583,53.9621,27.5956,53.9739) # north belt
@@ -47,7 +39,7 @@ def cleandir(path):
4739
train = lamps[:TRAIN]
4840
valid = lamps[TRAIN:]
4941

50-
target = cleandir('lamps-expand/train/lamp')
42+
target = helpers.cleandir('lamps-expand/train/lamp')
5143
for lamp in train:
5244
h = w = EXPAND_PAD + TILESIZE + EXPAND_PAD
5345
crop = layers.maxar.getcrop_wgs(lamp, h, w, IMZ)
@@ -57,7 +49,7 @@ def cleandir(path):
5749
cv2.imwrite(dst, crop)
5850

5951

60-
target = cleandir('lamps-expand/valid/lamp')
52+
target = helpers.cleandir('lamps-expand/valid/lamp')
6153
count = 0
6254
it = iter(valid)
6355
while count < VALID:
@@ -83,7 +75,7 @@ def cleandir(path):
8375
for i in range(VALID):
8476
data['v'].append(mp.random_negative())
8577

86-
target = cleandir('lamps-expand/train/nolamp')
78+
target = helpers.cleandir('lamps-expand/train/nolamp')
8779
for (tx,ty) in data['t']:
8880
wgs = layers.wgs_at_tile(tx, ty, IMZ)
8981
h = w = EXPAND_PAD + TILESIZE + EXPAND_PAD
@@ -93,7 +85,7 @@ def cleandir(path):
9385
dst = str(target / f"m_lat{lat}lng{lng}.jpg")
9486
cv2.imwrite(dst, crop)
9587

96-
target = cleandir('lamps-expand/valid/nolamp')
88+
target = helpers.cleandir('lamps-expand/valid/nolamp')
9789
for (tx,ty) in data['v']:
9890
fname = layers.maxar.download(tx, ty, IMZ)
9991
if fname is not None:

make_original.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import pathlib
1+
import random
22
import shutil
33
import os.path
4-
import random
5-
import cv2
6-
7-
import layers
8-
import loaders
9-
import helpers
104
import tarfile
115

6+
from lib import layers
7+
from lib import loaders
8+
from lib import helpers
129

1310
# this is 256 for all current imagery providers
1411
TILESIZE = 256
@@ -18,13 +15,6 @@
1815

1916
# if you want to train faster
2017
LIMIT = 5000
21-
22-
def cleandir(path):
23-
target = pathlib.Path(path)
24-
if os.path.isdir(target):
25-
shutil.rmtree(target)
26-
target.mkdir(parents=True, exist_ok=True)
27-
return target
2818

2919
if __name__ == "__main__":
3020
# box = (27.4583,53.9621,27.5956,53.9739) # north belt
@@ -38,7 +28,7 @@ def cleandir(path):
3828
random.shuffle(lamps)
3929
lamps = lamps[:LIMIT]
4030

41-
target = cleandir('lamps-orig/lamp')
31+
target = helpers.cleandir('lamps-orig/lamp')
4232
for lamp in lamps:
4333
fname = layers.maxar.gettile_wgs(lamp, IMZ, skipedge=True)
4434
if fname is not None:
@@ -57,7 +47,7 @@ def cleandir(path):
5747
while len(batch) < LIMIT:
5848
batch.append(mp.random_negative())
5949

60-
target = cleandir('lamps-orig/nolamp')
50+
target = helpers.cleandir('lamps-orig/nolamp')
6151
for (tx,ty) in batch:
6252
fname = layers.maxar.download(tx, ty, IMZ)
6353
if fname is not None:

minimap.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import cv2
2+
import numpy as np
3+
from lib import helpers
4+
from lib import layers
5+
6+
# shows how much is the box covered with cached tiles
7+
8+
IMZ = 18
9+
10+
if __name__ == "__main__":
11+
box = (27.4026,53.8306,27.7003,53.9739)
12+
source = layers.maxar.tiledir / f"z{IMZ}"
13+
localtiles = [layers.maxar.xy_fromfile(path) for path in source.glob("*.jpg")]
14+
# localtiles = localtiles[:1000]
15+
16+
W, S, E, N = box
17+
txmin, tymin = layers.maxar.tile_at_wgs((N, W), IMZ)
18+
txmax, tymax = layers.maxar.tile_at_wgs((S, E), IMZ)
19+
20+
width = txmax-txmin+2
21+
height = tymax-tymin+2
22+
print(f"map size: {width}x{height}px")
23+
canvas = np.zeros((height, width, 3), np.uint8)
24+
25+
for (tx,ty) in localtiles:
26+
fname = layers.maxar.download(tx, ty, IMZ)
27+
img = cv2.imread(fname)
28+
av = img.mean(axis=0).mean(axis=0)
29+
x = tx-txmin
30+
y = ty-tymin
31+
if helpers.outside((x,y), (0,0), (width, height)):
32+
continue
33+
canvas[y, x] = av
34+
35+
cv2.imshow('canvas', canvas)
36+
cv2.waitKey(0)

video.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import cv2
2-
import loaders
3-
import glob
4-
import imgdl
5-
import layers
2+
from pathlib import Path
3+
from lib import loaders
4+
from lib import layers
65

76
FRAMESIZE = 256
87

9-
def dir2vid(layer):
8+
def dir2vid(path):
109
images = []
1110
i = 0
12-
for fname in layer.cropdir.glob("*.jpg"):
11+
print(Path(path))
12+
for fname in Path(path).glob("*.jpg"):
13+
print(fname)
1314
img = cv2.imread(str(fname))
14-
1515
x = 95
1616
y = 135
1717
cv2.line(img, (x,y), (x+10,y+10), (0,0,255), 1)
@@ -22,19 +22,17 @@ def dir2vid(layer):
2222
if i > 10000:
2323
break
2424

25-
out = cv2.VideoWriter(f"./all-{layer.name}.avi", cv2.VideoWriter_fourcc(*'DIVX'), 60, (FRAMESIZE,FRAMESIZE))
25+
out = cv2.VideoWriter(f"./dir.avi", cv2.VideoWriter_fourcc(*'DIVX'), 60, (FRAMESIZE,FRAMESIZE))
2626

2727
for i in range(len(images)):
2828
out.write(images[i])
2929
out.release()
3030

31-
def list2vid(lamps, layer):
31+
def list2vid(lamps, layer, z):
3232
print(1)
3333
images = []
3434
for lamp in lamps:
35-
print("getting crop")
36-
img = imgdl.getcrop(None, layer, lamp, FRAMESIZE, FRAMESIZE)
37-
print("painting")
35+
img = layers.maxar.getcrop_wgs(lamp, FRAMESIZE, FRAMESIZE, z)
3836
x = 95
3937
y = 135
4038
cv2.line(img, (x,y), (x+10,y+10), (0,0,255), 1)
@@ -49,8 +47,8 @@ def list2vid(lamps, layer):
4947
out.release()
5048

5149
if __name__ == "__main__":
52-
# dir2vid(layers.maxar)
53-
# dir2vid(layers.dg)
54-
# lamps = loaders.bbox(27.5682,53.8469,27.5741,53.8688)
55-
# list2vid(lamps, layers.maxar)
56-
# list2vid(lamps, layers.dg)
50+
# dir2vid(layers.maxar, 19)
51+
dir2vid("lamps-expand/train/lamp")
52+
# lamps = loaders.query_nodes(27.4026,53.8306,27.7003,53.9739)
53+
# list2vid(lamps, layers.maxar, 19)
54+
# list2vid(lamps, layers.dg, 19)

0 commit comments

Comments
 (0)