コード例 #1
0
ファイル: inputs.py プロジェクト: ylvaldes/gradio
 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)
     im = im.convert('RGB')
     im = preprocessing_utils.resize_and_crop(im, (self.image_width, self.image_height))
     return np.array(im)
コード例 #2
0
 def rebuild(self, dir, data):
     """
     Default rebuild method to decode a base64 image
     """
     im = preprocessing_utils.decode_base64_to_image(data)
     timestamp = datetime.datetime.now()
     filename = f'input_{timestamp.strftime("%Y-%m-%d-%H-%M-%S")}.png'
     im.save('{}/{}'.format(dir, filename), 'PNG')
     return filename
コード例 #3
0
ファイル: inputs.py プロジェクト: xjzpguob/gradio-UI
 def rebuild_flagged(self, dir, msg):
     """
     Default rebuild method to decode a base64 image
     """
     im = preprocessing_utils.decode_base64_to_image(msg)
     timestamp = time.time() * 1000
     filename = f'input_{timestamp}.png'
     im.save(f'{dir}/{filename}', 'PNG')
     return filename
コード例 #4
0
 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
コード例 #5
0
 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 = 'output_{}.png'.format(timestamp.
                                       strftime("%Y-%m-%d-%H-%M-%S"))
     im.save('{}/{}'.format(dir, filename), 'PNG')
     return filename
コード例 #6
0
ファイル: inputs.py プロジェクト: ylvaldes/gradio
    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))
        return np.array(im)
コード例 #7
0
ファイル: inputs.py プロジェクト: ylvaldes/gradio
 def preprocess(self, inp):
     """
     Default preprocessing method for the SketchPad is to convert the sketch to black and white and resize 28x28
     """
     im_transparent = preprocessing_utils.decode_base64_to_image(inp)
     im = PIL.Image.new("RGBA", im_transparent.size, "WHITE")  # Create a white background for the alpha channel
     im.paste(im_transparent, (0, 0), im_transparent)
     im = im.convert('L')
     if self.invert_colors:
         im = PIL.ImageOps.invert(im)
     im = im.resize((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)
     return array
コード例 #8
0
 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
コード例 #9
0
ファイル: inputs.py プロジェクト: xjzpguob/gradio-UI
    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
コード例 #10
0
ファイル: networking.py プロジェクト: zy1620454507/gradio
        def do_POST(self):
            # Read body of the request.

            if self.path == "/api/predict/":
                # Make the prediction.
                self._set_headers()
                data_string = self.rfile.read(
                    int(self.headers["Content-Length"]))
                msg = json.loads(data_string)
                processed_input = interface.input_interface.preprocess(
                    msg["data"])
                prediction = interface.predict(processed_input)
                processed_output = interface.output_interface.postprocess(
                    prediction)
                output = {"action": "output", "data": processed_output}
                if interface.saliency is not None:
                    import numpy as np
                    saliency = interface.saliency(interface.model_obj,
                                                  processed_input, prediction)
                    output['saliency'] = saliency.tolist()

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

            elif self.path == "/api/flag/":
                self._set_headers()
                data_string = self.rfile.read(
                    int(self.headers["Content-Length"]))
                msg = json.loads(data_string)
                flag_dir = os.path.join(directory_to_serve, FLAGGING_DIRECTORY)
                os.makedirs(flag_dir, exist_ok=True)
                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'
                    }

                    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}'
                    }

                    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())

            else:
                self.send_error(404, 'Path not found: %s' % self.path)