rogerxavier
commited on
Commit
•
e3f7bf2
1
Parent(s):
e43936c
Update 1removeMask.py
Browse files- 1removeMask.py +191 -25
1removeMask.py
CHANGED
@@ -1,18 +1,179 @@
|
|
1 |
-
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
3 |
import requests
|
4 |
import os
|
5 |
-
from
|
6 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
def inpaint(img_path:str,mask_path:str)->"img content (resp.content)":
|
9 |
-
|
10 |
-
|
|
|
11 |
# 将字节数据转换为Base64编码的字符串
|
12 |
|
13 |
files = {
|
14 |
"image": image_bytes,
|
15 |
-
"mask":mask_bytes
|
16 |
}
|
17 |
payload = {
|
18 |
"ldmSteps": 25,
|
@@ -49,32 +210,32 @@ def inpaint(img_path:str,mask_path:str)->"img content (resp.content)":
|
|
49 |
"p2pGuidanceScale": 7.5,
|
50 |
"controlnet_conditioning_scale": 0.4,
|
51 |
"controlnet_method": "control_v11p_sd15_canny"
|
52 |
-
}#payload用data
|
|
|
|
|
|
|
53 |
|
54 |
-
#不使用header
|
55 |
-
#resp = requests.post("https://sanster-lama-cleaner-lama.hf.space/inpaint", data=payload, files=files)
|
56 |
-
#使用自己的space
|
57 |
-
resp = requests.post("https://rogerxavier-lama-cleaner-lama.hf.space/inpaint", data=payload, files=files)
|
58 |
-
|
59 |
-
print("请求lama clenaer状态是",resp.status_code)
|
60 |
-
return bytes(resp.content)
|
61 |
|
62 |
-
|
|
|
|
|
|
|
63 |
print(new_save_path)
|
64 |
try:
|
65 |
-
|
|
|
|
|
|
|
66 |
# 如果需要指定图像格式,可以在保存时指定
|
67 |
img.save(new_save_path, format="JPEG")
|
68 |
except Exception as e:
|
69 |
-
|
70 |
-
print(e,new_save_path,"图片返回有问题,跳过并删除图片.这里的路径是新保存路径")
|
71 |
os.remove(old_img_path)
|
72 |
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
if __name__ == '__main__':
|
|
|
78 |
# 获取当前目录的子目录的路径
|
79 |
img_path = 'manga'
|
80 |
subdir_path = os.path.join(os.getcwd(), img_path)
|
@@ -101,9 +262,14 @@ if __name__ == '__main__':
|
|
101 |
new_img_path = os.path.join(new_img_dir, os.path.basename(img_file))
|
102 |
|
103 |
if not os.path.exists(new_img_path):
|
104 |
-
|
105 |
# 处理图片并保存
|
106 |
-
img_inpainted = inpaint(img_path=img_file, mask_path='mask/0.jpg')
|
107 |
-
save_img(img_content=img_inpainted, new_save_path=new_img_path,old_img_path=img_file)
|
108 |
else:
|
109 |
-
print(f"Skipping {new_img_path} as it already exists.")
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import base64
|
2 |
+
|
3 |
+
# import requests
|
4 |
+
# import os
|
5 |
+
# from PIL import Image
|
6 |
+
# import io
|
7 |
+
|
8 |
+
# def inpaint(img_path:str,mask_path:str)->"img content (resp.content)":
|
9 |
+
# image_bytes = open(img_path, 'rb')
|
10 |
+
# mask_bytes = open(mask_path, 'rb')
|
11 |
+
# # 将字节数据转换为Base64编码的字符串
|
12 |
+
|
13 |
+
# files = {
|
14 |
+
# "image": image_bytes,
|
15 |
+
# "mask":mask_bytes
|
16 |
+
# }
|
17 |
+
# payload = {
|
18 |
+
# "ldmSteps": 25,
|
19 |
+
# "ldmSampler": "plms",
|
20 |
+
# "zitsWireframe": True,
|
21 |
+
# "hdStrategy": "Crop",
|
22 |
+
# "hdStrategyCropMargin": 196,
|
23 |
+
# "hdStrategyCropTrigerSize": 800,
|
24 |
+
# "hdStrategyResizeLimit": 2048,
|
25 |
+
# "prompt": "",
|
26 |
+
# "negativePrompt": "",
|
27 |
+
# "croperX": 307,
|
28 |
+
# "croperY": 544,
|
29 |
+
# "croperHeight": 512,
|
30 |
+
# "croperWidth": 512,
|
31 |
+
# "useCroper": False,
|
32 |
+
# "sdMaskBlur": 5,
|
33 |
+
# "sdStrength": 0.75,
|
34 |
+
# "sdSteps": 50,
|
35 |
+
# "sdGuidanceScale": 7.5,
|
36 |
+
# "sdSampler": "uni_pc",
|
37 |
+
# "sdSeed": -1,
|
38 |
+
# "sdMatchHistograms": False,
|
39 |
+
# "sdScale": 1,
|
40 |
+
# "cv2Radius": 5,
|
41 |
+
# "cv2Flag": "INPAINT_NS",
|
42 |
+
# "paintByExampleSteps": 50,
|
43 |
+
# "paintByExampleGuidanceScale": 7.5,
|
44 |
+
# "paintByExampleSeed": -1,
|
45 |
+
# "paintByExampleMaskBlur": 5,
|
46 |
+
# "paintByExampleMatchHistograms": False,
|
47 |
+
# "p2pSteps": 50,
|
48 |
+
# "p2pImageGuidanceScale": 1.5,
|
49 |
+
# "p2pGuidanceScale": 7.5,
|
50 |
+
# "controlnet_conditioning_scale": 0.4,
|
51 |
+
# "controlnet_method": "control_v11p_sd15_canny"
|
52 |
+
# }#payload用data
|
53 |
+
|
54 |
+
# #不使用header
|
55 |
+
# #resp = requests.post("https://sanster-lama-cleaner-lama.hf.space/inpaint", data=payload, files=files)
|
56 |
+
# #使用自己的space
|
57 |
+
# resp = requests.post("https://rogerxavier-lama-cleaner-lama.hf.space/inpaint", data=payload, files=files)
|
58 |
+
|
59 |
+
# print("请求lama clenaer状态是",resp.status_code)
|
60 |
+
# return bytes(resp.content)
|
61 |
+
|
62 |
+
# def save_img(img_content:"要处理的图片数据",new_save_path:"新文件的保存路径(包含后缀)",old_img_path:"旧文件路径")->"void生成新的文件保存 ,传入旧文件路径是为了删除有问题的旧文件":
|
63 |
+
# print(new_save_path)
|
64 |
+
# try:
|
65 |
+
# img = Image.open(io.BytesIO(img_content))
|
66 |
+
# # 如果需要指定图像格式,可以在保存时指定
|
67 |
+
# img.save(new_save_path, format="JPEG")
|
68 |
+
# except Exception as e:
|
69 |
+
# #对于可能异常的图片->比如因为不合规导致resp.content没有正常返回
|
70 |
+
# print(e,new_save_path,"图片返回有问题,跳过并删除图片.这里的路径是新保存路径")
|
71 |
+
# os.remove(old_img_path)
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
# if __name__ == '__main__':
|
78 |
+
# # 获取当前目录的子目录的路径
|
79 |
+
# img_path = 'manga'
|
80 |
+
# subdir_path = os.path.join(os.getcwd(), img_path)
|
81 |
+
|
82 |
+
# # 图片素材获取(包含子目录下所有图片)
|
83 |
+
# image_files = []
|
84 |
+
# for root, dirs, files in os.walk(subdir_path):
|
85 |
+
# for file in files:
|
86 |
+
# if file.endswith(".jpg") or file.endswith(".png"):
|
87 |
+
# image_files.append(os.path.relpath(os.path.join(root, file)))
|
88 |
+
|
89 |
+
# # 创建处理后的子目录在与image_files同级目录下
|
90 |
+
# processed_subdir_path = os.path.join(os.path.dirname(subdir_path), f"{img_path}1")
|
91 |
+
# os.makedirs(processed_subdir_path, exist_ok=True)
|
92 |
+
|
93 |
+
# # 对image_files进行某种处理,生成新图片,并保存在处理后的子目录中
|
94 |
+
# for img_file in image_files:
|
95 |
+
# # 处理图片的代码(这里仅作示例)
|
96 |
+
# # 假设处理后的图片为new_img
|
97 |
+
# img_dir = os.path.dirname(img_file)
|
98 |
+
# new_img_dir = os.path.join(processed_subdir_path, img_dir)
|
99 |
+
# os.makedirs(new_img_dir, exist_ok=True)
|
100 |
+
|
101 |
+
# new_img_path = os.path.join(new_img_dir, os.path.basename(img_file))
|
102 |
+
|
103 |
+
# if not os.path.exists(new_img_path):
|
104 |
+
# #如果已经处理过那么跳过
|
105 |
+
# # 处理图片并保存
|
106 |
+
# img_inpainted = inpaint(img_path=img_file, mask_path='mask/0.jpg')#上传的遮罩保存都是0开始
|
107 |
+
# save_img(img_content=img_inpainted, new_save_path=new_img_path,old_img_path=img_file)
|
108 |
+
# else:
|
109 |
+
# print(f"Skipping {new_img_path} as it already exists.")
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
import base64
|
120 |
import requests
|
121 |
import os
|
122 |
+
from typing import List
|
123 |
import io
|
124 |
+
from PIL import Image
|
125 |
+
from pydantic import BaseModel
|
126 |
+
from lama_cleaner.server import process
|
127 |
+
from lama_cleaner.server import main#先初始化才能用process
|
128 |
+
#C:\Users\17331\.cache\torch\hub 缓存下载位置,记得删
|
129 |
+
|
130 |
+
class FakeArgs(BaseModel):
|
131 |
+
host: str = "0.0.0.0"
|
132 |
+
port: int = 7860
|
133 |
+
model: str = 'lama'
|
134 |
+
hf_access_token: str = ""
|
135 |
+
sd_enable_xformers: bool = False
|
136 |
+
sd_disable_nsfw: bool = False
|
137 |
+
sd_cpu_textencoder: bool = True
|
138 |
+
sd_controlnet: bool = False
|
139 |
+
sd_controlnet_method: str = "control_v11p_sd15_canny"
|
140 |
+
sd_local_model_path: str = ""
|
141 |
+
sd_run_local: bool = False
|
142 |
+
local_files_only: bool = False
|
143 |
+
cpu_offload: bool = False
|
144 |
+
device: str = "cpu"
|
145 |
+
gui: bool = False
|
146 |
+
gui_size: List[int] = [1000, 1000]
|
147 |
+
input: str = ''
|
148 |
+
disable_model_switch: bool = True
|
149 |
+
debug: bool = False
|
150 |
+
no_half: bool = False
|
151 |
+
disable_nsfw: bool = False
|
152 |
+
enable_xformers: bool = False
|
153 |
+
enable_interactive_seg: bool = True
|
154 |
+
interactive_seg_model: str = "vit_b"
|
155 |
+
interactive_seg_device: str = "cpu"
|
156 |
+
enable_remove_bg: bool = False
|
157 |
+
enable_anime_seg: bool = False
|
158 |
+
enable_realesrgan: bool = False
|
159 |
+
enable_gfpgan: bool = False
|
160 |
+
gfpgan_device: str = "cpu"
|
161 |
+
enable_restoreformer: bool = False
|
162 |
+
enable_gif: bool = False
|
163 |
+
quality: int = 95
|
164 |
+
model_dir: str = None
|
165 |
+
output_dir: str = None
|
166 |
+
|
167 |
|
168 |
+
def inpaint(img_path: str, mask_path: str) -> "img content (resp.content)":
|
169 |
+
# urllib3 1.26.4 兼容
|
170 |
+
image_bytes = open('image.jpg', 'rb')
|
171 |
+
mask_bytes = open('mask.jpg', 'rb')
|
172 |
# 将字节数据转换为Base64编码的字符串
|
173 |
|
174 |
files = {
|
175 |
"image": image_bytes,
|
176 |
+
"mask": mask_bytes
|
177 |
}
|
178 |
payload = {
|
179 |
"ldmSteps": 25,
|
|
|
210 |
"p2pGuidanceScale": 7.5,
|
211 |
"controlnet_conditioning_scale": 0.4,
|
212 |
"controlnet_method": "control_v11p_sd15_canny"
|
213 |
+
} # payload用data
|
214 |
+
|
215 |
+
resp = process(files=files, payload=payload)
|
216 |
+
return resp
|
217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
+
|
220 |
+
|
221 |
+
def save_img(img_content: "要处理的图片数据", new_save_path: "新文件的保存路径(包含后缀)",
|
222 |
+
old_img_path: "旧文件路径") -> "void生成新的文件保存 ,传入旧文件路径是为了删除有问题的旧文件":
|
223 |
print(new_save_path)
|
224 |
try:
|
225 |
+
# 从 _io.BytesIO 对象中读取字节数据
|
226 |
+
resp_bytes = img_content.read()
|
227 |
+
# 使用 Image.open() 方法打开图片
|
228 |
+
img = Image.open(io.BytesIO(resp_bytes))
|
229 |
# 如果需要指定图像格式,可以在保存时指定
|
230 |
img.save(new_save_path, format="JPEG")
|
231 |
except Exception as e:
|
232 |
+
# 对于可能异常的图片->比如因为不合规导致resp.content没有正常返回
|
233 |
+
print(e, new_save_path, "图片返回有问题,跳过并删除图片.这里的路径是新保存路径")
|
234 |
os.remove(old_img_path)
|
235 |
|
236 |
|
|
|
|
|
|
|
237 |
if __name__ == '__main__':
|
238 |
+
main(FakeArgs())#初始化model
|
239 |
# 获取当前目录的子目录的路径
|
240 |
img_path = 'manga'
|
241 |
subdir_path = os.path.join(os.getcwd(), img_path)
|
|
|
262 |
new_img_path = os.path.join(new_img_dir, os.path.basename(img_file))
|
263 |
|
264 |
if not os.path.exists(new_img_path):
|
265 |
+
# 如果已经处理过那么跳过
|
266 |
# 处理图片并保存
|
267 |
+
img_inpainted = inpaint(img_path=img_file, mask_path='mask/0.jpg') # 上传的遮罩保存都是0开始
|
268 |
+
save_img(img_content=img_inpainted, new_save_path=new_img_path, old_img_path=img_file)
|
269 |
else:
|
270 |
+
print(f"Skipping {new_img_path} as it already exists.")
|
271 |
+
|
272 |
+
|
273 |
+
|
274 |
+
|
275 |
+
|