File size: 8,629 Bytes
e4545e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""
https://github.com/Trinkle23897/Fast-Poisson-Image-Editing
MIT License

Copyright (c) 2022 Jiayi Weng

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import time
import argparse
import os
import fpie
from process import ALL_BACKEND, CPU_COUNT, DEFAULT_BACKEND
from fpie.io import read_images, write_image
from process import BaseProcessor, EquProcessor, GridProcessor

from PIL import Image
import numpy as np
import skimage
import skimage.measure
import scipy
import scipy.signal


class PhotometricCorrection:
    def __init__(self,quite=False):
        self.get_parser("cli")
        args=self.parser.parse_args(["--method","grid","-g","src","-s","a","-t","a","-o","a"])
        args.mpi_sync_interval = getattr(args, "mpi_sync_interval", 0)
        self.backend=args.backend
        self.args=args
        self.quite=quite
        proc: BaseProcessor
        proc = GridProcessor(
            args.gradient,
            args.backend,
            args.cpu,
            args.mpi_sync_interval,
            args.block_size,
            args.grid_x,
            args.grid_y,
        )
        print(
            f"[PIE]Successfully initialize PIE {args.method} solver "
            f"with {args.backend} backend"
        )
        self.proc=proc

    def run(self, original_image, inpainted_image, mode="mask_mode"):
        print(f"[PIE] start")
        if mode=="disabled":
            return inpainted_image
        input_arr=np.array(original_image)
        if input_arr[:,:,-1].sum()<1:
            return inpainted_image
        output_arr=np.array(inpainted_image)
        mask=input_arr[:,:,-1]
        mask=255-mask
        if mask.sum()<1 and mode=="mask_mode":
            mode=""
        if mode=="mask_mode":
            mask = skimage.measure.block_reduce(mask, (8, 8), np.max)
            mask = mask.repeat(8, axis=0).repeat(8, axis=1)
        else:
            mask[8:-9,8:-9]=255
        mask = mask[:,:,np.newaxis].repeat(3,axis=2)
        nmask=mask.copy()
        output_arr2=output_arr[:,:,0:3].copy()
        input_arr2=input_arr[:,:,0:3].copy()
        output_arr2[nmask<128]=0
        input_arr2[nmask>=128]=0
        output_arr2+=input_arr2
        src = output_arr2[:,:,0:3]
        tgt = src.copy()
        proc=self.proc
        args=self.args
        if proc.root:
            n = proc.reset(src, mask, tgt, (args.h0, args.w0), (args.h1, args.w1))
        proc.sync()
        if proc.root:
            result = tgt
            t = time.time()
        if args.p == 0:
            args.p = args.n

        for i in range(0, args.n, args.p):
            if proc.root:
                result, err = proc.step(args.p)  # type: ignore
                print(f"[PIE] Iter {i + args.p}, abs_err {err}")
            else:
                proc.step(args.p)

        if proc.root:
            dt = time.time() - t
            print(f"[PIE] Time elapsed: {dt:.4f}s")
            # make sure consistent with dummy process
            return Image.fromarray(result)


    def get_parser(self,gen_type: str) -> argparse.Namespace:
        parser = argparse.ArgumentParser()
        parser.add_argument(
            "-v", "--version", action="store_true", help="show the version and exit"
        )
        parser.add_argument(
            "--check-backend", action="store_true", help="print all available backends"
        )
        if gen_type == "gui" and "mpi" in ALL_BACKEND:
            # gui doesn't support MPI backend
            ALL_BACKEND.remove("mpi")
        parser.add_argument(
            "-b",
            "--backend",
            type=str,
            choices=ALL_BACKEND,
            default=DEFAULT_BACKEND,
            help="backend choice",
        )
        parser.add_argument(
            "-c",
            "--cpu",
            type=int,
            default=CPU_COUNT,
            help="number of CPU used",
        )
        parser.add_argument(
            "-z",
            "--block-size",
            type=int,
            default=1024,
            help="cuda block size (only for equ solver)",
        )
        parser.add_argument(
            "--method",
            type=str,
            choices=["equ", "grid"],
            default="equ",
            help="how to parallelize computation",
        )
        parser.add_argument("-s", "--source", type=str, help="source image filename")
        if gen_type == "cli":
            parser.add_argument(
                "-m",
                "--mask",
                type=str,
                help="mask image filename (default is to use the whole source image)",
                default="",
            )
        parser.add_argument("-t", "--target", type=str, help="target image filename")
        parser.add_argument("-o", "--output", type=str, help="output image filename")
        if gen_type == "cli":
            parser.add_argument(
                "-h0", type=int, help="mask position (height) on source image", default=0
            )
            parser.add_argument(
                "-w0", type=int, help="mask position (width) on source image", default=0
            )
            parser.add_argument(
                "-h1", type=int, help="mask position (height) on target image", default=0
            )
            parser.add_argument(
                "-w1", type=int, help="mask position (width) on target image", default=0
            )
        parser.add_argument(
            "-g",
            "--gradient",
            type=str,
            choices=["max", "src", "avg"],
            default="max",
            help="how to calculate gradient for PIE",
        )
        parser.add_argument(
            "-n",
            type=int,
            help="how many iteration would you perfer, the more the better",
            default=5000,
        )
        if gen_type == "cli":
            parser.add_argument(
                "-p", type=int, help="output result every P iteration", default=0
            )
        if "mpi" in ALL_BACKEND:
            parser.add_argument(
                "--mpi-sync-interval",
                type=int,
                help="MPI sync iteration interval",
                default=100,
            )
        parser.add_argument(
            "--grid-x", type=int, help="x axis stride for grid solver", default=8
        )
        parser.add_argument(
            "--grid-y", type=int, help="y axis stride for grid solver", default=8
        )
        self.parser=parser

if __name__ =="__main__":
    import sys
    import io
    import base64
    from PIL import Image
    def base64_to_pil(base64_str):
        data = base64.b64decode(str(base64_str))
        pil = Image.open(io.BytesIO(data))
        return pil

    def pil_to_base64(out_pil):
        out_buffer = io.BytesIO()
        out_pil.save(out_buffer, format="PNG")
        out_buffer.seek(0)
        base64_bytes = base64.b64encode(out_buffer.read())
        base64_str = base64_bytes.decode("ascii")
        return base64_str
    correction_func=PhotometricCorrection(quite=True)
    while True:
        buffer = sys.stdin.readline()
        print(f"[PIE] suprocess {len(buffer)} {type(buffer)} ")
        if len(buffer)==0:
            break
        if isinstance(buffer,str):
            lst=buffer.strip().split(",")
        else:
            lst=buffer.decode("ascii").strip().split(",")
        img0=base64_to_pil(lst[0])
        img1=base64_to_pil(lst[1])
        ret=correction_func.run(img0,img1,mode=lst[2])
        ret_base64=pil_to_base64(ret)
        if isinstance(buffer,str):
            sys.stdout.write(f"{ret_base64}\n")
        else:
            sys.stdout.write(f"{ret_base64}\n".encode())
        sys.stdout.flush()