import{S as Nl,i as Bl,s as ql,e as n,k as p,w as u,t as m,M as Xl,c as o,d as e,m as r,a as i,x as d,h,b as c,N as Y,G as a,g as l,y as g,L as Gl,q as y,o as b,B as j,v as Yl}from"../../chunks/vendor-hf-doc-builder.js";import{I as me}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as f}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Hl}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function Sl(Va){let U,he,x,k,Ds,H,_t,Ps,It,ue,S,de,W,kt,Js,Wt,Ct,ge,C,Et,Us,zt,Zt,ye,E,Vt,xs,$t,Rt,be,Ts,Qt,je,vs,Nt,fe,M,Ks,Bt,qt,Os,Xt,Gt,se,Yt,Me,_s,Ht,we,A,Je,T,z,ee,F,St,te,At,Ue,w,Ft,Is,Lt,Dt,L,Pt,Kt,xe,D,Te,Z,Ot,P,sa,ea,ve,K,_e,O,ks,$a,Ie,Ws,ta,ke,ss,We,Cs,aa,Ce,es,Ee,ts,Es,Ra,ze,v,V,ae,as,la,le,na,Ze,zs,oa,Ve,$,ia,ls,pa,ra,$e,ns,Re,Zs,ca,Qe,os,Ne,is,Vs,Qa,Be,R,ma,$s,ha,ua,qe,ps,Xe,Q,da,Rs,ga,ya,Ge,rs,Ye,cs,Qs,Na,He,_,N,ne,ms,ba,oe,ja,Se,Ns,fa,Ae,hs,Fe,B,Ma,ie,wa,Ja,Le,us,De,ds,Bs,Ba,Pe,I,q,pe,gs,Ua,re,xa,Ke,qs,Ta,Oe,Xs,va,st,ys,et,Gs,_a,tt,bs,at,js,Ys,qa,lt,X,Ia,ce,ka,Wa,nt,fs,ot,G,Ca,Hs,Ea,za,it,Ms,pt,ws,Ss,Xa,rt,As,Za,ct,J,Ga,mt;return H=new me({}),S=new Hl({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/zero_shot_object_detection.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/zero_shot_object_detection.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/zero_shot_object_detection.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/zero_shot_object_detection.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/zero_shot_object_detection.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/zero_shot_object_detection.ipynb"}]}}),A=new f({props:{code:"cGlwJTIwaW5zdGFsbCUyMC1xJTIwdHJhbnNmb3JtZXJz",highlighted:"pip install -q transformers"}}),F=new me({}),D=new f({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBY2hlY2twb2ludCUyMCUzRCUyMCUyMmdvb2dsZSUyRm93bHZpdC1iYXNlLXBhdGNoMzIlMjIlMEFkZXRlY3RvciUyMCUzRCUyMHBpcGVsaW5lKG1vZGVsJTNEY2hlY2twb2ludCUyQyUyMHRhc2slM0QlMjJ6ZXJvLXNob3Qtb2JqZWN0LWRldGVjdGlvbiUyMik=",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;google/owlvit-base-patch32&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>detector = pipeline(model=checkpoint, task=<span class="hljs-string">&quot;zero-shot-object-detection&quot;</span>)`}}),K=new f({props:{code:"aW1wb3J0JTIwc2tpbWFnZSUwQWltcG9ydCUyMG51bXB5JTIwYXMlMjBucCUwQWZyb20lMjBQSUwlMjBpbXBvcnQlMjBJbWFnZSUwQSUwQWltYWdlJTIwJTNEJTIwc2tpbWFnZS5kYXRhLmFzdHJvbmF1dCgpJTBBaW1hZ2UlMjAlM0QlMjBJbWFnZS5mcm9tYXJyYXkobnAudWludDgoaW1hZ2UpKS5jb252ZXJ0KCUyMlJHQiUyMiklMEElMEFpbWFnZQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> skimage
<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-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image

<span class="hljs-meta">&gt;&gt;&gt; </span>image = skimage.data.astronaut()
<span class="hljs-meta">&gt;&gt;&gt; </span>image = Image.fromarray(np.uint8(image)).convert(<span class="hljs-string">&quot;RGB&quot;</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>image`}}),ss=new f({props:{code:"cHJlZGljdGlvbnMlMjAlM0QlMjBkZXRlY3RvciglMEElMjAlMjAlMjAlMjBpbWFnZSUyQyUwQSUyMCUyMCUyMCUyMGNhbmRpZGF0ZV9sYWJlbHMlM0QlNUIlMjJodW1hbiUyMGZhY2UlMjIlMkMlMjAlMjJyb2NrZXQlMjIlMkMlMjAlMjJuYXNhJTIwYmFkZ2UlMjIlMkMlMjAlMjJzdGFyLXNwYW5nbGVkJTIwYmFubmVyJTIyJTVEJTJDJTBBKSUwQXByZWRpY3Rpb25z",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>predictions = detector(
<span class="hljs-meta">... </span>    image,
<span class="hljs-meta">... </span>    candidate_labels=[<span class="hljs-string">&quot;human face&quot;</span>, <span class="hljs-string">&quot;rocket&quot;</span>, <span class="hljs-string">&quot;nasa badge&quot;</span>, <span class="hljs-string">&quot;star-spangled banner&quot;</span>],
<span class="hljs-meta">... </span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>predictions
[{<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.3571370542049408</span>,
  <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;human face&#x27;</span>,
  <span class="hljs-string">&#x27;box&#x27;</span>: {<span class="hljs-string">&#x27;xmin&#x27;</span>: <span class="hljs-number">180</span>, <span class="hljs-string">&#x27;ymin&#x27;</span>: <span class="hljs-number">71</span>, <span class="hljs-string">&#x27;xmax&#x27;</span>: <span class="hljs-number">271</span>, <span class="hljs-string">&#x27;ymax&#x27;</span>: <span class="hljs-number">178</span>}},
 {<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.28099656105041504</span>,
  <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;nasa badge&#x27;</span>,
  <span class="hljs-string">&#x27;box&#x27;</span>: {<span class="hljs-string">&#x27;xmin&#x27;</span>: <span class="hljs-number">129</span>, <span class="hljs-string">&#x27;ymin&#x27;</span>: <span class="hljs-number">348</span>, <span class="hljs-string">&#x27;xmax&#x27;</span>: <span class="hljs-number">206</span>, <span class="hljs-string">&#x27;ymax&#x27;</span>: <span class="hljs-number">427</span>}},
 {<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.2110239565372467</span>,
  <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;rocket&#x27;</span>,
  <span class="hljs-string">&#x27;box&#x27;</span>: {<span class="hljs-string">&#x27;xmin&#x27;</span>: <span class="hljs-number">350</span>, <span class="hljs-string">&#x27;ymin&#x27;</span>: -<span class="hljs-number">1</span>, <span class="hljs-string">&#x27;xmax&#x27;</span>: <span class="hljs-number">468</span>, <span class="hljs-string">&#x27;ymax&#x27;</span>: <span class="hljs-number">288</span>}},
 {<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.13790413737297058</span>,
  <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;star-spangled banner&#x27;</span>,
  <span class="hljs-string">&#x27;box&#x27;</span>: {<span class="hljs-string">&#x27;xmin&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;ymin&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;xmax&#x27;</span>: <span class="hljs-number">105</span>, <span class="hljs-string">&#x27;ymax&#x27;</span>: <span class="hljs-number">509</span>}},
 {<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.11950037628412247</span>,
  <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;nasa badge&#x27;</span>,
  <span class="hljs-string">&#x27;box&#x27;</span>: {<span class="hljs-string">&#x27;xmin&#x27;</span>: <span class="hljs-number">277</span>, <span class="hljs-string">&#x27;ymin&#x27;</span>: <span class="hljs-number">338</span>, <span class="hljs-string">&#x27;xmax&#x27;</span>: <span class="hljs-number">327</span>, <span class="hljs-string">&#x27;ymax&#x27;</span>: <span class="hljs-number">380</span>}},
 {<span class="hljs-string">&#x27;score&#x27;</span>: <span class="hljs-number">0.10649408400058746</span>,
  <span class="hljs-string">&#x27;label&#x27;</span>: <span class="hljs-string">&#x27;rocket&#x27;</span>,
  <span class="hljs-string">&#x27;box&#x27;</span>: {<span class="hljs-string">&#x27;xmin&#x27;</span>: <span class="hljs-number">358</span>, <span class="hljs-string">&#x27;ymin&#x27;</span>: <span class="hljs-number">64</span>, <span class="hljs-string">&#x27;xmax&#x27;</span>: <span class="hljs-number">424</span>, <span class="hljs-string">&#x27;ymax&#x27;</span>: <span class="hljs-number">280</span>}}]`}}),es=new f({props:{code:"ZnJvbSUyMFBJTCUyMGltcG9ydCUyMEltYWdlRHJhdyUwQSUwQWRyYXclMjAlM0QlMjBJbWFnZURyYXcuRHJhdyhpbWFnZSklMEElMEFmb3IlMjBwcmVkaWN0aW9uJTIwaW4lMjBwcmVkaWN0aW9ucyUzQSUwQSUyMCUyMCUyMCUyMGJveCUyMCUzRCUyMHByZWRpY3Rpb24lNUIlMjJib3glMjIlNUQlMEElMjAlMjAlMjAlMjBsYWJlbCUyMCUzRCUyMHByZWRpY3Rpb24lNUIlMjJsYWJlbCUyMiU1RCUwQSUyMCUyMCUyMCUyMHNjb3JlJTIwJTNEJTIwcHJlZGljdGlvbiU1QiUyMnNjb3JlJTIyJTVEJTBBJTBBJTIwJTIwJTIwJTIweG1pbiUyQyUyMHltaW4lMkMlMjB4bWF4JTJDJTIweW1heCUyMCUzRCUyMGJveC52YWx1ZXMoKSUwQSUyMCUyMCUyMCUyMGRyYXcucmVjdGFuZ2xlKCh4bWluJTJDJTIweW1pbiUyQyUyMHhtYXglMkMlMjB5bWF4KSUyQyUyMG91dGxpbmUlM0QlMjJyZWQlMjIlMkMlMjB3aWR0aCUzRDEpJTBBJTIwJTIwJTIwJTIwZHJhdy50ZXh0KCh4bWluJTJDJTIweW1pbiklMkMlMjBmJTIyJTdCbGFiZWwlN0QlM0ElMjAlN0Jyb3VuZChzY29yZSUyQzIpJTdEJTIyJTJDJTIwZmlsbCUzRCUyMndoaXRlJTIyKSUwQSUwQWltYWdl",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> ImageDraw

<span class="hljs-meta">&gt;&gt;&gt; </span>draw = ImageDraw.Draw(image)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> prediction <span class="hljs-keyword">in</span> predictions:
<span class="hljs-meta">... </span>    box = prediction[<span class="hljs-string">&quot;box&quot;</span>]
<span class="hljs-meta">... </span>    label = prediction[<span class="hljs-string">&quot;label&quot;</span>]
<span class="hljs-meta">... </span>    score = prediction[<span class="hljs-string">&quot;score&quot;</span>]

<span class="hljs-meta">... </span>    xmin, ymin, xmax, ymax = box.values()
<span class="hljs-meta">... </span>    draw.rectangle((xmin, ymin, xmax, ymax), outline=<span class="hljs-string">&quot;red&quot;</span>, width=<span class="hljs-number">1</span>)
<span class="hljs-meta">... </span>    draw.text((xmin, ymin), <span class="hljs-string">f&quot;<span class="hljs-subst">{label}</span>: <span class="hljs-subst">{<span class="hljs-built_in">round</span>(score,<span class="hljs-number">2</span>)}</span>&quot;</span>, fill=<span class="hljs-string">&quot;white&quot;</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>image`}}),as=new me({}),ns=new f({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Qcm9jZXNzb3IlMkMlMjBBdXRvTW9kZWxGb3JaZXJvU2hvdE9iamVjdERldGVjdGlvbiUwQSUwQW1vZGVsJTIwJTNEJTIwQXV0b01vZGVsRm9yWmVyb1Nob3RPYmplY3REZXRlY3Rpb24uZnJvbV9wcmV0cmFpbmVkKGNoZWNrcG9pbnQpJTBBcHJvY2Vzc29yJTIwJTNEJTIwQXV0b1Byb2Nlc3Nvci5mcm9tX3ByZXRyYWluZWQoY2hlY2twb2ludCk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoProcessor, AutoModelForZeroShotObjectDetection

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForZeroShotObjectDetection.from_pretrained(checkpoint)
<span class="hljs-meta">&gt;&gt;&gt; </span>processor = AutoProcessor.from_pretrained(checkpoint)`}}),os=new f({props:{code:"aW1wb3J0JTIwcmVxdWVzdHMlMEElMEF1cmwlMjAlM0QlMjAlMjJodHRwcyUzQSUyRiUyRnVuc3BsYXNoLmNvbSUyRnBob3RvcyUyRm9qMHplWTJMdGs0JTJGZG93bmxvYWQlM0ZpeGlkJTNETW53eE1qQTNmREI4TVh4elpXRnlZMmg4TVRSOGZIQnBZMjVwWTN4bGJud3dmSHg4ZkRFMk56YzBPVEUxTkRrJTI2Zm9yY2UlM0R0cnVlJTI2dyUzRDY0MCUyMiUwQWltJTIwJTNEJTIwSW1hZ2Uub3BlbihyZXF1ZXN0cy5nZXQodXJsJTJDJTIwc3RyZWFtJTNEVHJ1ZSkucmF3KSUwQWlt",highlighted:`<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/oj0zeY2Ltk4/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MTR8fHBpY25pY3xlbnwwfHx8fDE2Nzc0OTE1NDk&amp;force=true&amp;w=640&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>im = 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>im`}}),ps=new f({props:{code:"dGV4dF9xdWVyaWVzJTIwJTNEJTIwJTVCJTIyaGF0JTIyJTJDJTIwJTIyYm9vayUyMiUyQyUyMCUyMnN1bmdsYXNzZXMlMjIlMkMlMjAlMjJjYW1lcmElMjIlNUQlMEFpbnB1dHMlMjAlM0QlMjBwcm9jZXNzb3IodGV4dCUzRHRleHRfcXVlcmllcyUyQyUyMGltYWdlcyUzRGltJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMik=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>text_queries = [<span class="hljs-string">&quot;hat&quot;</span>, <span class="hljs-string">&quot;book&quot;</span>, <span class="hljs-string">&quot;sunglasses&quot;</span>, <span class="hljs-string">&quot;camera&quot;</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>inputs = processor(text=text_queries, images=im, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)`}}),rs=new f({props:{code:"aW1wb3J0JTIwdG9yY2glMEElMEF3aXRoJTIwdG9yY2gubm9fZ3JhZCgpJTNBJTBBJTIwJTIwJTIwJTIwb3V0cHV0cyUyMCUzRCUyMG1vZGVsKCoqaW5wdXRzKSUwQSUyMCUyMCUyMCUyMHRhcmdldF9zaXplcyUyMCUzRCUyMHRvcmNoLnRlbnNvciglNUJpbS5zaXplJTVCJTNBJTNBLTElNUQlNUQpJTBBJTIwJTIwJTIwJTIwcmVzdWx0cyUyMCUzRCUyMHByb2Nlc3Nvci5wb3N0X3Byb2Nlc3Nfb2JqZWN0X2RldGVjdGlvbihvdXRwdXRzJTJDJTIwdGhyZXNob2xkJTNEMC4xJTJDJTIwdGFyZ2V0X3NpemVzJTNEdGFyZ2V0X3NpemVzKSU1QjAlNUQlMEElMEFkcmF3JTIwJTNEJTIwSW1hZ2VEcmF3LkRyYXcoaW0pJTBBJTBBc2NvcmVzJTIwJTNEJTIwcmVzdWx0cyU1QiUyMnNjb3JlcyUyMiU1RC50b2xpc3QoKSUwQWxhYmVscyUyMCUzRCUyMHJlc3VsdHMlNUIlMjJsYWJlbHMlMjIlNUQudG9saXN0KCklMEFib3hlcyUyMCUzRCUyMHJlc3VsdHMlNUIlMjJib3hlcyUyMiU1RC50b2xpc3QoKSUwQSUwQWZvciUyMGJveCUyQyUyMHNjb3JlJTJDJTIwbGFiZWwlMjBpbiUyMHppcChib3hlcyUyQyUyMHNjb3JlcyUyQyUyMGxhYmVscyklM0ElMEElMjAlMjAlMjAlMjB4bWluJTJDJTIweW1pbiUyQyUyMHhtYXglMkMlMjB5bWF4JTIwJTNEJTIwYm94JTBBJTIwJTIwJTIwJTIwZHJhdy5yZWN0YW5nbGUoKHhtaW4lMkMlMjB5bWluJTJDJTIweG1heCUyQyUyMHltYXgpJTJDJTIwb3V0bGluZSUzRCUyMnJlZCUyMiUyQyUyMHdpZHRoJTNEMSklMEElMjAlMjAlMjAlMjBkcmF3LnRleHQoKHhtaW4lMkMlMjB5bWluKSUyQyUyMGYlMjIlN0J0ZXh0X3F1ZXJpZXMlNUJsYWJlbCU1RCU3RCUzQSUyMCU3QnJvdW5kKHNjb3JlJTJDMiklN0QlMjIlMkMlMjBmaWxsJTNEJTIyd2hpdGUlMjIpJTBBJTBBaW0=",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(**inputs)
<span class="hljs-meta">... </span>    target_sizes = torch.tensor([im.size[::-<span class="hljs-number">1</span>]])
<span class="hljs-meta">... </span>    results = processor.post_process_object_detection(outputs, threshold=<span class="hljs-number">0.1</span>, target_sizes=target_sizes)[<span class="hljs-number">0</span>]

<span class="hljs-meta">&gt;&gt;&gt; </span>draw = ImageDraw.Draw(im)

<span class="hljs-meta">&gt;&gt;&gt; </span>scores = results[<span class="hljs-string">&quot;scores&quot;</span>].tolist()
<span class="hljs-meta">&gt;&gt;&gt; </span>labels = results[<span class="hljs-string">&quot;labels&quot;</span>].tolist()
<span class="hljs-meta">&gt;&gt;&gt; </span>boxes = results[<span class="hljs-string">&quot;boxes&quot;</span>].tolist()

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> box, score, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(boxes, scores, labels):
<span class="hljs-meta">... </span>    xmin, ymin, xmax, ymax = box
<span class="hljs-meta">... </span>    draw.rectangle((xmin, ymin, xmax, ymax), outline=<span class="hljs-string">&quot;red&quot;</span>, width=<span class="hljs-number">1</span>)
<span class="hljs-meta">... </span>    draw.text((xmin, ymin), <span class="hljs-string">f&quot;<span class="hljs-subst">{text_queries[label]}</span>: <span class="hljs-subst">{<span class="hljs-built_in">round</span>(score,<span class="hljs-number">2</span>)}</span>&quot;</span>, fill=<span class="hljs-string">&quot;white&quot;</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>im`}}),ms=new me({}),hs=new f({props:{code:"aW1hZ2VzJTIwJTNEJTIwJTVCaW1hZ2UlMkMlMjBpbSU1RCUwQXRleHRfcXVlcmllcyUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMCU1QiUyMmh1bWFuJTIwZmFjZSUyMiUyQyUyMCUyMnJvY2tldCUyMiUyQyUyMCUyMm5hc2ElMjBiYWRnZSUyMiUyQyUyMCUyMnN0YXItc3BhbmdsZWQlMjBiYW5uZXIlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjAlNUIlMjJoYXQlMjIlMkMlMjAlMjJib29rJTIyJTJDJTIwJTIyc3VuZ2xhc3NlcyUyMiUyQyUyMCUyMmNhbWVyYSUyMiU1RCUyQyUwQSU1RCUwQWlucHV0cyUyMCUzRCUyMHByb2Nlc3Nvcih0ZXh0JTNEdGV4dF9xdWVyaWVzJTJDJTIwaW1hZ2VzJTNEaW1hZ2VzJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMik=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>images = [image, im]
<span class="hljs-meta">&gt;&gt;&gt; </span>text_queries = [
<span class="hljs-meta">... </span>    [<span class="hljs-string">&quot;human face&quot;</span>, <span class="hljs-string">&quot;rocket&quot;</span>, <span class="hljs-string">&quot;nasa badge&quot;</span>, <span class="hljs-string">&quot;star-spangled banner&quot;</span>],
<span class="hljs-meta">... </span>    [<span class="hljs-string">&quot;hat&quot;</span>, <span class="hljs-string">&quot;book&quot;</span>, <span class="hljs-string">&quot;sunglasses&quot;</span>, <span class="hljs-string">&quot;camera&quot;</span>],
<span class="hljs-meta">... </span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>inputs = processor(text=text_queries, images=images, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)`}}),us=new f({props:{code:"d2l0aCUyMHRvcmNoLm5vX2dyYWQoKSUzQSUwQSUyMCUyMCUyMCUyMG91dHB1dHMlMjAlM0QlMjBtb2RlbCgqKmlucHV0cyklMEElMjAlMjAlMjAlMjB0YXJnZXRfc2l6ZXMlMjAlM0QlMjAlNUJ4LnNpemUlNUIlM0ElM0EtMSU1RCUyMGZvciUyMHglMjBpbiUyMGltYWdlcyU1RCUwQSUyMCUyMCUyMCUyMHJlc3VsdHMlMjAlM0QlMjBwcm9jZXNzb3IucG9zdF9wcm9jZXNzX29iamVjdF9kZXRlY3Rpb24ob3V0cHV0cyUyQyUyMHRocmVzaG9sZCUzRDAuMSUyQyUyMHRhcmdldF9zaXplcyUzRHRhcmdldF9zaXplcyklMEElMEFpbWFnZV9pZHglMjAlM0QlMjAxJTBBZHJhdyUyMCUzRCUyMEltYWdlRHJhdy5EcmF3KGltYWdlcyU1QmltYWdlX2lkeCU1RCklMEElMEFzY29yZXMlMjAlM0QlMjByZXN1bHRzJTVCaW1hZ2VfaWR4JTVEJTVCJTIyc2NvcmVzJTIyJTVELnRvbGlzdCgpJTBBbGFiZWxzJTIwJTNEJTIwcmVzdWx0cyU1QmltYWdlX2lkeCU1RCU1QiUyMmxhYmVscyUyMiU1RC50b2xpc3QoKSUwQWJveGVzJTIwJTNEJTIwcmVzdWx0cyU1QmltYWdlX2lkeCU1RCU1QiUyMmJveGVzJTIyJTVELnRvbGlzdCgpJTBBJTBBZm9yJTIwYm94JTJDJTIwc2NvcmUlMkMlMjBsYWJlbCUyMGluJTIwemlwKGJveGVzJTJDJTIwc2NvcmVzJTJDJTIwbGFiZWxzKSUzQSUwQSUyMCUyMCUyMCUyMHhtaW4lMkMlMjB5bWluJTJDJTIweG1heCUyQyUyMHltYXglMjAlM0QlMjBib3glMEElMjAlMjAlMjAlMjBkcmF3LnJlY3RhbmdsZSgoeG1pbiUyQyUyMHltaW4lMkMlMjB4bWF4JTJDJTIweW1heCklMkMlMjBvdXRsaW5lJTNEJTIycmVkJTIyJTJDJTIwd2lkdGglM0QxKSUwQSUyMCUyMCUyMCUyMGRyYXcudGV4dCgoeG1pbiUyQyUyMHltaW4pJTJDJTIwZiUyMiU3QnRleHRfcXVlcmllcyU1QmltYWdlX2lkeCU1RCU1QmxhYmVsJTVEJTdEJTNBJTIwJTdCcm91bmQoc2NvcmUlMkMyKSU3RCUyMiUyQyUyMGZpbGwlM0QlMjJ3aGl0ZSUyMiklMEElMEFpbWFnZXMlNUJpbWFnZV9pZHglNUQ=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">with</span> torch.no_grad():
<span class="hljs-meta">... </span>    outputs = model(**inputs)
<span class="hljs-meta">... </span>    target_sizes = [x.size[::-<span class="hljs-number">1</span>] <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> images]
<span class="hljs-meta">... </span>    results = processor.post_process_object_detection(outputs, threshold=<span class="hljs-number">0.1</span>, target_sizes=target_sizes)

<span class="hljs-meta">&gt;&gt;&gt; </span>image_idx = <span class="hljs-number">1</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>draw = ImageDraw.Draw(images[image_idx])

<span class="hljs-meta">&gt;&gt;&gt; </span>scores = results[image_idx][<span class="hljs-string">&quot;scores&quot;</span>].tolist()
<span class="hljs-meta">&gt;&gt;&gt; </span>labels = results[image_idx][<span class="hljs-string">&quot;labels&quot;</span>].tolist()
<span class="hljs-meta">&gt;&gt;&gt; </span>boxes = results[image_idx][<span class="hljs-string">&quot;boxes&quot;</span>].tolist()

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> box, score, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(boxes, scores, labels):
<span class="hljs-meta">... </span>    xmin, ymin, xmax, ymax = box
<span class="hljs-meta">... </span>    draw.rectangle((xmin, ymin, xmax, ymax), outline=<span class="hljs-string">&quot;red&quot;</span>, width=<span class="hljs-number">1</span>)
<span class="hljs-meta">... </span>    draw.text((xmin, ymin), <span class="hljs-string">f&quot;<span class="hljs-subst">{text_queries[image_idx][label]}</span>: <span class="hljs-subst">{<span class="hljs-built_in">round</span>(score,<span class="hljs-number">2</span>)}</span>&quot;</span>, fill=<span class="hljs-string">&quot;white&quot;</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>images[image_idx]`}}),gs=new me({}),ys=new f({props:{code:"dXJsJTIwJTNEJTIwJTIyaHR0cCUzQSUyRiUyRmltYWdlcy5jb2NvZGF0YXNldC5vcmclMkZ2YWwyMDE3JTJGMDAwMDAwMDM5NzY5LmpwZyUyMiUwQWltYWdlX3RhcmdldCUyMCUzRCUyMEltYWdlLm9wZW4ocmVxdWVzdHMuZ2V0KHVybCUyQyUyMHN0cmVhbSUzRFRydWUpLnJhdyklMEElMEFxdWVyeV91cmwlMjAlM0QlMjAlMjJodHRwJTNBJTJGJTJGaW1hZ2VzLmNvY29kYXRhc2V0Lm9yZyUyRnZhbDIwMTclMkYwMDAwMDA1MjQyODAuanBnJTIyJTBBcXVlcnlfaW1hZ2UlMjAlM0QlMjBJbWFnZS5vcGVuKHJlcXVlc3RzLmdldChxdWVyeV91cmwlMkMlMjBzdHJlYW0lM0RUcnVlKS5yYXcp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>url = <span class="hljs-string">&quot;http://images.cocodataset.org/val2017/000000039769.jpg&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>image_target = 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>query_url = <span class="hljs-string">&quot;http://images.cocodataset.org/val2017/000000524280.jpg&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>query_image = Image.<span class="hljs-built_in">open</span>(requests.get(query_url, stream=<span class="hljs-literal">True</span>).raw)`}}),bs=new f({props:{code:"aW1wb3J0JTIwbWF0cGxvdGxpYi5weXBsb3QlMjBhcyUyMHBsdCUwQSUwQWZpZyUyQyUyMGF4JTIwJTNEJTIwcGx0LnN1YnBsb3RzKDElMkMlMjAyKSUwQWF4JTVCMCU1RC5pbXNob3coaW1hZ2VfdGFyZ2V0KSUwQWF4JTVCMSU1RC5pbXNob3cocXVlcnlfaW1hZ2Up",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt

<span class="hljs-meta">&gt;&gt;&gt; </span>fig, ax = plt.subplots(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>ax[<span class="hljs-number">0</span>].imshow(image_target)
<span class="hljs-meta">&gt;&gt;&gt; </span>ax[<span class="hljs-number">1</span>].imshow(query_image)`}}),fs=new f({props:{code:"aW5wdXRzJTIwJTNEJTIwcHJvY2Vzc29yKGltYWdlcyUzRGltYWdlX3RhcmdldCUyQyUyMHF1ZXJ5X2ltYWdlcyUzRHF1ZXJ5X2ltYWdlJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMik=",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span>inputs = processor(images=image_target, query_images=query_image, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)'}}),Ms=new f({props:{code:"d2l0aCUyMHRvcmNoLm5vX2dyYWQoKSUzQSUwQSUyMCUyMCUyMCUyMG91dHB1dHMlMjAlM0QlMjBtb2RlbC5pbWFnZV9ndWlkZWRfZGV0ZWN0aW9uKCoqaW5wdXRzKSUwQSUyMCUyMCUyMCUyMHRhcmdldF9zaXplcyUyMCUzRCUyMHRvcmNoLnRlbnNvciglNUJpbWFnZV90YXJnZXQuc2l6ZSU1QiUzQSUzQS0xJTVEJTVEKSUwQSUyMCUyMCUyMCUyMHJlc3VsdHMlMjAlM0QlMjBwcm9jZXNzb3IucG9zdF9wcm9jZXNzX2ltYWdlX2d1aWRlZF9kZXRlY3Rpb24ob3V0cHV0cyUzRG91dHB1dHMlMkMlMjB0YXJnZXRfc2l6ZXMlM0R0YXJnZXRfc2l6ZXMpJTVCMCU1RCUwQSUwQWRyYXclMjAlM0QlMjBJbWFnZURyYXcuRHJhdyhpbWFnZV90YXJnZXQpJTBBJTBBc2NvcmVzJTIwJTNEJTIwcmVzdWx0cyU1QiUyMnNjb3JlcyUyMiU1RC50b2xpc3QoKSUwQWJveGVzJTIwJTNEJTIwcmVzdWx0cyU1QiUyMmJveGVzJTIyJTVELnRvbGlzdCgpJTBBJTBBZm9yJTIwYm94JTJDJTIwc2NvcmUlMkMlMjBsYWJlbCUyMGluJTIwemlwKGJveGVzJTJDJTIwc2NvcmVzJTJDJTIwbGFiZWxzKSUzQSUwQSUyMCUyMCUyMCUyMHhtaW4lMkMlMjB5bWluJTJDJTIweG1heCUyQyUyMHltYXglMjAlM0QlMjBib3glMEElMjAlMjAlMjAlMjBkcmF3LnJlY3RhbmdsZSgoeG1pbiUyQyUyMHltaW4lMkMlMjB4bWF4JTJDJTIweW1heCklMkMlMjBvdXRsaW5lJTNEJTIyd2hpdGUlMjIlMkMlMjB3aWR0aCUzRDQpJTBBJTBBaW1hZ2VfdGFyZ2V0",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">with</span> torch.no_grad():
<span class="hljs-meta">... </span>    outputs = model.image_guided_detection(**inputs)
<span class="hljs-meta">... </span>    target_sizes = torch.tensor([image_target.size[::-<span class="hljs-number">1</span>]])
<span class="hljs-meta">... </span>    results = processor.post_process_image_guided_detection(outputs=outputs, target_sizes=target_sizes)[<span class="hljs-number">0</span>]

<span class="hljs-meta">&gt;&gt;&gt; </span>draw = ImageDraw.Draw(image_target)

<span class="hljs-meta">&gt;&gt;&gt; </span>scores = results[<span class="hljs-string">&quot;scores&quot;</span>].tolist()
<span class="hljs-meta">&gt;&gt;&gt; </span>boxes = results[<span class="hljs-string">&quot;boxes&quot;</span>].tolist()

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> box, score, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(boxes, scores, labels):
<span class="hljs-meta">... </span>    xmin, ymin, xmax, ymax = box
<span class="hljs-meta">... </span>    draw.rectangle((xmin, ymin, xmax, ymax), outline=<span class="hljs-string">&quot;white&quot;</span>, width=<span class="hljs-number">4</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>image_target`}}),{c(){U=n("meta"),he=p(),x=n("h1"),k=n("a"),Ds=n("span"),u(H.$$.fragment),_t=p(),Ps=n("span"),It=m("Zero-shot object detection"),ue=p(),u(S.$$.fragment),de=p(),W=n("p"),kt=m("Traditionally, models used for "),Js=n("a"),Wt=m("object detection"),Ct=m(` require labeled image datasets for training,
and are limited to detecting the set of classes from the training data.`),ge=p(),C=n("p"),Et=m("Zero-shot object detection is supported by the "),Us=n("a"),zt=m("OWL-ViT"),Zt=m(` model which uses a different approach. OWL-ViT
is an open-vocabulary object detector. It means that it can detect objects in images based on free-text queries without
the need to fine-tune the model on labeled datasets.`),ye=p(),E=n("p"),Vt=m("OWL-ViT leverages multi-modal representations to perform open-vocabulary detection. It combines "),xs=n("a"),$t=m("CLIP"),Rt=m(` with
lightweight object classification and localization heads. Open-vocabulary detection is achieved by embedding free-text queries with the text encoder of CLIP and using them as input to the object classification and localization heads.
associate images and their corresponding textual descriptions, and ViT processes image patches as inputs. The authors
of OWL-ViT first trained CLIP from scratch and then fine-tuned OWL-ViT end to end on standard object detection datasets using
a bipartite matching loss.`),be=p(),Ts=n("p"),Qt=m("With this approach, the model can detect objects based on textual descriptions without prior training on labeled datasets."),je=p(),vs=n("p"),Nt=m("In this guide, you will learn how to use OWL-ViT:"),fe=p(),M=n("ul"),Ks=n("li"),Bt=m("to detect objects based on text prompts"),qt=p(),Os=n("li"),Xt=m("for batch object detection"),Gt=p(),se=n("li"),Yt=m("for image-guided object detection"),Me=p(),_s=n("p"),Ht=m("Before you begin, make sure you have all the necessary libraries installed:"),we=p(),u(A.$$.fragment),Je=p(),T=n("h2"),z=n("a"),ee=n("span"),u(F.$$.fragment),St=p(),te=n("span"),At=m("Zero-shot object detection pipeline"),Ue=p(),w=n("p"),Ft=m("The simplest way to try out inference with OWL-ViT is to use it in a "),Is=n("a"),Lt=m("pipeline()"),Dt=m(`. Instantiate a pipeline
for zero-shot object detection from a `),L=n("a"),Pt=m("checkpoint on the Hugging Face Hub"),Kt=m(":"),xe=p(),u(D.$$.fragment),Te=p(),Z=n("p"),Ot=m(`Next, choose an image you\u2019d like to detect objects in. Here we\u2019ll use the image of astronaut Eileen Collins that is
a part of the `),P=n("a"),sa=m("NASA"),ea=m(" Great Images dataset."),ve=p(),u(K.$$.fragment),_e=p(),O=n("div"),ks=n("img"),Ie=p(),Ws=n("p"),ta=m(`Pass the image and the candidate object labels to look for to the pipeline.
Here we pass the image directly; other suitable options include a local path to an image or an image url. We also pass text descriptions for all items we want to query the image for.`),ke=p(),u(ss.$$.fragment),We=p(),Cs=n("p"),aa=m("Let\u2019s visualize the predictions:"),Ce=p(),u(es.$$.fragment),Ee=p(),ts=n("div"),Es=n("img"),ze=p(),v=n("h2"),V=n("a"),ae=n("span"),u(as.$$.fragment),la=p(),le=n("span"),na=m("Text-prompted zero-shot object detection by hand"),Ze=p(),zs=n("p"),oa=m(`Now that you\u2019ve seen how to use the zero-shot object detection pipeline, let\u2019s replicate the same
result manually.`),Ve=p(),$=n("p"),ia=m("Start by loading the model and associated processor from a "),ls=n("a"),pa=m("checkpoint on the Hugging Face Hub"),ra=m(`.
Here we\u2019ll use the same checkpoint as before:`),$e=p(),u(ns.$$.fragment),Re=p(),Zs=n("p"),ca=m("Let\u2019s take a different image to switch things up."),Qe=p(),u(os.$$.fragment),Ne=p(),is=n("div"),Vs=n("img"),Be=p(),R=n("p"),ma=m(`Use the processor to prepare the inputs for the model. The processor combines an image processor that prepares the
image for the model by resizing and normalizing it, and a `),$s=n("a"),ha=m("CLIPTokenizer"),ua=m(" that takes care of the text inputs."),qe=p(),u(ps.$$.fragment),Xe=p(),Q=n("p"),da=m(`Pass the inputs through the model, post-process, and visualize the results. Since the image processor resized images before
feeding them to the model, you need to use the `),Rs=n("a"),ga=m("post_process_object_detection()"),ya=m(` method to make sure the predicted bounding
boxes have the correct coordinates relative to the original image:`),Ge=p(),u(rs.$$.fragment),Ye=p(),cs=n("div"),Qs=n("img"),He=p(),_=n("h2"),N=n("a"),ne=n("span"),u(ms.$$.fragment),ba=p(),oe=n("span"),ja=m("Batch processing"),Se=p(),Ns=n("p"),fa=m(`You can pass multiple sets of images and text queries to search for different (or same) objects in several images.
Let\u2019s use both an astronaut image and the beach image together.
For batch processing, you should pass text queries as a nested list to the processor and images as lists of PIL images,
PyTorch tensors, or NumPy arrays.`),Ae=p(),u(hs.$$.fragment),Fe=p(),B=n("p"),Ma=m(`Previously for post-processing you passed the single image\u2019s size as a tensor, but you can also pass a tuple, or, in case
of several images, a list of tuples. Let\u2019s create predictions for the two examples, and visualize the second one (`),ie=n("code"),wa=m("image_idx = 1"),Ja=m(")."),Le=p(),u(us.$$.fragment),De=p(),ds=n("div"),Bs=n("img"),Pe=p(),I=n("h2"),q=n("a"),pe=n("span"),u(gs.$$.fragment),Ua=p(),re=n("span"),xa=m("Image-guided object detection"),Ke=p(),qs=n("p"),Ta=m(`In addition to zero-shot object detection with text queries, OWL-ViT offers image-guided object detection. This means
you can use an image query to find similar objects in the target image.
Unlike text queries, only a single example image is allowed.`),Oe=p(),Xs=n("p"),va=m(`Let\u2019s take an image with two cats on a couch as a target image, and an image of a single cat
as a query:`),st=p(),u(ys.$$.fragment),et=p(),Gs=n("p"),_a=m("Let\u2019s take a quick look at the images:"),tt=p(),u(bs.$$.fragment),at=p(),js=n("div"),Ys=n("img"),lt=p(),X=n("p"),Ia=m("In the preprocessing step, instead of text queries, you now need to use "),ce=n("code"),ka=m("query_images"),Wa=m(":"),nt=p(),u(fs.$$.fragment),ot=p(),G=n("p"),Ca=m("For predictions, instead of passing the inputs to the model, pass them to "),Hs=n("a"),Ea=m("image_guided_detection()"),za=m(`. Draw the predictions
as before except now there are no labels.`),it=p(),u(Ms.$$.fragment),pt=p(),ws=n("div"),Ss=n("img"),rt=p(),As=n("p"),Za=m("If you\u2019d like to interactively try out inference with OWL-ViT, check out this demo:"),ct=p(),J=n("iframe"),this.h()},l(s){const t=Xl('[data-svelte="svelte-1phssyn"]',document.head);U=o(t,"META",{name:!0,content:!0}),t.forEach(e),he=r(s),x=o(s,"H1",{class:!0});var ht=i(x);k=o(ht,"A",{id:!0,class:!0,href:!0});var Ya=i(k);Ds=o(Ya,"SPAN",{});var Ha=i(Ds);d(H.$$.fragment,Ha),Ha.forEach(e),Ya.forEach(e),_t=r(ht),Ps=o(ht,"SPAN",{});var Sa=i(Ps);It=h(Sa,"Zero-shot object detection"),Sa.forEach(e),ht.forEach(e),ue=r(s),d(S.$$.fragment,s),de=r(s),W=o(s,"P",{});var ut=i(W);kt=h(ut,"Traditionally, models used for "),Js=o(ut,"A",{href:!0});var Aa=i(Js);Wt=h(Aa,"object detection"),Aa.forEach(e),Ct=h(ut,` require labeled image datasets for training,
and are limited to detecting the set of classes from the training data.`),ut.forEach(e),ge=r(s),C=o(s,"P",{});var dt=i(C);Et=h(dt,"Zero-shot object detection is supported by the "),Us=o(dt,"A",{href:!0});var Fa=i(Us);zt=h(Fa,"OWL-ViT"),Fa.forEach(e),Zt=h(dt,` model which uses a different approach. OWL-ViT
is an open-vocabulary object detector. It means that it can detect objects in images based on free-text queries without
the need to fine-tune the model on labeled datasets.`),dt.forEach(e),ye=r(s),E=o(s,"P",{});var gt=i(E);Vt=h(gt,"OWL-ViT leverages multi-modal representations to perform open-vocabulary detection. It combines "),xs=o(gt,"A",{href:!0});var La=i(xs);$t=h(La,"CLIP"),La.forEach(e),Rt=h(gt,` with
lightweight object classification and localization heads. Open-vocabulary detection is achieved by embedding free-text queries with the text encoder of CLIP and using them as input to the object classification and localization heads.
associate images and their corresponding textual descriptions, and ViT processes image patches as inputs. The authors
of OWL-ViT first trained CLIP from scratch and then fine-tuned OWL-ViT end to end on standard object detection datasets using
a bipartite matching loss.`),gt.forEach(e),be=r(s),Ts=o(s,"P",{});var Da=i(Ts);Qt=h(Da,"With this approach, the model can detect objects based on textual descriptions without prior training on labeled datasets."),Da.forEach(e),je=r(s),vs=o(s,"P",{});var Pa=i(vs);Nt=h(Pa,"In this guide, you will learn how to use OWL-ViT:"),Pa.forEach(e),fe=r(s),M=o(s,"UL",{});var Fs=i(M);Ks=o(Fs,"LI",{});var Ka=i(Ks);Bt=h(Ka,"to detect objects based on text prompts"),Ka.forEach(e),qt=r(Fs),Os=o(Fs,"LI",{});var Oa=i(Os);Xt=h(Oa,"for batch object detection"),Oa.forEach(e),Gt=r(Fs),se=o(Fs,"LI",{});var sl=i(se);Yt=h(sl,"for image-guided object detection"),sl.forEach(e),Fs.forEach(e),Me=r(s),_s=o(s,"P",{});var el=i(_s);Ht=h(el,"Before you begin, make sure you have all the necessary libraries installed:"),el.forEach(e),we=r(s),d(A.$$.fragment,s),Je=r(s),T=o(s,"H2",{class:!0});var yt=i(T);z=o(yt,"A",{id:!0,class:!0,href:!0});var tl=i(z);ee=o(tl,"SPAN",{});var al=i(ee);d(F.$$.fragment,al),al.forEach(e),tl.forEach(e),St=r(yt),te=o(yt,"SPAN",{});var ll=i(te);At=h(ll,"Zero-shot object detection pipeline"),ll.forEach(e),yt.forEach(e),Ue=r(s),w=o(s,"P",{});var Ls=i(w);Ft=h(Ls,"The simplest way to try out inference with OWL-ViT is to use it in a "),Is=o(Ls,"A",{href:!0});var nl=i(Is);Lt=h(nl,"pipeline()"),nl.forEach(e),Dt=h(Ls,`. Instantiate a pipeline
for zero-shot object detection from a `),L=o(Ls,"A",{href:!0,rel:!0});var ol=i(L);Pt=h(ol,"checkpoint on the Hugging Face Hub"),ol.forEach(e),Kt=h(Ls,":"),Ls.forEach(e),xe=r(s),d(D.$$.fragment,s),Te=r(s),Z=o(s,"P",{});var bt=i(Z);Ot=h(bt,`Next, choose an image you\u2019d like to detect objects in. Here we\u2019ll use the image of astronaut Eileen Collins that is
a part of the `),P=o(bt,"A",{href:!0,rel:!0});var il=i(P);sa=h(il,"NASA"),il.forEach(e),ea=h(bt," Great Images dataset."),bt.forEach(e),ve=r(s),d(K.$$.fragment,s),_e=r(s),O=o(s,"DIV",{class:!0});var pl=i(O);ks=o(pl,"IMG",{src:!0,alt:!0}),pl.forEach(e),Ie=r(s),Ws=o(s,"P",{});var rl=i(Ws);ta=h(rl,`Pass the image and the candidate object labels to look for to the pipeline.
Here we pass the image directly; other suitable options include a local path to an image or an image url. We also pass text descriptions for all items we want to query the image for.`),rl.forEach(e),ke=r(s),d(ss.$$.fragment,s),We=r(s),Cs=o(s,"P",{});var cl=i(Cs);aa=h(cl,"Let\u2019s visualize the predictions:"),cl.forEach(e),Ce=r(s),d(es.$$.fragment,s),Ee=r(s),ts=o(s,"DIV",{class:!0});var ml=i(ts);Es=o(ml,"IMG",{src:!0,alt:!0}),ml.forEach(e),ze=r(s),v=o(s,"H2",{class:!0});var jt=i(v);V=o(jt,"A",{id:!0,class:!0,href:!0});var hl=i(V);ae=o(hl,"SPAN",{});var ul=i(ae);d(as.$$.fragment,ul),ul.forEach(e),hl.forEach(e),la=r(jt),le=o(jt,"SPAN",{});var dl=i(le);na=h(dl,"Text-prompted zero-shot object detection by hand"),dl.forEach(e),jt.forEach(e),Ze=r(s),zs=o(s,"P",{});var gl=i(zs);oa=h(gl,`Now that you\u2019ve seen how to use the zero-shot object detection pipeline, let\u2019s replicate the same
result manually.`),gl.forEach(e),Ve=r(s),$=o(s,"P",{});var ft=i($);ia=h(ft,"Start by loading the model and associated processor from a "),ls=o(ft,"A",{href:!0,rel:!0});var yl=i(ls);pa=h(yl,"checkpoint on the Hugging Face Hub"),yl.forEach(e),ra=h(ft,`.
Here we\u2019ll use the same checkpoint as before:`),ft.forEach(e),$e=r(s),d(ns.$$.fragment,s),Re=r(s),Zs=o(s,"P",{});var bl=i(Zs);ca=h(bl,"Let\u2019s take a different image to switch things up."),bl.forEach(e),Qe=r(s),d(os.$$.fragment,s),Ne=r(s),is=o(s,"DIV",{class:!0});var jl=i(is);Vs=o(jl,"IMG",{src:!0,alt:!0}),jl.forEach(e),Be=r(s),R=o(s,"P",{});var Mt=i(R);ma=h(Mt,`Use the processor to prepare the inputs for the model. The processor combines an image processor that prepares the
image for the model by resizing and normalizing it, and a `),$s=o(Mt,"A",{href:!0});var fl=i($s);ha=h(fl,"CLIPTokenizer"),fl.forEach(e),ua=h(Mt," that takes care of the text inputs."),Mt.forEach(e),qe=r(s),d(ps.$$.fragment,s),Xe=r(s),Q=o(s,"P",{});var wt=i(Q);da=h(wt,`Pass the inputs through the model, post-process, and visualize the results. Since the image processor resized images before
feeding them to the model, you need to use the `),Rs=o(wt,"A",{href:!0});var Ml=i(Rs);ga=h(Ml,"post_process_object_detection()"),Ml.forEach(e),ya=h(wt,` method to make sure the predicted bounding
boxes have the correct coordinates relative to the original image:`),wt.forEach(e),Ge=r(s),d(rs.$$.fragment,s),Ye=r(s),cs=o(s,"DIV",{class:!0});var wl=i(cs);Qs=o(wl,"IMG",{src:!0,alt:!0}),wl.forEach(e),He=r(s),_=o(s,"H2",{class:!0});var Jt=i(_);N=o(Jt,"A",{id:!0,class:!0,href:!0});var Jl=i(N);ne=o(Jl,"SPAN",{});var Ul=i(ne);d(ms.$$.fragment,Ul),Ul.forEach(e),Jl.forEach(e),ba=r(Jt),oe=o(Jt,"SPAN",{});var xl=i(oe);ja=h(xl,"Batch processing"),xl.forEach(e),Jt.forEach(e),Se=r(s),Ns=o(s,"P",{});var Tl=i(Ns);fa=h(Tl,`You can pass multiple sets of images and text queries to search for different (or same) objects in several images.
Let\u2019s use both an astronaut image and the beach image together.
For batch processing, you should pass text queries as a nested list to the processor and images as lists of PIL images,
PyTorch tensors, or NumPy arrays.`),Tl.forEach(e),Ae=r(s),d(hs.$$.fragment,s),Fe=r(s),B=o(s,"P",{});var Ut=i(B);Ma=h(Ut,`Previously for post-processing you passed the single image\u2019s size as a tensor, but you can also pass a tuple, or, in case
of several images, a list of tuples. Let\u2019s create predictions for the two examples, and visualize the second one (`),ie=o(Ut,"CODE",{});var vl=i(ie);wa=h(vl,"image_idx = 1"),vl.forEach(e),Ja=h(Ut,")."),Ut.forEach(e),Le=r(s),d(us.$$.fragment,s),De=r(s),ds=o(s,"DIV",{class:!0});var _l=i(ds);Bs=o(_l,"IMG",{src:!0,alt:!0}),_l.forEach(e),Pe=r(s),I=o(s,"H2",{class:!0});var xt=i(I);q=o(xt,"A",{id:!0,class:!0,href:!0});var Il=i(q);pe=o(Il,"SPAN",{});var kl=i(pe);d(gs.$$.fragment,kl),kl.forEach(e),Il.forEach(e),Ua=r(xt),re=o(xt,"SPAN",{});var Wl=i(re);xa=h(Wl,"Image-guided object detection"),Wl.forEach(e),xt.forEach(e),Ke=r(s),qs=o(s,"P",{});var Cl=i(qs);Ta=h(Cl,`In addition to zero-shot object detection with text queries, OWL-ViT offers image-guided object detection. This means
you can use an image query to find similar objects in the target image.
Unlike text queries, only a single example image is allowed.`),Cl.forEach(e),Oe=r(s),Xs=o(s,"P",{});var El=i(Xs);va=h(El,`Let\u2019s take an image with two cats on a couch as a target image, and an image of a single cat
as a query:`),El.forEach(e),st=r(s),d(ys.$$.fragment,s),et=r(s),Gs=o(s,"P",{});var zl=i(Gs);_a=h(zl,"Let\u2019s take a quick look at the images:"),zl.forEach(e),tt=r(s),d(bs.$$.fragment,s),at=r(s),js=o(s,"DIV",{class:!0});var Zl=i(js);Ys=o(Zl,"IMG",{src:!0,alt:!0}),Zl.forEach(e),lt=r(s),X=o(s,"P",{});var Tt=i(X);Ia=h(Tt,"In the preprocessing step, instead of text queries, you now need to use "),ce=o(Tt,"CODE",{});var Vl=i(ce);ka=h(Vl,"query_images"),Vl.forEach(e),Wa=h(Tt,":"),Tt.forEach(e),nt=r(s),d(fs.$$.fragment,s),ot=r(s),G=o(s,"P",{});var vt=i(G);Ca=h(vt,"For predictions, instead of passing the inputs to the model, pass them to "),Hs=o(vt,"A",{href:!0});var $l=i(Hs);Ea=h($l,"image_guided_detection()"),$l.forEach(e),za=h(vt,`. Draw the predictions
as before except now there are no labels.`),vt.forEach(e),it=r(s),d(Ms.$$.fragment,s),pt=r(s),ws=o(s,"DIV",{class:!0});var Rl=i(ws);Ss=o(Rl,"IMG",{src:!0,alt:!0}),Rl.forEach(e),rt=r(s),As=o(s,"P",{});var Ql=i(As);Za=h(Ql,"If you\u2019d like to interactively try out inference with OWL-ViT, check out this demo:"),Ql.forEach(e),ct=r(s),J=o(s,"IFRAME",{src:!0,frameborder:!0,width:!0,height:!0}),i(J).forEach(e),this.h()},h(){c(U,"name","hf:doc:metadata"),c(U,"content",JSON.stringify(Al)),c(k,"id","zeroshot-object-detection"),c(k,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(k,"href","#zeroshot-object-detection"),c(x,"class","relative group"),c(Js,"href","object_detection"),c(Us,"href","../model_doc/owlvit"),c(xs,"href","../model_doc/clip"),c(z,"id","zeroshot-object-detection-pipeline"),c(z,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(z,"href","#zeroshot-object-detection-pipeline"),c(T,"class","relative group"),c(Is,"href","/docs/transformers/v4.32.0/en/main_classes/pipelines#transformers.pipeline"),c(L,"href","https://huggingface.co/models?other=owlvit"),c(L,"rel","nofollow"),c(P,"href","https://www.nasa.gov/multimedia/imagegallery/index.html"),c(P,"rel","nofollow"),Y(ks.src,$a="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/zero-sh-obj-detection_1.png")||c(ks,"src",$a),c(ks,"alt","Astronaut Eileen Collins"),c(O,"class","flex justify-center"),Y(Es.src,Ra="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/zero-sh-obj-detection_2.png")||c(Es,"src",Ra),c(Es,"alt","Visualized predictions on NASA image"),c(ts,"class","flex justify-center"),c(V,"id","textprompted-zeroshot-object-detection-by-hand"),c(V,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(V,"href","#textprompted-zeroshot-object-detection-by-hand"),c(v,"class","relative group"),c(ls,"href","https://huggingface.co/models?other=owlvit"),c(ls,"rel","nofollow"),Y(Vs.src,Qa="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/zero-sh-obj-detection_3.png")||c(Vs,"src",Qa),c(Vs,"alt","Beach photo"),c(is,"class","flex justify-center"),c($s,"href","/docs/transformers/v4.32.0/en/model_doc/clip#transformers.CLIPTokenizer"),c(Rs,"href","/docs/transformers/v4.32.0/en/model_doc/owlvit#transformers.OwlViTImageProcessor.post_process_object_detection"),Y(Qs.src,Na="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/zero-sh-obj-detection_4.png")||c(Qs,"src",Na),c(Qs,"alt","Beach photo with detected objects"),c(cs,"class","flex justify-center"),c(N,"id","batch-processing"),c(N,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(N,"href","#batch-processing"),c(_,"class","relative group"),Y(Bs.src,Ba="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/zero-sh-obj-detection_4.png")||c(Bs,"src",Ba),c(Bs,"alt","Beach photo with detected objects"),c(ds,"class","flex justify-center"),c(q,"id","imageguided-object-detection"),c(q,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(q,"href","#imageguided-object-detection"),c(I,"class","relative group"),Y(Ys.src,qa="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/zero-sh-obj-detection_5.png")||c(Ys,"src",qa),c(Ys,"alt","Cats"),c(js,"class","flex justify-center"),c(Hs,"href","/docs/transformers/v4.32.0/en/model_doc/owlvit#transformers.OwlViTForObjectDetection.image_guided_detection"),Y(Ss.src,Xa="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/zero-sh-obj-detection_6.png")||c(Ss,"src",Xa),c(Ss,"alt","Cats with bounding boxes"),c(ws,"class","flex justify-center"),Y(J.src,Ga="https://adirik-owl-vit.hf.space")||c(J,"src",Ga),c(J,"frameborder","0"),c(J,"width","850"),c(J,"height","450")},m(s,t){a(document.head,U),l(s,he,t),l(s,x,t),a(x,k),a(k,Ds),g(H,Ds,null),a(x,_t),a(x,Ps),a(Ps,It),l(s,ue,t),g(S,s,t),l(s,de,t),l(s,W,t),a(W,kt),a(W,Js),a(Js,Wt),a(W,Ct),l(s,ge,t),l(s,C,t),a(C,Et),a(C,Us),a(Us,zt),a(C,Zt),l(s,ye,t),l(s,E,t),a(E,Vt),a(E,xs),a(xs,$t),a(E,Rt),l(s,be,t),l(s,Ts,t),a(Ts,Qt),l(s,je,t),l(s,vs,t),a(vs,Nt),l(s,fe,t),l(s,M,t),a(M,Ks),a(Ks,Bt),a(M,qt),a(M,Os),a(Os,Xt),a(M,Gt),a(M,se),a(se,Yt),l(s,Me,t),l(s,_s,t),a(_s,Ht),l(s,we,t),g(A,s,t),l(s,Je,t),l(s,T,t),a(T,z),a(z,ee),g(F,ee,null),a(T,St),a(T,te),a(te,At),l(s,Ue,t),l(s,w,t),a(w,Ft),a(w,Is),a(Is,Lt),a(w,Dt),a(w,L),a(L,Pt),a(w,Kt),l(s,xe,t),g(D,s,t),l(s,Te,t),l(s,Z,t),a(Z,Ot),a(Z,P),a(P,sa),a(Z,ea),l(s,ve,t),g(K,s,t),l(s,_e,t),l(s,O,t),a(O,ks),l(s,Ie,t),l(s,Ws,t),a(Ws,ta),l(s,ke,t),g(ss,s,t),l(s,We,t),l(s,Cs,t),a(Cs,aa),l(s,Ce,t),g(es,s,t),l(s,Ee,t),l(s,ts,t),a(ts,Es),l(s,ze,t),l(s,v,t),a(v,V),a(V,ae),g(as,ae,null),a(v,la),a(v,le),a(le,na),l(s,Ze,t),l(s,zs,t),a(zs,oa),l(s,Ve,t),l(s,$,t),a($,ia),a($,ls),a(ls,pa),a($,ra),l(s,$e,t),g(ns,s,t),l(s,Re,t),l(s,Zs,t),a(Zs,ca),l(s,Qe,t),g(os,s,t),l(s,Ne,t),l(s,is,t),a(is,Vs),l(s,Be,t),l(s,R,t),a(R,ma),a(R,$s),a($s,ha),a(R,ua),l(s,qe,t),g(ps,s,t),l(s,Xe,t),l(s,Q,t),a(Q,da),a(Q,Rs),a(Rs,ga),a(Q,ya),l(s,Ge,t),g(rs,s,t),l(s,Ye,t),l(s,cs,t),a(cs,Qs),l(s,He,t),l(s,_,t),a(_,N),a(N,ne),g(ms,ne,null),a(_,ba),a(_,oe),a(oe,ja),l(s,Se,t),l(s,Ns,t),a(Ns,fa),l(s,Ae,t),g(hs,s,t),l(s,Fe,t),l(s,B,t),a(B,Ma),a(B,ie),a(ie,wa),a(B,Ja),l(s,Le,t),g(us,s,t),l(s,De,t),l(s,ds,t),a(ds,Bs),l(s,Pe,t),l(s,I,t),a(I,q),a(q,pe),g(gs,pe,null),a(I,Ua),a(I,re),a(re,xa),l(s,Ke,t),l(s,qs,t),a(qs,Ta),l(s,Oe,t),l(s,Xs,t),a(Xs,va),l(s,st,t),g(ys,s,t),l(s,et,t),l(s,Gs,t),a(Gs,_a),l(s,tt,t),g(bs,s,t),l(s,at,t),l(s,js,t),a(js,Ys),l(s,lt,t),l(s,X,t),a(X,Ia),a(X,ce),a(ce,ka),a(X,Wa),l(s,nt,t),g(fs,s,t),l(s,ot,t),l(s,G,t),a(G,Ca),a(G,Hs),a(Hs,Ea),a(G,za),l(s,it,t),g(Ms,s,t),l(s,pt,t),l(s,ws,t),a(ws,Ss),l(s,rt,t),l(s,As,t),a(As,Za),l(s,ct,t),l(s,J,t),mt=!0},p:Gl,i(s){mt||(y(H.$$.fragment,s),y(S.$$.fragment,s),y(A.$$.fragment,s),y(F.$$.fragment,s),y(D.$$.fragment,s),y(K.$$.fragment,s),y(ss.$$.fragment,s),y(es.$$.fragment,s),y(as.$$.fragment,s),y(ns.$$.fragment,s),y(os.$$.fragment,s),y(ps.$$.fragment,s),y(rs.$$.fragment,s),y(ms.$$.fragment,s),y(hs.$$.fragment,s),y(us.$$.fragment,s),y(gs.$$.fragment,s),y(ys.$$.fragment,s),y(bs.$$.fragment,s),y(fs.$$.fragment,s),y(Ms.$$.fragment,s),mt=!0)},o(s){b(H.$$.fragment,s),b(S.$$.fragment,s),b(A.$$.fragment,s),b(F.$$.fragment,s),b(D.$$.fragment,s),b(K.$$.fragment,s),b(ss.$$.fragment,s),b(es.$$.fragment,s),b(as.$$.fragment,s),b(ns.$$.fragment,s),b(os.$$.fragment,s),b(ps.$$.fragment,s),b(rs.$$.fragment,s),b(ms.$$.fragment,s),b(hs.$$.fragment,s),b(us.$$.fragment,s),b(gs.$$.fragment,s),b(ys.$$.fragment,s),b(bs.$$.fragment,s),b(fs.$$.fragment,s),b(Ms.$$.fragment,s),mt=!1},d(s){e(U),s&&e(he),s&&e(x),j(H),s&&e(ue),j(S,s),s&&e(de),s&&e(W),s&&e(ge),s&&e(C),s&&e(ye),s&&e(E),s&&e(be),s&&e(Ts),s&&e(je),s&&e(vs),s&&e(fe),s&&e(M),s&&e(Me),s&&e(_s),s&&e(we),j(A,s),s&&e(Je),s&&e(T),j(F),s&&e(Ue),s&&e(w),s&&e(xe),j(D,s),s&&e(Te),s&&e(Z),s&&e(ve),j(K,s),s&&e(_e),s&&e(O),s&&e(Ie),s&&e(Ws),s&&e(ke),j(ss,s),s&&e(We),s&&e(Cs),s&&e(Ce),j(es,s),s&&e(Ee),s&&e(ts),s&&e(ze),s&&e(v),j(as),s&&e(Ze),s&&e(zs),s&&e(Ve),s&&e($),s&&e($e),j(ns,s),s&&e(Re),s&&e(Zs),s&&e(Qe),j(os,s),s&&e(Ne),s&&e(is),s&&e(Be),s&&e(R),s&&e(qe),j(ps,s),s&&e(Xe),s&&e(Q),s&&e(Ge),j(rs,s),s&&e(Ye),s&&e(cs),s&&e(He),s&&e(_),j(ms),s&&e(Se),s&&e(Ns),s&&e(Ae),j(hs,s),s&&e(Fe),s&&e(B),s&&e(Le),j(us,s),s&&e(De),s&&e(ds),s&&e(Pe),s&&e(I),j(gs),s&&e(Ke),s&&e(qs),s&&e(Oe),s&&e(Xs),s&&e(st),j(ys,s),s&&e(et),s&&e(Gs),s&&e(tt),j(bs,s),s&&e(at),s&&e(js),s&&e(lt),s&&e(X),s&&e(nt),j(fs,s),s&&e(ot),s&&e(G),s&&e(it),j(Ms,s),s&&e(pt),s&&e(ws),s&&e(rt),s&&e(As),s&&e(ct),s&&e(J)}}}const Al={local:"zeroshot-object-detection",sections:[{local:"zeroshot-object-detection-pipeline",title:"Zero-shot object detection pipeline"},{local:"textprompted-zeroshot-object-detection-by-hand",title:"Text-prompted zero-shot object detection by hand"},{local:"batch-processing",title:"Batch processing"},{local:"imageguided-object-detection",title:"Image-guided object detection"}],title:"Zero-shot object detection"};function Fl(Va){return Yl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ol extends Nl{constructor(U){super();Bl(this,U,Fl,Sl,ql,{})}}export{Ol as default,Al as metadata};
