-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetect_shadows.py
66 lines (45 loc) · 1.69 KB
/
detect_shadows.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
#!/usr/bin/env python
import os
import argparse
import numpy as np
import cv2 as cv
import patched_cnn as pcnn
import image_processor as ip
import copy
parser = argparse.ArgumentParser()
parser.add_argument("--image", help="path to image.")
parser.add_argument("--model", help="path to trained model h5 file.")
args = parser.parse_args()
image_source = args.image
model_source = args.model
img = cv.imread(image_source)
imgLab = cv.cvtColor(img, cv.COLOR_BGR2LAB)
# print(np.concatenate(( img, imgLab ), 2).shape)
input = np.array([ cv.resize( imgLab, (32, 32) ) ]) # input to nn should be (1, 32, 32, 3)
cnn = pcnn.Patched_CNN()
cnn.load_model(model_source)
print("...initialized model...")
pred = cnn.predict(input)
shadow_mask = cv.resize( pred.reshape(32, 32), (img.shape[1], img.shape[0]) )
outImg = copy.deepcopy(img)
imgp = ip.ImageProcessed(img, "image_name")
imgp.shadow_mask = shadow_mask
imgp._segment()
imgp.label_shadow_segments(0.1)
imgp.showShadows()
for col in range(outImg.shape[0]):
for row in range(outImg.shape[1]):
# if shadow_mask[col, row] > 0.5:
# outImg[col, row][0] = 255.0 * shadow_mask[col, row]
# outImg[col, row][0] = outImg[col, row][0] * (1-shadow_mask[col, row])
outImg[col, row][1] = 255 * (shadow_mask[col, row])
# outImg[col, row][2] = 255.0 * shadow_mask[col, row]
# cv.namedWindow("original", cv.WINDOW_NORMAL)
cv.namedWindow("mask", cv.WINDOW_NORMAL)
# cv.imshow("original", img)
cv.imshow("mask", outImg)
# # cv.imshow("predicted shadow mask", shadow_mask)
cv.waitKey()
# for i in range(len(img_processed.segments)):
# img_processed.segments[i]['is_shadow'] = pred[i] == 1
# img_processed.showShadows()