<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: OpenCV определение лица]]></title>
		<link>http://forum.script-coding.com/viewtopic.php?id=17653</link>
		<atom:link href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=17653&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: OpenCV определение лица».]]></description>
		<lastBuildDate>Sun, 12 Mar 2023 01:51:50 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[AHK: OpenCV определение лица]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=157090#p157090</link>
			<description><![CDATA[<p>Публикую, чисто в образовательных целях.<br />Удобней использовать OpenCV com:<br /><a href="http://forum.script-coding.com/viewtopic.php?id=17642">http://forum.script-coding.com/viewtopic.php?id=17642</a></p><p>Перенес код с AutoIt:<br /><a href="https://www.autoitscript.com/forum/topic/160732-opencv-udf/?page=2&amp;tab=comments#comment-1305145">https://www.autoitscript.com/forum/topi … nt-1305145</a><br />Используется 4 dll 2413.6 версии:<br /></p><div class="quotebox"><blockquote><p>Global opencv := { opencv_core: &quot;opencv_core2413.dll&quot; <br />, opencv_highgui: &quot;opencv_highgui2413.dll&quot;<br />, opencv_imgproc: &quot;opencv_imgproc2413.dll&quot;<br />, opencv_objdetect: &quot;opencv_objdetect2413.dll&quot; }</p></blockquote></div><p><a href="https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.13">https://sourceforge.net/projects/opencv … win/2.4.13</a><br />Лежат для 32бит:<br /></p><div class="quotebox"><blockquote><p>opencv-2.4.13.6-vc14\opencv\build\x86\vc14\bin\</p></blockquote></div><p>Для 64бит:<br /></p><div class="quotebox"><blockquote><p>opencv-2.4.13.6-vc14\opencv\build\x64\vc14\bin\</p></blockquote></div><p>haarcascade_frontalface_default.xml лежит в<br /></p><div class="quotebox"><blockquote><p>opencv-2.4.13.6-vc14\opencv\sources\data\haarcascades_GPU\haarcascade_frontalface_default.xml</p></blockquote></div><p>Код:<br /></p><div class="codebox"><pre><code>file := &quot;lena.jpg&quot;
window := &quot;gui&quot;
haarcascade := &quot;haarcascade_frontalface_default.xml&quot;
scale := 1.3


;start dll opencv
Global opencv := { opencv_core: &quot;opencv_core2413.dll&quot; 
, opencv_highgui: &quot;opencv_highgui2413.dll&quot;
, opencv_imgproc: &quot;opencv_imgproc2413.dll&quot;
, opencv_objdetect: &quot;opencv_objdetect2413.dll&quot; }
_OpenCV_Startup()

;//load haar data
cascade := _cvLoad(haarcascade)

;// load IPL type image
pimg := _cvLoadImage(file)

;// show our input image
_cvShowImage(window, pimg)

;// Create destination images
pimgGrayScale := _cvCreateImage(_cvGetSize(pimg, width, height), 8, 1)
pimgsmall := _cvCreateImage(_cvSize(Round(width/scale), Round(height/scale)), 8, 1)

;//converting the original image into grayscale, resize, and equalize
_cvCvtColor(pimg, pimgGrayScale, CV_BGR2GRAY := 6)
_cvResize(pimgGrayScale, pimgsmall, CV_INTER_LINEAR := 1)
_cvEqualizeHist(pimgsmall, pimgsmall)

; //setup memory block for sequence storage
pstorage := _cvCreateMemStorage(0)   ; //storage area for all contours
_cvClearMemStorage(pstorage)

;// Detect features using haar cascades store as CvSeq
objects := _cvHaarDetectObjects(pimgsmall, cascade, pstorage, 1.1, 8, 0, _cvSize(30, 30))

;// Draw rectangles around detected features
loop % NumGet(objects+0, 8+A_PtrSize*4, &quot;int&quot;)   ;  total found
{
   r := _cvGetSeqElem(objects, A_Index)
   x := NumGet(r+0, 0, &quot;int&quot;)*scale
   y := NumGet(r+0, 4, &quot;int&quot;)*scale
   w := NumGet(r+0, 8, &quot;int&quot;)*scale
   h := NumGet(r+0, 12, &quot;int&quot;)*scale
   _cvRectangle(pimg, _cvPoint(x, y), _cvPoint(x+w, y+h), _CV_RGB(vScalar, 255, 0, 0), 2, 8, 0)

   ;// Update input image
   _cvShowImage(window, pimg)
}
msgbox done

_cvReleaseImage(pimgsmall)
_cvReleaseImage(pimgGrayScale)
_cvReleaseImage(pimg)
_cvDestroyAllWindows()
_cvClearMemStorage(pstorage)
_cvClearSeq(objects)
_Opencv_CloseDLL()
ExitApp






_OpenCV_Startup()
{
   For key, value in opencv
      DllCall(&quot;LoadLibrary&quot;, &quot;Str&quot;, value, &quot;Ptr&quot;)
}

_cvLoadImage(filename, iscolor := 1)  ;  CV_LOAD_IMAGE_COLOR
{
   Result := DllCall(opencv.opencv_highgui &quot;\cvLoadImage&quot;, &quot;AStr&quot;, filename, &quot;int&quot;, iscolor, &quot;Cdecl ptr&quot;)
   if !Result or ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
   return Result
}

_cvNamedWindow(name, flags := 0x00000001)   ;   CV_WINDOW_AUTOSIZE 
{
   DllCall(opencv.opencv_highgui &quot;\cvNamedWindow&quot;, &quot;AStr&quot;, name, &quot;int&quot;, flags, &quot;Cdecl ptr&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvShowImage(name, pimage)
{
   DllCall(opencv.opencv_highgui &quot;\cvShowImage&quot;, &quot;AStr&quot;, name, &quot;ptr&quot;, pimage, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvGetSize(pimage, ByRef width := 0, ByRef height := 0)
{
   WidthHeight := DllCall(opencv.opencv_core &quot;\cvGetSize&quot;, &quot;ptr&quot;, pimage, &quot;Cdecl int64&quot;)
   width := 0xFFFFFFFF &amp; WidthHeight
   height := WidthHeight &gt;&gt; 32
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
   return WidthHeight
}

_cvCreateMat(cvrows, cvcols, cvtype)
{
   Result := DllCall(opencv.opencv_core &quot;\cvCreateMat&quot;, &quot;int&quot;, cvrows, &quot;int&quot;, cvcols, &quot;int&quot;, cvtype, &quot;Cdecl ptr&quot;)
   if !Result or ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
   return Result
}

_cvMatchTemplate(cvimage, cvtempl, cvresult, cvmethod)
{
   DllCall(opencv.opencv_imgproc &quot;\cvMatchTemplate&quot;, &quot;ptr&quot;, cvimage, &quot;ptr&quot;, cvtempl, &quot;ptr&quot;, cvresult, &quot;int&quot;, cvmethod, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvNormalize(cvsrc, cvdst, cva, cvb, cvnorm_type, cvmask)
{
   DllCall(opencv.opencv_core &quot;\cvNormalize&quot;, &quot;ptr&quot;, cvsrc, &quot;ptr&quot;, cvdst, &quot;Double&quot;, cva, &quot;Double&quot;, cvb, &quot;int&quot;, cvnorm_type, &quot;ptr&quot;, cvmask, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvThreshold(cvsrc, cvdst, cvthreshold, cvmax_value, cvthreshold_type)
{
   DllCall(opencv.opencv_imgproc &quot;\cvThreshold&quot;, &quot;ptr&quot;, cvsrc, &quot;ptr&quot;, cvdst, &quot;Double&quot;, cvthreshold, &quot;Double&quot;, cvmax_value, &quot;int&quot;, cvthreshold_type, &quot;Cdecl double&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvSize(width, height)
{
   return width&amp;0xFFFFFFFF|height&lt;&lt;32
}

_cvPoint(x, y)
{
   return x&amp;0xFFFFFFFF|y&lt;&lt;32
}

_cvCreateImage(cvsize, cvdepth, cvchannels)
{
   Result := DllCall(opencv.opencv_core &quot;\cvCreateImage&quot;, &quot;int64&quot;, cvsize, &quot;int&quot;, cvdepth, &quot;int&quot;, cvchannels, &quot;Cdecl ptr&quot;)
   if !Result or ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
   return Result
}

_cvArrTohBitmap(pimage, byref width = &quot;&quot;, byref height = &quot;&quot;) { 
	Result := _cvCreateImage(_cvGetSize(pimage, width, height), 8, 4)
	_cvCvtColor(pimage, Result, 0)  
	pData := NumGet(Result + 0, 68 + 20 * (A_PtrSize = 8), &quot;Ptr&quot;)
	hBitmap := DllCall(&quot;CreateBitmap&quot;, &quot;Int&quot;, width, &quot;Int&quot;, height, &quot;UInt&quot;, 1, &quot;UInt&quot;, 32, &quot;Ptr&quot;, pData)
	Return hBitmap, _cvReleaseImage(Result)
}

_cvScalar(ByRef vScalar, value)
{
   VarSetCapacity(vScalar, 32, 0)
   if IsObject(value)
   {
      loop 4
         NumPut(value%A_Index%, vScalar, (A_Index-1)*8, &quot;double&quot;)
   }
   else
   {
      loop 4
         NumPut(value, vScalar, (A_Index-1)*8, &quot;double&quot;)
   }
   return &amp;vScalar
}

_CV_RGB(ByRef vScalar, cvRed, cvGreen, cvBlue)
{
   VarSetCapacity(vScalar, 32, 0)
   NumPut(cvBlue, vScalar, 0, &quot;double&quot;)
   NumPut(cvGreen, vScalar, 8, &quot;double&quot;)
   NumPut(cvRed, vScalar, 16, &quot;double&quot;)
   NumPut(0, vScalar, 24, &quot;double&quot;)
   return &amp;vScalar
}

_cvSet(cvarr, cvvalue, cvmask := 0)
{
   if (A_PtrSize = 4)
      DllCall(opencv.opencv_core &quot;\cvSet&quot;, &quot;ptr&quot;, cvarr, &quot;double&quot;, NumGet(cvvalue+0, 0, &quot;double&quot;), &quot;double&quot;, NumGet(cvvalue+0, 8, &quot;double&quot;), &quot;double&quot;, NumGet(cvvalue+0, 16, &quot;double&quot;), &quot;double&quot;, NumGet(cvvalue+0, 24, &quot;double&quot;), &quot;ptr&quot;, cvmask, &quot;Cdecl&quot;)
   else
      DllCall(opencv.opencv_core &quot;\cvSet&quot;, &quot;ptr&quot;, cvarr, &quot;ptr&quot;, cvvalue, &quot;ptr&quot;, cvmask, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvMinMaxLoc(cvarr, cvmin_val, cvmax_val, cvmin_loc, cvmax_loc, cvmask)
{
   DllCall(opencv.opencv_core &quot;\cvMinMaxLoc&quot;, &quot;ptr&quot;, cvarr, &quot;ptr&quot;, cvmin_val, &quot;ptr&quot;, cvmax_val, &quot;ptr&quot;, cvmin_loc, &quot;ptr&quot;, cvmax_loc, &quot;ptr&quot;, cvmask, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvRectangle(cvimg, cvpt1, cvpt2, cvcolor, cvthickness, cvline_type, cvshift)
{
   if (A_PtrSize = 4)
      DllCall(opencv.opencv_core &quot;\cvRectangle&quot;, &quot;ptr&quot;, cvimg, &quot;int64&quot;, cvpt1, &quot;int64&quot;, cvpt2, &quot;double&quot;, NumGet(cvcolor+0, 0, &quot;double&quot;), &quot;double&quot;, NumGet(cvcolor+0, 8, &quot;double&quot;), &quot;double&quot;, NumGet(cvcolor+0, 16, &quot;double&quot;), &quot;double&quot;, NumGet(cvcolor+0, 24, &quot;double&quot;), &quot;int&quot;, cvthickness, &quot;int&quot;, cvline_type, &quot;int&quot;, cvshift, &quot;Cdecl&quot;)
   else
      DllCall(opencv.opencv_core &quot;\cvRectangle&quot;, &quot;ptr&quot;, cvimg, &quot;int64&quot;, cvpt1, &quot;int64&quot;, cvpt2, &quot;ptr&quot;, cvcolor, &quot;int&quot;, cvthickness, &quot;int&quot;, cvline_type, &quot;int&quot;, cvshift, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvCvtColor(cvsrc, cvdst, cvcode)
{
   DllCall(opencv.opencv_imgproc &quot;\cvCvtColor&quot;, &quot;ptr&quot;, cvsrc, &quot;ptr&quot;, cvdst, &quot;int&quot;, cvcode, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvResize(cvsrc, cvdst, cvinterpolation := 1)   ; CV_INTER_LINEAR
{
   DllCall(opencv.opencv_imgproc &quot;\cvResize&quot;, &quot;ptr&quot;, cvsrc, &quot;ptr&quot;, cvdst, &quot;int&quot;, cvinterpolation, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvEqualizeHist(cvsrc, cvdst)
{
   DllCall(opencv.opencv_imgproc &quot;\cvEqualizeHist&quot;, &quot;ptr&quot;, cvsrc, &quot;ptr&quot;, cvdst, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvLoad(cvfilename, cvmemstorage := 0, cvname := 0, cvreal_name := 0)
{
   Result := DllCall(opencv.opencv_core &quot;\cvLoad&quot;, &quot;AStr&quot;, cvfilename, &quot;ptr&quot;, cvmemstorage, &quot;ptr&quot;, cvname, &quot;ptr&quot;, cvreal_name, &quot;Cdecl ptr&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
   return Result
}

_cvCreateMemStorage(cvblock_size)
{
   Result := DllCall(opencv.opencv_core &quot;\cvCreateMemStorage&quot;, &quot;int&quot;, cvblock_size, &quot;Cdecl ptr&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
   return Result
}

_cvClearMemStorage(cvstorage)
{
   DllCall(opencv.opencv_core &quot;\cvClearMemStorage&quot;, &quot;ptr&quot;, cvstorage, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvHaarDetectObjects(cvimage, cvcascade, cvstorage, cvscale_factor := 1.1, cvmin_neighbors := 3, cvflags := 0, cvmin_size := 0, cvmax_size := 0)
{
   Result := DllCall(opencv.opencv_objdetect &quot;\cvHaarDetectObjects&quot;, &quot;ptr&quot;, cvimage, &quot;ptr&quot;, cvcascade, &quot;ptr&quot;, cvstorage, &quot;double&quot;, cvscale_factor, &quot;int&quot;, cvmin_neighbors, &quot;int&quot;, cvflags, &quot;int64&quot;, cvmin_size, &quot;int64&quot;, cvmax_size, &quot;Cdecl ptr&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
   return Result
}

_cvGetSeqElem(cvseq, cvindex)
{
   Result := DllCall(opencv.opencv_core &quot;\cvGetSeqElem&quot;, &quot;ptr&quot;, cvseq, &quot;int&quot;, cvindex, &quot;Cdecl ptr&quot;)
   if !Result or ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
   return Result
}

_cvReleaseImage(pimage)
{
   DllCall(opencv.opencv_core &quot;\cvReleaseImage&quot;, &quot;ptr*&quot;, pimage, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvReleaseMat(cvmat)
{
   DllCall(opencv.opencv_core &quot;\cvReleaseMat&quot;, &quot;ptr*&quot;, cvmat, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvDestroyAllWindows()
{
   DllCall(opencv.opencv_highgui &quot;\cvDestroyAllWindows&quot;, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_cvClearSeq(cvseq)
{
   DllCall(opencv.opencv_core &quot;\cvClearSeq&quot;, &quot;ptr&quot;, cvseq, &quot;Cdecl&quot;)
   if ErrorLevel
      _Error(A_ThisFunc &quot;`n&quot; ErrorLevel)
}

_Opencv_CloseDLL()
{
   For key, value in opencv
      if hModule := DllCall(&quot;GetModuleHandle&quot;, &quot;str&quot;, value)
         DllCall(&quot;FreeLibrary&quot;, &quot;ptr&quot;, hModule)
}

_Error(val)
{
   msgbox % val &quot;`nLastError - &quot; A_LastError
   ExitApp
}</code></pre></div><p>Код по поиску совпадений картинок через openCV.<br />Функции нужно скопировать с предыдущего кода.<br />Перенес отсюда:<br /><a href="https://www.autoitscript.com/forum/topic/160732-opencv-udf/?page=2&amp;tab=comments#comment-1280955">https://www.autoitscript.com/forum/topi … nt-1280955</a><br />Код с threshold = 0.9:<br /></p><div class="codebox"><pre><code>file1 := &quot;1.jpg&quot;
file2 := &quot;2.jpg&quot;
window := &quot;gui&quot;

;start dll opencv
Global opencv := { opencv_core: &quot;opencv_core2413.dll&quot; 
, opencv_highgui: &quot;opencv_highgui2413.dll&quot;
, opencv_imgproc: &quot;opencv_imgproc2413.dll&quot; }
_OpenCV_Startup()

;// load IPL type image both images must have same number of bits and color channels!
pimg := _cvLoadImage(file1)
ptemp := _cvLoadImage(file2) ;image to look for

;// Create some windows to show the input and output images in.
_cvNamedWindow(window)

;// show our input image
_cvShowImage(window, pimg)

;// Determine images height and width to create results matrix
_cvGetSize(pimg, width, height)
_cvGetSize(ptemp, width2, height2)
rw :=  width - width2 + 1
rh :=  height - height2 + 1

;// Create Opencv matrix object 32 bit floating
presult := _cvCreateMat(rh, rw, CV_32FC1 := 5)

;// Template matching
_cvMatchTemplate(pimg, ptemp, presult, CV_TM_CCOEFF_NORMED := 5)
_cvNormalize(presult, presult, 0, CV_C := 1, CV_MINMAX := 32, 0)
_cvThreshold(presult, presult, thresh := 0.9, maxval := 1, CV_THRESH_BINARY := 0)

;//locate matches
;
;// Create minmax variables to pass to opencv
VarSetCapacity(pmaxloc, 8, 0)
VarSetCapacity(pminloc, 8, 0)
VarSetCapacity(pmaxval, 8, 0)
VarSetCapacity(pminval, 8, 0)

;// create mask to find multiple matches
pmask := _cvCreateImage(_cvSize(rw, rh), IPL_DEPTH_8U := 8, channels := 1)
_cvSet(pmask, _cvScalar(vScalar, 1))

;// Find location of maximum
_cvMinMaxLoc(presult, &amp;pminval, &amp;pmaxval, &amp;pminloc, &amp;pmaxloc, pmask)

loop
{
   ;// Mask it to find others if exists and draw red rectangle on input image
   _cvRectangle(pmask, _cvPoint(NumGet(pmaxloc, 0, &quot;int&quot;)-5, NumGet(pmaxloc, 4, &quot;int&quot;)-5), _cvPoint(NumGet(pmaxloc, 0, &quot;int&quot;)+ width2, NumGet(pmaxloc, 4, &quot;int&quot;)+height2), _cvScalar(vScalar, 0), -1, 8, 0)
   _cvRectangle(pimg, _cvPoint(NumGet(pmaxloc, 0, &quot;int&quot;)-5, NumGet(pmaxloc, 4, &quot;int&quot;)-5), _cvPoint(NumGet(pmaxloc, 0, &quot;int&quot;)+ width2+10, NumGet(pmaxloc, 4, &quot;int&quot;)+height2+10), _CV_RGB(vScalar, 255, 0, 0), 2, 8, 0)

   ;// Update input image
   _cvShowImage(window, pimg)

   ;// Find location of maximum
   _cvMinMaxLoc(presult, &amp;pminval, &amp;pmaxval, &amp;pminloc, &amp;pmaxloc, pmask)
   if (NumGet(pmaxval, 0, &quot;double&quot;) &lt; 0.99)
      break
   sleep 1000
}
msgbox done
_cvReleaseImage(pmask)
_cvReleaseMat(presult)
_cvReleaseImage(ptemp)
_cvReleaseImage(pimg)
_cvDestroyAllWindows()
_Opencv_CloseDLL()
ExitApp</code></pre></div><p><a href="http://forum.script-coding.com/viewtopic.php?id=14477">Тема для обсуждения</a></p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Sun, 12 Mar 2023 01:51:50 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=157090#p157090</guid>
		</item>
	</channel>
</rss>
