<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Распознавание лиц (Face detection) с UWP API (Win10)]]></title>
	<link rel="self" href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=17673&amp;type=atom" />
	<updated>2023-03-12T15:03:21Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.script-coding.com/viewtopic.php?id=17673</id>
		<entry>
			<title type="html"><![CDATA[AHK: Распознавание лиц (Face detection) с UWP API (Win10)]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=157119#p157119" />
			<content type="html"><![CDATA[<p><a href="https://docs.microsoft.com/en-us/uwp/api/windows.media.faceanalysis">https://docs.microsoft.com/en-us/uwp/ap … ceanalysis</a><br />Чем больше картинка, тем дольше, но и возможно точнее происходит распознование.<br />Параметр maxheight := 2000 означает, что если высота картинки будет более 2000 пикселей, то энкодер ее уменьшит до 2000.<br />Число стоит подобрать под свои запросы.<br /></p><div class="codebox"><pre><code>msgbox % facedetect(&quot;face.jpg&quot;)
ExitApp



facedetect(file, maxheight := 2000)
{
   static BitmapDecoderStatics, BitmapTransform, SoftwareBitmapStatics, FaceDetector, SupportedBitmapPixelFormats
   if (FaceDetector = &quot;&quot;)
   {
      CreateClass(&quot;Windows.Graphics.Imaging.BitmapDecoder&quot;, IBitmapDecoderStatics := &quot;{438CCB26-BCEF-4E95-BAD6-23A822E58D01}&quot;, BitmapDecoderStatics)
      CreateClass(&quot;Windows.Graphics.Imaging.BitmapTransform&quot;,, BitmapTransform)
      CreateClass(&quot;Windows.Graphics.Imaging.SoftwareBitmap&quot;, ISoftwareBitmapStatics := &quot;{DF0385DB-672F-4A9D-806E-C2442F343E86}&quot;, SoftwareBitmapStatics)
      CreateClass(&quot;Windows.Media.FaceAnalysis.FaceDetector&quot;, IFaceDetectorStatics := &quot;{BC042D67-9047-33F6-881B-6746C1B218B8}&quot;, FaceDetectorStatics)
      DllCall(NumGet(NumGet(FaceDetectorStatics+0)+6*A_PtrSize), &quot;ptr&quot;, FaceDetectorStatics, &quot;ptr*&quot;, FaceDetector)   ; CreateAsync
      WaitForAsync(FaceDetector)
      DllCall(NumGet(NumGet(FaceDetectorStatics+0)+7*A_PtrSize), &quot;ptr&quot;, FaceDetectorStatics, &quot;ptr*&quot;, ReadOnlyList)   ; GetSupportedBitmapPixelFormats
      DllCall(NumGet(NumGet(ReadOnlyList+0)+7*A_PtrSize), &quot;ptr&quot;, ReadOnlyList, &quot;int*&quot;, count)   ; count
      loop % count
      {
         DllCall(NumGet(NumGet(ReadOnlyList+0)+6*A_PtrSize), &quot;ptr&quot;, ReadOnlyList, &quot;int&quot;, A_Index-1, &quot;uint*&quot;, BitmapPixelFormat)   ; get_Item
         SupportedBitmapPixelFormats .= &quot;|&quot; BitmapPixelFormat &quot;|&quot;
      }
      ObjRelease(FaceDetectorStatics)
      ObjRelease(ReadOnlyList)
   }
   if (SubStr(file, 2, 1) != &quot;:&quot;)
      file := A_ScriptDir &quot;\&quot; file
   if !FileExist(file) or InStr(FileExist(file), &quot;D&quot;)
   {
      msgbox File &quot;%file%&quot; does not exist
      ExitApp
   }   
   VarSetCapacity(GUID, 16)
   DllCall(&quot;ole32\CLSIDFromString&quot;, &quot;wstr&quot;, IID_RandomAccessStream := &quot;{905A0FE1-BC53-11DF-8C49-001E4FC686DA}&quot;, &quot;ptr&quot;, &amp;GUID)
   DllCall(&quot;ShCore\CreateRandomAccessStreamOnFile&quot;, &quot;wstr&quot;, file, &quot;uint&quot;, Read := 0, &quot;ptr&quot;, &amp;GUID, &quot;ptr*&quot;, IRandomAccessStream)
   DllCall(NumGet(NumGet(BitmapDecoderStatics+0)+14*A_PtrSize), &quot;ptr&quot;, BitmapDecoderStatics, &quot;ptr&quot;, IRandomAccessStream, &quot;ptr*&quot;, BitmapDecoder)   ; CreateAsync
   WaitForAsync(BitmapDecoder)
   BitmapFrame := ComObjQuery(BitmapDecoder, IBitmapFrame := &quot;{72A49A1C-8081-438D-91BC-94ECFC8185C6}&quot;)
   DllCall(NumGet(NumGet(BitmapFrame+0)+12*A_PtrSize), &quot;ptr&quot;, BitmapFrame, &quot;uint*&quot;, width)   ; get_PixelWidth
   DllCall(NumGet(NumGet(BitmapFrame+0)+13*A_PtrSize), &quot;ptr&quot;, BitmapFrame, &quot;uint*&quot;, height)   ; get_PixelHeight
   DllCall(NumGet(NumGet(BitmapFrame+0)+8*A_PtrSize), &quot;ptr&quot;, BitmapFrame, &quot;uint*&quot;, BitmapPixelFormat)   ; get_BitmapPixelFormat
   BitmapFrameWithSoftwareBitmap := ComObjQuery(BitmapDecoder, IBitmapFrameWithSoftwareBitmap := &quot;{FE287C9A-420C-4963-87AD-691436E08383}&quot;)
   if (height &gt; maxheight)
   {
      DllCall(NumGet(NumGet(BitmapTransform+0)+7*A_PtrSize), &quot;ptr&quot;, BitmapTransform, &quot;int&quot;, floor(maxheight/height*width))   ; put_ScaledWidth
      DllCall(NumGet(NumGet(BitmapTransform+0)+9*A_PtrSize), &quot;ptr&quot;, BitmapTransform, &quot;int&quot;, maxheight)   ; put_ScaledHeight
      DllCall(NumGet(NumGet(BitmapFrameWithSoftwareBitmap+0)+8*A_PtrSize), &quot;ptr&quot;, BitmapFrameWithSoftwareBitmap, &quot;uint&quot;, BitmapPixelFormat, &quot;uint&quot;, Premultiplied := 0, &quot;ptr&quot;, BitmapTransform, &quot;uint&quot;, IgnoreExifOrientation := 0, &quot;uint&quot;, DoNotColorManage := 0, &quot;ptr*&quot;, SoftwareBitmap)   ; GetSoftwareBitmapTransformedAsync
   }
   else
      DllCall(NumGet(NumGet(BitmapFrameWithSoftwareBitmap+0)+6*A_PtrSize), &quot;ptr&quot;, BitmapFrameWithSoftwareBitmap, &quot;ptr*&quot;, SoftwareBitmap)   ; GetSoftwareBitmapAsync
   WaitForAsync(SoftwareBitmap)
   if !InStr(SupportedBitmapPixelFormats, &quot;|&quot; BitmapPixelFormat &quot;|&quot;)
   {
      DllCall(NumGet(NumGet(SoftwareBitmapStatics+0)+7*A_PtrSize), &quot;ptr&quot;, SoftwareBitmapStatics, &quot;ptr&quot;, SoftwareBitmap, &quot;uint&quot;, Gray8 := 62, &quot;ptr*&quot;, SoftwareBitmapTemp)   ; Convert
      Close := ComObjQuery(SoftwareBitmap, IClosable := &quot;{30D5A829-7FA4-4026-83BB-D75BAE4EA99E}&quot;)
      DllCall(NumGet(NumGet(Close+0)+6*A_PtrSize), &quot;ptr&quot;, Close)   ; Close
      ObjRelease(Close)
      ObjRelease(SoftwareBitmap)
      SoftwareBitmap := SoftwareBitmapTemp
   }
   DllCall(NumGet(NumGet(FaceDetector+0)+6*A_PtrSize), &quot;ptr&quot;, FaceDetector, ptr, SoftwareBitmap, &quot;ptr*&quot;, DetectedFaceList)   ; DetectFacesAsync
   WaitForAsync(DetectedFaceList)
   DllCall(NumGet(NumGet(DetectedFaceList+0)+7*A_PtrSize), &quot;ptr&quot;, DetectedFaceList, &quot;int*&quot;, count)   ; count
   loop % count
   {
      varsetcapacity(bounds, 16, 0)
      DllCall(NumGet(NumGet(DetectedFaceList+0)+6*A_PtrSize), &quot;ptr&quot;, DetectedFaceList, &quot;int&quot;, A_Index-1, &quot;ptr*&quot;, DetectedFace)   ; get_Item
      DllCall(NumGet(NumGet(DetectedFace+0)+6*A_PtrSize), &quot;ptr&quot;, DetectedFace, &quot;ptr&quot;, &amp;bounds)   ; BitmapBounds
      x := numget(bounds, 0, &quot;uint&quot;)
      y := numget(bounds, 4, &quot;uint&quot;)
      width := numget(bounds, 8, &quot;uint&quot;)
      height := numget(bounds, 12, &quot;uint&quot;)
      result .= &quot;face&quot; A_Index &quot;: x=&quot; x &quot;, y=&quot; y &quot;, width=&quot; width &quot;, height=&quot; height &quot;`n&quot;
      ObjRelease(DetectedFace)
   }
   Close := ComObjQuery(IRandomAccessStream, IClosable := &quot;{30D5A829-7FA4-4026-83BB-D75BAE4EA99E}&quot;)
   DllCall(NumGet(NumGet(Close+0)+6*A_PtrSize), &quot;ptr&quot;, Close)   ; Close
   ObjRelease(Close)
   Close := ComObjQuery(SoftwareBitmap, IClosable := &quot;{30D5A829-7FA4-4026-83BB-D75BAE4EA99E}&quot;)
   DllCall(NumGet(NumGet(Close+0)+6*A_PtrSize), &quot;ptr&quot;, Close)   ; Close
   ObjRelease(Close)
   ObjRelease(IRandomAccessStream)
   ObjRelease(BitmapDecoder)
   ObjRelease(BitmapFrame)
   ObjRelease(BitmapFrameWithSoftwareBitmap)
   ObjRelease(SoftwareBitmap)
   ObjRelease(DetectedFaceList)
   return result
}



CreateClass(string, interface := &quot;&quot;, ByRef Class := &quot;&quot;)
{
   CreateHString(string, hString)
   if (interface = &quot;&quot;)
      result := DllCall(&quot;Combase.dll\RoActivateInstance&quot;, &quot;ptr&quot;, hString, &quot;ptr*&quot;, Class, &quot;uint&quot;)
   else
   {
      VarSetCapacity(GUID, 16)
      DllCall(&quot;ole32\CLSIDFromString&quot;, &quot;wstr&quot;, interface, &quot;ptr&quot;, &amp;GUID)
      result := DllCall(&quot;Combase.dll\RoGetActivationFactory&quot;, &quot;ptr&quot;, hString, &quot;ptr&quot;, &amp;GUID, &quot;ptr*&quot;, Class, &quot;uint&quot;)
   }
   if (result != 0)
   {
      if (result = 0x80004002)
         msgbox No such interface supported
      else if (result = 0x80040154)
         msgbox Class not registered
      else
         msgbox error: %result%
      ExitApp
   }
   DeleteHString(hString)
}

CreateHString(string, ByRef hString)
{
    DllCall(&quot;Combase.dll\WindowsCreateString&quot;, &quot;wstr&quot;, string, &quot;uint&quot;, StrLen(string), &quot;ptr*&quot;, hString)
}

DeleteHString(hString)
{
   DllCall(&quot;Combase.dll\WindowsDeleteString&quot;, &quot;ptr&quot;, hString)
}

WaitForAsync(ByRef Object)
{
   AsyncInfo := ComObjQuery(Object, IAsyncInfo := &quot;{00000036-0000-0000-C000-000000000046}&quot;)
   loop
   {
      DllCall(NumGet(NumGet(AsyncInfo+0)+7*A_PtrSize), &quot;ptr&quot;, AsyncInfo, &quot;uint*&quot;, status)   ; IAsyncInfo.Status
      if (status != 0)
      {
         if (status != 1)
         {
            DllCall(NumGet(NumGet(AsyncInfo+0)+8*A_PtrSize), &quot;ptr&quot;, AsyncInfo, &quot;uint*&quot;, ErrorCode)   ; IAsyncInfo.ErrorCode
            msgbox AsyncInfo status error: %ErrorCode%
            ExitApp
         }
         ObjRelease(AsyncInfo)
         break
      }
      sleep 10
   }
   DllCall(NumGet(NumGet(Object+0)+8*A_PtrSize), &quot;ptr&quot;, Object, &quot;ptr*&quot;, ObjectResult)   ; GetResults
   ObjRelease(Object)
   Object := ObjectResult
}</code></pre></div><p><a href="http://forum.script-coding.com/viewtopic.php?id=15199">Тема для обсуждения</a></p>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2023-03-12T15:03:21Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=157119#p157119</id>
		</entry>
</feed>
