1

Тема: AHK: Искусственный интеллект и машинное обучение Win10

https://docs.microsoft.com/en-us/windows/ai/
Необходимо скачать одну из моделей отсюда:
https://github.com/onnx/models
Я тестировал с этой:
https://github.com/onnx/models/blob/mas … 1.0-9.onnx
Для этой модели необходимо скачать классификацию в виде txt файла отсюда.
https://github.com/onnx/models/blob/mas … synset.txt
После этого открываем эту модель в приложении netron.
https://netron.app/
И смотрим ее свойства.
Например squeezenet1.0-9.onnx имеет
data_0 as input
softmaxout_1 as output
and output type as type: float32[1,1000,1,1].
Все эти данные нужно добавить в скрипт ahk.
После чего выделить файл с изображением в проводнике и нажать f11.
В результате скрипт должен распознать то, что изображено на картинке.

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



CreateClass(string, interface, ByRef Class)
{
   CreateHString(string, hString)
   VarSetCapacity(GUID, 16)
   DllCall("ole32\CLSIDFromString", "wstr", interface, "ptr", &GUID)
   result := DllCall("Combase.dll\RoGetActivationFactory", "ptr", hString, "ptr", &GUID, "ptr*", Class, "uint")
   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("Combase.dll\WindowsCreateString", "wstr", string, "uint", StrLen(string), "ptr*", hString)
}

DeleteHString(hString)
{
   DllCall("Combase.dll\WindowsDeleteString", "ptr", hString)
}

WaitForAsync(ByRef Object)
{
   AsyncInfo := ComObjQuery(Object, IAsyncInfo := "{00000036-0000-0000-C000-000000000046}")
   loop
   {
      DllCall(NumGet(NumGet(AsyncInfo+0)+7*A_PtrSize), "ptr", AsyncInfo, "uint*", status)   ; IAsyncInfo.Status
      if (status != 0)
      {
         if (status != 1)
         {
            DllCall(NumGet(NumGet(AsyncInfo+0)+8*A_PtrSize), "ptr", AsyncInfo, "uint*", ErrorCode)   ; IAsyncInfo.ErrorCode
            msgbox AsyncInfo status error: %ErrorCode%
            ExitApp
         }
         ObjRelease(AsyncInfo)
         break
      }
      sleep 10
   }
   DllCall(NumGet(NumGet(Object+0)+8*A_PtrSize), "ptr", Object, "ptr*", ObjectResult)   ; GetResults
   ObjReleaseClose(Object)
   Object := ObjectResult
}

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

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

IIterable_First(this, first)
{
   NumPut(Iterator, first+0, 0, "ptr")
   return 
}

IIterator_new()
{
   VTBL := ["QueryInterface", "AddRef", "Release", "GetIids", "GetRuntimeClassName", "GetTrustLevel", "get_Current", "get_HasCurrent", "MoveNext"]
   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("IIterator_" 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, "int64")
   return
}

IIterator_get_HasCurrent(this, hasCurrent)
{
   numput(1, hasCurrent+0, 0, "char")
   return
}

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


Explorer_GetSelection() {
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
      Return
   
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")
      shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
   else {
      for window in shellWindows
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   for item in shellFolderView.SelectedItems
      result .= (result = "" ? "" : "`n") . item.Path
   if !result
      result := shellFolderView.Folder.Self.Path
   Return result
}

Тема для обсуждения