From c84a67544af1ac48994be182a73d5de6a2aec1f0 Mon Sep 17 00:00:00 2001 From: dawoodkhan82 Date: Thu, 19 Nov 2020 03:02:11 -0500 Subject: [PATCH 1/4] add package version string --- gradio/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gradio/__init__.py b/gradio/__init__.py index bc36d88ec7..b9ff55429c 100644 --- a/gradio/__init__.py +++ b/gradio/__init__.py @@ -1 +1,5 @@ from gradio.interface import * # This makes it possible to import `Interface` as `gradio.Interface`. +import pkg_resources + +current_pkg_version = pkg_resources.require("gradio")[0].version +__version__ = current_pkg_version \ No newline at end of file From acd806dda86473915d1562fdcd18c582ab4b0f93 Mon Sep 17 00:00:00 2001 From: Ali Abid Date: Thu, 19 Nov 2020 06:32:04 -0800 Subject: [PATCH 2/4] webcam fixes --- .../static/js/interfaces/input/image.js | 38 ++++++++++++------- demo/webcam.py | 2 +- gradio/static/js/interfaces/input/image.js | 38 ++++++++++++------- 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/build/lib/gradio/static/js/interfaces/input/image.js b/build/lib/gradio/static/js/interfaces/input/image.js index 7c53e87cf3..bced9f0f7f 100644 --- a/build/lib/gradio/static/js/interfaces/input/image.js +++ b/build/lib/gradio/static/js/interfaces/input/image.js @@ -87,14 +87,18 @@ const image_input = { dest_height: dim, }) Webcam.attach(this.target.find(".webcam_box")[0]); - io.target.find(".webcam").click(function() { - Webcam.snap(function(image_data) { - io.target.find(".webcam").hide(); - io.target.find(".image_display").removeClass("hide"); - io.set_image_data(image_data, /*update_editor=*/true); - io.state = "IMAGE_LOADED"; - }); - }) + if (io.io_master.config.live) { + io.target.find(".webcam span").hide(); + } else { + io.target.find(".webcam").click(function() { + Webcam.snap(function(image_data) { + io.target.find(".webcam").hide(); + io.target.find(".image_display").removeClass("hide"); + io.set_image_data(image_data, /*update_editor=*/true); + io.state = "IMAGE_LOADED"; + }); + }) + } } else if (this.source == "canvas") { io.target.find(".sketchpad").removeClass("hide"); var dimension = Math.min(this.target.find(".canvas_holder").width(), @@ -156,12 +160,20 @@ const image_input = { io.io_master.input(io.id, this.image_data); } } else if (this.source == "webcam") { + if (!Webcam.loaded) { + io.io_master.no_input(); + return; + } Webcam.snap(function(image_data) { - io.target.find(".webcam").hide(); - io.target.find(".image_display").removeClass("hide"); - io.set_image_data(image_data, /*update_editor=*/true); - io.state = "IMAGE_LOADED"; - io.io_master.input(io.id, image_data); + if (io.io_master.config.live) { + io.io_master.input(io.id, image_data); + } else { + io.target.find(".webcam").hide(); + io.target.find(".image_display").removeClass("hide"); + io.set_image_data(image_data, /*update_editor=*/true); + io.state = "IMAGE_LOADED"; + io.io_master.input(io.id, image_data); + } }); } else { io.io_master.no_input(); diff --git a/demo/webcam.py b/demo/webcam.py index 1632cd516a..a72a9d2477 100644 --- a/demo/webcam.py +++ b/demo/webcam.py @@ -8,7 +8,7 @@ def snap(image): return np.flipud(image) -iface = gr.Interface(snap, gr.inputs.Image(shape=(100,100), image_mode="L", source="webcam"), "image") +iface = gr.Interface(snap, gr.inputs.Image(shape=(100,100), image_mode="L", source="webcam"), "image", live=True) iface.test_launch() if __name__ == "__main__": diff --git a/gradio/static/js/interfaces/input/image.js b/gradio/static/js/interfaces/input/image.js index 7c53e87cf3..bced9f0f7f 100644 --- a/gradio/static/js/interfaces/input/image.js +++ b/gradio/static/js/interfaces/input/image.js @@ -87,14 +87,18 @@ const image_input = { dest_height: dim, }) Webcam.attach(this.target.find(".webcam_box")[0]); - io.target.find(".webcam").click(function() { - Webcam.snap(function(image_data) { - io.target.find(".webcam").hide(); - io.target.find(".image_display").removeClass("hide"); - io.set_image_data(image_data, /*update_editor=*/true); - io.state = "IMAGE_LOADED"; - }); - }) + if (io.io_master.config.live) { + io.target.find(".webcam span").hide(); + } else { + io.target.find(".webcam").click(function() { + Webcam.snap(function(image_data) { + io.target.find(".webcam").hide(); + io.target.find(".image_display").removeClass("hide"); + io.set_image_data(image_data, /*update_editor=*/true); + io.state = "IMAGE_LOADED"; + }); + }) + } } else if (this.source == "canvas") { io.target.find(".sketchpad").removeClass("hide"); var dimension = Math.min(this.target.find(".canvas_holder").width(), @@ -156,12 +160,20 @@ const image_input = { io.io_master.input(io.id, this.image_data); } } else if (this.source == "webcam") { + if (!Webcam.loaded) { + io.io_master.no_input(); + return; + } Webcam.snap(function(image_data) { - io.target.find(".webcam").hide(); - io.target.find(".image_display").removeClass("hide"); - io.set_image_data(image_data, /*update_editor=*/true); - io.state = "IMAGE_LOADED"; - io.io_master.input(io.id, image_data); + if (io.io_master.config.live) { + io.io_master.input(io.id, image_data); + } else { + io.target.find(".webcam").hide(); + io.target.find(".image_display").removeClass("hide"); + io.set_image_data(image_data, /*update_editor=*/true); + io.state = "IMAGE_LOADED"; + io.io_master.input(io.id, image_data); + } }); } else { io.io_master.no_input(); From 960aa2a807d039966598072669d8bfeb519aa184 Mon Sep 17 00:00:00 2001 From: Ali Abid Date: Fri, 20 Nov 2020 08:15:02 -0800 Subject: [PATCH 3/4] clear on load example --- build/lib/gradio/__init__.py | 4 ++++ build/lib/gradio/static/js/gradio.js | 7 +++++-- gradio/static/js/gradio.js | 6 ++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/build/lib/gradio/__init__.py b/build/lib/gradio/__init__.py index bc36d88ec7..b9ff55429c 100644 --- a/build/lib/gradio/__init__.py +++ b/build/lib/gradio/__init__.py @@ -1 +1,5 @@ from gradio.interface import * # This makes it possible to import `Interface` as `gradio.Interface`. +import pkg_resources + +current_pkg_version = pkg_resources.require("gradio")[0].version +__version__ = current_pkg_version \ No newline at end of file diff --git a/build/lib/gradio/static/js/gradio.js b/build/lib/gradio/static/js/gradio.js index f299e78490..5e90dd48f2 100644 --- a/build/lib/gradio/static/js/gradio.js +++ b/build/lib/gradio/static/js/gradio.js @@ -51,6 +51,7 @@ function gradio(config, fn, target, example_file_path) { +
@@ -166,7 +167,7 @@ function gradio(config, fn, target, example_file_path) { } io_master.input_interfaces = input_interfaces; io_master.output_interfaces = output_interfaces; - target.find(".clear").click(function() { + function clear_all() { for (let input_interface of input_interfaces) { input_interface.clear(); } @@ -181,7 +182,8 @@ function gradio(config, fn, target, example_file_path) { target.find(".output_interfaces").css("opacity", 1); io_master.last_input = null; io_master.last_output = null; - }); + } + target.find(".clear").click(clear_all); if (!config["allow_screenshot"] && !config["allow_flagging"] && !config["allow_interpretation"]) { target.find(".screenshot, .record, .flag, .interpret").css("visibility", "hidden"); @@ -215,6 +217,7 @@ function gradio(config, fn, target, example_file_path) { } } function load_example(example_id) { + clear_all(); for (let [i, value] of config["examples"][example_id].entries()) { input_interfaces[i].load_example(value); }; diff --git a/gradio/static/js/gradio.js b/gradio/static/js/gradio.js index 677f8d3d82..5e90dd48f2 100644 --- a/gradio/static/js/gradio.js +++ b/gradio/static/js/gradio.js @@ -167,7 +167,7 @@ function gradio(config, fn, target, example_file_path) { } io_master.input_interfaces = input_interfaces; io_master.output_interfaces = output_interfaces; - target.find(".clear").click(function() { + function clear_all() { for (let input_interface of input_interfaces) { input_interface.clear(); } @@ -182,7 +182,8 @@ function gradio(config, fn, target, example_file_path) { target.find(".output_interfaces").css("opacity", 1); io_master.last_input = null; io_master.last_output = null; - }); + } + target.find(".clear").click(clear_all); if (!config["allow_screenshot"] && !config["allow_flagging"] && !config["allow_interpretation"]) { target.find(".screenshot, .record, .flag, .interpret").css("visibility", "hidden"); @@ -216,6 +217,7 @@ function gradio(config, fn, target, example_file_path) { } } function load_example(example_id) { + clear_all(); for (let [i, value] of config["examples"][example_id].entries()) { input_interfaces[i].load_example(value); }; From bd7e7c38f5b404342fec7863dfc8baee3a0547e9 Mon Sep 17 00:00:00 2001 From: Ali Abid Date: Fri, 20 Nov 2020 13:09:46 -0800 Subject: [PATCH 4/4] fix image saliency --- build/lib/gradio/inputs.py | 10 +++++++--- build/lib/gradio/static/js/interfaces/input/image.js | 6 +++++- gradio/inputs.py | 10 +++++++--- gradio/static/js/interfaces/input/image.js | 6 +++++- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/build/lib/gradio/inputs.py b/build/lib/gradio/inputs.py index 89e30093b7..2e99fd9d2e 100644 --- a/build/lib/gradio/inputs.py +++ b/build/lib/gradio/inputs.py @@ -531,7 +531,7 @@ class Image(InputComponent): def __init__(self, shape=None, image_mode='RGB', invert_colors=False, source="upload", tool="editor", type="numpy", label=None): ''' Parameters: - shape (Tuple[int, int]): shape to crop and resize image to; if None, matches input image size. + shape (Tuple[int, int]): (width, height) shape to crop and resize image to; if None, matches input image size. image_mode (str): "RGB" if color, or "L" if black and white. invert_colors (bool): whether to invert the image as a preprocessing step. source (str): Source of image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "canvas" defaults to a white image that can be edited and drawn upon with tools. @@ -559,6 +559,7 @@ class Image(InputComponent): def get_template_context(self): return { "image_mode": self.image_mode, + "shape": self.shape, "source": self.source, "tool": self.tool, **super().get_template_context() @@ -571,8 +572,7 @@ class Image(InputComponent): warnings.simplefilter("ignore") im = im.convert(self.image_mode) if self.shape is not None: - im = processing_utils.resize_and_crop( - im, (self.shape[0], self.shape[1])) + im = processing_utils.resize_and_crop(im, self.shape) if self.invert_colors: im = PIL.ImageOps.invert(im) if self.type == "pil": @@ -610,6 +610,8 @@ class Image(InputComponent): def get_interpretation_neighbors(self, x): x = processing_utils.decode_base64_to_image(x) + if self.shape is not None: + x = processing_utils.resize_and_crop(x, self.shape) image = np.array(x) segments_slic = slic(image, self.interpretation_segments, compactness=10, sigma=1) leave_one_out_tokens, masks = [], [] @@ -629,6 +631,8 @@ class Image(InputComponent): (List[List[float]]): A 2D array representing the interpretation score of each pixel of the image. """ x = processing_utils.decode_base64_to_image(x) + if self.shape is not None: + x = processing_utils.resize_and_crop(x, self.shape) x = np.array(x) output_scores = np.zeros((x.shape[0], x.shape[1])) diff --git a/build/lib/gradio/static/js/interfaces/input/image.js b/build/lib/gradio/static/js/interfaces/input/image.js index bced9f0f7f..ff1400f1e7 100644 --- a/build/lib/gradio/static/js/interfaces/input/image.js +++ b/build/lib/gradio/static/js/interfaces/input/image.js @@ -47,6 +47,7 @@ const image_input = { `, init: function(opts) { var io = this; + this.shape = opts.shape; this.source = opts.source; this.tool = opts.tool; if (this.tool == "select") { @@ -200,7 +201,10 @@ const image_input = { show_interpretation: function(data) { if (this.target.find(".image_preview").attr("src")) { var img = this.target.find(".image_preview")[0]; - var size = getObjectFitSize(true, img.width, img.height, img.naturalWidth, img.naturalHeight) + var size = getObjectFitSize(true, img.width, img.height, img.naturalWidth, img.naturalHeight); + if (this.shape) { + size = getObjectFitSize(true, size.width, size.height, this.shape[0], this.shape[1]) + } var width = size.width; var height = size.height; this.target.find(".saliency_holder").removeClass("hide").html(` diff --git a/gradio/inputs.py b/gradio/inputs.py index 89e30093b7..2e99fd9d2e 100644 --- a/gradio/inputs.py +++ b/gradio/inputs.py @@ -531,7 +531,7 @@ class Image(InputComponent): def __init__(self, shape=None, image_mode='RGB', invert_colors=False, source="upload", tool="editor", type="numpy", label=None): ''' Parameters: - shape (Tuple[int, int]): shape to crop and resize image to; if None, matches input image size. + shape (Tuple[int, int]): (width, height) shape to crop and resize image to; if None, matches input image size. image_mode (str): "RGB" if color, or "L" if black and white. invert_colors (bool): whether to invert the image as a preprocessing step. source (str): Source of image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "canvas" defaults to a white image that can be edited and drawn upon with tools. @@ -559,6 +559,7 @@ class Image(InputComponent): def get_template_context(self): return { "image_mode": self.image_mode, + "shape": self.shape, "source": self.source, "tool": self.tool, **super().get_template_context() @@ -571,8 +572,7 @@ class Image(InputComponent): warnings.simplefilter("ignore") im = im.convert(self.image_mode) if self.shape is not None: - im = processing_utils.resize_and_crop( - im, (self.shape[0], self.shape[1])) + im = processing_utils.resize_and_crop(im, self.shape) if self.invert_colors: im = PIL.ImageOps.invert(im) if self.type == "pil": @@ -610,6 +610,8 @@ class Image(InputComponent): def get_interpretation_neighbors(self, x): x = processing_utils.decode_base64_to_image(x) + if self.shape is not None: + x = processing_utils.resize_and_crop(x, self.shape) image = np.array(x) segments_slic = slic(image, self.interpretation_segments, compactness=10, sigma=1) leave_one_out_tokens, masks = [], [] @@ -629,6 +631,8 @@ class Image(InputComponent): (List[List[float]]): A 2D array representing the interpretation score of each pixel of the image. """ x = processing_utils.decode_base64_to_image(x) + if self.shape is not None: + x = processing_utils.resize_and_crop(x, self.shape) x = np.array(x) output_scores = np.zeros((x.shape[0], x.shape[1])) diff --git a/gradio/static/js/interfaces/input/image.js b/gradio/static/js/interfaces/input/image.js index bced9f0f7f..ff1400f1e7 100644 --- a/gradio/static/js/interfaces/input/image.js +++ b/gradio/static/js/interfaces/input/image.js @@ -47,6 +47,7 @@ const image_input = { `, init: function(opts) { var io = this; + this.shape = opts.shape; this.source = opts.source; this.tool = opts.tool; if (this.tool == "select") { @@ -200,7 +201,10 @@ const image_input = { show_interpretation: function(data) { if (this.target.find(".image_preview").attr("src")) { var img = this.target.find(".image_preview")[0]; - var size = getObjectFitSize(true, img.width, img.height, img.naturalWidth, img.naturalHeight) + var size = getObjectFitSize(true, img.width, img.height, img.naturalWidth, img.naturalHeight); + if (this.shape) { + size = getObjectFitSize(true, size.width, size.height, this.shape[0], this.shape[1]) + } var width = size.width; var height = size.height; this.target.find(".saliency_holder").removeClass("hide").html(`