{ "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 }