Skip to content

Commit

Permalink
deploy: 42aa32f
Browse files Browse the repository at this point in the history
  • Loading branch information
HYLcool committed Mar 15, 2024
1 parent d24b6fd commit da2c59b
Show file tree
Hide file tree
Showing 34 changed files with 1,827 additions and 1,207 deletions.
Binary file modified .doctrees/data_juicer.ops.filter.doctree
Binary file not shown.
Binary file modified .doctrees/data_juicer.ops.mapper.doctree
Binary file not shown.
Binary file modified .doctrees/data_juicer.utils.doctree
Binary file not shown.
Binary file modified .doctrees/environment.pickle
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>data_juicer.ops.filter.face_area_filter &mdash; data_juicer 0.2.0 documentation</title>
<title>data_juicer.ops.filter.image_face_ratio_filter &mdash; data_juicer 0.2.0 documentation</title>
<link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css?v=80d5e7a1" />
<link rel="stylesheet" type="text/css" href="../../../../_static/css/theme.css?v=19f00094" />

Expand Down Expand Up @@ -61,7 +61,7 @@
<ul class="wy-breadcrumbs">
<li><a href="../../../../index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item"><a href="../../../index.html">Module code</a></li>
<li class="breadcrumb-item active">data_juicer.ops.filter.face_area_filter</li>
<li class="breadcrumb-item active">data_juicer.ops.filter.image_face_ratio_filter</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
Expand All @@ -70,7 +70,7 @@
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">

<h1>Source code for data_juicer.ops.filter.face_area_filter</h1><div class="highlight"><pre>
<h1>Source code for data_juicer.ops.filter.image_face_ratio_filter</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">jsonargparse.typing</span> <span class="kn">import</span> <span class="n">ClosedUnitInterval</span>
<span class="kn">from</span> <span class="nn">loguru</span> <span class="kn">import</span> <span class="n">logger</span>
Expand All @@ -83,19 +83,21 @@ <h1>Source code for data_juicer.ops.filter.face_area_filter</h1><div class="high
<span class="kn">from</span> <span class="nn">..base_op</span> <span class="kn">import</span> <span class="n">OPERATORS</span><span class="p">,</span> <span class="n">Filter</span>
<span class="kn">from</span> <span class="nn">..op_fusion</span> <span class="kn">import</span> <span class="n">LOADED_IMAGES</span>

<span class="n">OP_NAME</span> <span class="o">=</span> <span class="s1">&#39;face_area_filter&#39;</span>
<span class="n">OP_NAME</span> <span class="o">=</span> <span class="s1">&#39;image_face_ratio_filter&#39;</span>

