<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Mediapipe]]></title>
	<link rel="self" href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=17640&amp;type=atom" />
	<updated>2023-03-10T12:25:31Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.script-coding.com/viewtopic.php?id=17640</id>
		<entry>
			<title type="html"><![CDATA[AHK: Mediapipe]]></title>
			<link rel="alternate" href="http://forum.script-coding.com/viewtopic.php?pid=157068#p157068" />
			<content type="html"><![CDATA[<p><strong>smbape</strong> проделал большую и сложную работу и создал библиотеку mediapipe com для autoit.<br /><a href="https://www.autoitscript.com/forum/topic/209339-mediapipe-udf/">https://www.autoitscript.com/forum/topi … apipe-udf/</a><br /><a href="https://google.github.io/mediapipe/">https://google.github.io/mediapipe/</a><br />На autohotkey можно запустить примерно так (пример обнаружения рук):<br /><a href="https://github.com/smbape/node-autoit-mediapipe-com/blob/main/examples/autoit/03-hands.au3">https://github.com/smbape/node-autoit-m … -hands.au3</a><br /></p><div class="codebox"><pre><code>ImagePath := &quot;C:\Users\malcev\Desktop\mediapipe\examples\data\brooke-cagle-mt2fyrdXxzk-unsplash.jpg&quot;

opencv_world_path := A_ScriptDir &quot;\opencv-4.7.0-windows\opencv\build\x64\vc16\bin\opencv_world470.dll&quot;
opencv_ffmpeg_path := A_ScriptDir &quot;\opencv-4.7.0-windows\opencv\build\x64\vc16\bin\opencv_videoio_ffmpeg470_64.dll&quot;
autoit_opencv_com_path := A_ScriptDir &quot;\autoit-opencv-com\autoit_opencv_com470.dll&quot;
autoit_mediapipe_com_path := A_ScriptDir &quot;\autoit-mediapipe-com\autoit_mediapipe_com-0.9.3.0-470.dll&quot;

hOpencv := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, opencv_world_path, &quot;ptr&quot;)
hOpencvFfmpeg := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, opencv_ffmpeg_path, &quot;ptr&quot;)
hOpencvCom := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, autoit_opencv_com_path, &quot;ptr&quot;)
hMediapipeCom := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, autoit_mediapipe_com_path, &quot;ptr&quot;)
ComObjCreate := Func(&quot;_ComObjCreate&quot;).Bind(autoit_opencv_com_path, autoit_mediapipe_com_path)
Mediapipe_Params := Func(&quot;_Mediapipe_Params&quot;).Bind(ComObjCreate)
resource_util := ComObjCreate.Call(&quot;Mediapipe.mediapipe.autoit._framework_bindings.resource_util&quot;)
resource_util.set_resource_dir(RegexReplace(autoit_mediapipe_com_path, &quot;^(.+)\\.*$&quot;, &quot;$1&quot;))

; comobject need to create with call.
cv := ComObjCreate.Call(&quot;OpenCV.cv&quot;)
mp := ComObjCreate.Call(&quot;Mediapipe.mediapipe&quot;)

image := cv.imread(ImagePath)
cv.imshow(&quot;Image&quot;, image)

mp_hands := mp.solutions.hands
mp_drawing := mp.solutions.drawing_utils
mp_drawing_styles := mp.solutions.drawing_styles

hands := mp_hands.Hands(Mediapipe_Params.Call({&quot;static_image_mode&quot;: &quot;True&quot;, &quot;max_num_hands&quot;: 2, &quot;min_detection_confidence&quot;: 0.7}))

; Convert the BGR image to RGB, flip the image around y-axis for correct handedness output and process it with MediaPipe Hands.
results := hands.process(cv.flip(cv.cvtColor(image, CV_COLOR_BGR2RGB := 4), 1))

loop % results.item(&quot;multi_handedness&quot;).MaxIndex() + 1
   msgbox % results.item(&quot;multi_handedness&quot;)[A_Index-1].__str__

if (results.item(&quot;multi_hand_landmarks&quot;) = -2147352572)
{
   msgbox No hand detection
   exitapp
}

; Draw hand landmarks of each hand.
image_width := image.width
image_height := image.height
annotated_image := cv.flip(image.copy(), 1)

