Spaces:
Runtime error
Runtime error
GoBoKyung commited on
Commit ยท
5ffae2f
1
Parent(s): 074b669
city
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from matplotlib import gridspec
|
| 3 |
-
import matplotlib.pyplot as plt
|
| 4 |
-
import numpy as np
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
import tensorflow as tf
|
| 7 |
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
|
| 8 |
import os
|
|
@@ -15,7 +13,7 @@ model = TFSegformerForSemanticSegmentation.from_pretrained(
|
|
| 15 |
)
|
| 16 |
|
| 17 |
def ade_palette():
|
| 18 |
-
"""ADE20K
|
| 19 |
return [
|
| 20 |
[204, 87, 92],
|
| 21 |
[112, 185, 212],
|
|
@@ -48,39 +46,18 @@ colormap = np.asarray(ade_palette())
|
|
| 48 |
|
| 49 |
def label_to_color_image(label):
|
| 50 |
if label.ndim != 2:
|
| 51 |
-
raise ValueError("2-D
|
| 52 |
|
| 53 |
if np.max(label) >= len(colormap):
|
| 54 |
-
raise ValueError("
|
| 55 |
return colormap[label]
|
| 56 |
|
| 57 |
-
def draw_plot(pred_img, seg):
|
| 58 |
-
fig = plt.figure(figsize=(20, 15))
|
| 59 |
-
|
| 60 |
-
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
| 61 |
-
|
| 62 |
-
plt.subplot(grid_spec[0])
|
| 63 |
-
plt.imshow(pred_img)
|
| 64 |
-
plt.axis('off')
|
| 65 |
-
LABEL_NAMES = np.asarray(labels_list)
|
| 66 |
-
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
| 67 |
-
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
| 68 |
-
|
| 69 |
-
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
| 70 |
-
ax = plt.subplot(grid_spec[1])
|
| 71 |
-
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
| 72 |
-
ax.yaxis.tick_right()
|
| 73 |
-
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
| 74 |
-
plt.xticks([], [])
|
| 75 |
-
ax.tick_params(width=0.0, labelsize=25)
|
| 76 |
-
return fig
|
| 77 |
-
|
| 78 |
def sepia(input_text):
|
| 79 |
-
#
|
| 80 |
if not os.path.isfile(input_text):
|
| 81 |
-
return "
|
| 82 |
|
| 83 |
-
#
|
| 84 |
input_img = Image.open(input_text)
|
| 85 |
|
| 86 |
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
|
@@ -102,13 +79,13 @@ def sepia(input_text):
|
|
| 102 |
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
| 103 |
pred_img = pred_img.astype(np.uint8)
|
| 104 |
|
| 105 |
-
#
|
| 106 |
pred_img = Image.fromarray(pred_img)
|
| 107 |
|
| 108 |
return pred_img
|
| 109 |
|
| 110 |
-
#
|
| 111 |
iface = gr.Interface(fn=sepia, inputs="image", outputs="image")
|
| 112 |
|
| 113 |
-
#
|
| 114 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
import tensorflow as tf
|
| 5 |
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
|
| 6 |
import os
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
def ade_palette():
|
| 16 |
+
"""ADE20K palette that maps each class to RGB values."""
|
| 17 |
return [
|
| 18 |
[204, 87, 92],
|
| 19 |
[112, 185, 212],
|
|
|
|
| 46 |
|
| 47 |
def label_to_color_image(label):
|
| 48 |
if label.ndim != 2:
|
| 49 |
+
raise ValueError("Expect 2-D input label")
|
| 50 |
|
| 51 |
if np.max(label) >= len(colormap):
|
| 52 |
+
raise ValueError("label value too large.")
|
| 53 |
return colormap[label]
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def sepia(input_text):
|
| 56 |
+
# Check if the input text is a valid file path
|
| 57 |
if not os.path.isfile(input_text):
|
| 58 |
+
return "Invalid file path. Please enter a valid image file path."
|
| 59 |
|
| 60 |
+
# Load the image using the input text (assumed to be a path to an image)
|
| 61 |
input_img = Image.open(input_text)
|
| 62 |
|
| 63 |
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
|
|
|
| 79 |
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
| 80 |
pred_img = pred_img.astype(np.uint8)
|
| 81 |
|
| 82 |
+
# Convert the image array to a Pillow (PIL) image
|
| 83 |
pred_img = Image.fromarray(pred_img)
|
| 84 |
|
| 85 |
return pred_img
|
| 86 |
|
| 87 |
+
# Define the Gradio interface
|
| 88 |
iface = gr.Interface(fn=sepia, inputs="image", outputs="image")
|
| 89 |
|
| 90 |
+
# Launch the Gradio app
|
| 91 |
iface.launch()
|