Spaces:
Runtime error
Runtime error
Commit
·
edb8a3c
1
Parent(s):
73d3712
basic func
Browse files
app.py
CHANGED
@@ -1,18 +1,67 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
gr_input = [
|
7 |
-
gr.inputs.Image(
|
|
|
|
|
8 |
]
|
9 |
|
10 |
gr_output = [
|
|
|
|
|
11 |
gr.outputs.Image(type="pil",label="Output Image")
|
12 |
]
|
13 |
|
14 |
iface = gr.Interface(
|
15 |
-
fn=
|
16 |
title="Space Title Here",
|
17 |
description = "Description Here",
|
18 |
inputs = gr_input,
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
from layers import BilinearUpSampling2D
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
from huggingface_hub import from_pretrained_keras
|
8 |
+
|
9 |
+
custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': None}
|
10 |
+
print('Loading model...')
|
11 |
+
model = from_pretrained_keras("keras-io/monocular-depth-estimation", custom_objects=custom_objects, compile=False)
|
12 |
+
print('Successfully loaded model...')
|
13 |
+
|
14 |
+
import importlib
|
15 |
+
import utils
|
16 |
+
importlib.reload(utils)
|
17 |
+
|
18 |
+
def infer(image, min_th, max_th):
|
19 |
+
print('_'*20)
|
20 |
+
inputs = utils.load_images([image])
|
21 |
+
outputs = utils.predict(model, inputs)
|
22 |
+
|
23 |
+
plasma = plt.get_cmap('plasma')
|
24 |
+
rescaled = outputs[0][:, :, 0]
|
25 |
+
print("Min Max Bef", np.min(rescaled), np.max(rescaled))
|
26 |
+
rescaled = rescaled - np.min(rescaled)
|
27 |
+
rescaled = rescaled / np.max(rescaled)
|
28 |
+
|
29 |
+
image_out = plasma(rescaled)[:, :, :3]
|
30 |
+
|
31 |
+
print("Min Max Aft", np.min(rescaled), np.max(rescaled))
|
32 |
+
|
33 |
+
print("Shape Scaled:",rescaled.shape)
|
34 |
+
filtered = rescaled
|
35 |
+
# filtered[filtered[:, :, 0] < min_th/100, 0] = 0
|
36 |
+
# filtered[filtered[:, :, 0] < min_th/100, 1] = 0
|
37 |
+
# filtered[filtered[:, :, 0] < min_th/100, 2] = 0
|
38 |
+
# filt_arr = filtered[((filtered[:,0] > min_th/100) & (filtered[:,0] < max_th/100))]
|
39 |
+
filt_arr = (filtered > min_th/100) * filtered * (filtered < max_th/100)
|
40 |
+
|
41 |
+
|
42 |
+
print("Shape Image:",image.shape)
|
43 |
+
print("Shape Image filt:",im_filt.shape)
|
44 |
+
print("Shape Image Heat:",image_out.shape)
|
45 |
+
im_filt = plasma(filt_arr)[:, :, :3]
|
46 |
+
return image_out, im_filt, image
|
47 |
+
|
48 |
+
# def detr(im):
|
49 |
+
# return im
|
50 |
|
51 |
gr_input = [
|
52 |
+
gr.inputs.Image(label="image", type="numpy", shape=(640, 480))
|
53 |
+
,gr.inputs.Slider(minimum=0, maximum=100, step=5, default=0, label="Minimum Threshold")
|
54 |
+
,gr.inputs.Slider(minimum=0, maximum=100, step=5, default=100, label="Maximum Threshold")
|
55 |
]
|
56 |
|
57 |
gr_output = [
|
58 |
+
gr.outputs.Image(type="pil",label="HeatMap Image"),
|
59 |
+
gr.outputs.Image(type="pil",label="Filtered Image"),
|
60 |
gr.outputs.Image(type="pil",label="Output Image")
|
61 |
]
|
62 |
|
63 |
iface = gr.Interface(
|
64 |
+
fn=infer,
|
65 |
title="Space Title Here",
|
66 |
description = "Description Here",
|
67 |
inputs = gr_input,
|
layers.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tensorflow.keras.layers import Layer, InputSpec
|
2 |
+
import keras.utils.conv_utils as conv_utils
|
3 |
+
import tensorflow as tf
|
4 |
+
import tensorflow.keras.backend as K
|
5 |
+
|
6 |
+
|
7 |
+
def normalize_data_format(value):
|
8 |
+
if value is None:
|
9 |
+
value = K.image_data_format()
|
10 |
+
data_format = value.lower()
|
11 |
+
if data_format not in {'channels_first', 'channels_last'}:
|
12 |
+
raise ValueError('The `data_format` argument must be one of '
|
13 |
+
'"channels_first", "channels_last". Received: ' +
|
14 |
+
str(value))
|
15 |
+
return data_format
|
16 |
+
|
17 |
+
|
18 |
+
class BilinearUpSampling2D(Layer):
|
19 |
+
def __init__(self, size=(2, 2), data_format=None, **kwargs):
|
20 |
+
super(BilinearUpSampling2D, self).__init__(**kwargs)
|
21 |
+
self.data_format = normalize_data_format(data_format)
|
22 |
+
self.size = conv_utils.normalize_tuple(size, 2, 'size')
|
23 |
+
self.input_spec = InputSpec(ndim=4)
|
24 |
+
|
25 |
+
def compute_output_shape(self, input_shape):
|
26 |
+
if self.data_format == 'channels_first':
|
27 |
+
height = self.size[0] * input_shape[2] if input_shape[2] is not None else None
|
28 |
+
width = self.size[1] * input_shape[3] if input_shape[3] is not None else None
|
29 |
+
return (input_shape[0],
|
30 |
+
input_shape[1],
|
31 |
+
height,
|
32 |
+
width)
|
33 |
+
elif self.data_format == 'channels_last':
|
34 |
+
height = self.size[0] * input_shape[1] if input_shape[1] is not None else None
|
35 |
+
width = self.size[1] * input_shape[2] if input_shape[2] is not None else None
|
36 |
+
return (input_shape[0],
|
37 |
+
height,
|
38 |
+
width,
|
39 |
+
input_shape[3])
|
40 |
+
|
41 |
+
def call(self, inputs):
|
42 |
+
input_shape = K.shape(inputs)
|
43 |
+
if self.data_format == 'channels_first':
|
44 |
+
height = self.size[0] * input_shape[2] if input_shape[2] is not None else None
|
45 |
+
width = self.size[1] * input_shape[3] if input_shape[3] is not None else None
|
46 |
+
elif self.data_format == 'channels_last':
|
47 |
+
height = self.size[0] * input_shape[1] if input_shape[1] is not None else None
|
48 |
+
width = self.size[1] * input_shape[2] if input_shape[2] is not None else None
|
49 |
+
|
50 |
+
return tf.image.resize(inputs, [height, width], method=tf.image.ResizeMethod.BILINEAR)
|
51 |
+
|
52 |
+
def get_config(self):
|
53 |
+
config = {'size': self.size, 'data_format': self.data_format}
|
54 |
+
base_config = super(BilinearUpSampling2D, self).get_config()
|
55 |
+
return dict(list(base_config.items()) + list(config.items()))
|
utils.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
|
4 |
+
def depth_norm(x, maxDepth):
|
5 |
+
return maxDepth / x
|
6 |
+
|
7 |
+
|
8 |
+
def predict(model, images, minDepth=10, maxDepth=1000, batch_size=2):
|
9 |
+
# Support multiple RGBs, one RGB image, even grayscale
|
10 |
+
if len(images.shape) < 3: images = np.stack((images, images, images), axis=2)
|
11 |
+
if len(images.shape) < 4: images = images.reshape((1, images.shape[0], images.shape[1], images.shape[2]))
|
12 |
+
# Compute predictions
|
13 |
+
predictions = model.predict(images, batch_size=batch_size)
|
14 |
+
# Put in expected range
|
15 |
+
print("Max Depth:", np.amax(predictions), maxDepth)
|
16 |
+
print("Min Depth:", np.amin(predictions), minDepth)
|
17 |
+
return np.clip(depth_norm(predictions, maxDepth=maxDepth), minDepth, maxDepth) / maxDepth
|
18 |
+
|
19 |
+
|
20 |
+
def load_images(image_files):
|
21 |
+
loaded_images = []
|
22 |
+
for file in image_files:
|
23 |
+
x = np.clip(file.reshape(480, 640, 3) / 255, 0, 1)
|
24 |
+
loaded_images.append(x)
|
25 |
+
return np.stack(loaded_images, axis=0)
|