fbpx

Сравнение двух фото( Нужно найти ошибку в коде )

289 просмотраnumpy opencv python-3.6 python3.6 windows-10
0
1 Комментарий

Мне надо по пикселям сравнить фото и при каждом совпадающем пикселе добавлять переменную на 1 ) спасибо
Только ЖЕЛАТЕЛЬНО исправить эту ошибку , а не подключать другие библеотеки
Заранее Спасибо
Фото еталон в 128Х128 и фото с камеры тоже делаю в 128Х128

<em><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">128</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">128</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">roi_gray</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="o">==</span><span class="n">etalon</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]:</span>
                    <span class="n">sgodztvo</span><span class="o">+=</span><span class="mi">1</span>
        <span class="k">print</span><span class="p">(</span><span class="n">sgodztvo</span></em><span class="p"><em>)
</em><strong>
Все вроде норм написано НО почемуто )
</strong><em>Traceback (most recent call last):</em>
<em>File “C:\Users\Genya\Desktop\INTRESTING\FacialRecognitionProject\TEST\15.10.18\Lico_default.py”, line 31, in <module></em>
<em>if roi_gray==etalon:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()</em><strong><em>
Ну я если надо вот фулл код
</em></strong></span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">cv2</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="n">cap</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">VideoCapture</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">cap</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">640</span><span class="p">)</span> <span class="c1"># set Width</span>
<span class="n">cap</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">480</span><span class="p">)</span> <span class="c1"># set Height</span>
<span class="n">etalon</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="s1">'./Evgeniy/etalot.png'</span><span class="p">)</span>
<span class="n">faceCascade</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">CascadeClassifier</span><span class="p">(</span><span class="s1">'lico.xml'</span><span class="p">)</span>
<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
    <span class="n">ret</span><span class="p">,</span> <span class="n">img</span> <span class="o">=</span> <span class="n">cap</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
    <span class="n">gray</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">cvtColor</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">cv2</span><span class="o">.</span><span class="n">COLOR_BGR2GRAY</span><span class="p">)</span>
    <span class="n">faces</span> <span class="o">=</span> <span class="n">faceCascade</span><span class="o">.</span><span class="n">detectMultiScale</span><span class="p">(</span>
        <span class="n">gray</span><span class="p">,</span>     
        <span class="n">scaleFactor</span><span class="o">=</span><span class="mf">1.2</span><span class="p">,</span>
        <span class="n">minNeighbors</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>     
        <span class="n">minSize</span><span class="o">=</span><span class="p">(</span><span class="mi">30</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
    <span class="p">)</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">w</span><span class="p">,</span><span class="n">h</span><span class="p">)</span> <span class="ow">in</span> <span class="n">faces</span><span class="p">:</span>
        <span class="n">sgodztvo</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">roi_color</span> <span class="o">=</span> <span class="n">img</span><span class="p">[</span><span class="n">y</span><span class="p">:</span><span class="n">y</span><span class="o">+</span><span class="n">h</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">w</span><span class="p">]</span>
        <span class="n">roi_gray</span> <span class="o">=</span> <span class="n">gray</span><span class="p">[</span><span class="n">y</span><span class="p">:</span><span class="n">y</span><span class="o">+</span><span class="n">h</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span><span class="n">x</span><span class="o">+</span><span class="n">w</span><span class="p">]</span>
        <span class="n">roi_gray</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="n">roi_gray</span><span class="p">,(</span><span class="mi">128</span><span class="p">,</span><span class="mi">128</span><span class="p">))</span>
        <span class="c1">#cv2.imwrite("Evgeniy/etalot.png",roi_gray)</span>
        <span class="n">cv2</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="s2">"cut"</span><span class="p">,</span><span class="n">roi_gray</span><span class="p">)</span>
        <span class="n">cv2</span><span class="o">.</span><span class="n">rectangle</span><span class="p">(</span><span class="n">img</span><span class="p">,(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">),(</span><span class="n">x</span><span class="o">+</span><span class="n">w</span><span class="p">,</span><span class="n">y</span><span class="o">+</span><span class="n">h</span><span class="p">),(</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span><span class="mi">2</span><span class="p">)</span>
        <span class="c1">#roi_gray = gray[y:y+h, x:x+w]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">128</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">128</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">roi_gray</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span><span class="o">==</span><span class="n">etalon</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]:</span>
                    <span class="n">sgodztvo</span><span class="o">+=</span><span class="mi">1</span>
        <span class="k">print</span><span class="p">(</span><span class="n">sgodztvo</span><span class="p">)</span>

<em>Pyhon 3.6.3
Windows 10
open-cv</em>

Monty Python добавил комментарий

Что-то не то с кодом. Закиньте на pastebin.

Добавить комментарий

0 Answers

Python Опубликовано 21.04.2019
Напишите свой ответ на данный вопрос.
Scroll Up