; Print index finger tip coordinates
loop % results.item(&quot;multi_hand_landmarks&quot;).MaxIndex() + 1
{
   hand_landmarks := results.item(&quot;multi_hand_landmarks&quot;)[A_Index-1]
   msgbox % &quot;Index finger tip coordinate:`n&quot; hand_landmarks.landmark(mp_hands.HandLandmark.INDEX_FINGER_TIP).x * image_width &quot;`n&quot; hand_landmarks.landmark(mp_hands.HandLandmark.INDEX_FINGER_TIP).y * image_height
   mp_drawing.draw_landmarks(annotated_image, hand_landmarks, mp_hands.HAND_CONNECTIONS, mp_drawing_styles.get_default_hand_landmarks_style(), mp_drawing_styles.get_default_hand_connections_style())
}

cv.imshow(&quot;hands&quot;, cv.flip(annotated_image, 1))
cv.waitKey()
cv.destroyAllWindows()
return


_Mediapipe_Params(ComObjCreate, paramArray)
{
   static NamedParameters
   if !NamedParameters
      NamedParameters := ComObjCreate.Call(&quot;Mediapipe.NamedParameters&quot;)
   arr := ComObjArray(VT_VARIANT:=12, paramArray.Count())
   for k, v in paramArray
   {
      arr%k% := ComObjArray(VT_VARIANT:=12, 2)
      arr%k%[0] := k
      if v is float
         v+=0
      else if (v = &quot;True&quot;)
         v := ComObj(0xB, -1)
      else if (v = &quot;False&quot;)
         v := ComObj(0xB, 0)
      else if (v = &quot;Null&quot;)
         v := ComObj(1, 0)
      arr%k%[1] := v
      arr[A_Index-1] := arr%k%
   }
   params := NamedParameters.create(arr)
   return params
}

_ComObjCreate(opencvPath, mediapipePath, comobject)
{
   if InStr(comobject, &quot;opencv&quot;)
      path := opencvPath
   else
      path := mediapipePath
   DllCall(path &quot;\DllActivateManifest&quot;)
   comobject := ComObjCreate(comobject)
   DllCall(path &quot;\DllDeactivateActCtx&quot;)
   return comobject
}</code></pre></div><p>Пример Face mesh:<br /><a href="https://github.com/smbape/node-autoit-mediapipe-com/blob/v0.3.0/examples/autoit/02-face_mesh.au3">https://github.com/smbape/node-autoit-m … e_mesh.au3</a><br /></p><div class="codebox"><pre><code>opencv_world_path := A_ScriptDir &quot;\opencv-4.7.0-windows\opencv\build\x64\vc16\bin\opencv_world470.dll&quot;
opencv_ffmpeg_path := A_ScriptDir &quot;\opencv-4.7.0-windows\opencv\build\x64\vc16\bin\opencv_videoio_ffmpeg470_64.dll&quot;
autoit_opencv_com_path := A_ScriptDir &quot;\autoit-opencv-com\autoit_opencv_com470.dll&quot;
autoit_mediapipe_com_path := A_ScriptDir &quot;\autoit-mediapipe-com\autoit_mediapipe_com-0.9.3.0-470.dll&quot;

hOpencv := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, opencv_world_path, &quot;ptr&quot;)
hOpencvFfmpeg := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, opencv_ffmpeg_path, &quot;ptr&quot;)
hOpencvCom := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, autoit_opencv_com_path, &quot;ptr&quot;)
hMediapipeCom := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, autoit_mediapipe_com_path, &quot;ptr&quot;)
ComObjCreate := Func(&quot;_ComObjCreate&quot;).Bind(autoit_opencv_com_path, autoit_mediapipe_com_path)
Mediapipe_Params := Func(&quot;_Mediapipe_Params&quot;).Bind(ComObjCreate)
resource_util := ComObjCreate.Call(&quot;Mediapipe.mediapipe.autoit._framework_bindings.resource_util&quot;)
resource_util.set_resource_dir(RegexReplace(autoit_mediapipe_com_path, &quot;^(.+)\\.*$&quot;, &quot;$1&quot;))

