vahidrezanezhad commited on
Commit
97d54df
·
verified ·
1 Parent(s): e337e1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -105
app.py CHANGED
@@ -16,150 +16,77 @@ def do_prediction(img):
16
  img_width_model=model.layers[len(model.layers)-1].output_shape[2]
17
  n_classes=model.layers[len(model.layers)-1].output_shape[3]
18
 
19
- print(img_height_model, img_width_model, n_classes,'didi')
 
20
 
21
- kernel = np.ones((5,5),np.uint8)
22
- margin = int(0.1 * img_width_model)
23
 
 
 
24
  width_mid = img_width_model - 2 * margin
25
  height_mid = img_height_model - 2 * margin
26
-
27
  img = img / float(255.0)
28
-
29
  img_h = img.shape[0]
30
  img_w = img.shape[1]
31
-
32
  prediction_true = np.zeros((img_h, img_w, 3))
33
  mask_true = np.zeros((img_h, img_w))
34
  nxf = img_w / float(width_mid)
35
  nyf = img_h / float(height_mid)
36
-
37
- if nxf > int(nxf):
38
- nxf = int(nxf) + 1
39
- else:
40
- nxf = int(nxf)
41
-
42
- if nyf > int(nyf):
43
- nyf = int(nyf) + 1
44
- else:
45
- nyf = int(nyf)
46
 
47
  for i in range(nxf):
48
  for j in range(nyf):
49
-
50
  if i == 0:
51
  index_x_d = i * width_mid
52
  index_x_u = index_x_d + img_width_model
53
- elif i > 0:
54
  index_x_d = i * width_mid
55
  index_x_u = index_x_d + img_width_model
56
-
57
  if j == 0:
58
  index_y_d = j * height_mid
59
  index_y_u = index_y_d + img_height_model
60
- elif j > 0:
61
  index_y_d = j * height_mid
62
  index_y_u = index_y_d + img_height_model
63
-
64
  if index_x_u > img_w:
65
  index_x_u = img_w
66
  index_x_d = img_w - img_width_model
67
  if index_y_u > img_h:
68
  index_y_u = img_h
69
  index_y_d = img_h - img_height_model
70
-
71
-
72
 
73
  img_patch = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
74
-
75
- label_p_pred = model.predict(
76
- img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]), verbose=0)
77
-
78
  seg = np.argmax(label_p_pred, axis=2)
79
-
80
-
81
- seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
82
-
83
- if i==0 and j==0:
84
- seg_color = seg_color[0:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
85
- seg = seg[0:seg.shape[0] - margin, 0:seg.shape[1] - margin]
86
-
87
- mask_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
88
- prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin,
89
- :] = seg_color
90
-
91
- elif i==nxf-1 and j==nyf-1:
92
- seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - 0, :]
93
- seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - 0]
94
-
95
- mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0] = seg
96
- prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0,
97
- :] = seg_color
98
-
99
- elif i==0 and j==nyf-1:
100
- seg_color = seg_color[margin:seg_color.shape[0] - 0, 0:seg_color.shape[1] - margin, :]
101
- seg = seg[margin:seg.shape[0] - 0, 0:seg.shape[1] - margin]
102
-
103
- mask_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin] = seg
104
- prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin,
105
- :] = seg_color
106
-
107
- elif i==nxf-1 and j==0:
108
- seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
109
- seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - 0]
110
-
111
- mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
112
- prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0,
113
- :] = seg_color
114
-
115
- elif i==0 and j!=0 and j!=nyf-1:
116
- seg_color = seg_color[margin:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
117
- seg = seg[margin:seg.shape[0] - margin, 0:seg.shape[1] - margin]
118
-
119
- mask_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
120
- prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin,
121
- :] = seg_color
122
-
123
- elif i==nxf-1 and j!=0 and j!=nyf-1:
124
- seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
125
- seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - 0]
126
-
127
- mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
128
- prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0,
129
- :] = seg_color
130
-
131
- elif i!=0 and i!=nxf-1 and j==0:
132
- seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
133
- seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - margin]
134
-
135
- mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
136
- prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin,
137
- :] = seg_color
138
-
139
- elif i!=0 and i!=nxf-1 and j==nyf-1:
140
- seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - margin, :]
141
- seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - margin]
142
-
143
- mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin] = seg
144
- prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin,
145
- :] = seg_color
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  else:
148
- seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
149
- seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - margin]
150
-
151
- mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
152
- prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin,
153
- :] = seg_color
154
 
155
  prediction_true = prediction_true.astype(np.uint8)
156
 
157
- y_predi=cv2.resize( y_predi, ( img.shape[1],img.shape[0]) ,interpolation=cv2.INTER_NEAREST)
158
- #return y_predi
159
-
160
 
