Advertisement
tabnation

ahk face dect

Jan 20th, 2022
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. f1::
  2. Path=%A_Desktop%\facepics\*.JPG
  3. global picswithface
  4.  
  5. Loop,Files,% Path
  6. {
  7. file=%A_Desktop%\facepics\%A_LoopFileName%
  8. facedetect(file)
  9. }
  10.  
  11. msgbox,HAS FACES`n---------------------------%picswithface%
  12. ExitApp
  13. ;return
  14.  
  15. facedetect(file, maxheight := 2000)
  16. {
  17. static BitmapDecoderStatics, BitmapEncoderStatics, SoftwareBitmapStatics, FaceDetector, SupportedBitmapPixelFormats
  18. if (FaceDetector = "")
  19. {
  20. CreateClass("Windows.Graphics.Imaging.BitmapDecoder", IBitmapDecoderStatics := "{438CCB26-BCEF-4E95-BAD6-23A822E58D01}", BitmapDecoderStatics)
  21. CreateClass("Windows.Graphics.Imaging.BitmapEncoder", IBitmapEncoderStatics := "{A74356A7-A4E4-4EB9-8E40-564DE7E1CCB2}", BitmapEncoderStatics)
  22. CreateClass("Windows.Graphics.Imaging.SoftwareBitmap", ISoftwareBitmapStatics := "{DF0385DB-672F-4A9D-806E-C2442F343E86}", SoftwareBitmapStatics)
  23. CreateClass("Windows.Media.FaceAnalysis.FaceDetector", IFaceDetectorStatics := "{BC042D67-9047-33F6-881B-6746C1B218B8}", FaceDetectorStatics)
  24. DllCall(NumGet(NumGet(FaceDetectorStatics+0)+6*A_PtrSize), "ptr", FaceDetectorStatics, "ptr*", FaceDetector) ; CreateAsync
  25. WaitForAsync(FaceDetector)
  26. DllCall(NumGet(NumGet(FaceDetectorStatics+0)+7*A_PtrSize), "ptr", FaceDetectorStatics, "ptr*", ReadOnlyList) ; GetSupportedBitmapPixelFormats
  27. DllCall(NumGet(NumGet(ReadOnlyList+0)+7*A_PtrSize), "ptr", ReadOnlyList, "int*", count) ; count
  28. loop % count
  29. {
  30. DllCall(NumGet(NumGet(ReadOnlyList+0)+6*A_PtrSize), "ptr", ReadOnlyList, "int", A_Index-1, "uint*", BitmapPixelFormat) ; get_Item
  31. SupportedBitmapPixelFormats .= "|" BitmapPixelFormat "|"
  32. }
  33. ObjRelease(FaceDetectorStatics)
  34. ObjRelease(ReadOnlyList)
  35. }
  36. if (SubStr(file, 2, 1) != ":")
  37. file := A_ScriptDir "\" file
  38. if !FileExist(file) or InStr(FileExist(file), "D")
  39. {
  40. msgbox File "%file%" does not exist
  41. ExitApp
  42. }
  43. VarSetCapacity(GUID, 16)
  44. DllCall("ole32\CLSIDFromString", "wstr", IID_RandomAccessStream := "{905A0FE1-BC53-11DF-8C49-001E4FC686DA}", "ptr", &GUID)
  45. DllCall("ShCore\CreateRandomAccessStreamOnFile", "wstr", file, "uint", Read := 0, "ptr", &GUID, "ptr*", IRandomAccessStream)
  46. DllCall(NumGet(NumGet(BitmapDecoderStatics+0)+14*A_PtrSize), "ptr", BitmapDecoderStatics, "ptr", IRandomAccessStream, "ptr*", BitmapDecoder) ; CreateAsync
  47. WaitForAsync(BitmapDecoder)
  48. BitmapFrame := ComObjQuery(BitmapDecoder, IBitmapFrame := "{72A49A1C-8081-438D-91BC-94ECFC8185C6}")
  49. DllCall(NumGet(NumGet(BitmapFrame+0)+12*A_PtrSize), "ptr", BitmapFrame, "uint*", width) ; get_PixelWidth
  50. DllCall(NumGet(NumGet(BitmapFrame+0)+13*A_PtrSize), "ptr", BitmapFrame, "uint*", height) ; get_PixelHeight
  51. DllCall(NumGet(NumGet(BitmapFrame+0)+8*A_PtrSize), "ptr", BitmapFrame, "uint*", BitmapPixelFormat) ; get_BitmapPixelFormat
  52. BitmapFrameWithSoftwareBitmap := ComObjQuery(BitmapDecoder, IBitmapFrameWithSoftwareBitmap := "{FE287C9A-420C-4963-87AD-691436E08383}")
  53. if (height > maxheight)
  54. {
  55. DllCall(NumGet(NumGet(BitmapEncoderStatics+0)+15*A_PtrSize), "ptr", BitmapEncoderStatics, "ptr", IRandomAccessStream, "ptr", BitmapDecoder, "ptr*", BitmapEncoder) ; CreateForTranscodingAsync
  56. WaitForAsync(BitmapEncoder)
  57. DllCall(NumGet(NumGet(BitmapEncoder+0)+15*A_PtrSize), "ptr", BitmapEncoder, "ptr*", BitmapTransform) ; BitmapTransform
  58. DllCall(NumGet(NumGet(BitmapTransform+0)+7*A_PtrSize), "ptr", BitmapTransform, "int", floor(maxheight/height*width)) ; put_ScaledWidth
  59. DllCall(NumGet(NumGet(BitmapTransform+0)+9*A_PtrSize), "ptr", BitmapTransform, "int", maxheight) ; put_ScaledHeight
  60. DllCall(NumGet(NumGet(BitmapFrameWithSoftwareBitmap+0)+8*A_PtrSize), "ptr", BitmapFrameWithSoftwareBitmap, "uint", BitmapPixelFormat, "uint", Premultiplied := 0, "ptr", BitmapTransform, "uint", IgnoreExifOrientation := 0, "uint", DoNotColorManage := 0, "ptr*", SoftwareBitmap) ; GetSoftwareBitmapTransformedAsync
  61. }
  62. else
  63. DllCall(NumGet(NumGet(BitmapFrameWithSoftwareBitmap+0)+6*A_PtrSize), "ptr", BitmapFrameWithSoftwareBitmap, "ptr*", SoftwareBitmap) ; GetSoftwareBitmapAsync
  64. WaitForAsync(SoftwareBitmap)
  65. if !InStr(SupportedBitmapPixelFormats, "|" BitmapPixelFormat "|")
  66. {
  67. DllCall(NumGet(NumGet(SoftwareBitmapStatics+0)+7*A_PtrSize), "ptr", SoftwareBitmapStatics, "ptr", SoftwareBitmap, "uint", Gray8 := 62, "ptr*", SoftwareBitmapTemp) ; Convert
  68. Close := ComObjQuery(SoftwareBitmap, IClosable := "{30D5A829-7FA4-4026-83BB-D75BAE4EA99E}")
  69. DllCall(NumGet(NumGet(Close+0)+6*A_PtrSize), "ptr", Close) ; Close
  70. ObjRelease(Close)
  71. ObjRelease(SoftwareBitmap)
  72. SoftwareBitmap := SoftwareBitmapTemp
  73. }
  74. DllCall(NumGet(NumGet(FaceDetector+0)+6*A_PtrSize), "ptr", FaceDetector, ptr, SoftwareBitmap, "ptr*", DetectedFaceList) ; DetectFacesAsync
  75. WaitForAsync(DetectedFaceList)
  76. DllCall(NumGet(NumGet(DetectedFaceList+0)+7*A_PtrSize), "ptr", DetectedFaceList, "int*", count) ; count
  77. loop % count
  78. {
  79. varsetcapacity(bounds, 16, 0)
  80. DllCall(NumGet(NumGet(DetectedFaceList+0)+6*A_PtrSize), "ptr", DetectedFaceList, "int", A_Index-1, "ptr*", DetectedFace) ; get_Item
  81. DllCall(NumGet(NumGet(DetectedFace+0)+6*A_PtrSize), "ptr", DetectedFace, "ptr", &bounds) ; BitmapBounds
  82. x := numget(bounds, 0, "uint")
  83. y := numget(bounds, 4, "uint")
  84. width := numget(bounds, 8, "uint")
  85. height := numget(bounds, 12, "uint")
  86. result .=A_LoopFileName ;": x=" x ", y=" y ", width=" width ", height=" height "`n"
  87. ObjRelease(DetectedFace)
  88. }
  89. Close := ComObjQuery(IRandomAccessStream, IClosable := "{30D5A829-7FA4-4026-83BB-D75BAE4EA99E}")
  90. DllCall(NumGet(NumGet(Close+0)+6*A_PtrSize), "ptr", Close) ; Close
  91. ObjRelease(Close)
  92. Close := ComObjQuery(SoftwareBitmap, IClosable := "{30D5A829-7FA4-4026-83BB-D75BAE4EA99E}")
  93. DllCall(NumGet(NumGet(Close+0)+6*A_PtrSize), "ptr", Close) ; Close
  94. ObjRelease(Close)
  95. ObjRelease(IRandomAccessStream)
  96. ObjRelease(BitmapDecoder)
  97. ObjRelease(BitmapFrame)
  98. if (height > maxheight)
  99. {
  100. ObjRelease(BitmapEncoder)
  101. ObjRelease(BitmapTransform)
  102. }
  103. ObjRelease(BitmapFrameWithSoftwareBitmap)
  104. ObjRelease(SoftwareBitmap)
  105. ObjRelease(DetectedFaceList)
  106. if RegExMatch(result,"\D")
  107. {
  108. picswithface = %picswithface% `n%result%
  109. }
  110. return
  111. }
  112.  
  113. CreateClass(string, interface, ByRef Class)
  114. {
  115. CreateHString(string, hString)
  116. VarSetCapacity(GUID, 16)
  117. DllCall("ole32\CLSIDFromString", "wstr", interface, "ptr", &GUID)
  118. result := DllCall("Combase.dll\RoGetActivationFactory", "ptr", hString, "ptr", &GUID, "ptr*", Class, "uint")
  119. if (result != 0)
  120. {
  121. if (result = 0x80004002)
  122. msgbox No such interface supported
  123. else if (result = 0x80040154)
  124. msgbox Class not registered
  125. else
  126. msgbox error: %result%
  127. ExitApp
  128. }
  129. DeleteHString(hString)
  130. }
  131.  
  132. CreateHString(string, ByRef hString)
  133. {
  134. DllCall("Combase.dll\WindowsCreateString", "wstr", string, "uint", StrLen(string), "ptr*", hString)
  135. }
  136.  
  137. DeleteHString(hString)
  138. {
  139. DllCall("Combase.dll\WindowsDeleteString", "ptr", hString)
  140. }
  141.  
  142. WaitForAsync(ByRef Object)
  143. {
  144. AsyncInfo := ComObjQuery(Object, IAsyncInfo := "{00000036-0000-0000-C000-000000000046}")
  145. loop
  146. {
  147. DllCall(NumGet(NumGet(AsyncInfo+0)+7*A_PtrSize), "ptr", AsyncInfo, "uint*", status) ; IAsyncInfo.Status
  148. if (status != 0)
  149. {
  150. if (status != 1)
  151. {
  152. DllCall(NumGet(NumGet(AsyncInfo+0)+8*A_PtrSize), "ptr", AsyncInfo, "uint*", ErrorCode) ; IAsyncInfo.ErrorCode
  153. msgbox AsyncInfo status error: %ErrorCode%
  154. ExitApp
  155. }
  156. ObjRelease(AsyncInfo)
  157. break
  158. }
  159. sleep 10
  160. }
  161. DllCall(NumGet(NumGet(Object+0)+8*A_PtrSize), "ptr", Object, "ptr*", ObjectResult) ; GetResults
  162. ObjRelease(Object)
  163. Object := ObjectResult
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement