Update app.py
Browse files
app.py
CHANGED
@@ -137,6 +137,49 @@ def infer(f_in, interpolation, fps_output):
|
|
137 |
return final_vid, files
|
138 |
|
139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
def logscale(linear):
|
141 |
return int(math.pow(2, linear))
|
142 |
|
@@ -145,14 +188,35 @@ def linscale(linear):
|
|
145 |
|
146 |
def sharpest(fl, i):
|
147 |
break_vid = get_frames(fl, "vid_input_frame", "origin", i)
|
|
|
|
|
148 |
blur_s = []
|
149 |
for jdx, fr in enumerate(break_vid[0]):
|
150 |
-
|
|
|
151 |
print(str(int(blur_s[jdx])))
|
152 |
|
153 |
-
|
154 |
-
fl = break_vid[0][
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
return fl
|
157 |
|
158 |
def sortFiles(e):
|
@@ -167,9 +231,10 @@ def loadf(f):
|
|
167 |
for i, fl in enumerate(f):
|
168 |
ftype = fl.split('/')
|
169 |
if ftype[len(ftype)-1].split('.')[1] == 'mp4':
|
170 |
-
|
171 |
else:
|
172 |
-
|
|
|
173 |
return fnew, fnew
|
174 |
else:
|
175 |
return f, f
|
|
|
137 |
return final_vid, files
|
138 |
|
139 |
|
140 |
+
def remove_bg(frame):
|
141 |
+
b = 5
|
142 |
+
#subtract background (get scene with shadow) and correct hue against light
|
143 |
+
bg = cv2.medianBlur(frame, 255)
|
144 |
+
bg_gray = cv2.cvtColor(cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
|
145 |
+
bg_diff = (bg-bg_gray).astype(np.int16)
|
146 |
+
frame_ = ((bg.astype(np.int16)-frame.astype(np.int16))+127).astype(np.uint8)
|
147 |
+
frame = (frame.astype(np.int16)-bg_diff).astype(np.uint8)
|
148 |
+
frame_ = cv2.bilateralFilter(frame_, b*4, b*8, b*2)
|
149 |
+
|
150 |
+
#remove regions of low saturation (get scene without shadow)
|
151 |
+
mask = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV)
|
152 |
+
m = cv2.inRange(mask, np.array([0,0,0]), np.array([180,32,256]))
|
153 |
+
frame[m>0] = (127,127,127)
|
154 |
+
frame_ = cv2.medianBlur(frame_, b)
|
155 |
+
element = cv2.getStructuringElement(cv2.MORPH_RECT, (2*b+1, 2*b+1), (b,b))
|
156 |
+
frame_ = cv2.erode(cv2.dilate(frame_, element), element)
|
157 |
+
frame = cv2.medianBlur(frame, b)
|
158 |
+
|
159 |
+
|
160 |
+
m_ = frame_.reshape((-1,3))
|
161 |
+
# convert to np.float32
|
162 |
+
m_ = np.float32(m_)
|
163 |
+
# define criteria, number of clusters(K) and apply kmeans()
|
164 |
+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 16, 1.0)
|
165 |
+
K = 3
|
166 |
+
ret,label,center=cv2.kmeans(m_,K,None,criteria,16,cv2.KMEANS_PP_CENTERS)
|
167 |
+
# Now convert back into uint8, and make original image
|
168 |
+
center = np.uint8(center)
|
169 |
+
res = center[label.flatten()]
|
170 |
+
frame_ = res.reshape((frame_.shape))
|
171 |
+
|
172 |
+
|
173 |
+
#remove shadows at edges
|
174 |
+
cv2.rectangle(frame_,(0,0),(frame_.shape[1]-1,frame_.shape[0]-1),(142,142,142),7)
|
175 |
+
mask = cv2.floodFill(frame_, None, (0, 0), 255, 0, 0, (4 | cv2.FLOODFILL_FIXED_RANGE))[2] #(4 | cv2.FLOODFILL_FIXED_RANGE | cv2.FLOODFILL_MASK_ONLY | 255 << 8)
|
176 |
+
# 255 << 8 tells to fill with the value 255)
|
177 |
+
mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
|
178 |
+
frame_[mask>0] = (125,125,125)
|
179 |
+
frame_[frame_[:,:,0]>=142] = (255,255,255)
|
180 |
+
|
181 |
+
return frame#, frame_
|
182 |
+
|
183 |
def logscale(linear):
|
184 |
return int(math.pow(2, linear))
|
185 |
|
|
|
188 |
|
189 |
def sharpest(fl, i):
|
190 |
break_vid = get_frames(fl, "vid_input_frame", "origin", i)
|
191 |
+
|
192 |
+
frames = []
|
193 |
blur_s = []
|
194 |
for jdx, fr in enumerate(break_vid[0]):
|
195 |
+
frames.append(cv2.imread(fr).astype(np.uint8))
|
196 |
+
blur_s.append(cv2.Laplacian(cv2.cvtColor(frames[len(frames)-1], cv2.COLOR_BGR2GRAY), cv2.CV_64F).var())
|
197 |
print(str(int(blur_s[jdx])))
|
198 |
|
199 |
+
indx = np.argmax(blur_s)
|
200 |
+
fl = break_vid[0][indx]
|
201 |
+
|
202 |
+
n = 25
|
203 |
+
half = int(n/2)
|
204 |
+
if indx-half < 0:
|
205 |
+
n = indx*2+1
|
206 |
+
elif indx+half >= len(frames):
|
207 |
+
n = (len(frames)-1-indx)*2+1
|
208 |
+
|
209 |
+
#denoise
|
210 |
+
frame = cv2.fastNlMeansDenoisingColoredMulti(
|
211 |
+
srcImgs = frames,
|
212 |
+
imgToDenoiseIndex = indx,
|
213 |
+
temporalWindowSize = n,
|
214 |
+
hColor = 5,
|
215 |
+
templateWindowSize = 21,
|
216 |
+
searchWindowSize = 21)
|
217 |
+
|
218 |
+
cv2.imwrite(fl, frame)
|
219 |
+
print(str(i) +'th file, sharpest frame: '+str(indx)+', name: '+fl)
|
220 |
return fl
|
221 |
|
222 |
def sortFiles(e):
|
|
|
231 |
for i, fl in enumerate(f):
|
232 |
ftype = fl.split('/')
|
233 |
if ftype[len(ftype)-1].split('.')[1] == 'mp4':
|
234 |
+
fl = remove_bg(sharpest(fl, i))
|
235 |
else:
|
236 |
+
fl = remove_bg(fl)
|
237 |
+
fnew.append(fl)
|
238 |
return fnew, fnew
|
239 |
else:
|
240 |
return f, f
|