2022-01-21 21:44:12 +08:00
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
2022-04-05 18:08:53 +08:00
|
|
|
from copy import deepcopy
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2022-04-05 18:08:53 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2021-11-04 05:17:26 +08:00
|
|
|
import numpy as np
|
2022-01-21 21:44:12 +08:00
|
|
|
from PIL import Image
|
2021-11-03 05:22:52 +08:00
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
import gradio as gr
|
2022-04-20 02:27:32 +08:00
|
|
|
from gradio import media_data
|
2022-03-30 18:26:39 +08:00
|
|
|
|
2021-11-10 02:30:59 +08:00
|
|
|
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
2021-11-09 22:48:55 +08:00
|
|
|
|
|
|
|
|
2021-11-03 05:22:52 +08:00
|
|
|
class ImagePreprocessing(unittest.TestCase):
|
|
|
|
def test_decode_base64_to_image(self):
|
|
|
|
output_image = gr.processing_utils.decode_base64_to_image(
|
2022-04-05 18:08:53 +08:00
|
|
|
deepcopy(media_data.BASE64_IMAGE)
|
2022-01-21 21:44:12 +08:00
|
|
|
)
|
2021-11-03 05:22:52 +08:00
|
|
|
self.assertIsInstance(output_image, Image.Image)
|
|
|
|
|
|
|
|
def test_encode_url_or_file_to_base64(self):
|
|
|
|
output_base64 = gr.processing_utils.encode_url_or_file_to_base64(
|
2022-04-06 20:55:51 +08:00
|
|
|
"gradio/test_data/test_image.png"
|
2022-01-21 21:44:12 +08:00
|
|
|
)
|
2022-04-05 18:08:53 +08:00
|
|
|
self.assertEquals(output_base64, deepcopy(media_data.BASE64_IMAGE))
|
2021-11-03 05:22:52 +08:00
|
|
|
|
|
|
|
def test_encode_file_to_base64(self):
|
|
|
|
output_base64 = gr.processing_utils.encode_file_to_base64(
|
2022-04-06 20:55:51 +08:00
|
|
|
"gradio/test_data/test_image.png"
|
2022-01-21 21:44:12 +08:00
|
|
|
)
|
2022-04-05 18:08:53 +08:00
|
|
|
self.assertEquals(output_base64, deepcopy(media_data.BASE64_IMAGE))
|
2021-11-03 05:22:52 +08:00
|
|
|
|
2021-11-09 04:45:25 +08:00
|
|
|
def test_encode_url_to_base64(self):
|
|
|
|
output_base64 = gr.processing_utils.encode_url_to_base64(
|
2022-04-19 22:32:40 +08:00
|
|
|
"https://raw.githubusercontent.com/gradio-app/gradio/main/gradio/test_data/test_image.png"
|
2022-01-21 21:44:12 +08:00
|
|
|
)
|
2022-04-05 18:08:53 +08:00
|
|
|
self.assertEqual(output_base64, deepcopy(media_data.BASE64_IMAGE))
|
2021-11-04 05:17:26 +08:00
|
|
|
|
2022-04-05 18:08:53 +08:00
|
|
|
def test_encode_plot_to_base64(self):
|
|
|
|
plt.plot([1, 2, 3, 4])
|
|
|
|
output_base64 = gr.processing_utils.encode_plot_to_base64(plt)
|
|
|
|
self.assertTrue(
|
|
|
|
output_base64.startswith("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAo")
|
|
|
|
)
|
2021-11-04 05:17:26 +08:00
|
|
|
|
|
|
|
def test_encode_array_to_base64(self):
|
2022-04-06 20:55:51 +08:00
|
|
|
img = Image.open("gradio/test_data/test_image.png")
|
2021-11-04 05:17:26 +08:00
|
|
|
img = img.convert("RGB")
|
|
|
|
numpy_data = np.asarray(img, dtype=np.uint8)
|
|
|
|
output_base64 = gr.processing_utils.encode_array_to_base64(numpy_data)
|
2022-04-05 18:08:53 +08:00
|
|
|
self.assertEqual(output_base64, deepcopy(media_data.ARRAY_TO_BASE64_IMAGE))
|
2021-11-09 04:37:32 +08:00
|
|
|
|
|
|
|
def test_resize_and_crop(self):
|
2022-04-06 20:55:51 +08:00
|
|
|
img = Image.open("gradio/test_data/test_image.png")
|
2021-11-09 04:37:32 +08:00
|
|
|
new_img = gr.processing_utils.resize_and_crop(img, (20, 20))
|
|
|
|
self.assertEqual(new_img.size, (20, 20))
|
2022-01-21 21:44:12 +08:00
|
|
|
self.assertRaises(
|
|
|
|
ValueError,
|
|
|
|
gr.processing_utils.resize_and_crop,
|
|
|
|
**{"img": img, "size": (20, 20), "crop_type": "test"}
|
|
|
|
)
|
|
|
|
|
2021-11-09 04:37:32 +08:00
|
|
|
|
|
|
|
class AudioPreprocessing(unittest.TestCase):
|
|
|
|
def test_audio_from_file(self):
|
2022-04-06 20:55:51 +08:00
|
|
|
audio = gr.processing_utils.audio_from_file("gradio/test_data/test_audio.wav")
|
2021-11-09 04:37:32 +08:00
|
|
|
self.assertEqual(audio[0], 22050)
|
|
|
|
self.assertIsInstance(audio[1], np.ndarray)
|
|
|
|
|
|
|
|
def test_audio_to_file(self):
|
2022-04-06 20:55:51 +08:00
|
|
|
audio = gr.processing_utils.audio_from_file("gradio/test_data/test_audio.wav")
|
2021-11-09 04:37:32 +08:00
|
|
|
gr.processing_utils.audio_to_file(audio[0], audio[1], "test_audio_to_file")
|
|
|
|
self.assertTrue(os.path.exists("test_audio_to_file"))
|
|
|
|
os.remove("test_audio_to_file")
|
|
|
|
|
2021-11-04 05:17:26 +08:00
|
|
|
|
|
|
|
class OutputPreprocessing(unittest.TestCase):
|
2021-11-09 04:37:32 +08:00
|
|
|
def test_decode_base64_to_binary(self):
|
2022-04-05 18:08:53 +08:00
|
|
|
binary = gr.processing_utils.decode_base64_to_binary(
|
|
|
|
deepcopy(media_data.BASE64_IMAGE)
|
|
|
|
)
|
|
|
|
self.assertEqual(deepcopy(media_data.BINARY_IMAGE), binary)
|
2021-11-09 04:37:32 +08:00
|
|
|
|
|
|
|
def test_decode_base64_to_file(self):
|
2022-04-05 18:08:53 +08:00
|
|
|
temp_file = gr.processing_utils.decode_base64_to_file(
|
|
|
|
deepcopy(media_data.BASE64_IMAGE)
|
|
|
|
)
|
2021-11-09 04:37:32 +08:00
|
|
|
self.assertIsInstance(temp_file, tempfile._TemporaryFileWrapper)
|
|
|
|
|
|
|
|
def test_create_tmp_copy_of_file(self):
|
2022-01-21 21:44:12 +08:00
|
|
|
temp_file = gr.processing_utils.create_tmp_copy_of_file("test.txt")
|
2021-11-09 04:37:32 +08:00
|
|
|
self.assertIsInstance(temp_file, tempfile._TemporaryFileWrapper)
|
2021-11-04 05:17:26 +08:00
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
float_dtype_list = [
|
|
|
|
float,
|
|
|
|
float,
|
|
|
|
np.double,
|
|
|
|
np.single,
|
|
|
|
np.float32,
|
|
|
|
np.float64,
|
|
|
|
"float32",
|
|
|
|
"float64",
|
|
|
|
]
|
2021-11-09 04:37:32 +08:00
|
|
|
|
2021-11-04 05:17:26 +08:00
|
|
|
def test_float_conversion_dtype(self):
|
|
|
|
"""Test any convertion from a float dtype to an other."""
|
|
|
|
|
|
|
|
x = np.array([-1, 1])
|
|
|
|
# Test all combinations of dtypes conversions
|
2022-01-21 21:44:12 +08:00
|
|
|
dtype_combin = np.array(
|
|
|
|
np.meshgrid(
|
|
|
|
OutputPreprocessing.float_dtype_list,
|
|
|
|
OutputPreprocessing.float_dtype_list,
|
|
|
|
)
|
|
|
|
).T.reshape(-1, 2)
|
2021-11-04 05:17:26 +08:00
|
|
|
|
|
|
|
for dtype_in, dtype_out in dtype_combin:
|
|
|
|
x = x.astype(dtype_in)
|
|
|
|
y = gr.processing_utils._convert(x, dtype_out)
|
|
|
|
assert y.dtype == np.dtype(dtype_out)
|
|
|
|
|
|
|
|
def test_subclass_conversion(self):
|
|
|
|
"""Check subclass conversion behavior"""
|
|
|
|
x = np.array([-1, 1])
|
|
|
|
for dtype in OutputPreprocessing.float_dtype_list:
|
|
|
|
x = x.astype(dtype)
|
|
|
|
y = gr.processing_utils._convert(x, np.floating)
|
|
|
|
assert y.dtype == x.dtype
|
2021-11-03 05:22:52 +08:00
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-11-03 05:22:52 +08:00
|
|
|
unittest.main()
|