How to use the gradio.preprocessing_utils function in gradio

To help you get started, we’ve selected a few gradio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gradio-app / gradio-UI / gradio / outputs.py View on Github external
def rebuild_flagged(self, dir, msg):
        """
        Default rebuild method to decode a base64 image
        """
        im = preprocessing_utils.decode_base64_to_image(msg)
        timestamp = datetime.datetime.now()
        filename = f'output_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.png'
        im.save(f'{dir}/{filename}', 'PNG')
        return filename
github gradio-app / gradio-UI / gradio / networking.py View on Github external
output = {'input': interface.input_interface.rebuild_flagged(flag_dir, msg['data']['input_data']),
                          'output': interface.output_interface.rebuild_flagged(flag_dir, msg['data']['output_data']),
                          'message': msg['data']['message']}
                with open(os.path.join(flag_dir, FLAGGING_FILENAME), 'a+') as f:
                    f.write(json.dumps(output))
                    f.write("\n")

            #TODO(abidlabs): clean this up
            elif self.path == "/api/auto/rotation":
                from gradio import validation_data, preprocessing_utils
                import numpy as np

                self._set_headers()
                data_string = self.rfile.read(int(self.headers["Content-Length"]))
                msg = json.loads(data_string)
                img_orig = preprocessing_utils.decode_base64_to_image(msg["data"])
                img_orig = img_orig.convert('RGB')
                img_orig = img_orig.resize((224, 224))

                flag_dir = os.path.join(directory_to_serve, FLAGGING_DIRECTORY)
                os.makedirs(flag_dir, exist_ok=True)

                for deg in range(-180, 180+45, 45):
                    img = img_orig.rotate(deg)
                    img_array = np.array(img) / 127.5 - 1
                    prediction = interface.predict(np.expand_dims(img_array, axis=0))
                    processed_output = interface.output_interface.postprocess(prediction)
                    output = {'input': interface.input_interface.save_to_file(flag_dir, img),
                              'output': interface.output_interface.rebuild_flagged(
                                  flag_dir, {'data': {'output': processed_output}}),
                              'message': f'rotation by {deg} degrees'}
github gradio-app / gradio-UI / gradio / networking.py View on Github external
with open(os.path.join(flag_dir, FLAGGING_FILENAME), 'a+') as f:
                        f.write(json.dumps(output))
                        f.write("\n")

                # Prepare return json dictionary.
                self.wfile.write(json.dumps({}).encode())

            elif self.path == "/api/auto/lighting":
                from gradio import validation_data, preprocessing_utils
                import numpy as np
                from PIL import ImageEnhance

                self._set_headers()
                data_string = self.rfile.read(int(self.headers["Content-Length"]))
                msg = json.loads(data_string)
                img_orig = preprocessing_utils.decode_base64_to_image(msg["data"])
                img_orig = img_orig.convert('RGB')
                img_orig = img_orig.resize((224, 224))
                enhancer = ImageEnhance.Brightness(img_orig)

                flag_dir = os.path.join(directory_to_serve, FLAGGING_DIRECTORY)
                os.makedirs(flag_dir, exist_ok=True)

                for i in range(9):
                    img = enhancer.enhance(i/4)
                    img_array = np.array(img) / 127.5 - 1
                    prediction = interface.predict(np.expand_dims(img_array, axis=0))
                    processed_output = interface.output_interface.postprocess(prediction)
                    output = {'input': interface.input_interface.save_to_file(flag_dir, img),
                              'output': interface.output_interface.rebuild_flagged(
                                  flag_dir, {'data': {'output': processed_output}}),
                              'message': f'brighting adjustment by a factor of {i}'}
github gradio-app / gradio-UI / build / lib / gradio / inputs.py View on Github external
def preprocess(self, inp):
        """
        Default preprocessing method for the SketchPad is to convert the sketch to black and white and resize 28x28
        """
        im = preprocessing_utils.decode_base64_to_image(inp)
        im = im.convert('L')
        if self.invert_colors:
            im = ImageOps.invert(im)
        im = preprocessing_utils.resize_and_crop(im, (self.image_width, self.image_height))
        if self.flatten:
            array = np.array(im).flatten().reshape(1, self.image_width * self.image_height)
        else:
            array = np.array(im).flatten().reshape(1, self.image_width, self.image_height)
        array = array * self.scale + self.shift
        array = array.astype(self.dtype)
        return array
github gradio-app / gradio-UI / build / lib / gradio / networking.py View on Github external
output = {'input': interface.input_interface.rebuild_flagged(flag_dir, msg),
                          'output': interface.output_interface.rebuild_flagged(flag_dir, msg),
                          'message': msg['data']['message']}
                with open(os.path.join(flag_dir, FLAGGING_FILENAME), 'a+') as f:
                    f.write(json.dumps(output))
                    f.write("\n")

            #TODO(abidlabs): clean this up
            elif self.path == "/api/auto/rotation":
                from gradio import validation_data, preprocessing_utils
                import numpy as np

                self._set_headers()
                data_string = self.rfile.read(int(self.headers["Content-Length"]))
                msg = json.loads(data_string)
                img_orig = preprocessing_utils.decode_base64_to_image(msg["data"])
                img_orig = img_orig.convert('RGB')
                img_orig = img_orig.resize((224, 224))

                flag_dir = os.path.join(directory_to_serve, FLAGGING_DIRECTORY)
                os.makedirs(flag_dir, exist_ok=True)

                for deg in range(-180, 180+45, 45):
                    img = img_orig.rotate(deg)
                    img_array = np.array(img) / 127.5 - 1
                    prediction = interface.predict(np.expand_dims(img_array, axis=0))
                    processed_output = interface.output_interface.postprocess(prediction)
                    output = {'input': interface.input_interface.save_to_file(flag_dir, img),
                              'output': interface.output_interface.rebuild_flagged(
                                  flag_dir, {'data': {'output': processed_output}}),
                              'message': f'rotation by {deg} degrees'}
github gradio-app / gradio-UI / gradio / inputs.py View on Github external
def preprocess(self, inp):
        """
        By default, no pre-processing is applied to a microphone input file
        """
        file_obj = preprocessing_utils.decode_base64_to_wav_file(inp)
        mfcc_array = preprocessing_utils.generate_mfcc_features_from_audio_file(file_obj.name)
        return mfcc_array
github gradio-app / gradio-UI / build / lib / gradio / inputs.py View on Github external
def rebuild_flagged(self, dir, msg):
        """
        Default rebuild method to decode a base64 image
        """
        inp = msg['data']['input']
        im = preprocessing_utils.decode_base64_to_image(inp)
        timestamp = datetime.datetime.now()
        filename = f'input_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.png'
        im.save(f'{dir}/{filename}', 'PNG')
        return filename
github gradio-app / gradio-UI / build / lib / gradio / inputs.py View on Github external
def preprocess(self, inp):
        """
        Default preprocessing method for is to convert the picture to black and white and resize to be 48x48
        """
        im = preprocessing_utils.decode_base64_to_image(inp)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            im = im.convert(self.image_mode)

        im = preprocessing_utils.resize_and_crop(im, (self.image_width, self.image_height))
        im = np.array(im).flatten()
        im = im * self.scale + self.shift
        if self.num_channels is None:
            array = im.reshape(1, self.image_width, self.image_height)
        else:
            array = im.reshape(1, self.image_width, self.image_height, self.num_channels)
        return array