This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
278 lines (206 loc) · 8.37 KB
/
app.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import subprocess
import random
import streamlit as st
import cv2 as cv
import numpy as np
import pandas as pd
from myapp.DeepSegmentor.options.test_options import TestOptions
path = 'myapp/DeepSegmentor/datasets/DeepCrack/'
test_path = 'test_img/'
results_path = ['myapp/DeepSegmentor/results',
'myapp/DeepSegmentor/results/deepcrack',
'myapp/DeepSegmentor/results/deepcrack/test_latest',
'myapp/DeepSegmentor/results/deepcrack/test_latest/images/']
max_pixels = 500 # width or height
command_inference = 'python3 test.py --dataroot myapp/DeepSegmentor/datasets/DeepCrack --name deepcrack --model deepcrack --dataset_mode deepcrack --batch_size 1 --num_classes 1 --norm batch --num_test 10000 --display_sides 1'
command_png2mp4 = 'ffmpeg -framerate 30 -i ' + \
results_path[-1] + '%1d_fused.png -vcodec libx264 output.mp4'
def make_abs_path():
"""
Handle folder myapp as library
"""
os.path.abspath(os.getcwd())
return True
def create_folders():
"""
Create default folders is not exist.
"""
# Where images are being stored
if not os.path.isdir(path):
os.mkdir(path+test_path)
for tmp_path in results_path:
# Where results inference appears
if not os.path.isdir(tmp_path):
os.mkdir(tmp_path)
def clean_temp():
"""
Remove all files in specific paths
"""
paths_to_remove = ['myapp/DeepSegmentor/datasets/DeepCrack/test_img/',
'myapp/DeepSegmentor/results/deepcrack/test_latest/images']
try:
for path in paths_to_remove:
for f in os.listdir(path):
os.remove(os.path.join(path, f))
except Exception as e:
print(e)
mp4_to_remove = ['temporal.mp4',
'output.mp4']
for f in mp4_to_remove:
try:
os.remove(f)
except Exception as e:
print(e)
def clean_other_files_from_results(path=results_path[-1]):
"""
Removing all temporal files generated by DeepCrack
Like _image _label_viz _side1..5
"""
for f in os.listdir(path):
if not 'fused' in f:
print("Removing..."+path+f)
os.remove(path+f)
return True
def reduce_dims(width, height, scale_percent):
"""
Reduce dimensions until fit in less of max_pixels
"""
while width > max_pixels or height > max_pixels:
st.text('Reescalando: Weight-{} Height-{}'.format(width, height))
width = int(width * scale_percent / 100)
height = int(height * scale_percent / 100)
dim = (width, height)
return dim
def resize_one_image(filename_path):
"""
Resize one image with filepath
"""
temp_img = cv.imread(filename_path)
scale_percent = 80 # downscale percent
width = int(temp_img.shape[1])
height = int(temp_img.shape[0])
# Resize image to reduce inference time and max virtual memory needed
reduced_dim = reduce_dims(width, height, scale_percent)
resized = cv.resize(temp_img, reduced_dim,
interpolation=cv.INTER_AREA)
return resized, reduced_dim
def resize_all_images_from_path(path):
"""
Resize all images given a path.
"""
# Resize all images
for f in os.listdir(path):
st.text(str(path+f))
resized, reduced_dim = resize_one_image(path+f) # Resized
cv.imwrite(path+f, resized) # Saved
return reduced_dim
def split_video_by_frame(video_path, input_drop_path):
"""
This script will split video into frames with opencv
"""
# Author: https://gist.github.com/keithweaver/70df4922fec74ea87405b83840b45d57
cap = cv.VideoCapture(video_path)
currentFrame = 0
while(True):
try:
# Capture frame-by-frame
ret, frame = cap.read()
# Saves image of the current frame in jpg file
name = input_drop_path + str(currentFrame) + '.jpg'
print('Creating...' + name)
cv.imwrite(name, frame)
# To stop duplicate images
currentFrame += 1
except Exception as e:
break
print(e)
# When everything done, release the capture
try:
cap.release()
cv.destroyAllWindows()
except Exception as e:
print(e)
return True
if __name__ == '__main__':
make_abs_path() # Handle folder as library
clean_temp() # Clean temporal files on each upload
create_folders() # Create paths for inputs and results
# General description
st.title("Deep crack")
st.text("Only CPU")
st.text("Neural network, dataset, pretrained weights and paper: https://github.com/yhlleo/DeepSegmentor")
st.text("WebApp: https://github.com/DZDL/crack-detector")
# Upload file
st.subheader("- Choose a file (video or image)")
uploaded_file = st.file_uploader("Elige una imagen compatible", type=[
'png', 'jpg', 'bmp', 'jpeg', 'mp4'])
if uploaded_file is not None: # File > 0 bytes
file_details = {"FileName": uploaded_file.name,
"FileType": uploaded_file.type,
"FileSize": uploaded_file.size}
st.write(file_details)
#######################
# VIDEO UPLOADED FILE
#######################
if file_details['FileType'] == 'video/mp4':
with open('temporal.mp4', 'wb') as f:
f.write(uploaded_file.getbuffer())
split_video_by_frame('temporal.mp4', path+test_path)
random_filename = random.choice(os.listdir(path+test_path))
st.image(path+test_path+random_filename, caption='Random image',
channels="BGR", use_column_width=True)
# Applying neural network: DeepCrack - Liu, 2019
st.subheader('Executing neural network DeepCrack... ')
# Resize all images
reduced_dim = resize_all_images_from_path(path+test_path)
# INFERENCE
result = os.popen(command_inference).read()
# For CPU NN Inference we need to be sure that no gpu is being used by
# the general script
st.text(result)
st.text("GPUS:"+"(if null -> cpu) \n")
# Clean other files
clean_other_files_from_results() # default on result path
# JPG -> MP4
result = os.popen(command_png2mp4).read()
st.text(result)
# Display video
st.subheader("Video output")
st.video('output.mp4')
#######################
# IMAGE UPLOADED FILE
#######################
elif (file_details['FileType'] == 'image/png' or
file_details['FileType'] == 'image/jpg' or
file_details['FileType'] == 'image/jpeg' or
file_details['FileType'] == 'image/bmp'):
file_bytes = np.asarray(
bytearray(uploaded_file.read()), dtype=np.uint8)
image = cv.imdecode(file_bytes, 1)
cv.imwrite(path+test_path+uploaded_file.name, image)
st.write("This is your uploaded image:")
st.image(image, caption='This is the uploaded image',
channels="BGR", use_column_width=True)
resized, reduced_dim = resize_one_image(
path+test_path+uploaded_file.name)
# Display rezized image
st.subheader("Resizing image...")
st.image(resized, caption='Reducing size due cpu limitations',
channels="BGR", use_column_width=True)
cv.imwrite(path+test_path+uploaded_file.name, resized)
# Applying neural network: DeepCrack - Liu, 2019
st.subheader('Executing neural network DeepCrack... ')
# INFERENCE
result = os.popen(command_inference).read()
# For CPU NN Inference we need to be sure that no gpu is being used by
# the general script
st.text("GPUS:"+result+"(if null -> cpu) \n")
# Results
st.subheader('Inference output: result image')
# Get result image and display
# st.text("Abriendo {}".format(results_path[-1]+uploaded_file.name[:-4]+"_fused.png"))
result_image = cv.imread(
results_path[-1]+uploaded_file.name[:-4]+"_fused.png")
st.image(result_image, caption='Output image',
channels="BGR", use_column_width=True)