import{s as ee,o as se,n as ae}from"../chunks/scheduler.25b97de1.js";import{S as le,i as ne,g as i,s as l,r,A as ie,h as p,f as s,c as n,j as te,u as h,x as o,k as O,y as pe,a,v as c,d,t as u,w as g}from"../chunks/index.d9030fc9.js";import{T as oe}from"../chunks/Tip.baa67368.js";import{C as M}from"../chunks/CodeBlock.e6cd0d95.js";import{H as Gt,E as me}from"../chunks/EditOnGithub.91d95064.js";function re(tt){let m,j='To see all architectures and checkpoints compatible with this task, we recommend checking the <a href="https://huggingface.co/tasks/depth-anything" rel="nofollow">task-page</a>';return{c(){m=i("p"),m.innerHTML=j},l(f){m=p(f,"P",{"data-svelte-h":!0}),o(m)!=="svelte-15v7fmc"&&(m.innerHTML=j)},m(f,D){a(f,m,D)},p:ae,d(f){f&&s(m)}}}function he(tt){let m,j,f,D,v,et,J,Bt=`Monocular depth estimation is a computer vision task that involves predicting the depth information of a scene from a
single image. In other words, it is the process of estimating the distance of objects in a scene from
a single camera viewpoint.`,st,T,Ht=`Monocular depth estimation has various applications, including 3D reconstruction, augmented reality, autonomous driving,
and robotics. It is a challenging task as it requires the model to understand the complex relationships between objects
in the scene and the corresponding depth information, which can be affected by factors such as lighting conditions,
occlusion, and texture.`,at,y,lt,U,It="In this guide you’ll learn how to:",nt,C,zt="<li>create a depth estimation pipeline</li> <li>run depth estimation inference by hand</li>",it,k,Et="Before you begin, make sure you have all the necessary libraries installed:",pt,x,ot,Z,mt,W,Vt=`The simplest way to try out inference with a model supporting depth estimation is to use the corresponding <a href="/docs/transformers/pr_30674/en/main_classes/pipelines#transformers.pipeline">pipeline()</a>.
Instantiate a pipeline from a <a href="https://huggingface.co/models?pipeline_tag=depth-estimation&amp;sort=downloads" rel="nofollow">checkpoint on the Hugging Face Hub</a>:`,rt,R,ht,_,Nt="Next, choose an image to analyze:",ct,G,dt,w,Xt='<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/depth-estimation-example.jpg" alt="Photo of a busy street"/>',ut,B,Qt="Pass the image to the pipeline.",gt,H,ft,I,Ft=`The pipeline returns a dictionary with two entries. The first one, called <code>predicted_depth</code>, is a tensor with the values
being the depth expressed in meters for each pixel.
The second one, <code>depth</code>, is a PIL image that visualizes the depth estimation result.`,Mt,z,Yt="Let’s take a look at the visualized result:",yt,E,wt,$,St='<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/depth-visualization.png" alt="Depth estimation visualization"/>',$t,V,bt,N,Lt="Now that you’ve seen how to use the depth estimation pipeline, let’s see how we can replicate the same result by hand.",jt,X,Pt=`Start by loading the model and associated processor from a <a href="https://huggingface.co/models?pipeline_tag=depth-estimation&amp;sort=downloads" rel="nofollow">checkpoint on the Hugging Face Hub</a>.
Here we’ll use the same checkpoint as before:`,vt,Q,Jt,F,qt=`Prepare the image input for the model using the <code>image_processor</code> that will take care of the necessary image transformations
such as resizing and normalization:`,Tt,Y,Ut,S,At="Pass the prepared inputs through the model:",Ct,L,kt,P,Dt="Visualize the results:",xt,q,Zt,b,Kt='<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/depth-visualization.png" alt="Depth estimation visualization"/>',Wt,A,Rt,K,_t;return v=new Gt({props:{title:"Monocular depth estimation",local:"monocular-depth-estimation",headingTag:"h1"}}),y=new oe({props:{$$slots:{default:[re]},$$scope:{ctx:tt}}}),x=new M({props:{code:"cGlwJTIwaW5zdGFsbCUyMC1xJTIwdHJhbnNmb3JtZXJz",highlighted:"pip install -q transformers",wrap:!1}}),Z=new Gt({props:{title:"Depth estimation pipeline",local:"depth-estimation-pipeline",headingTag:"h2"}}),R=new M({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBY2hlY2twb2ludCUyMCUzRCUyMCUyMnZpbnZpbm8wMiUyRmdscG4tbnl1JTIyJTBBZGVwdGhfZXN0aW1hdG9yJTIwJTNEJTIwcGlwZWxpbmUoJTIyZGVwdGgtZXN0aW1hdGlvbiUyMiUyQyUyMG1vZGVsJTNEY2hlY2twb2ludCk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

<span class="hljs-meta">&gt;&gt;&gt; </span>checkpoint = <span class="hljs-string">&quot;vinvino02/glpn-nyu&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>depth_estimator = pipeline(<span class="hljs-string">&quot;depth-estimation&quot;</span>, model=checkpoint)`,wrap:!1}}),G=new M({props:{code:"ZnJvbSUyMFBJTCUyMGltcG9ydCUyMEltYWdlJTBBaW1wb3J0JTIwcmVxdWVzdHMlMEElMEF1cmwlMjAlM0QlMjAlMjJodHRwcyUzQSUyRiUyRnVuc3BsYXNoLmNvbSUyRnBob3RvcyUyRkh3QkFzU2JQQkRVJTJGZG93bmxvYWQlM0ZpeGlkJTNETW53eE1qQTNmREI4TVh4elpXRnlZMmg4TXpSOGZHTmhjaVV5TUdsdUpUSXdkR2hsSlRJd2MzUnlaV1YwZkdWdWZEQjhNSHg4ZkRFMk56ZzVNREV3T0RnJTI2Zm9yY2UlM0R0cnVlJTI2dyUzRDY0MCUyMiUwQWltYWdlJTIwJTNEJTIwSW1hZ2Uub3BlbihyZXF1ZXN0cy5nZXQodXJsJTJDJTIwc3RyZWFtJTNEVHJ1ZSkucmF3KSUwQWltYWdl",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> requests

<span class="hljs-meta">&gt;&gt;&gt; </span>url = <span class="hljs-string">&quot;https://unsplash.com/photos/HwBAsSbPBDU/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MzR8fGNhciUyMGluJTIwdGhlJTIwc3RyZWV0fGVufDB8MHx8fDE2Nzg5MDEwODg&amp;force=true&amp;w=640&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>image = Image.<span class="hljs-built_in">open</span>(requests.get(url, stream=<span class="hljs-literal">True</span>).raw)
<span class="hljs-meta">&gt;&gt;&gt; </span>image`,wrap:!1}}),H=new M({props:{code:"cHJlZGljdGlvbnMlMjAlM0QlMjBkZXB0aF9lc3RpbWF0b3IoaW1hZ2Up",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>predictions = depth_estimator(image)',wrap:!1}}),E=new M({props:{code:"cHJlZGljdGlvbnMlNUIlMjJkZXB0aCUyMiU1RA==",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>predictions[<span class="hljs-string">&quot;depth&quot;</span>]',wrap:!1}}),V=new Gt({props:{title:"Depth estimation inference by hand",local:"depth-estimation-inference-by-hand",headingTag:"h2"}}),Q=new M({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9JbWFnZVByb2Nlc3NvciUyQyUyMEF1dG9Nb2RlbEZvckRlcHRoRXN0aW1hdGlvbiUwQSUwQWNoZWNrcG9pbnQlMjAlM0QlMjAlMjJ2aW52aW5vMDIlMkZnbHBuLW55dSUyMiUwQSUwQWltYWdlX3Byb2Nlc3NvciUyMCUzRCUyMEF1dG9JbWFnZVByb2Nlc3Nvci5mcm9tX3ByZXRyYWluZWQoY2hlY2twb2ludCklMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvckRlcHRoRXN0aW1hdGlvbi5mcm9tX3ByZXRyYWluZWQoY2hlY2twb2ludCk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoImageProcessor, AutoModelForDepthEstimation

<span class="hljs-meta">&gt;&gt;&gt; </span>checkpoint = <span class="hljs-string">&quot;vinvino02/glpn-nyu&quot;</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>image_processor = AutoImageProcessor.from_pretrained(checkpoint)
<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForDepthEstimation.from_pretrained(checkpoint)`,wrap:!1}}),Y=new M({props:{code:"cGl4ZWxfdmFsdWVzJTIwJTNEJTIwaW1hZ2VfcHJvY2Vzc29yKGltYWdlJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMikucGl4ZWxfdmFsdWVz",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>pixel_values = image_processor(image, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>).pixel_values',wrap:!1}}),L=new M({props:{code:"aW1wb3J0JTIwdG9yY2glMEElMEF3aXRoJTIwdG9yY2gubm9fZ3JhZCgpJTNBJTBBJTIwJTIwJTIwJTIwb3V0cHV0cyUyMCUzRCUyMG1vZGVsKHBpeGVsX3ZhbHVlcyklMEElMjAlMjAlMjAlMjBwcmVkaWN0ZWRfZGVwdGglMjAlM0QlMjBvdXRwdXRzLnByZWRpY3RlZF9kZXB0aA==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> torch

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">with</span> torch.no_grad():
<span class="hljs-meta">... </span>    outputs = model(pixel_values)
<span class="hljs-meta">... </span>    predicted_depth = outputs.predicted_depth`,wrap:!1}}),q=new M({props:{code:"aW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBJTIzJTIwaW50ZXJwb2xhdGUlMjB0byUyMG9yaWdpbmFsJTIwc2l6ZSUwQXByZWRpY3Rpb24lMjAlM0QlMjB0b3JjaC5ubi5mdW5jdGlvbmFsLmludGVycG9sYXRlKCUwQSUyMCUyMCUyMCUyMHByZWRpY3RlZF9kZXB0aC51bnNxdWVlemUoMSklMkMlMEElMjAlMjAlMjAlMjBzaXplJTNEaW1hZ2Uuc2l6ZSU1QiUzQSUzQS0xJTVEJTJDJTBBJTIwJTIwJTIwJTIwbW9kZSUzRCUyMmJpY3ViaWMlMjIlMkMlMEElMjAlMjAlMjAlMjBhbGlnbl9jb3JuZXJzJTNERmFsc2UlMkMlMEEpLnNxdWVlemUoKSUwQW91dHB1dCUyMCUzRCUyMHByZWRpY3Rpb24ubnVtcHkoKSUwQSUwQWZvcm1hdHRlZCUyMCUzRCUyMChvdXRwdXQlMjAqJTIwMjU1JTIwJTJGJTIwbnAubWF4KG91dHB1dCkpLmFzdHlwZSglMjJ1aW50OCUyMiklMEFkZXB0aCUyMCUzRCUyMEltYWdlLmZyb21hcnJheShmb3JtYXR0ZWQpJTBBZGVwdGg=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># interpolate to original size</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>prediction = torch.nn.functional.interpolate(
<span class="hljs-meta">... </span>    predicted_depth.unsqueeze(<span class="hljs-number">1</span>),
<span class="hljs-meta">... </span>    size=image.size[::-<span class="hljs-number">1</span>],
<span class="hljs-meta">... </span>    mode=<span class="hljs-string">&quot;bicubic&quot;</span>,
<span class="hljs-meta">... </span>    align_corners=<span class="hljs-literal">False</span>,
<span class="hljs-meta">... </span>).squeeze()
<span class="hljs-meta">&gt;&gt;&gt; </span>output = prediction.numpy()

<span class="hljs-meta">&gt;&gt;&gt; </span>formatted = (output * <span class="hljs-number">255</span> / np.<span class="hljs-built_in">max</span>(output)).astype(<span class="hljs-string">&quot;uint8&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>depth = Image.fromarray(formatted)
<span class="hljs-meta">&gt;&gt;&gt; </span>depth`,wrap:!1}}),A=new me({props:{source:"https://github.com/huggingface/transformers/blob/main/docs/source/en/tasks/monocular_depth_estimation.md"}}),{c(){m=i("meta"),j=l(),f=i("p"),D=l(),r(v.$$.fragment),et=l(),J=i("p"),J.textContent=Bt,st=l(),T=i("p"),T.textContent=Ht,at=l(),r(y.$$.fragment),lt=l(),U=i("p"),U.textContent=It,nt=l(),C=i("ul"),C.innerHTML=zt,it=l(),k=i("p"),k.textContent=Et,pt=l(),r(x.$$.fragment),ot=l(),r(Z.$$.fragment),mt=l(),W=i("p"),W.innerHTML=Vt,rt=l(),r(R.$$.fragment),ht=l(),_=i("p"),_.textContent=Nt,ct=l(),r(G.$$.fragment),dt=l(),w=i("div"),w.innerHTML=Xt,ut=l(),B=i("p"),B.textContent=Qt,gt=l(),r(H.$$.fragment),ft=l(),I=i("p"),I.innerHTML=Ft,Mt=l(),z=i("p"),z.textContent=Yt,yt=l(),r(E.$$.fragment),wt=l(),$=i("div"),$.innerHTML=St,$t=l(),r(V.$$.fragment),bt=l(),N=i("p"),N.textContent=Lt,jt=l(),X=i("p"),X.innerHTML=Pt,vt=l(),r(Q.$$.fragment),Jt=l(),F=i("p"),F.innerHTML=qt,Tt=l(),r(Y.$$.fragment),Ut=l(),S=i("p"),S.textContent=At,Ct=l(),r(L.$$.fragment),kt=l(),P=i("p"),P.textContent=Dt,xt=l(),r(q.$$.fragment),Zt=l(),b=i("div"),b.innerHTML=Kt,Wt=l(),r(A.$$.fragment),Rt=l(),K=i("p"),this.h()},l(t){const e=ie("svelte-u9bgzb",document.head);m=p(e,"META",{name:!0,content:!0}),e.forEach(s),j=n(t),f=p(t,"P",{}),te(f).forEach(s),D=n(t),h(v.$$.fragment,t),et=n(t),J=p(t,"P",{"data-svelte-h":!0}),o(J)!=="svelte-1elgt0a"&&(J.textContent=Bt),st=n(t),T=p(t,"P",{"data-svelte-h":!0}),o(T)!=="svelte-1bkqs5v"&&(T.textContent=Ht),at=n(t),h(y.$$.fragment,t),lt=n(t),U=p(t,"P",{"data-svelte-h":!0}),o(U)!=="svelte-jr2b5g"&&(U.textContent=It),nt=n(t),C=p(t,"UL",{"data-svelte-h":!0}),o(C)!=="svelte-tj6led"&&(C.innerHTML=zt),it=n(t),k=p(t,"P",{"data-svelte-h":!0}),o(k)!=="svelte-1c9nexd"&&(k.textContent=Et),pt=n(t),h(x.$$.fragment,t),ot=n(t),h(Z.$$.fragment,t),mt=n(t),W=p(t,"P",{"data-svelte-h":!0}),o(W)!=="svelte-13qxau"&&(W.innerHTML=Vt),rt=n(t),h(R.$$.fragment,t),ht=n(t),_=p(t,"P",{"data-svelte-h":!0}),o(_)!=="svelte-wuz5lr"&&(_.textContent=Nt),ct=n(t),h(G.$$.fragment,t),dt=n(t),w=p(t,"DIV",{class:!0,"data-svelte-h":!0}),o(w)!=="svelte-10bakl"&&(w.innerHTML=Xt),ut=n(t),B=p(t,"P",{"data-svelte-h":!0}),o(B)!=="svelte-mcr1tn"&&(B.textContent=Qt),gt=n(t),h(H.$$.fragment,t),ft=n(t),I=p(t,"P",{"data-svelte-h":!0}),o(I)!=="svelte-1jckqfu"&&(I.innerHTML=Ft),Mt=n(t),z=p(t,"P",{"data-svelte-h":!0}),o(z)!=="svelte-1dzpyfr"&&(z.textContent=Yt),yt=n(t),h(E.$$.fragment,t),wt=n(t),$=p(t,"DIV",{class:!0,"data-svelte-h":!0}),o($)!=="svelte-43wxxb"&&($.innerHTML=St),$t=n(t),h(V.$$.fragment,t),bt=n(t),N=p(t,"P",{"data-svelte-h":!0}),o(N)!=="svelte-1u79cc9"&&(N.textContent=Lt),jt=n(t),X=p(t,"P",{"data-svelte-h":!0}),o(X)!=="svelte-1r8mctn"&&(X.innerHTML=Pt),vt=n(t),h(Q.$$.fragment,t),Jt=n(t),F=p(t,"P",{"data-svelte-h":!0}),o(F)!=="svelte-hhrw3"&&(F.innerHTML=qt),Tt=n(t),h(Y.$$.fragment,t),Ut=n(t),S=p(t,"P",{"data-svelte-h":!0}),o(S)!=="svelte-1yk8q0z"&&(S.textContent=At),Ct=n(t),h(L.$$.fragment,t),kt=n(t),P=p(t,"P",{"data-svelte-h":!0}),o(P)!=="svelte-6ebm65"&&(P.textContent=Dt),xt=n(t),h(q.$$.fragment,t),Zt=n(t),b=p(t,"DIV",{class:!0,"data-svelte-h":!0}),o(b)!=="svelte-43wxxb"&&(b.innerHTML=Kt),Wt=n(t),h(A.$$.fragment,t),Rt=n(t),K=p(t,"P",{}),te(K).forEach(s),this.h()},h(){O(m,"name","hf:doc:metadata"),O(m,"content",ce),O(w,"class","flex justify-center"),O($,"class","flex justify-center"),O(b,"class","flex justify-center")},m(t,e){pe(document.head,m),a(t,j,e),a(t,f,e),a(t,D,e),c(v,t,e),a(t,et,e),a(t,J,e),a(t,st,e),a(t,T,e),a(t,at,e),c(y,t,e),a(t,lt,e),a(t,U,e),a(t,nt,e),a(t,C,e),a(t,it,e),a(t,k,e),a(t,pt,e),c(x,t,e),a(t,ot,e),c(Z,t,e),a(t,mt,e),a(t,W,e),a(t,rt,e),c(R,t,e),a(t,ht,e),a(t,_,e),a(t,ct,e),c(G,t,e),a(t,dt,e),a(t,w,e),a(t,ut,e),a(t,B,e),a(t,gt,e),c(H,t,e),a(t,ft,e),a(t,I,e),a(t,Mt,e),a(t,z,e),a(t,yt,e),c(E,t,e),a(t,wt,e),a(t,$,e),a(t,$t,e),c(V,t,e),a(t,bt,e),a(t,N,e),a(t,jt,e),a(t,X,e),a(t,vt,e),c(Q,t,e),a(t,Jt,e),a(t,F,e),a(t,Tt,e),c(Y,t,e),a(t,Ut,e),a(t,S,e),a(t,Ct,e),c(L,t,e),a(t,kt,e),a(t,P,e),a(t,xt,e),c(q,t,e),a(t,Zt,e),a(t,b,e),a(t,Wt,e),c(A,t,e),a(t,Rt,e),a(t,K,e),_t=!0},p(t,[e]){const Ot={};e&2&&(Ot.$$scope={dirty:e,ctx:t}),y.$set(Ot)},i(t){_t||(d(v.$$.fragment,t),d(y.$$.fragment,t),d(x.$$.fragment,t),d(Z.$$.fragment,t),d(R.$$.fragment,t),d(G.$$.fragment,t),d(H.$$.fragment,t),d(E.$$.fragment,t),d(V.$$.fragment,t),d(Q.$$.fragment,t),d(Y.$$.fragment,t),d(L.$$.fragment,t),d(q.$$.fragment,t),d(A.$$.fragment,t),_t=!0)},o(t){u(v.$$.fragment,t),u(y.$$.fragment,t),u(x.$$.fragment,t),u(Z.$$.fragment,t),u(R.$$.fragment,t),u(G.$$.fragment,t),u(H.$$.fragment,t),u(E.$$.fragment,t),u(V.$$.fragment,t),u(Q.$$.fragment,t),u(Y.$$.fragment,t),u(L.$$.fragment,t),u(q.$$.fragment,t),u(A.$$.fragment,t),_t=!1},d(t){t&&(s(j),s(f),s(D),s(et),s(J),s(st),s(T),s(at),s(lt),s(U),s(nt),s(C),s(it),s(k),s(pt),s(ot),s(mt),s(W),s(rt),s(ht),s(_),s(ct),s(dt),s(w),s(ut),s(B),s(gt),s(ft),s(I),s(Mt),s(z),s(yt),s(wt),s($),s($t),s(bt),s(N),s(jt),s(X),s(vt),s(Jt),s(F),s(Tt),s(Ut),s(S),s(Ct),s(kt),s(P),s(xt),s(Zt),s(b),s(Wt),s(Rt),s(K)),s(m),g(v,t),g(y,t),g(x,t),g(Z,t),g(R,t),g(G,t),g(H,t),g(E,t),g(V,t),g(Q,t),g(Y,t),g(L,t),g(q,t),g(A,t)}}}const ce='{"title":"Monocular depth estimation","local":"monocular-depth-estimation","sections":[{"title":"Depth estimation pipeline","local":"depth-estimation-pipeline","sections":[],"depth":2},{"title":"Depth estimation inference by hand","local":"depth-estimation-inference-by-hand","sections":[],"depth":2}],"depth":1}';function de(tt){return se(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class we extends le{constructor(m){super(),ne(this,m,de,he,ee,{})}}export{we as component};
