Spaces:
Runtime error
Runtime error
Commit
·
0106fbc
1
Parent(s):
ec0fdfd
test
Browse files
app.py
CHANGED
|
@@ -1,102 +1,7 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import sys
|
| 4 |
-
sys.path.append(".")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
from PIL import Image
|
| 12 |
-
import time
|
| 13 |
-
|
| 14 |
-
import imageio
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# def run(initial_image, mask, Backgrounds, Backgrounds_complexity, Size, Angle, Steps, num_of_Images):
|
| 18 |
-
def run(source_img, Backgrounds, Backgrounds_complexity, Size, Angle, Steps, num_of_Images):
|
| 19 |
-
print('-------------------starting to process-------------------')
|
| 20 |
-
if os.path.exists('results'):
|
| 21 |
-
shutil.rmtree("results")
|
| 22 |
-
if os.path.exists('tmp'):
|
| 23 |
-
shutil.rmtree("tmp")
|
| 24 |
-
time.sleep(1)
|
| 25 |
-
os.makedirs('results', exist_ok=True)
|
| 26 |
-
os.makedirs('tmp/img', exist_ok=True)
|
| 27 |
-
os.makedirs('tmp/mask', exist_ok=True)
|
| 28 |
-
os.makedirs('tmp/bg', exist_ok=True)
|
| 29 |
-
|
| 30 |
-
'''
|
| 31 |
-
print('-----initial_image: ', initial_image)
|
| 32 |
-
init_image = Image.open(initial_image)
|
| 33 |
-
mask = Image.open(mask)
|
| 34 |
-
init_image = init_image.resize((256,256))
|
| 35 |
-
mask = mask.resize((256,256))
|
| 36 |
-
init_image.save("tmp/img/input.JPEG")
|
| 37 |
-
mask.save("tmp/mask/input.png")
|
| 38 |
-
'''
|
| 39 |
-
imageio.imwrite("tmp/img/input.JPEG", source_img["image"])
|
| 40 |
-
imageio.imwrite("tmp/mask/input.png", source_img["mask"])
|
| 41 |
-
|
| 42 |
-
initial_image = Image.open('tmp/img/input.JPEG').resize((256,256))
|
| 43 |
-
initial_image.save('tmp/img/input.JPEG')
|
| 44 |
-
mask = Image.open('tmp/mask/input.png').resize((256,256))
|
| 45 |
-
mask.save('tmp/mask/input.png')
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if Backgrounds:
|
| 49 |
-
background_specific = Backgrounds
|
| 50 |
-
if background_specific is not None:
|
| 51 |
-
background_specific = Image.open(background_specific).convert('RGB') # Specified background
|
| 52 |
-
background_specific = background_specific.resize((256,256))
|
| 53 |
-
background_specific.save('tmp/bg/bg.png')
|
| 54 |
-
background_specific = '../tmp/bg/bg.png'
|
| 55 |
-
else:
|
| 56 |
-
background_specific = ""
|
| 57 |
-
|
| 58 |
-
Backgrounds_complexity = Backgrounds_complexity
|
| 59 |
-
Size = Size
|
| 60 |
-
Angle = Angle
|
| 61 |
-
Steps = Steps
|
| 62 |
-
num_of_Images = num_of_Images
|
| 63 |
-
print(Backgrounds_complexity, background_specific, Size, Angle, Steps, num_of_Images)
|
| 64 |
-
p = subprocess.Popen(["sh", "run.sh", str(Backgrounds_complexity), background_specific, str(Size), str(Angle), str(Steps), str(num_of_Images)])
|
| 65 |
-
|
| 66 |
-
# subprocess.Popen(["cd", "object_removal/TFill/"])
|
| 67 |
-
# subprocess.Popen(["python", "test.py"])
|
| 68 |
-
|
| 69 |
-
return_code = p.wait()
|
| 70 |
-
print('----return_code: ', return_code)
|
| 71 |
-
|
| 72 |
-
if os.path.exists('results/edited.png'):
|
| 73 |
-
return Image.open('results/edited.png')
|
| 74 |
-
else:
|
| 75 |
-
return Image.open('tmp/img/input.JPEG')
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
image = gr.outputs.Image(type="pil", label="Your result")
|
| 79 |
-
css = ".output-image{height: 528px !important} .output-carousel .output-image{height:272px !important} a{text-decoration: underline}"
|
| 80 |
-
iface = gr.Interface(fn=run, inputs=[
|
| 81 |
-
# gr.inputs.Image(type="filepath", label='initial_image'),
|
| 82 |
-
gr.Image(source="upload", type="numpy", tool="sketch", elem_id="source_container"),
|
| 83 |
-
# gr.inputs.Image(type="filepath", label='mask - object mask', optional=True),
|
| 84 |
-
gr.inputs.Image(type="filepath", label='Backgrounds - optional, specified backgrounds'),
|
| 85 |
-
gr.inputs.Slider(label="Backgrounds_complexity - How complicated you wish to the generated image to be", default=0, step=1, minimum=-30, maximum=30),
|
| 86 |
-
gr.inputs.Slider(label="Size - Object pixel rates", default=0.1, step=0.02, minimum=0.01, maximum=0.5),
|
| 87 |
-
gr.inputs.Slider(label="Angle - Object angle", default=0, step=10, minimum=-180, maximum=180),
|
| 88 |
-
gr.inputs.Slider(label="Steps - more steps can increase quality but will take longer to generate",default=10,maximum=100,minimum=1,step=1),
|
| 89 |
-
gr.inputs.Slider(label="num_of_Images - How many images you wish to generate", default=2, step=1, minimum=1, maximum=4),
|
| 90 |
-
|
| 91 |
-
# gr.inputs.Radio(label="Width", choices=[32,64,128,256],default=256),
|
| 92 |
-
# gr.inputs.Radio(label="Height", choices=[32,64,128,256],default=256),
|
| 93 |
-
# gr.inputs.Textbox(label="Prompt - try adding increments to your prompt such as 'oil on canvas', 'a painting', 'a book cover'",default="chalk pastel drawing of a dog wearing a funny hat"),
|
| 94 |
-
#gr.inputs.Slider(label="ETA - between 0 and 1. Lower values can provide better quality, higher values can be more diverse",default=0.0,minimum=0.0, maximum=1.0,step=0.1),
|
| 95 |
-
],
|
| 96 |
-
# outputs=[image,gr.outputs.Carousel(label="Individual images",components=["image"]),gr.outputs.Textbox(label="Error")],
|
| 97 |
-
outputs=["image"],
|
| 98 |
-
css=css,
|
| 99 |
-
title="Image Editing with Controls of Object Attributes including Backgrounds, Sizes, Positions and Directions",
|
| 100 |
-
description="Gradio Demo for Image Editing with Controls of Object Attributes including Backgrounds, Sizes, Positions and Directions",
|
| 101 |
-
article="ImageNet-E")
|
| 102 |
-
iface.launch(enable_queue=True,share=True)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
def greet(name):
|
| 4 |
+
return "Hello " + name + "!!"
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import sys
|
| 4 |
+
sys.path.append(".")
|
| 5 |
+
|
| 6 |
+
#@title Import stuff
|
| 7 |
+
import gc
|
| 8 |
+
|
| 9 |
+
import subprocess
|
| 10 |
+
import shutil
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import time
|
| 13 |
+
|
| 14 |
+
import imageio
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# def run(initial_image, mask, Backgrounds, Backgrounds_complexity, Size, Angle, Steps, num_of_Images):
|
| 18 |
+
def run(source_img, Backgrounds, Backgrounds_complexity, Size, Angle, Steps, num_of_Images):
|
| 19 |
+
print('-------------------starting to process-------------------')
|
| 20 |
+
if os.path.exists('results'):
|
| 21 |
+
shutil.rmtree("results")
|
| 22 |
+
if os.path.exists('tmp'):
|
| 23 |
+
shutil.rmtree("tmp")
|
| 24 |
+
time.sleep(1)
|
| 25 |
+
os.makedirs('results', exist_ok=True)
|
| 26 |
+
os.makedirs('tmp/img', exist_ok=True)
|
| 27 |
+
os.makedirs('tmp/mask', exist_ok=True)
|
| 28 |
+
os.makedirs('tmp/bg', exist_ok=True)
|
| 29 |
+
|
| 30 |
+
'''
|
| 31 |
+
print('-----initial_image: ', initial_image)
|
| 32 |
+
init_image = Image.open(initial_image)
|
| 33 |
+
mask = Image.open(mask)
|
| 34 |
+
init_image = init_image.resize((256,256))
|
| 35 |
+
mask = mask.resize((256,256))
|
| 36 |
+
init_image.save("tmp/img/input.JPEG")
|
| 37 |
+
mask.save("tmp/mask/input.png")
|
| 38 |
+
'''
|
| 39 |
+
imageio.imwrite("tmp/img/input.JPEG", source_img["image"])
|
| 40 |
+
imageio.imwrite("tmp/mask/input.png", source_img["mask"])
|
| 41 |
+
|
| 42 |
+
initial_image = Image.open('tmp/img/input.JPEG').resize((256,256))
|
| 43 |
+
initial_image.save('tmp/img/input.JPEG')
|
| 44 |
+
mask = Image.open('tmp/mask/input.png').resize((256,256))
|
| 45 |
+
mask.save('tmp/mask/input.png')
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
if Backgrounds:
|
| 49 |
+
background_specific = Backgrounds
|
| 50 |
+
if background_specific is not None:
|
| 51 |
+
background_specific = Image.open(background_specific).convert('RGB') # Specified background
|
| 52 |
+
background_specific = background_specific.resize((256,256))
|
| 53 |
+
background_specific.save('tmp/bg/bg.png')
|
| 54 |
+
background_specific = '../tmp/bg/bg.png'
|
| 55 |
+
else:
|
| 56 |
+
background_specific = ""
|
| 57 |
+
|
| 58 |
+
Backgrounds_complexity = Backgrounds_complexity
|
| 59 |
+
Size = Size
|
| 60 |
+
Angle = Angle
|
| 61 |
+
Steps = Steps
|
| 62 |
+
num_of_Images = num_of_Images
|
| 63 |
+
print(Backgrounds_complexity, background_specific, Size, Angle, Steps, num_of_Images)
|
| 64 |
+
p = subprocess.Popen(["sh", "run.sh", str(Backgrounds_complexity), background_specific, str(Size), str(Angle), str(Steps), str(num_of_Images)])
|
| 65 |
+
|
| 66 |
+
# subprocess.Popen(["cd", "object_removal/TFill/"])
|
| 67 |
+
# subprocess.Popen(["python", "test.py"])
|
| 68 |
+
|
| 69 |
+
return_code = p.wait()
|
| 70 |
+
print('----return_code: ', return_code)
|
| 71 |
+
|
| 72 |
+
if os.path.exists('results/edited.png'):
|
| 73 |
+
return Image.open('results/edited.png')
|
| 74 |
+
else:
|
| 75 |
+
return Image.open('tmp/img/input.JPEG')
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
image = gr.outputs.Image(type="pil", label="Your result")
|
| 79 |
+
css = ".output-image{height: 528px !important} .output-carousel .output-image{height:272px !important} a{text-decoration: underline}"
|
| 80 |
+
iface = gr.Interface(fn=run, inputs=[
|
| 81 |
+
# gr.inputs.Image(type="filepath", label='initial_image'),
|
| 82 |
+
gr.Image(source="upload", type="numpy", tool="sketch", elem_id="source_container"),
|
| 83 |
+
# gr.inputs.Image(type="filepath", label='mask - object mask', optional=True),
|
| 84 |
+
gr.inputs.Image(type="filepath", label='Backgrounds - optional, specified backgrounds'),
|
| 85 |
+
gr.inputs.Slider(label="Backgrounds_complexity - How complicated you wish to the generated image to be", default=0, step=1, minimum=-30, maximum=30),
|
| 86 |
+
gr.inputs.Slider(label="Size - Object pixel rates", default=0.1, step=0.02, minimum=0.01, maximum=0.5),
|
| 87 |
+
gr.inputs.Slider(label="Angle - Object angle", default=0, step=10, minimum=-180, maximum=180),
|
| 88 |
+
gr.inputs.Slider(label="Steps - more steps can increase quality but will take longer to generate",default=10,maximum=100,minimum=1,step=1),
|
| 89 |
+
gr.inputs.Slider(label="num_of_Images - How many images you wish to generate", default=2, step=1, minimum=1, maximum=4),
|
| 90 |
+
|
| 91 |
+
# gr.inputs.Radio(label="Width", choices=[32,64,128,256],default=256),
|
| 92 |
+
# gr.inputs.Radio(label="Height", choices=[32,64,128,256],default=256),
|
| 93 |
+
# gr.inputs.Textbox(label="Prompt - try adding increments to your prompt such as 'oil on canvas', 'a painting', 'a book cover'",default="chalk pastel drawing of a dog wearing a funny hat"),
|
| 94 |
+
#gr.inputs.Slider(label="ETA - between 0 and 1. Lower values can provide better quality, higher values can be more diverse",default=0.0,minimum=0.0, maximum=1.0,step=0.1),
|
| 95 |
+
],
|
| 96 |
+
# outputs=[image,gr.outputs.Carousel(label="Individual images",components=["image"]),gr.outputs.Textbox(label="Error")],
|
| 97 |
+
outputs=["image"],
|
| 98 |
+
css=css,
|
| 99 |
+
title="Image Editing with Controls of Object Attributes including Backgrounds, Sizes, Positions and Directions",
|
| 100 |
+
description="Gradio Demo for Image Editing with Controls of Object Attributes including Backgrounds, Sizes, Positions and Directions",
|
| 101 |
+
article="ImageNet-E")
|
| 102 |
+
iface.launch(enable_queue=True,share=True)
|