File size: 4,162 Bytes
6d580b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c9afb9
6d580b8
 
 
 
 
18ca939
 
6d580b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcfb055
 
 
 
 
 
 
 
 
 
 
 
 
 
35d2c61
51cca02
32767fa
9c9afb9
35d2c61
 
 
 
 
 
 
 
 
bcfb055
 
 
 
8c84c61
6d580b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18ca939
40329b6
9fd24f9
6d580b8
bcfb055
 
 
6d580b8
 
 
 
 
 
 
bcfb055
6d580b8
 
 
bcfb055
6d580b8
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Portrait Photo Generator App

# Imports
from PIL import Image, ImageFilter
import numpy as np
from transformers import pipeline
import gradio as gr
import os

model = pipeline("image-segmentation", model="facebook/detr-resnet-50-panoptic")

pred = []

def img_resize(image):
    width = 1280
    width_percent = (width / float(image.size[0]))
    height = int((float(image.size[1]) * float(width_percent)))
    return image.resize((width, height))

def image_objects(image):
    global pred
    
    image = img_resize(image)
    pred = model(image)
    pred_object_list = [str(i)+'_'+x['label'] for i, x in enumerate(pred)]
    return gr.Dropdown.update(choices = pred_object_list, interactive = True)



def blurr_object(image, object, blur_strength):
    image = img_resize(image)

    object_number = int(object.split('_')[0])
    mask_array = np.asarray(pred[object_number]['mask'])/255
    image_array = np.asarray(image)

    mask_array_three_channel = np.zeros_like(image_array)
    mask_array_three_channel[:,:,0] = mask_array
    mask_array_three_channel[:,:,1] = mask_array
    mask_array_three_channel[:,:,2] = mask_array

    segmented_image = image_array*mask_array_three_channel

    blur_image = np.asarray(image.filter(ImageFilter.GaussianBlur(radius=blur_strength)))
    mask_array_three_channel_invert = 1-mask_array_three_channel
    blur_image_reverse_mask = blur_image*mask_array_three_channel_invert
    
    #seg_out=image_array.astype(np.uint8)

    
    blurred_output_image = Image.fromarray((blur_image_reverse_mask).astype(np.uint8)+segmented_image.astype(np.uint8))
    for _ in range(int(blur_strength//2.5)):
        blurred_output_image = blurred_output_image.filter(ImageFilter.SMOOTH_MORE)
    return blurred_output_image

    
def get_seg(image, object):
    image = img_resize(image)

    #blur_image_reverse_mask = blur_image*mask_array_three_channel_invert
    seg_box=[]
    for i in range(len(pred)):
        #object_number = int(object.split('_')[0])
        mask_array = np.asarray(pred[i]['mask'])/255
        image_array = np.asarray(image)
    
        mask_array_three_channel = np.zeros_like(image_array)
        mask_array_three_channel[:,:,0] = mask_array
        mask_array_three_channel[:,:,1] = mask_array
        mask_array_three_channel[:,:,2] = mask_array
    
        segmented_image = image_array*mask_array_three_channel
        seg_out=segmented_image.astype(np.uint8)
        seg_box.append(seg_out)  

    return(seg_box)



app = gr.Blocks()


with app:
    gr.Markdown(
            """
            ## Portrait Photo Generator
            - Create stunning portrait photos by blurring the background of your selected object.
            - Adjust the blurring strength using the slider.
            """)
    with gr.Row():
        with gr.Column():
            gr.Markdown(
            """
            ### Input Image
            """)
            image_input = gr.Image(type="pil")
            

        with gr.Column():
            with gr.Row():
                gr.Markdown(
                """
                ### Found Objects
                """)
            with gr.Row():
                blur_slider = gr.Slider(minimum=0.5, maximum=10, value=3, label="Adject Blur Strength")
            with gr.Row():
                object_output = gr.Dropdown(label="Select Object From Dropdown")

    
    with gr.Row():
        with gr.Column():
            gr.Markdown(
                """
                ### Blurred Image Output
                """)
            image_output = gr.Image()
        with gr.Column():
            seg_btn = gr.Button(label="Run")
            gal1=gr.Gallery(type="filepath").style(grid=10)

    seg_btn.click(get_seg, inputs=[image_input,object_output], outputs=gal1)

    
    image_input.change(fn=image_objects, 
        inputs=image_input, 
        outputs=object_output
        )

    object_output.change(fn=blurr_object, 
        inputs=[image_input, object_output, blur_slider],
        outputs=[image_output])

    blur_slider.change(fn=blurr_object, 
        inputs=[image_input, object_output, blur_slider],
        outputs=[image_output])
    

app.launch()