File size: 9,703 Bytes
e3f7bf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e2d513
e3f7bf2
 
 
 
42de2d8
e3f7bf2
1e2d513
 
e3f7bf2
1e2d513
e3f7bf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e2d513
e3f7bf2
 
42de2d8
 
1e2d513
 
 
 
e3f7bf2
1e2d513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3f7bf2
 
 
 
1e2d513
 
e3f7bf2
 
 
 
1e2d513
 
e3f7bf2
 
 
 
1e2d513
 
 
e3f7bf2
 
1e2d513
 
 
 
e3f7bf2
1e2d513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3f7bf2
1e2d513
e3f7bf2
 
1e2d513
e3f7bf2
 
 
 
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# import base64

# import requests
# import os
# from PIL import Image
# import io

# def inpaint(img_path:str,mask_path:str)->"img content (resp.content)":
#     image_bytes = open(img_path, 'rb')
#     mask_bytes = open(mask_path, 'rb')
#     # 将字节数据转换为Base64编码的字符串

#     files = {
#         "image": image_bytes,
#         "mask":mask_bytes
#     }
#     payload = {
#         "ldmSteps": 25,
#         "ldmSampler": "plms",
#         "zitsWireframe": True,
#         "hdStrategy": "Crop",
#         "hdStrategyCropMargin": 196,
#         "hdStrategyCropTrigerSize": 800,
#         "hdStrategyResizeLimit": 2048,
#         "prompt": "",
#         "negativePrompt": "",
#         "croperX": 307,
#         "croperY": 544,
#         "croperHeight": 512,
#         "croperWidth": 512,
#         "useCroper": False,
#         "sdMaskBlur": 5,
#         "sdStrength": 0.75,
#         "sdSteps": 50,
#         "sdGuidanceScale": 7.5,
#         "sdSampler": "uni_pc",
#         "sdSeed": -1,
#         "sdMatchHistograms": False,
#         "sdScale": 1,
#         "cv2Radius": 5,
#         "cv2Flag": "INPAINT_NS",
#         "paintByExampleSteps": 50,
#         "paintByExampleGuidanceScale": 7.5,
#         "paintByExampleSeed": -1,
#         "paintByExampleMaskBlur": 5,
#         "paintByExampleMatchHistograms": False,
#         "p2pSteps": 50,
#         "p2pImageGuidanceScale": 1.5,
#         "p2pGuidanceScale": 7.5,
#         "controlnet_conditioning_scale": 0.4,
#         "controlnet_method": "control_v11p_sd15_canny"
#     }#payload用data

#     #不使用header
#     #resp = requests.post("https://sanster-lama-cleaner-lama.hf.space/inpaint", data=payload, files=files)
#     #使用自己的space
#     resp = requests.post("https://rogerxavier-lama-cleaner-lama.hf.space/inpaint", data=payload, files=files)
    
#     print("请求lama clenaer状态是",resp.status_code)
#     return bytes(resp.content)

# def save_img(img_content:"要处理的图片数据",new_save_path:"新文件的保存路径(包含后缀)",old_img_path:"旧文件路径")->"void生成新的文件保存 ,传入旧文件路径是为了删除有问题的旧文件":
#     print(new_save_path)
#     try:
#         img = Image.open(io.BytesIO(img_content))
#         # 如果需要指定图像格式,可以在保存时指定
#         img.save(new_save_path, format="JPEG")
#     except Exception as e:
#         #对于可能异常的图片->比如因为不合规导致resp.content没有正常返回
#         print(e,new_save_path,"图片返回有问题,跳过并删除图片.这里的路径是新保存路径")
#         os.remove(old_img_path)





# if __name__ == '__main__':
#     # 获取当前目录的子目录的路径
#     img_path = 'manga'
#     subdir_path = os.path.join(os.getcwd(), img_path)

#     # 图片素材获取(包含子目录下所有图片)
#     image_files = []
#     for root, dirs, files in os.walk(subdir_path):
#         for file in files:
#             if file.endswith(".jpg") or file.endswith(".png"):
#                 image_files.append(os.path.relpath(os.path.join(root, file)))

#     # 创建处理后的子目录在与image_files同级目录下
#     processed_subdir_path = os.path.join(os.path.dirname(subdir_path), f"{img_path}1")
#     os.makedirs(processed_subdir_path, exist_ok=True)

#     # 对image_files进行某种处理,生成新图片,并保存在处理后的子目录中
#     for img_file in image_files:
#         # 处理图片的代码(这里仅作示例)
#         # 假设处理后的图片为new_img
#         img_dir = os.path.dirname(img_file)
#         new_img_dir = os.path.join(processed_subdir_path, img_dir)
#         os.makedirs(new_img_dir, exist_ok=True)

#         new_img_path = os.path.join(new_img_dir, os.path.basename(img_file))

#         if not os.path.exists(new_img_path):
#             #如果已经处理过那么跳过
#             # 处理图片并保存
#             img_inpainted = inpaint(img_path=img_file, mask_path='mask/0.jpg')#上传的遮罩保存都是0开始
#             save_img(img_content=img_inpainted, new_save_path=new_img_path,old_img_path=img_file)
#         else:
#             print(f"Skipping {new_img_path} as it already exists.")










import base64
import requests
import os
from typing import List
import io
from PIL import Image
from pydantic import BaseModel
from lama_cleaner.server import process
from lama_cleaner.server import main#先初始化才能用process
#C:\Users\17331\.cache\torch\hub  缓存下载位置,记得删

