EgoHackZero
commited on
Commit
·
e132a83
1
Parent(s):
7cfabd8
first try
Browse files- app.py +45 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
# Загрузка модели
|
8 |
+
midas = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")
|
9 |
+
midas.eval()
|
10 |
+
|
11 |
+
midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms")
|
12 |
+
transform = midas_transforms.small_transform
|
13 |
+
|
14 |
+
def predict_depth(image):
|
15 |
+
img = np.array(image)
|
16 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
17 |
+
input_tensor = transform(img_rgb).unsqueeze(0)
|
18 |
+
|
19 |
+
with torch.no_grad():
|
20 |
+
prediction = midas(input_tensor)
|
21 |
+
prediction = torch.nn.functional.interpolate(
|
22 |
+
prediction.unsqueeze(1),
|
23 |
+
size=img_rgb.shape[:2],
|
24 |
+
mode="bicubic",
|
25 |
+
align_corners=False,
|
26 |
+
).squeeze()
|
27 |
+
|
28 |
+
depth_map = prediction.cpu().numpy()
|
29 |
+
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
30 |
+
depth_map = (depth_map * 255).astype(np.uint8)
|
31 |
+
depth_img = Image.fromarray(depth_map)
|
32 |
+
|
33 |
+
return depth_img
|
34 |
+
|
35 |
+
# Интерфейс Gradio
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=predict_depth,
|
38 |
+
inputs=gr.Image(type="pil"),
|
39 |
+
outputs=gr.Image(type="pil"),
|
40 |
+
title="MiDaS Depth Estimation",
|
41 |
+
description="Загрузите изображение и получите карту глубины."
|
42 |
+
)
|
43 |
+
|
44 |
+
if __name__ == "__main__":
|
45 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
timm
|
4 |
+
opencv-python
|
5 |
+
gradio
|