Akjava commited on
Commit
0a56124
·
1 Parent(s): 67b5033
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ __pycache__
2
+ *.egg-info
3
+ .ipynb_checkpoints
4
+ dist
5
+ .gradio
6
+ build
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import re
4
+ from PIL import Image,ImageFilter
5
+
6
+ import os
7
+ import numpy as np
8
+
9
+
10
+
11
+
12
+ def process_images(fg_image, bg_image,fg_image_mask=None,dilate=0,blur=0):
13
+ # I'm not sure when this happen maybe api calling
14
+
15
+ #Basically ImageEditor's value are dictionary,If not convert value to dict
16
+ if not isinstance(fg_image, dict):
17
+ if fg_image_mask == None:
18
+ print("empty mask")
19
+ return image,None
20
+ else:
21
+ image = dict({'background': image, 'layers': [fg_image_mask]}) #no need?
22
+
23
+ if fg_image_mask!=None:
24
+ mask = fg_image_mask
25
+ else:
26
+ if len(fg_image['layers']) == 0:
27
+ print("empty mask")
28
+ return image,None
29
+ #print("use layer")
30
+ mask = fg_image['layers'][0]
31
+
32
+ mask = mask.convert("L")
33
+
34
+ if dilate>0:
35
+ if dilate%2 ==0:
36
+ dilate -= 1
37
+ mask = mask.filter(ImageFilter.MaxFilter(dilate))
38
+
39
+
40
+ if blur>0:
41
+ mask = mask.filter(ImageFilter.GaussianBlur(radius=blur))
42
+
43
+
44
+ image2 = fg_image["background"].convert("RGBA")
45
+
46
+ if bg_image == None:
47
+ image2_masked = Image.composite(image2, Image.new("RGBA", image2.size, (0, 0, 0, 0)), mask)
48
+ return image2_masked,mask
49
+
50
+
51
+ bg_image = bg_image.convert("RGBA")
52
+ bg_image.paste(image2, (0, 0), mask)
53
+
54
+ return [bg_image,mask]
55
+
56
+
57
+ def read_file(path: str) -> str:
58
+ with open(path, 'r', encoding='utf-8') as f:
59
+ content = f.read()
60
+
61
+ return content
62
+
63
+ css="""
64
+ #col-left {
65
+ margin: 0 auto;
66
+ max-width: 640px;
67
+ }
68
+ #col-right {
69
+ margin: 0 auto;
70
+ max-width: 640px;
71
+ }
72
+ """
73
+ demo_blocks = gr.Blocks(css=css, elem_id="demo-container")
74
+ with demo_blocks as demo:
75
+ with gr.Column():
76
+ gr.HTML(read_file("demo_header.html"))
77
+ with gr.Row():
78
+ with gr.Column():
79
+ image = gr.ImageEditor(height=800,sources=['upload','clipboard'],transforms=[],image_mode='RGB', layers=False, elem_id="Foreground", type="pil", label="Foreground",brush=gr.Brush(colors=["#fff"], color_mode="fixed"))
80
+
81
+
82
+ btn = gr.Button("Paste to BG", elem_id="run_button",variant="primary")
83
+
84
+ bg_image = gr.Image(sources=['upload','clipboard'], elem_id="bg_image", type="pil", label="Background Image",height=400, value=None)
85
+ image_mask = gr.Image(sources=['upload','clipboard'], elem_id="mask_upload", type="pil", label="Mask Uploaded",height=400, value=None)
86
+ with gr.Accordion(label="Advanced Settings", open=False):
87
+ with gr.Row( equal_height=True):
88
+ blur = gr.Slider(
89
+ label="blur",
90
+ minimum=0,
91
+ maximum=100,
92
+ step=1,
93
+ value=5)
94
+
95
+ dilate = gr.Slider(
96
+ label="dilate",
97
+ minimum=0,
98
+ maximum=100,
99
+ step=1,
100
+ value=0)
101
+ id_input=gr.Text(label="Name", visible=False)
102
+
103
+ with gr.Column():
104
+ image_out = gr.Image(height=800,sources=[],label="Output", elem_id="output-img",format="png")
105
+ mask_out = gr.Image(height=800,sources=[],label="Mask", elem_id="mask-img",format="jpeg")
106
+
107
+
108
+
109
+
110
+ btn.click(fn=process_images, inputs=[image, bg_image,image_mask,dilate,blur], outputs =[image_out,mask_out], api_name='infer')
111
+ gr.Examples(
112
+ examples=[
113
+ ["images/00533245_00003200_mouth.jpg", "images/00533245_00004200_eyes.jpg","images/00533245_99_mask.jpg",5,18,"images/00533245_mixed.jpg"],
114
+ ["images/00346245_00006200.jpg", "images/00346245_00003200.jpg","images/00346245_mask.jpg",10,0,"images/00346245_mixed.jpg"]
115
+
116
+ ]
117
+ ,
118
+ inputs=[image,bg_image,image_mask,blur,dilate,image_out]
119
+ )
120
+ gr.HTML(
121
+ """
122
+
123
+ """
124
+ )
125
+
126
+ demo_blocks.queue(max_size=25).launch(share=False,debug=True)
demo_header.html ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div style="text-align: center;">
2
+ <h1>
3
+ Simple Paste Image to Background
4
+ </h1>
5
+ <p>Making face images for <a href="https://huggingface.co/spaces/Akjava/godot-huggingface-chain">AI Diagram Chat with Voice/Face Character</a></p>
6
+ <p>
7
+
8
+ See examples.
9
+ If you choose transparent image ,this paste no effect without mask so far
10
+ </p>
11
+ <p>Hint:Use blur and dilate</p>
12
+ </div>
images/00346245_00003200.jpg ADDED
images/00346245_00006200.jpg ADDED
images/00346245_mask.jpg ADDED
images/00346245_mixed.jpg ADDED
images/00533245_00003200_mouth.jpg ADDED
images/00533245_00004200_eyes.jpg ADDED
images/00533245_99_mask.jpg ADDED
images/00533245_mixed.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ spaces
2
+ numpy