cv := ComObjCreate.Call(&quot;OpenCV.cv&quot;)
mp := ComObjCreate.Call(&quot;Mediapipe.mediapipe&quot;)
image_path := A_ScriptDir &quot;/testdata/portrait.jpg&quot;
download_utils := mp.solutions.download_utils
download_utils.download(&quot;https://github.com/tensorflow/tfjs-models/raw/master/face-detection/test_data/portrait.jpg&quot;, image_path)

mp_face_mesh := mp.solutions.face_mesh
mp_drawing := mp.solutions.drawing_utils
mp_drawing_styles := mp.solutions.drawing_styles

image := cv.imread(image_path)
cv.imshow(&quot;Image&quot;, image)

face_mesh := mp_face_mesh.FaceMesh(Mediapipe_Params.Call({&quot;static_image_mode&quot;: &quot;True&quot;, &quot;refine_landmarks&quot;: &quot;True&quot;, &quot;max_num_faces&quot;: 2, &quot;min_detection_confidence&quot;: 0.5}))

; Convert the BGR image to RGB, and process it with MediaPipe Face Mesh.
results := face_mesh.process(cv.cvtColor(image, CV_COLOR_BGR2RGB := 4))

loop % results.item(&quot;multi_face_landmarks&quot;).MaxIndex() + 1
   msgbox % results.item(&quot;multi_face_landmarks&quot;)[A_Index-1].__str__

if (results.item(&quot;multi_face_landmarks&quot;) = -2147352572)
{
   msgbox No face detection
   exitapp
}

; Draw face detections of each face.
image_width := image.width
image_height := image.height
annotated_image := image.copy()

loop % results.item(&quot;multi_face_landmarks&quot;).MaxIndex() + 1
{
   face_landmarks := results.item(&quot;multi_face_landmarks&quot;)[A_Index-1]
   mp_drawing.draw_landmarks(Mediapipe_Params.Call({&quot;image&quot;: annotated_image, &quot;landmark_list&quot;: face_landmarks, &quot;connections&quot;: mp_face_mesh.FACEMESH_TESSELATION, &quot;landmark_drawing_spec&quot;: &quot;Null&quot;, &quot;connection_drawing_spec&quot;: mp_drawing_styles.get_default_face_mesh_tesselation_style(1)}))
   mp_drawing.draw_landmarks(Mediapipe_Params.Call({&quot;image&quot;: annotated_image, &quot;landmark_list&quot;: face_landmarks, &quot;connections&quot;: mp_face_mesh.FACEMESH_CONTOURS, &quot;landmark_drawing_spec&quot;: &quot;Null&quot;, &quot;connection_drawing_spec&quot;: mp_drawing_styles.get_default_face_mesh_contours_style(1)}))
   mp_drawing.draw_landmarks(Mediapipe_Params.Call({&quot;image&quot;: annotated_image, &quot;landmark_list&quot;: face_landmarks, &quot;connections&quot;: mp_face_mesh.FACEMESH_IRISES, &quot;landmark_drawing_spec&quot;: &quot;Null&quot;, &quot;connection_drawing_spec&quot;: mp_drawing_styles.get_default_face_mesh_iris_connections_style(1)}))
}

cv.imshow(&quot;face mesh&quot;, annotated_image)
cv.waitKey()
cv.destroyAllWindows()
return


_Mediapipe_Params(ComObjCreate, paramArray)
{
   static NamedParameters
   if !NamedParameters
      NamedParameters := ComObjCreate.Call(&quot;Mediapipe.NamedParameters&quot;)
   arr := ComObjArray(VT_VARIANT:=12, paramArray.Count())
   for k, v in paramArray
   {
      arr%k% := ComObjArray(VT_VARIANT:=12, 2)
      arr%k%[0] := k
      if v is float
         v+=0
      else if (v = &quot;True&quot;)
         v := ComObj(0xB, -1)
      else if (v = &quot;False&quot;)
         v := ComObj(0xB, 0)
      else if (v = &quot;Null&quot;)
         v := ComObj(1, 0)
      arr%k%[1] := v
      arr[A_Index-1] := arr%k%
   }
   params := NamedParameters.create(arr)
   return params
}

_ComObjCreate(opencvPath, mediapipePath, comobject)
{
   if InStr(comobject, &quot;opencv&quot;)
      path := opencvPath
   else
      path := mediapipePath
   DllCall(path &quot;\DllActivateManifest&quot;)
   comobject := ComObjCreate(comobject)
   DllCall(path &quot;\DllDeactivateActCtx&quot;)
   return comobject
}</code></pre></div><p>Пример классификации текста:<br /><a href="https://github.com/smbape/node-autoit-mediapipe-com/blob/v0.4.0/examples/googlesamples/examples/text_classification/autoit/text_classifier.au3">https://github.com/smbape/node-autoit-m … sifier.au3</a><br />Надо скачать готовую модель отсюда:<br /><a href="https://storage.googleapis.com/mediapipe-tasks/text_classifier/bert_text_classifier.tflite">https://storage.googleapis.com/mediapip … ier.tflite</a><br />и сохранить в папке со скриптом.<br /></p><div class="codebox"><pre><code>input_text := &quot;I&#039;m looking forward to what will come next.&quot;

opencv_world_path := A_ScriptDir &quot;\opencv-4.7.0-windows\opencv\build\x64\vc16\bin\opencv_world470.dll&quot;
opencv_ffmpeg_path := A_ScriptDir &quot;\opencv-4.7.0-windows\opencv\build\x64\vc16\bin\opencv_videoio_ffmpeg470_64.dll&quot;
autoit_opencv_com_path := A_ScriptDir &quot;\autoit-opencv-com\autoit_opencv_com470.dll&quot;
autoit_mediapipe_com_path := A_ScriptDir &quot;\autoit-mediapipe-com\autoit_mediapipe_com-0.9.3.0-470.dll&quot;
model_file := A_ScriptDir &quot;\bert_text_classifier.tflite&quot;

hOpencv := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, opencv_world_path, &quot;ptr&quot;)
hOpencvFfmpeg := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, opencv_ffmpeg_path, &quot;ptr&quot;)
hOpencvCom := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, autoit_opencv_com_path, &quot;ptr&quot;)
hMediapipeCom := DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, autoit_mediapipe_com_path, &quot;ptr&quot;)
ComObjCreate := Func(&quot;_ComObjCreate&quot;).Bind(autoit_opencv_com_path, autoit_mediapipe_com_path)
Mediapipe_Params := Func(&quot;_Mediapipe_Params&quot;).Bind(ComObjCreate)
resource_util := ComObjCreate.Call(&quot;Mediapipe.mediapipe.autoit._framework_bindings.resource_util&quot;)
resource_util.set_resource_dir(RegexReplace(autoit_mediapipe_com_path, &quot;^(.+)\\.*$&quot;, &quot;$1&quot;))

