Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -233,29 +233,30 @@ def depth_anything(img_path = './input', outdir = './output/depth_anything', enc
|
|
| 233 |
|
| 234 |
cv2.imwrite(os.path.join(outdir, filename[:filename.rfind('.')] + '_img_depth.png'), final_result)
|
| 235 |
|
| 236 |
-
def teed_imgs(img_path
|
| 237 |
-
|
| 238 |
os.makedirs('teed_tmp', exist_ok=True)
|
|
|
|
|
|
|
| 239 |
if os.path.isfile(img_path):
|
| 240 |
-
|
| 241 |
-
if extension:
|
| 242 |
-
if extension.lower() == 'txt':
|
| 243 |
-
with open(img_path, 'r',encoding= 'utf-8') as f:
|
| 244 |
-
filenames = f.read().splitlines()
|
| 245 |
-
elif extension.lower() in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp','tif']:
|
| 246 |
-
filenames = [img_path]
|
| 247 |
else:
|
| 248 |
filenames = os.listdir(img_path)
|
| 249 |
-
filenames = [os.path.join(img_path, filename) for filename in filenames if not filename.startswith('.') and filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp','tif'))]
|
| 250 |
filenames.sort()
|
|
|
|
| 251 |
for filename in tqdm(filenames):
|
| 252 |
if gaussianBlur[0] != 0:
|
| 253 |
img = cv2.imread(filename)
|
| 254 |
-
img = cv2.GaussianBlur(img, (gaussianBlur[1],gaussianBlur[1]), gaussianBlur[2])
|
| 255 |
cv2.imwrite(os.path.join('teed_tmp', os.path.basename(filename)), img)
|
| 256 |
else:
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
| 258 |
teed.main(args, train_info)
|
|
|
|
| 259 |
shutil.rmtree('teed_tmp')
|
| 260 |
|
| 261 |
def merge_2_images(img1, img2, mode, erosion_para = [[0,0],[0,0]], dilate = [0,0]): #将 img1 合并至 img2,调整大小与 img2 相同
|
|
@@ -342,11 +343,26 @@ def process_images(input_folder='./output/merged_imgs'):
|
|
| 342 |
print(f"Error processing file {image_file}: {e}")
|
| 343 |
|
| 344 |
def process_line(img_path='./input', outdir='./output'):
|
|
|
|
| 345 |
depth_anything(img_path, os.path.join(outdir, "depth_anything"))
|
|
|
|
|
|
|
| 346 |
teed_imgs(img_path, os.path.join(outdir, "teed_imgs"), [1, 7, 2])
|
|
|
|
|
|
|
| 347 |
teed_imgs(os.path.join(outdir, "depth_anything"), os.path.join(outdir, "dp_teed_imgs"), [0, 7, 2])
|
| 348 |
-
|
| 349 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
|
| 352 |
|
|
@@ -357,16 +373,14 @@ if __name__ == '__main__':
|
|
| 357 |
from PIL import Image
|
| 358 |
|
| 359 |
def gradio_process_line(img):
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
|
|
|
| 363 |
|
| 364 |
-
|
| 365 |
-
|
| 366 |
|
| 367 |
-
# 返回输出图像
|
| 368 |
-
output_image_path = './output/merged_imgs/your_final_image_name.png' # 修改为实际输出路径
|
| 369 |
-
return Image.open(output_image_path)
|
| 370 |
|
| 371 |
# 定义 Gradio 接口
|
| 372 |
iface = gr.Interface(
|
|
@@ -378,7 +392,7 @@ if __name__ == '__main__':
|
|
| 378 |
)
|
| 379 |
|
| 380 |
# 启动 Gradio 应用
|
| 381 |
-
iface.launch(
|
| 382 |
|
| 383 |
|
| 384 |
|
|
|
|
| 233 |
|
| 234 |
cv2.imwrite(os.path.join(outdir, filename[:filename.rfind('.')] + '_img_depth.png'), final_result)
|
| 235 |
|
| 236 |
+
def teed_imgs(img_path='./input', outdir='./output/teed_imgs', gaussianBlur=[0, 3, 0]):
|
| 237 |
+
os.makedirs(outdir, exist_ok=True)
|
| 238 |
os.makedirs('teed_tmp', exist_ok=True)
|
| 239 |
+
|
| 240 |
+
# 处理上传的单个图片
|
| 241 |
if os.path.isfile(img_path):
|
| 242 |
+
filenames = [img_path]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
else:
|
| 244 |
filenames = os.listdir(img_path)
|
| 245 |
+
filenames = [os.path.join(img_path, filename) for filename in filenames if not filename.startswith('.') and filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', 'tif'))]
|
| 246 |
filenames.sort()
|
| 247 |
+
|
| 248 |
for filename in tqdm(filenames):
|
| 249 |
if gaussianBlur[0] != 0:
|
| 250 |
img = cv2.imread(filename)
|
| 251 |
+
img = cv2.GaussianBlur(img, (gaussianBlur[1], gaussianBlur[1]), gaussianBlur[2])
|
| 252 |
cv2.imwrite(os.path.join('teed_tmp', os.path.basename(filename)), img)
|
| 253 |
else:
|
| 254 |
+
shutil.copy(filename, 'teed_tmp')
|
| 255 |
+
|
| 256 |
+
# 这里调用您的其他处理代码
|
| 257 |
+
args, train_info = parse_args(is_testing=True, pl_opt_dir=outdir)
|
| 258 |
teed.main(args, train_info)
|
| 259 |
+
|
| 260 |
shutil.rmtree('teed_tmp')
|
| 261 |
|
| 262 |
def merge_2_images(img1, img2, mode, erosion_para = [[0,0],[0,0]], dilate = [0,0]): #将 img1 合并至 img2,调整大小与 img2 相同
|
|
|
|
| 343 |
print(f"Error processing file {image_file}: {e}")
|
| 344 |
|
| 345 |
def process_line(img_path='./input', outdir='./output'):
|
| 346 |
+
# 处理深度图像
|
| 347 |
depth_anything(img_path, os.path.join(outdir, "depth_anything"))
|
| 348 |
+
|
| 349 |
+
# 处理 teed_imgs
|
| 350 |
teed_imgs(img_path, os.path.join(outdir, "teed_imgs"), [1, 7, 2])
|
| 351 |
+
|
| 352 |
+
# 处理深度图的 teed_imgs
|
| 353 |
teed_imgs(os.path.join(outdir, "depth_anything"), os.path.join(outdir, "dp_teed_imgs"), [0, 7, 2])
|
| 354 |
+
|
| 355 |
+
# 合并图像
|
| 356 |
+
merge_images_in_2_folder(
|
| 357 |
+
os.path.join(outdir, "teed_imgs"),
|
| 358 |
+
os.path.join(outdir, "dp_teed_imgs"),
|
| 359 |
+
os.path.join(outdir, "merged_imgs"),
|
| 360 |
+
'_depth',
|
| 361 |
+
1,
|
| 362 |
+
'multiply',
|
| 363 |
+
[[2, 0], [2, 1]],
|
| 364 |
+
[1, 0]
|
| 365 |
+
)
|
| 366 |
|
| 367 |
|
| 368 |
|
|
|
|
| 373 |
from PIL import Image
|
| 374 |
|
| 375 |
def gradio_process_line(img):
|
| 376 |
+
img_path = './temp_input.png'
|
| 377 |
+
img.save(img_path)
|
| 378 |
+
|
| 379 |
+
process_line(img_path, './output')
|
| 380 |
|
| 381 |
+
output_image_path = './output/teed_imgs/processed_image.png' # 更新为实际输出路径
|
| 382 |
+
return Image.open(output_image_path)
|
| 383 |
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
# 定义 Gradio 接口
|
| 386 |
iface = gr.Interface(
|
|
|
|
| 392 |
)
|
| 393 |
|
| 394 |
# 启动 Gradio 应用
|
| 395 |
+
iface.launch()
|
| 396 |
|
| 397 |
|
| 398 |
|