mirror of
https://github.com/gradio-app/gradio.git
synced 2025-01-06 10:25:17 +08:00
changes
This commit is contained in:
parent
1943ec2f2c
commit
11f178a134
@ -110,10 +110,10 @@ class Sketchpad(AbstractInput):
|
||||
|
||||
|
||||
class Webcam(AbstractInput):
|
||||
def __init__(self, image_width=224, image_height=224, num_channels=3, label=None):
|
||||
self.image_width = image_width
|
||||
self.image_height = image_height
|
||||
self.num_channels = num_channels
|
||||
def __init__(self, shape=(224, 224), label=None):
|
||||
self.image_width = shape[0]
|
||||
self.image_height = shape[1]
|
||||
self.num_channels = 3
|
||||
super().__init__(label)
|
||||
|
||||
def get_validation_inputs(self):
|
||||
@ -132,8 +132,7 @@ class Webcam(AbstractInput):
|
||||
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))
|
||||
array = np.array(im).flatten().reshape(self.image_width, self.image_height, self.num_channels)
|
||||
return array
|
||||
return np.array(im)
|
||||
|
||||
|
||||
class Textbox(AbstractInput):
|
||||
@ -244,16 +243,11 @@ class Checkbox(AbstractInput):
|
||||
|
||||
|
||||
class Image(AbstractInput):
|
||||
def __init__(self, cast_to=None, shape=(224, 224, 3), image_mode='RGB',
|
||||
scale=1/127.5, shift=-1, cropper_aspect_ratio=None, label=None):
|
||||
def __init__(self, cast_to=None, shape=(224, 224), image_mode='RGB', label=None):
|
||||
self.cast_to = cast_to
|
||||
self.image_width = shape[0]
|
||||
self.image_height = shape[1]
|
||||
self.num_channels = shape[2]
|
||||
self.image_mode = image_mode
|
||||
self.scale = scale
|
||||
self.shift = shift
|
||||
self.cropper_aspect_ratio = "false" if cropper_aspect_ratio is None else cropper_aspect_ratio
|
||||
super().__init__(label)
|
||||
|
||||
def get_validation_inputs(self):
|
||||
@ -300,14 +294,7 @@ class Image(AbstractInput):
|
||||
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(self.image_width, self.image_height)
|
||||
else:
|
||||
array = im.reshape(self.image_width, self.image_height, \
|
||||
self.num_channels)
|
||||
return array
|
||||
return np.array(im)
|
||||
|
||||
def process_example(self, example):
|
||||
if os.path.exists(example):
|
||||
|
@ -1,4 +1,4 @@
|
||||
from PIL import Image
|
||||
from PIL import Image, ImageOps
|
||||
from io import BytesIO
|
||||
import base64
|
||||
import tempfile
|
||||
@ -41,57 +41,24 @@ def encode_array_to_base64(image_array):
|
||||
return "data:image/png;base64," + base64_str
|
||||
|
||||
|
||||
def resize_and_crop(img, size, crop_type='top'):
|
||||
def resize_and_crop(img, size, crop_type='center'):
|
||||
"""
|
||||
Resize and crop an image to fit the specified size.
|
||||
args:
|
||||
img_path: path for the image to resize.
|
||||
modified_path: path to store the modified image.
|
||||
size: `(width, height)` tuple.
|
||||
crop_type: can be 'top', 'middle' or 'bottom', depending on this
|
||||
value, the image will cropped getting the 'top/left', 'middle' or
|
||||
'bottom/right' of the image to fit the size.
|
||||
raises:
|
||||
Exception: if can not open the file in img_path of there is problems
|
||||
to save the image.
|
||||
ValueError: if an invalid `crop_type` is provided.
|
||||
"""
|
||||
# Get current and desired ratio for the images
|
||||
img_ratio = img.size[0] // float(img.size[1])
|
||||
ratio = size[0] // float(size[1])
|
||||
# The image is scaled//cropped vertically or horizontally depending on the ratio
|
||||
if ratio > img_ratio:
|
||||
img = img.resize((size[0], size[0] * img.size[1] // img.size[0]),
|
||||
Image.ANTIALIAS)
|
||||
# Crop in the top, middle or bottom
|
||||
if crop_type == 'top':
|
||||
box = (0, 0, img.size[0], size[1])
|
||||
elif crop_type == 'middle':
|
||||
box = (0, (img.size[1] - size[1]) // 2, img.size[0], (img.size[1] + size[1]) // 2)
|
||||
elif crop_type == 'bottom':
|
||||
box = (0, img.size[1] - size[1], img.size[0], img.size[1])
|
||||
else:
|
||||
raise ValueError('ERROR: invalid value for crop_type')
|
||||
img = img.crop(box)
|
||||
elif ratio < img_ratio:
|
||||
img = img.resize((size[1] * img.size[0] // img.size[1], size[1]),
|
||||
Image.ANTIALIAS)
|
||||
# Crop in the top, middle or bottom
|
||||
if crop_type == 'top':
|
||||
box = (0, 0, size[0], img.size[1])
|
||||
elif crop_type == 'middle':
|
||||
box = ((img.size[0] - size[0]) // 2, 0, (img.size[0] + size[0]) // 2, img.size[1])
|
||||
elif crop_type == 'bottom':
|
||||
box = (img.size[0] - size[0], 0, img.size[0], img.size[1])
|
||||
else:
|
||||
raise ValueError('ERROR: invalid value for crop_type')
|
||||
img = img.crop(box)
|
||||
if crop_type == "top":
|
||||
center = (0, 0)
|
||||
elif crop_type == "center":
|
||||
center = (0.5, 0.5)
|
||||
else:
|
||||
img = img.resize((size[0], size[1]),
|
||||
Image.ANTIALIAS)
|
||||
# If the scale is the same, we do not need to crop
|
||||
return img
|
||||
|
||||
raise ValueError
|
||||
return ImageOps.fit(img, size, centering=center)
|
||||
|
||||
##################
|
||||
# AUDIO FILES
|
||||
|
@ -15,7 +15,7 @@ def flip2(image):
|
||||
|
||||
|
||||
gr.Interface(flip2,
|
||||
"image",
|
||||
gr.inputs.Image(shape=(50, 50, 3)),
|
||||
["image", "text"],
|
||||
examples=[
|
||||
["images/cheetah1.jpg"],
|
||||
|
@ -2,6 +2,6 @@ import gradio as gr
|
||||
import numpy as np
|
||||
|
||||
def snap(image):
|
||||
return np.flipud(image)
|
||||
return image
|
||||
|
||||
gr.Interface(snap, "webcam", "image").launch()
|
||||
gr.Interface(snap, gr.inputs.Webcam(shape=(50,100)), "image").launch()
|
@ -1,4 +1,4 @@
|
||||
from PIL import Image
|
||||
from PIL import Image, ImageOps
|
||||
from io import BytesIO
|
||||
import base64
|
||||
import tempfile
|
||||
@ -41,57 +41,24 @@ def encode_array_to_base64(image_array):
|
||||
return "data:image/png;base64," + base64_str
|
||||
|
||||
|
||||
def resize_and_crop(img, size, crop_type='top'):
|
||||
def resize_and_crop(img, size, crop_type='center'):
|
||||
"""
|
||||
Resize and crop an image to fit the specified size.
|
||||
args:
|
||||
img_path: path for the image to resize.
|
||||
modified_path: path to store the modified image.
|
||||
size: `(width, height)` tuple.
|
||||
crop_type: can be 'top', 'middle' or 'bottom', depending on this
|
||||
value, the image will cropped getting the 'top/left', 'middle' or
|
||||
'bottom/right' of the image to fit the size.
|
||||
raises:
|
||||
Exception: if can not open the file in img_path of there is problems
|
||||
to save the image.
|
||||
ValueError: if an invalid `crop_type` is provided.
|
||||
"""
|
||||
# Get current and desired ratio for the images
|
||||
img_ratio = img.size[0] // float(img.size[1])
|
||||
ratio = size[0] // float(size[1])
|
||||
# The image is scaled//cropped vertically or horizontally depending on the ratio
|
||||
if ratio > img_ratio:
|
||||
img = img.resize((size[0], size[0] * img.size[1] // img.size[0]),
|
||||
Image.ANTIALIAS)
|
||||
# Crop in the top, middle or bottom
|
||||
if crop_type == 'top':
|
||||
box = (0, 0, img.size[0], size[1])
|
||||
elif crop_type == 'middle':
|
||||
box = (0, (img.size[1] - size[1]) // 2, img.size[0], (img.size[1] + size[1]) // 2)
|
||||
elif crop_type == 'bottom':
|
||||
box = (0, img.size[1] - size[1], img.size[0], img.size[1])
|
||||
else:
|
||||
raise ValueError('ERROR: invalid value for crop_type')
|
||||
img = img.crop(box)
|
||||
elif ratio < img_ratio:
|
||||
img = img.resize((size[1] * img.size[0] // img.size[1], size[1]),
|
||||
Image.ANTIALIAS)
|
||||
# Crop in the top, middle or bottom
|
||||
if crop_type == 'top':
|
||||
box = (0, 0, size[0], img.size[1])
|
||||
elif crop_type == 'middle':
|
||||
box = ((img.size[0] - size[0]) // 2, 0, (img.size[0] + size[0]) // 2, img.size[1])
|
||||
elif crop_type == 'bottom':
|
||||
box = (img.size[0] - size[0], 0, img.size[0], img.size[1])
|
||||
else:
|
||||
raise ValueError('ERROR: invalid value for crop_type')
|
||||
img = img.crop(box)
|
||||
if crop_type == "top":
|
||||
center = (0, 0)
|
||||
elif crop_type == "center":
|
||||
center = (0.5, 0.5)
|
||||
else:
|
||||
img = img.resize((size[0], size[1]),
|
||||
Image.ANTIALIAS)
|
||||
# If the scale is the same, we do not need to crop
|
||||
return img
|
||||
|
||||
raise ValueError
|
||||
return ImageOps.fit(img, size, centering=center)
|
||||
|
||||
##################
|
||||
# AUDIO FILES
|
||||
|
Loading…
Reference in New Issue
Block a user