; comobject need to create with call.
mp := ComObjCreate.Call(&quot;Mediapipe.mediapipe&quot;)
autoit := ComObjCreate.Call(&quot;Mediapipe.mediapipe.tasks.autoit&quot;)
text := ComObjCreate.Call(&quot;Mediapipe.mediapipe.tasks.autoit.text&quot;)

; Create an TextClassifier object.
base_options := autoit.BaseOptions(Mediapipe_Params.Call({&quot;model_asset_path&quot;: model_file}))
options := text.TextClassifierOptions(Mediapipe_Params.Call({&quot;base_options&quot;: base_options}))
classifier := text.TextClassifier.create_from_options(options)

; Classify the input text.
classification_result := classifier.classify(input_text)

; Process the classification result. In this case, print out the most likely category.
top_category := classification_result.classifications(0).categories(0)
msgbox % top_category.category_name &quot; - &quot; top_category.score


_Mediapipe_Params(ComObjCreate, paramArray)
{
   static NamedParameters
   if !NamedParameters
      NamedParameters := ComObjCreate.Call(&quot;Mediapipe.NamedParameters&quot;)
   arr := ComObjArray(VT_VARIANT:=12, paramArray.Count())
   for k, v in paramArray
   {
      arr%k% := ComObjArray(VT_VARIANT:=12, 2)
      arr%k%[0] := k
      if v is float
         v+=0
      else if (v = &quot;True&quot;)
         v := ComObj(0xB, -1)
      else if (v = &quot;False&quot;)
         v := ComObj(0xB, 0)
      else if (v = &quot;Null&quot;)
         v := ComObj(1, 0)
      arr%k%[1] := v
      arr[A_Index-1] := arr%k%
   }
   params := NamedParameters.create(arr)
   return params
}

_ComObjCreate(opencvPath, mediapipePath, comobject)
{
   if InStr(comobject, &quot;opencv&quot;)
      path := opencvPath
   else
      path := mediapipePath
   DllCall(path &quot;\DllActivateManifest&quot;)
   comobject := ComObjCreate(comobject)
   DllCall(path &quot;\DllDeactivateActCtx&quot;)
   return comobject
}</code></pre></div><p>Хорошие уроки для начала изучения opencv+mediapipe:<br /><a href="https://www.youtube.com/watch?v=01sAkU_NvOY">https://www.youtube.com/watch?v=01sAkU_NvOY</a><br /><a href="http://forum.script-coding.com/viewtopic.php?id=17524">Тема для обсуждения</a></p>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>http://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2023-03-10T12:25:31Z</updated>
			<id>http://forum.script-coding.com/viewtopic.php?pid=157068#p157068</id>
		</entry>
</feed>