class FakeArgs(BaseModel):
    host: str = "0.0.0.0"
    port: int = 7860
    model: str = 'lama'
    hf_access_token: str = ""
    sd_enable_xformers: bool = False
    sd_disable_nsfw: bool = False
    sd_cpu_textencoder: bool = True
    sd_controlnet: bool = False
    sd_controlnet_method: str = "control_v11p_sd15_canny"
    sd_local_model_path: str = ""
    sd_run_local: bool = False
    local_files_only: bool = False
    cpu_offload: bool = False
    device: str = "cpu"
    gui: bool = False
    gui_size: List[int] = [1000, 1000]
    input: str = ''
    disable_model_switch: bool = True
    debug: bool = False
    no_half: bool = False
    disable_nsfw: bool = False
    enable_xformers: bool = False
    enable_interactive_seg: bool = True
    interactive_seg_model: str = "vit_b"
    interactive_seg_device: str = "cpu"
    enable_remove_bg: bool = False
    enable_anime_seg: bool = False
    enable_realesrgan: bool = False
    enable_gfpgan: bool = False
    gfpgan_device: str = "cpu"
    enable_restoreformer: bool = False
    enable_gif: bool = False
    quality: int = 95
    model_dir: str = None
    output_dir: str = None


def inpaint(img_path: str, mask_path: str) -> "img content (resp.content)":
    # urllib3 1.26.4  兼容
    image_bytes = open(img_path, 'rb')
    mask_bytes = open(mask_path, 'rb')
    # 将字节数据转换为Base64编码的字符串

    files = {
        "image": image_bytes,
        "mask": mask_bytes
    }
    payload = {
        "ldmSteps": 25,
        "ldmSampler": "plms",
        "zitsWireframe": True,
        "hdStrategy": "Crop",
        "hdStrategyCropMargin": 196,
        "hdStrategyCropTrigerSize": 800,
        "hdStrategyResizeLimit": 2048,
        "prompt": "",
        "negativePrompt": "",
        "croperX": 307,
        "croperY": 544,
        "croperHeight": 512,
        "croperWidth": 512,
        "useCroper": False,
        "sdMaskBlur": 5,
        "sdStrength": 0.75,
        "sdSteps": 50,
        "sdGuidanceScale": 7.5,
        "sdSampler": "uni_pc",
        "sdSeed": -1,
        "sdMatchHistograms": False,
        "sdScale": 1,
        "cv2Radius": 5,
        "cv2Flag": "INPAINT_NS",
        "paintByExampleSteps": 50,
        "paintByExampleGuidanceScale": 7.5,
        "paintByExampleSeed": -1,
        "paintByExampleMaskBlur": 5,
        "paintByExampleMatchHistograms": False,
        "p2pSteps": 50,
        "p2pImageGuidanceScale": 1.5,
        "p2pGuidanceScale": 7.5,
        "controlnet_conditioning_scale": 0.4,
        "controlnet_method": "control_v11p_sd15_canny"
    }  # payload用data

    resp = process(files=files, payload=payload)
    return resp




def save_img(img_content: "要处理的图片数据", new_save_path: "新文件的保存路径(包含后缀)",
             old_img_path: "旧文件路径") -> "void生成新的文件保存 ,传入旧文件路径是为了删除有问题的旧文件":
    print(new_save_path)
    try:
        # 从 _io.BytesIO 对象中读取字节数据
        resp_bytes = img_content.read()
        # 使用 Image.open() 方法打开图片
        img = Image.open(io.BytesIO(resp_bytes))
        # 如果需要指定图像格式,可以在保存时指定
        img.save(new_save_path, format="JPEG")
    except Exception as e:
        # 对于可能异常的图片->比如因为不合规导致resp.content没有正常返回
        print(e, new_save_path, "图片返回有问题,跳过并删除图片.这里的路径是新保存路径")
        os.remove(old_img_path)


if __name__ == '__main__':
    main(FakeArgs())#初始化model
    # 获取当前目录的子目录的路径
    img_path = 'manga'
    subdir_path = os.path.join(os.getcwd(), img_path)

    # 图片素材获取(包含子目录下所有图片)
    image_files = []
    for root, dirs, files in os.walk(subdir_path):
        for file in files:
            if file.endswith(".jpg") or file.endswith(".png"):
                image_files.append(os.path.relpath(os.path.join(root, file)))

    # 创建处理后的子目录在与image_files同级目录下
    processed_subdir_path = os.path.join(os.path.dirname(subdir_path), f"{img_path}1")
    os.makedirs(processed_subdir_path, exist_ok=True)

    # 对image_files进行某种处理,生成新图片,并保存在处理后的子目录中
    for img_file in image_files:
        # 处理图片的代码(这里仅作示例)
        # 假设处理后的图片为new_img
        img_dir = os.path.dirname(img_file)
        new_img_dir = os.path.join(processed_subdir_path, img_dir)
        os.makedirs(new_img_dir, exist_ok=True)

        new_img_path = os.path.join(new_img_dir, os.path.basename(img_file))

        if not os.path.exists(new_img_path):
            # 如果已经处理过那么跳过
            # 处理图片并保存
            img_inpainted = inpaint(img_path=img_file, mask_path='mask/0.jpg')  # 上传的遮罩保存都是0开始
            save_img(img_content=img_inpainted, new_save_path=new_img_path, old_img_path=img_file)
        else:
            print(f"Skipping {new_img_path} as it already exists.")