Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,18 +8,13 @@ import shutil
|
|
| 8 |
import os
|
| 9 |
import teed
|
| 10 |
|
| 11 |
-
|
| 12 |
from depthAnything.depth_anything.dpt import DepthAnything
|
| 13 |
from depthAnything.depth_anything.util.transform import Resize, NormalizeImage, PrepareForNet
|
| 14 |
from TEED.main import parse_args
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
# 这些函数无需修改
|
| 18 |
-
|
| 19 |
-
# 修改depth_anything函数,直接处理传入的图片
|
| 20 |
def depth_anything_image(image, encoder='vitl', pred_only=True, grayscale=True):
|
| 21 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 22 |
-
|
| 23 |
model_configs = {
|
| 24 |
'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
|
| 25 |
'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
|
|
@@ -37,11 +32,8 @@ def depth_anything_image(image, encoder='vitl', pred_only=True, grayscale=True):
|
|
| 37 |
PrepareForNet(),
|
| 38 |
])
|
| 39 |
|
| 40 |
-
raw_image = np.array(image.convert('RGB'))
|
| 41 |
-
raw_image = raw_image[:, :, ::-1].copy() # Convert RGB to BGR for OpenCV
|
| 42 |
-
|
| 43 |
h, w = raw_image.shape[:2]
|
| 44 |
-
|
| 45 |
image_tensor = transform({'image': raw_image / 255.0})['image']
|
| 46 |
image_tensor = torch.from_numpy(image_tensor).unsqueeze(0).to(DEVICE)
|
| 47 |
|
|
@@ -52,64 +44,39 @@ def depth_anything_image(image, encoder='vitl', pred_only=True, grayscale=True):
|
|
| 52 |
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
| 53 |
depth = depth.cpu().numpy().astype(np.uint8)
|
| 54 |
|
| 55 |
-
if grayscale
|
| 56 |
-
depth = np.repeat(depth[..., np.newaxis], 3, axis=-1)
|
| 57 |
-
else:
|
| 58 |
-
depth = cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO)
|
| 59 |
-
|
| 60 |
-
if pred_only:
|
| 61 |
-
return depth
|
| 62 |
-
else:
|
| 63 |
-
combined_results = np.hstack([raw_image, depth])
|
| 64 |
-
return combined_results
|
| 65 |
|
| 66 |
-
# TEED
|
| 67 |
def teed_process_image(image):
|
| 68 |
os.makedirs('./output/teed_imgs', exist_ok=True)
|
| 69 |
-
os.makedirs('teed_tmp', exist_ok=True)
|
| 70 |
|
| 71 |
-
# 将图像保存为临时文件
|
| 72 |
temp_image_path = './teed_tmp/temp_image.png'
|
| 73 |
cv2.imwrite(temp_image_path, np.array(image))
|
| 74 |
|
| 75 |
-
|
| 76 |
-
args, train_info = parse_args(is_testing=True, pl_opt_dir='./output/teed_imgs')
|
| 77 |
args.input_val_dir = './teed_tmp' # 临时目录
|
|
|
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
teed
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
|
|
|
| 85 |
return cv2.imread(os.path.join('./output/teed_imgs', 'processed_image.png'))
|
| 86 |
|
| 87 |
-
|
| 88 |
-
return processed_image
|
| 89 |
-
|
| 90 |
-
# 修改处理流程,处理单个上传的图片
|
| 91 |
def process_single_image(image):
|
| 92 |
-
# 深度处理
|
| 93 |
depth_result = depth_anything_image(image, 'vitl')
|
| 94 |
-
|
| 95 |
-
# TEED 边缘检测
|
| 96 |
teed_result = teed_process_image(image)
|
| 97 |
-
|
| 98 |
-
# 将两个结果叠加合并(例如 Multiply)
|
| 99 |
merged_result = multiply_blend(depth_result, teed_result)
|
| 100 |
-
|
| 101 |
return merged_result
|
| 102 |
|
| 103 |
-
# Gradio界面处理函数
|
| 104 |
def gradio_process_line(img):
|
| 105 |
-
# 保存上传的图像到临时路径
|
| 106 |
-
img_path = './temp_input.png'
|
| 107 |
-
img.save(img_path)
|
| 108 |
-
|
| 109 |
-
# 处理图像
|
| 110 |
processed_image = process_single_image(img)
|
| 111 |
-
|
| 112 |
-
# 返回处理后的图像
|
| 113 |
return Image.fromarray(processed_image)
|
| 114 |
|
| 115 |
# Gradio 界面
|
|
|
|
| 8 |
import os
|
| 9 |
import teed
|
| 10 |
|
|
|
|
| 11 |
from depthAnything.depth_anything.dpt import DepthAnything
|
| 12 |
from depthAnything.depth_anything.util.transform import Resize, NormalizeImage, PrepareForNet
|
| 13 |
from TEED.main import parse_args
|
| 14 |
|
| 15 |
+
# 深度处理函数
|
|
|
|
|
|
|
|
|
|
| 16 |
def depth_anything_image(image, encoder='vitl', pred_only=True, grayscale=True):
|
| 17 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
|
|
|
| 18 |
model_configs = {
|
| 19 |
'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
|
| 20 |
'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
|
|
|
|
| 32 |
PrepareForNet(),
|
| 33 |
])
|
| 34 |
|
| 35 |
+
raw_image = np.array(image.convert('RGB'))[:, :, ::-1].copy() # RGB to BGR
|
|
|
|
|
|
|
| 36 |
h, w = raw_image.shape[:2]
|
|
|
|
| 37 |
image_tensor = transform({'image': raw_image / 255.0})['image']
|
| 38 |
image_tensor = torch.from_numpy(image_tensor).unsqueeze(0).to(DEVICE)
|
| 39 |
|
|
|
|
| 44 |
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
| 45 |
depth = depth.cpu().numpy().astype(np.uint8)
|
| 46 |
|
| 47 |
+
return np.repeat(depth[..., np.newaxis], 3, axis=-1) if grayscale else cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# TEED 图像处理函数
|
| 50 |
def teed_process_image(image):
|
| 51 |
os.makedirs('./output/teed_imgs', exist_ok=True)
|
| 52 |
+
os.makedirs('./teed_tmp', exist_ok=True)
|
| 53 |
|
|
|
|
| 54 |
temp_image_path = './teed_tmp/temp_image.png'
|
| 55 |
cv2.imwrite(temp_image_path, np.array(image))
|
| 56 |
|
| 57 |
+
args = parse_args(is_testing=True, pl_opt_dir='./output/teed_imgs')
|
|
|
|
| 58 |
args.input_val_dir = './teed_tmp' # 临时目录
|
| 59 |
+
args.output_dir = './output/teed_imgs' # 输出目录
|
| 60 |
|
| 61 |
+
# 确保 TEED 处理的代码正常
|
| 62 |
+
if hasattr(teed, 'main'):
|
| 63 |
+
teed.main(args)
|
| 64 |
+
else:
|
| 65 |
+
print("TEED module does not have a main function.")
|
| 66 |
|
| 67 |
+
shutil.rmtree('./teed_tmp')
|
| 68 |
return cv2.imread(os.path.join('./output/teed_imgs', 'processed_image.png'))
|
| 69 |
|
| 70 |
+
# 处理单个图像
|
|
|
|
|
|
|
|
|
|
| 71 |
def process_single_image(image):
|
|
|
|
| 72 |
depth_result = depth_anything_image(image, 'vitl')
|
|
|
|
|
|
|
| 73 |
teed_result = teed_process_image(image)
|
|
|
|
|
|
|
| 74 |
merged_result = multiply_blend(depth_result, teed_result)
|
|
|
|
| 75 |
return merged_result
|
| 76 |
|
| 77 |
+
# Gradio 界面处理函数
|
| 78 |
def gradio_process_line(img):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
processed_image = process_single_image(img)
|
|
|
|
|
|
|
| 80 |
return Image.fromarray(processed_image)
|
| 81 |
|
| 82 |
# Gradio 界面
|