161
 
162
- print(y_predi.shape, np.unique(y_predi))
163
 
164
 
165
 
@@ -175,7 +102,7 @@ def do_prediction(img):
175
  print(prediction.shape)
176
 
177
  '''
178
- return y_predi
179
 
180
  iface = gr.Interface(fn=do_prediction, inputs=gr.Image(), outputs=gr.Image())
181
  iface.launch()
 
16
  img_width_model=model.layers[len(model.layers)-1].output_shape[2]
17
  n_classes=model.layers[len(model.layers)-1].output_shape[3]
18
 
19
+ if img.shape[0] < img_height_model:
20
+ img = resize_image(img, img_height_model, img.shape[1])
21
 
22
+ if img.shape[1] < img_width_model:
23
+ img = resize_image(img, img.shape[0], img_width_model)
24
 
25
+ marginal_of_patch_percent = 0.1
26
+ margin = int(marginal_of_patch_percent * img_height_model)
27
  width_mid = img_width_model - 2 * margin
28
  height_mid = img_height_model - 2 * margin
 
29
  img = img / float(255.0)
30
+ img = img.astype(np.float16)
31
  img_h = img.shape[0]
32
  img_w = img.shape[1]
 
33
  prediction_true = np.zeros((img_h, img_w, 3))
34
  mask_true = np.zeros((img_h, img_w))
35
  nxf = img_w / float(width_mid)
36
  nyf = img_h / float(height_mid)
37
+ nxf = int(nxf) + 1 if nxf > int(nxf) else int(nxf)
38
+ nyf = int(nyf) + 1 if nyf > int(nyf) else int(nyf)
 
 
 
 
 
 
 
 
39
 
40
  for i in range(nxf):
41
  for j in range(nyf):
 
42
  if i == 0:
43
  index_x_d = i * width_mid
44
  index_x_u = index_x_d + img_width_model
45
+ else:
46
  index_x_d = i * width_mid
47
  index_x_u = index_x_d + img_width_model
 
48
  if j == 0:
49
  index_y_d = j * height_mid
50
  index_y_u = index_y_d + img_height_model
51
+ else:
52
  index_y_d = j * height_mid
53
  index_y_u = index_y_d + img_height_model
 
54
  if index_x_u > img_w:
55
  index_x_u = img_w
56
  index_x_d = img_w - img_width_model
57
  if index_y_u > img_h:
58
  index_y_u = img_h
59
  index_y_d = img_h - img_height_model
 
 
60
 
61
  img_patch = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
62
+ label_p_pred = model.predict(img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]),
63
+ verbose=0)
 
 
64
  seg = np.argmax(label_p_pred, axis=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ if i == 0 and j == 0:
67
+ prediction_true[index_y_d + 0 : index_y_u - margin, index_x_d + 0 : index_x_u - margin, :] = seg_color
68
+ elif i == nxf - 1 and j == nyf - 1:
69
+ prediction_true[index_y_d + margin : index_y_u - 0, index_x_d + margin : index_x_u - 0, :] = seg_color
70
+ elif i == 0 and j == nyf - 1:
71
+ prediction_true[index_y_d + margin : index_y_u - 0, index_x_d + 0 : index_x_u - margin, :] = seg_color
72
+ elif i == nxf - 1 and j == 0:
73
+ prediction_true[index_y_d + 0 : index_y_u - margin, index_x_d + margin : index_x_u - 0, :] = seg_color
74
+ elif i == 0 and j != 0 and j != nyf - 1:
75
+ prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + 0 : index_x_u - margin, :] = seg_color
76
+ elif i == nxf - 1 and j != 0 and j != nyf - 1:
77
+ prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - 0, :] = seg_color
78
+ elif i != 0 and i != nxf - 1 and j == 0:
79
+ prediction_true[index_y_d + 0 : index_y_u - margin, index_x_d + margin : index_x_u - margin, :] = seg_color
80
+ elif i != 0 and i != nxf - 1 and j == nyf - 1:
81
+ prediction_true[index_y_d + margin : index_y_u - 0, index_x_d + margin : index_x_u - margin, :] = seg_color
82
  else:
83
+ prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin, :] = seg_color
 
 
 
 
 
84
 
85
  prediction_true = prediction_true.astype(np.uint8)
86
 
 
 
 
87
 
88
 
89
+ print(prediction_true.shape, np.unique(prediction_true))
90
 
91
 
92
 
 
102
  print(prediction.shape)
103
 
104
  '''
105
+ return prediction_true
106
 
107
  iface = gr.Interface(fn=do_prediction, inputs=gr.Image(), outputs=gr.Image())
108
  iface.launch()