File size: 5,116 Bytes
879577c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f564c670-1fa4-4b68-91e9-344e0c29e268",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using cache found in C:\\Users\\胡逸飞/.cache\\torch\\hub\\isl-org_ZoeDepth_main\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "img_size [384, 512]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using cache found in C:\\Users\\胡逸飞/.cache\\torch\\hub\\intel-isl_MiDaS_master\n",
      "D:\\SubDiffusion\\venv\\lib\\site-packages\\torch\\functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ..\\aten\\src\\ATen\\native\\TensorShape.cpp:3484.)\n",
      "  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Params passed to Resize transform:\n",
      "\twidth:  512\n",
      "\theight:  384\n",
      "\tresize_target:  True\n",
      "\tkeep_aspect_ratio:  True\n",
      "\tensure_multiple_of:  32\n",
      "\tresize_method:  minimal\n",
      "Using pretrained resource url::https://github.com/isl-org/ZoeDepth/releases/download/v1.0/ZoeD_M12_N.pt\n",
      "Loaded successfully\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import os\n",
    "import torchvision.transforms as transforms\n",
    "import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "import cv2\n",
    "import matplotlib.cm as cm\n",
    "from einops import rearrange\n",
    "repo = \"isl-org/ZoeDepth\"\n",
    "# Zoe_N\n",
    "model_zoe_n = torch.hub.load(repo, \"ZoeD_N\", pretrained=True)\n",
    "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
    "model = model_zoe_n.to(DEVICE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "309a4ce8-72d5-4d76-8eee-a85c66e5ae79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image = Image.open('7.jpg')\n",
    "# 调整图像到正方形\n",
    "max_size = 960\n",
    "if image.size[0] > image.size[1]:\n",
    "    wpercent = max_size / float(image.size[0])\n",
    "    hsize = int((float(image.size[1]) * float(wpercent)))\n",
    "    image = image.resize((max_size, hsize), Image.Resampling.BILINEAR)\n",
    "else:\n",
    "    hpercent = max_size / float(image.size[1])\n",
    "    wsize = int((float(image.size[0]) * float(hpercent)))\n",
    "    image = image.resize((wsize, max_size), Image.Resampling.BILINEAR)\n",
    "\n",
    "    # 创建一个n1*n1的纯黑色图像\n",
    "    background = Image.new('RGB', (960, 960), (0, 0, 0))\n",
    "\n",
    "# 计算输入图像在背景图像中的位置\n",
    "bg_w, bg_h = background.size\n",
    "img_w, img_h = image.size\n",
    "offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)\n",
    "\n",
    "# 将输入图像放在背景图像中\n",
    "background.paste(image, offset)\n",
    "input_image=np.array(background)\n",
    "\n",
    "# 将图像转换为张量\n",
    "#input_image = transforms.ToTensor()(background).unsqueeze(0).to(device)\n",
    "image_depth = input_image\n",
    "with torch.no_grad():\n",
    "    image_depth = torch.from_numpy(image_depth).float().to(device)\n",
    "    image_depth = image_depth / 255.0\n",
    "    image_depth = rearrange(image_depth, 'h w c -> 1 c h w')\n",
    "    depth = model.infer(image_depth)\n",
    "\n",
    "    depth = depth[0, 0].cpu().numpy()\n",
    "\n",
    "    vmin = np.percentile(depth, 2)\n",
    "    vmax = np.percentile(depth, 85)\n",
    "\n",
    "    depth -= vmin\n",
    "    depth /= vmax - vmin\n",
    "    depth = 1.0 - depth\n",
    "    depth_image = (depth * 255.0).clip(0, 255).astype(np.uint8)\n",
    "cv2.imwrite('depth_image.png', depth_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f0786ef5-0413-41cd-b54f-e012fc178aca",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "1e802ea9-0905-43d1-8ad2-5195c6a3054d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}