<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: Искусственный интеллект и машинное обучение Win10]]></title>
		<link>http://forum.script-coding.com/viewtopic.php?id=17644</link>
		<atom:link href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=17644&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: Искусственный интеллект и машинное обучение Win10».]]></description>
		<lastBuildDate>Fri, 10 Mar 2023 13:25:03 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[AHK: Искусственный интеллект и машинное обучение Win10]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=157072#p157072</link>
			<description><![CDATA[<p><a href="https://docs.microsoft.com/en-us/windows/ai/">https://docs.microsoft.com/en-us/windows/ai/</a><br />Необходимо скачать одну из моделей отсюда:<br /><a href="https://github.com/onnx/models">https://github.com/onnx/models</a><br />Я тестировал с этой:<br /><a href="https://github.com/onnx/models/blob/master/vision/classification/squeezenet/model/squeezenet1.0-9.onnx">https://github.com/onnx/models/blob/mas … 1.0-9.onnx</a><br />Для этой модели необходимо скачать классификацию в виде txt файла отсюда.<br /><a href="https://github.com/onnx/models/blob/master/vision/classification/synset.txt">https://github.com/onnx/models/blob/mas … synset.txt</a><br />После этого открываем эту модель в приложении netron. <br /><a href="https://netron.app/">https://netron.app/</a><br />И смотрим ее свойства.<br />Например squeezenet1.0-9.onnx имеет<br />data_0 as input<br />softmaxout_1 as output<br />and output type as type: float32[1,1000,1,1].<br />Все эти данные нужно добавить в скрипт ahk.<br />После чего выделить файл с изображением в проводнике и нажать f11.<br />В результате скрипт должен распознать то, что изображено на картинке. <br /></p><div class="codebox"><pre><code>setbatchlines -1
global labelsFile := &quot;labels.txt&quot;
global ONNXmodelPath := &quot;squeezenet1.0-9.onnx&quot;
global ONNXmodelInputName := &quot;data_0&quot;
global ONNXmodelOutputName := &quot;softmaxout_1&quot;
global ONNXmodelOutputShape := [1, 1000, 1, 1]

f11::
file := Explorer_GetSelection()
msgbox % MachineLearning(file)
return


MachineLearning(file)
{
   static init, labels, TensorFloat, hONNXmodelInputName, hONNXmodelOutputName, hRunId, BitmapDecoderStatics, GUID, VideoFrameStatics, ImageFeatureValueStatics, LearningModelSession, LearningModelBinding
   if !init
   {
      fileread, labels, % labelsFile
      global bufs := {}
      Iterable := IIterable_new()
      global Iterator := IIterator_new()
      CreateClass(&quot;Windows.AI.MachineLearning.TensorFloat&quot;, ITensorFloatStatics := &quot;{DBCD395B-3BA3-452F-B10D-3C135E573FA9}&quot;, TensorFloatStatics)
      DllCall(NumGet(NumGet(TensorFloatStatics+0)+7*A_PtrSize), &quot;ptr&quot;, TensorFloatStatics, &quot;ptr&quot;, Iterable, &quot;ptr*&quot;, TensorFloat)   ; TensorFloatStatics.Create
      CreateHString(ONNXmodelInputName, hONNXmodelInputName)
      CreateHString(ONNXmodelOutputName, hONNXmodelOutputName)
      CreateHString(&quot;RunId&quot;, hRunId)
      CreateClass(&quot;Windows.Graphics.Imaging.BitmapDecoder&quot;, IBitmapDecoderStatics := &quot;{438CCB26-BCEF-4E95-BAD6-23A822E58D01}&quot;, BitmapDecoderStatics)
      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)
   BitmapFrameWithSoftwareBitmap := ComObjQuery(BitmapDecoder, IBitmapFrameWithSoftwareBitmap := &quot;{FE287C9A-420C-4963-87AD-691436E08383}&quot;)
   DllCall(NumGet(NumGet(BitmapFrameWithSoftwareBitmap+0)+6*A_PtrSize), &quot;ptr&quot;, BitmapFrameWithSoftwareBitmap, &quot;ptr*&quot;, SoftwareBitmap)   ; GetSoftwareBitmapAsync
   WaitForAsync(SoftwareBitmap)
   if !init
      CreateClass(&quot;Windows.Media.VideoFrame&quot;, IVideoFrameStatics := &quot;{AB2A556F-6111-4B33-8EC3-2B209A02E17A}&quot;, VideoFrameStatics)
   DllCall(NumGet(NumGet(VideoFrameStatics+0)+8*A_PtrSize), &quot;ptr&quot;, VideoFrameStatics, &quot;ptr&quot;, SoftwareBitmap, &quot;ptr*&quot;, VideoFrame)   ; VideoFrameStatics.CreateWithSoftwareBitmap
   if !init
      CreateClass(&quot;Windows.AI.MachineLearning.ImageFeatureValue&quot;, IImageFeatureValueStatics := &quot;{1BC317FD-23CB-4610-B085-C8E1C87EBAA0}&quot;, ImageFeatureValueStatics)
   DllCall(NumGet(NumGet(ImageFeatureValueStatics+0)+6*A_PtrSize), &quot;ptr&quot;, ImageFeatureValueStatics, &quot;ptr&quot;, VideoFrame, &quot;ptr*&quot;, ImageFeatureValue)   ; ImageFeatureValueStatics.CreateFromVideoFrame
   if !init
   {
      CreateClass(&quot;Windows.AI.MachineLearning.LearningModel&quot;, ILearningModelStatics := &quot;{E3B977E8-6952-4E47-8EF4-1F7F07897C6D}&quot;, LearningModelStatics)
      CreateHString(ONNXmodelPath, hString)
      DllCall(NumGet(NumGet(LearningModelStatics+0)+8*A_PtrSize), &quot;ptr&quot;, LearningModelStatics, &quot;ptr&quot;, hString, &quot;ptr*&quot;, LearningModel)   ; LearningModel.LoadFromFilePath
      DeleteHString(hString)
      CreateClass(&quot;Windows.AI.MachineLearning.LearningModelDevice&quot;, ILearningModelDeviceFactory := &quot;{9CFFD74D-B1E5-4F20-80AD-0A56690DB06B}&quot;, LearningModelDeviceFactory)
      DllCall(NumGet(NumGet(LearningModelDeviceFactory+0)+6*A_PtrSize), &quot;ptr&quot;, LearningModelDeviceFactory, &quot;int&quot;, Cpu := 1, &quot;ptr*&quot;, LearningModelDevice)   ; LearningModelDeviceFactory.Create
      CreateClass(&quot;Windows.AI.MachineLearning.LearningModelSession&quot;, ILearningModelSessionFactory := &quot;{0F6B881D-1C9B-47B6-BFE0-F1CF62A67579}&quot;, LearningModelSessionFactory)
      DllCall(NumGet(NumGet(LearningModelSessionFactory+0)+7*A_PtrSize), &quot;ptr&quot;, LearningModelSessionFactory, &quot;ptr&quot;, LearningModel, &quot;ptr&quot;, LearningModelDevice, &quot;ptr*&quot;, LearningModelSession)   ; LearningModelSessionFactory.CreateFromModelOnDevice
      CreateClass(&quot;Windows.AI.MachineLearning.LearningModelBinding&quot;, ILearningModelBindingFactory := &quot;{C95F7A7A-E788-475E-8917-23AA381FAF0B}&quot;, LearningModelBindingFactory)
      DllCall(NumGet(NumGet(LearningModelBindingFactory+0)+6*A_PtrSize), &quot;ptr&quot;, LearningModelBindingFactory, &quot;ptr&quot;, LearningModelSession, &quot;ptr*&quot;, LearningModelBinding)   ; LearningModelBindingFactory.CreateFromSession
      ObjReleaseClose(TensorFloatStatics)
      ObjReleaseClose(LearningModelStatics)
      ObjReleaseClose(LearningModel)
      ObjReleaseClose(LearningModelDeviceFactory)
      ObjReleaseClose(LearningModelDevice)
      ObjReleaseClose(LearningModelSessionFactory)
      ObjReleaseClose(LearningModelBindingFactory)
      init := 1
   }
   else
      DllCall(NumGet(NumGet(LearningModelBinding+0)+8*A_PtrSize), &quot;ptr&quot;, LearningModelBinding)   ; LearningModelBinding.Clear
   DllCall(NumGet(NumGet(LearningModelBinding+0)+6*A_PtrSize), &quot;ptr&quot;, LearningModelBinding, &quot;ptr&quot;, hONNXmodelInputName, &quot;ptr&quot;, ImageFeatureValue)   ; LearningModelBinding.Bind
   DllCall(NumGet(NumGet(LearningModelBinding+0)+6*A_PtrSize), &quot;ptr&quot;, LearningModelBinding, &quot;ptr&quot;, hONNXmodelOutputName, &quot;ptr&quot;, TensorFloat)   ; LearningModelBinding.Bind
   DllCall(NumGet(NumGet(LearningModelSession+0)+11*A_PtrSize), &quot;ptr&quot;, LearningModelSession, &quot;ptr&quot;, LearningModelBinding, &quot;ptr&quot;, hRunId, &quot;ptr*&quot;, LearningModelEvaluationResult)   ; LearningModelSession.Evaluate
   DllCall(NumGet(NumGet(LearningModelEvaluationResult+0)+9*A_PtrSize), &quot;ptr&quot;, LearningModelEvaluationResult, &quot;ptr*&quot;, Outputs)   ; LearningModelEvaluationResult.Outputs
   DllCall(NumGet(NumGet(Outputs+0)+6*A_PtrSize), &quot;ptr&quot;, Outputs, &quot;ptr&quot;, hONNXmodelOutputName, &quot;ptr*&quot;, TensorFloatValue)   ; IMapView.Lookup
   DllCall(NumGet(NumGet(TensorFloatValue+0)+6*A_PtrSize), &quot;ptr&quot;, TensorFloatValue, &quot;ptr*&quot;, TensorFloatVectorView)   ; TensorFloat.GetAsVectorView
   DllCall(NumGet(NumGet(TensorFloatVectorView+0)+7*A_PtrSize), &quot;ptr&quot;, TensorFloatVectorView, &quot;int*&quot;, size)   ; IVectorView.GetSize
   loop % size
   {
      DllCall(NumGet(NumGet(TensorFloatVectorView+0)+6*A_PtrSize), &quot;ptr&quot;, TensorFloatVectorView, &quot;int&quot;, A_Index - 1, &quot;float*&quot;, value)   ; IVectorView.GetAt
      result .= value &quot;-&quot; A_Index &quot;`n&quot;
   }
   ObjReleaseClose(IRandomAccessStream)
   ObjReleaseClose(BitmapDecoder)
   ObjReleaseClose(BitmapFrameWithSoftwareBitmap)
   ObjReleaseClose(SoftwareBitmap)
   ObjReleaseClose(VideoFrame)
   ObjReleaseClose(ImageFeatureValue)
   ObjReleaseClose(LearningModelEvaluationResult)
   ObjReleaseClose(Outputs)
   ObjReleaseClose(TensorFloatValue)
   ObjReleaseClose(TensorFloatVectorView)
   result := SubStr(result, 1, -1)
   Sort, result, NR
   Loop, parse, result, `n
   {
      result_part := StrSplit(A_LoopField, &quot;-&quot;)
      resultParsed .= RegexReplace(labels, &quot;s)^(?:.*?\R){&quot; result_part[2]-1 &quot;}.+? (.+?)(?:\R|$).*$&quot;, &quot;$1&quot;) &quot; with confidence of &quot; result_part[1] &quot;`n&quot;
      if (A_Index = 3)
         break
   }
   return resultParsed
}



CreateClass(string, interface, ByRef Class)
{
   CreateHString(string, hString)
   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
   ObjReleaseClose(Object)
   Object := ObjectResult
}

ObjReleaseClose(ByRef Object)
{
   if Object
   {
      if (Close := ComObjQuery(Object, IClosable := &quot;{30D5A829-7FA4-4026-83BB-D75BAE4EA99E}&quot;))
      {
         DllCall(NumGet(NumGet(Close+0)+6*A_PtrSize), &quot;ptr&quot;, Close)   ; Close
         ObjRelease(Close)
      }
      ObjRelease(Object)
      Object := &quot;&quot;
   }
}

IIterable_new()
{
   VTBL := [&quot;QueryInterface&quot;, &quot;AddRef&quot;, &quot;Release&quot;, &quot;GetIids&quot;, &quot;GetRuntimeClassName&quot;, &quot;GetTrustLevel&quot;, &quot;First&quot;]
   bufs.SetCapacity(1, (VTBL.maxindex()+1)*A_PtrSize)
   buf := bufs.GetAddress(1)
   for k, v in VTBL
   {
      if (k = 7)
         NumPut(RegisterCallback(&quot;IIterable_&quot; v), buf + k*A_PtrSize)
   }
   NumPut(buf + A_PtrSize, buf + 0)
   return buf
}

IIterable_First(this, first)
{
   NumPut(Iterator, first+0, 0, &quot;ptr&quot;)
   return 
}

IIterator_new()
{
   VTBL := [&quot;QueryInterface&quot;, &quot;AddRef&quot;, &quot;Release&quot;, &quot;GetIids&quot;, &quot;GetRuntimeClassName&quot;, &quot;GetTrustLevel&quot;, &quot;get_Current&quot;, &quot;get_HasCurrent&quot;, &quot;MoveNext&quot;]
   bufs.SetCapacity(2, (VTBL.maxindex()+1)*A_PtrSize)
   buf := bufs.GetAddress(2)
   for k, v in VTBL
   {
      if k in 2,3,7,8,9
         NumPut(RegisterCallback(&quot;IIterator_&quot; v), buf + k*A_PtrSize)
   }
   NumPut(buf + A_PtrSize, buf + 0)
   return buf
}

IIterator_AddRef(this)
{
   return
}

IIterator_Release(this)
{
   return
}

IIterator_get_Current(this, current)
{
   numput(ONNXmodelOutputShape.RemoveAt(1), current+0, 0, &quot;int64&quot;)
   return
}

IIterator_get_HasCurrent(this, hasCurrent)
{
   numput(1, hasCurrent+0, 0, &quot;char&quot;)
   return
}

IIterator_MoveNext(this, hasCurrent)
{
   if (ONNXmodelOutputShape.Length() = 0)
      numput(0, hasCurrent+0, 0, &quot;char&quot;)
   else
      numput(1, hasCurrent+0, 0, &quot;char&quot;)
   return
}


Explorer_GetSelection() {
   WinGetClass, winClass, % &quot;ahk_id&quot; . hWnd := WinExist(&quot;A&quot;)
   if !(winClass ~=&quot;Progman|WorkerW|(Cabinet|Explore)WClass&quot;)
      Return
   
   shellWindows := ComObjCreate(&quot;Shell.Application&quot;).Windows
   if (winClass ~= &quot;Progman|WorkerW&quot;)
      shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
   else {
      for window in shellWindows
         if (hWnd = window.HWND) &amp;&amp; (shellFolderView := window.Document)
            break
   }
   for item in shellFolderView.SelectedItems
      result .= (result = &quot;&quot; ? &quot;&quot; : &quot;`n&quot;) . item.Path
   if !result
      result := shellFolderView.Folder.Self.Path
   Return result
}</code></pre></div><p><a href="http://forum.script-coding.com/viewtopic.php?id=16766">Тема для обсуждения</a></p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Fri, 10 Mar 2023 13:25:03 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=157072#p157072</guid>
		</item>
	</channel>
</rss>
