Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
!git clone https://github.com/jantic/DeOldify.git DeOldify
|
| 2 |
+
cd DeOldify
|
| 3 |
+
#NOTE: This must be the first call in order to work properly!
|
| 4 |
+
from deoldify import device
|
| 5 |
+
from deoldify.device_id import DeviceId
|
| 6 |
+
#choices: CPU, GPU0...GPU7
|
| 7 |
+
device.set(device=DeviceId.GPU0)
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
|
| 11 |
+
if not torch.cuda.is_available():
|
| 12 |
+
print('GPU not available.')
|
| 13 |
+
!mkdir 'models'
|
| 14 |
+
!wget https://data.deepai.org/deoldify/ColorizeArtistic_gen.pth -O ./models/ColorizeArtistic_gen.pth
|
| 15 |
+
import fastai
|
| 16 |
+
from deoldify.visualize import *
|
| 17 |
+
import warnings
|
| 18 |
+
warnings.filterwarnings("ignore", category=UserWarning, message=".*?Your .*? set is empty.*?")
|
| 19 |
+
colorizer = get_image_colorizer(artistic=True)
|
| 20 |
+
source_url = 'https://i.pinimg.com/originals/09/c2/22/09c222cc275efa908bdc140a750ca4e4.jpg'
|
| 21 |
+
render_factor = 10
|
| 22 |
+
watermarked = False
|
| 23 |
+
image_path_1 = colorizer.plot_transformed_image_from_url(url=source_url, render_factor=render_factor, compare=True, watermarked=watermarked)
|
| 24 |
+
#image_path_2 = colorizer.plot_transformed_image(path=source_path, render_factor=render_factor, compare=True)
|
| 25 |
+
print('image_path',image_path_1)
|
| 26 |
+
|
| 27 |
+
import gradio as gr
|
| 28 |
+
import tensorflow as tf
|
| 29 |
+
import numpy as np
|
| 30 |
+
import os
|
| 31 |
+
|
| 32 |
+
def Colorize(address,watermark,URL,render_factor):
|
| 33 |
+
text_box_output = "Successful"
|
| 34 |
+
watermarked = True
|
| 35 |
+
output_address = np.array([
|
| 36 |
+
[0,0],
|
| 37 |
+
[0,0],
|
| 38 |
+
], dtype=np.uint8)
|
| 39 |
+
if watermark == "Watermark On":
|
| 40 |
+
watermarked = True
|
| 41 |
+
render_factor = int(render_factor)
|
| 42 |
+
elif watermark == "Watermark Off":
|
| 43 |
+
watermarked = False
|
| 44 |
+
render_factor = int(render_factor)
|
| 45 |
+
else:
|
| 46 |
+
text_box_output = "Please set the watermark setting."
|
| 47 |
+
if URL != '':
|
| 48 |
+
try:
|
| 49 |
+
output_address = colorizer.plot_transformed_image_from_url(url=URL, render_factor=render_factor, compare=True, watermarked=watermarked)
|
| 50 |
+
except:
|
| 51 |
+
text_box_output = 'The URL is invalid'
|
| 52 |
+
else:
|
| 53 |
+
if address == None:
|
| 54 |
+
text_box_output = "Please provide the image"
|
| 55 |
+
else:
|
| 56 |
+
output_address = colorizer.plot_transformed_image(path=address, render_factor=render_factor, compare=True, watermarked=watermarked)
|
| 57 |
+
if address.endswith('tiff') == True:
|
| 58 |
+
text_box_output = "Files with tiff format cannot be displayed properly in this website, please download the image to view"
|
| 59 |
+
else:
|
| 60 |
+
text_box_output = "Colorization Accomplished"
|
| 61 |
+
return text_box_output,output_address
|
| 62 |
+
with gr.Blocks() as demo:
|
| 63 |
+
image_box=gr.Image(type="filepath")
|
| 64 |
+
image_output_box = gr.Image()
|
| 65 |
+
# text_box_1 = gr.Textbox(value="10",label="The render factor")
|
| 66 |
+
text_box_2 = gr.Textbox(label='URL')
|
| 67 |
+
drop_down_1 = gr.Dropdown(['Watermark On','Watermark Off'],label='WaterMark')
|
| 68 |
+
slider = gr.Slider(7,40,step=1,label='render factor')
|
| 69 |
+
text_box_output = gr.Textbox(label="Status",interactive=False)
|
| 70 |
+
slider.release(Colorize, inputs=[image_box,drop_down_1,text_box_2,slider], outputs=[text_box_output,image_output_box])
|
| 71 |
+
#b1=gr.Button("Colorized!")
|
| 72 |
+
#b1.click(Colorize, inputs=[image_box,drop_down_1,text_box_2,text_box_1], outputs= image_output_box)
|
| 73 |
+
|
| 74 |
+
demo.queue().launch(share=True,debug=True)
|