<span class="k">with</span> <span class="n">AvailabilityChecking</span><span class="p">([</span><span class="s1">&#39;dlib&#39;</span><span class="p">],</span> <span class="n">OP_NAME</span><span class="p">):</span>
<span class="kn">import</span> <span class="nn">dlib</span>


<div class="viewcode-block" id="FaceAreaFilter"><a class="viewcode-back" href="../../../../data_juicer.ops.filter.html#data_juicer.ops.filter.face_area_filter.FaceAreaFilter">[docs]</a><span class="nd">@OPERATORS</span><span class="o">.</span><span class="n">register_module</span><span class="p">(</span><span class="n">OP_NAME</span><span class="p">)</span>
<div class="viewcode-block" id="ImageFaceRatioFilter"><a class="viewcode-back" href="../../../../data_juicer.ops.filter.html#data_juicer.ops.filter.image_face_ratio_filter.ImageFaceRatioFilter">[docs]</a><span class="nd">@OPERATORS</span><span class="o">.</span><span class="n">register_module</span><span class="p">(</span><span class="n">OP_NAME</span><span class="p">)</span>
<span class="nd">@LOADED_IMAGES</span><span class="o">.</span><span class="n">register_module</span><span class="p">(</span><span class="n">OP_NAME</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">FaceAreaFilter</span><span class="p">(</span><span class="n">Filter</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Filter to keep samples with face area ratio within a specific range.</span>
<span class="k">class</span> <span class="nc">ImageFaceRatioFilter</span><span class="p">(</span><span class="n">Filter</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Filter to keep samples with face area ratios within a specific range.</span>
<span class="sd"> &quot;&quot;&quot;</span>

<div class="viewcode-block" id="FaceAreaFilter.__init__"><a class="viewcode-back" href="../../../../data_juicer.ops.filter.html#data_juicer.ops.filter.face_area_filter.FaceAreaFilter.__init__">[docs]</a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">_default_kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;upsample_num_times&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">}</span>

<div class="viewcode-block" id="ImageFaceRatioFilter.__init__"><a class="viewcode-back" href="../../../../data_juicer.ops.filter.html#data_juicer.ops.filter.image_face_ratio_filter.ImageFaceRatioFilter.__init__">[docs]</a> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">min_ratio</span><span class="p">:</span> <span class="n">ClosedUnitInterval</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">,</span>
<span class="n">max_ratio</span><span class="p">:</span> <span class="n">ClosedUnitInterval</span> <span class="o">=</span> <span class="mf">0.4</span><span class="p">,</span>
<span class="n">any_or_all</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;any&#39;</span><span class="p">,</span>
Expand All @@ -113,18 +115,15 @@ <h1>Source code for data_juicer.ops.filter.face_area_filter</h1><div class="high
<span class="sd"> :param args: Extra positional arguments.</span>
<span class="sd"> :param kwargs: Extra keyword arguments.</span>
<span class="sd"> &quot;&quot;&quot;</span>

<span class="c1"># Extract face detector arguments from kwargs</span>
<span class="n">detector_keys</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;upsample_num_times&#39;</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detector_kwargs</span> <span class="o">=</span> <span class="p">{</span>
<span class="n">key</span><span class="p">:</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">detector_keys</span> <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">kwargs</span>
<span class="p">}</span>

<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">min_ratio</span> <span class="o">=</span> <span class="n">min_ratio</span>
<span class="bp">self</span><span class="o">.</span><span class="n">max_ratio</span> <span class="o">=</span> <span class="n">max_ratio</span>

<span class="bp">self</span><span class="o">.</span><span class="n">extra_kwargs</span> <span class="o">=</span> <span class="p">{</span>
<span class="n">k</span><span class="p">:</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_default_kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="p">}</span>

<span class="k">if</span> <span class="n">any_or_all</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;any&#39;</span><span class="p">,</span> <span class="s1">&#39;all&#39;</span><span class="p">]:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Keep strategy [</span><span class="si">{</span><span class="n">any_or_all</span><span class="si">}</span><span class="s1">] is not supported. &#39;</span>
<span class="sa">f</span><span class="s1">&#39;Can only be one of [&quot;any&quot;, &quot;all&quot;].&#39;</span><span class="p">)</span>
Expand All @@ -133,7 +132,7 @@ <h1>Source code for data_juicer.ops.filter.face_area_filter</h1><div class="high
<span class="c1"># Initialize face detector</span>
<span class="bp">self</span><span class="o">.</span><span class="n">detector</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">get_frontal_face_detector</span><span class="p">()</span></div>

<div class="viewcode-block" id="FaceAreaFilter.compute_stats"><a class="viewcode-back" href="../../../../data_juicer.ops.filter.html#data_juicer.ops.filter.face_area_filter.FaceAreaFilter.compute_stats">[docs]</a> <span class="k">def</span> <span class="nf">compute_stats</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sample</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<div class="viewcode-block" id="ImageFaceRatioFilter.compute_stats"><a class="viewcode-back" href="../../../../data_juicer.ops.filter.html#data_juicer.ops.filter.image_face_ratio_filter.ImageFaceRatioFilter.compute_stats">[docs]</a> <span class="k">def</span> <span class="nf">compute_stats</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sample</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
<span class="c1"># check if it&#39;s computed already</span>
<span class="k">if</span> <span class="n">StatsKeys</span><span class="o">.</span><span class="n">face_ratios</span> <span class="ow">in</span> <span class="n">sample</span><span class="p">[</span><span class="n">Fields</span><span class="o">.</span><span class="n">stats</span><span class="p">]:</span>
<span class="k">return</span> <span class="n">sample</span>
Expand All @@ -153,7 +152,7 @@ <h1>Source code for data_juicer.ops.filter.face_area_filter</h1><div class="high
<span class="n">face_detections</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">image</span> <span class="ow">in</span> <span class="n">images</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="n">img</span> <span class="o">=</span> <span class="n">pil_to_opencv</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
<span class="n">dets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">detector_kwargs</span><span class="p">)</span>
<span class="n">dets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">detector</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">extra_kwargs</span><span class="p">)</span>
<span class="n">face_detections</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">[[</span>
<span class="nb">max</span><span class="p">(</span><span class="n">det</span><span class="o">.</span><span class="n">left</span><span class="p">(),</span> <span class="mi">0</span><span class="p">),</span>
<span class="nb">max</span><span class="p">(</span><span class="n">det</span><span class="o">.</span><span class="n">top</span><span class="p">(),</span> <span class="mi">0</span><span class="p">),</span>
Expand All @@ -176,7 +175,7 @@ <h1>Source code for data_juicer.ops.filter.face_area_filter</h1><div class="high
<span class="p">]</span>
<span class="k">return</span> <span class="n">sample</span></div>

<div class="viewcode-block" id="FaceAreaFilter.process"><a class="viewcode-back" href="../../../../data_juicer.ops.filter.html#data_juicer.ops.filter.face_area_filter.FaceAreaFilter.process">[docs]</a> <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sample</span><span class="p">):</span>
<div class="viewcode-block" id="ImageFaceRatioFilter.process"><a class="viewcode-back" href="../../../../data_juicer.ops.filter.html#data_juicer.ops.filter.image_face_ratio_filter.ImageFaceRatioFilter.process">[docs]</a> <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sample</span><span class="p">):</span>
<span class="n">face_ratios</span> <span class="o">=</span> <span class="n">sample</span><span class="p">[</span><span class="n">Fields</span><span class="o">.</span><span class="n">stats</span><span class="p">][</span><span class="n">StatsKeys</span><span class="o">.</span><span class="n">face_ratios</span><span class="p">]</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">face_ratios</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">True</span>
Expand Down
Loading

0 comments on commit da2c59b

Please sign in to comment.