Jump to content

Automate Task in Background - (Moved)


plainly
 Share

Recommended Posts

I'm a data analyst which basically means data scrubber. I'm trying to automate my tasks while also using my computer to do other work.

Currently working on a project that requires me to parse through a series of pictures, identify them as food, vehicles, people or some combination of those. Then based on that classification, do a series of things to the picture, i.e. adding lables, updating file name, writing a description, adding an entry to excel etc etc. I will manually classify the pictures via a specific watermark.

Ask; How do I recognize the watermark and perform the actions after?

Do
 $z = ImageSearch($x,$y,0,0,0,0,C:\\"")
Until $z = 1
    ControlSend...

I think conceptually that's the concept, but I believe I will need to capture the screen into a bitmap then run the search on the bitmap as per below.

#include <WinAPI.au3>
#Include <WinAPIEx.au3>
#include <Array.au3>

;My understanding is this basically captures the target I'm trying locate on, in my case it would be "Photos" app in windows
$window = "[CLASS:Notepad]"
$handle = WinGetHandle($window)
;This gets the positions
$aWinPos = WinGetPos($handle)

;not clear here what getting device context does here. I assume a continuation of getting the screen of the photo into bitmap memory.
$hDC = _WinAPI_GetWindowDC($handle)
$hMemDC = _WinAPI_CreateCompatibleDC($hDC)

$hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $aWinPos[2], $aWinPos[3])
_WinAPI_SelectObject($hMemDC, $hBitmap)

_PrintWindow($handle, $hMemDC)

;this is a pixel search; can i replace with imagesearch?
$lookup = _PixelSearchEx($hBitmap, 0x00FFFF)

_ArrayDisplay($lookup)

_WinAPI_DeleteDC($hMemDC)
_WinAPI_ReleaseDC($handle, $hDC)
_WinAPI_DeleteObject($hBitmap)

;func for pixelsearch; I might use the _Imagesearch Function from alternate source. I'll look for source
Func _PixelSearchEx($hBitmap, $iColor)

    Static $pProc = 0

    If Not $pProc Then
        If @AutoItX64 Then
            ; Not implemented!

            Return SetError(1, 0, 0)
        Else
            $pProc = __Init(Binary( _
                      '0x5553575631C0505050837C24280074118B6C2428837D14007407B801000000EB' & _
                        '0231C021C0747F8B6C24288B5D084B891C2431C03B04247F6DC7442404000000' & _
                        '008B6C24288B5D044B3B5C24047C528B6C24288B5D148B7C24048B34248B6C24' & _
                        '280FAF750401F7C1E70201FB895C24088B6C24088B5D0081E3FFFFFF003B5C24' & _
                        '2475188B5C24048B6C24288B7D082B3C244FC1E71009FB89D8EB14FF44240471' & _
                        'A0FF0C24718CB8FFFFFFFFEB0231C083C40C5E5F5B5DC21000'))
        EndIf
    EndIf

    Local $tDIB, $tInt, $tPos, $aPos, $Ret, $Error = True

    $hBitmap = _WinAPI_CopyBitmap($hBitmap)
    If @error Then
        Return SetError(1, 0, 0)
    EndIf
    Do
        $tDIB = DllStructCreate($tagDIBSECTION)
        If (Not _WinAPI_GetObject($hBitmap, DllStructGetSize($tDIB), DllStructGetPtr($tDIB))) Or (DllStructGetData($tDIB, 'bmBitsPixel') <> 32) Or (DllStructGetData($tDIB, 'biCompression')) Then
            ExitLoop
        EndIf
        $Ret = _WinAPI_CallWindowProc($pProc, 0, $iColor, DllStructGetPtr($tDIB), 0)
        If (@error) Or ($Ret = -1) Then
            ExitLoop
        EndIf
        $Error = False
    Until 1
    _WinAPI_DeleteObject($hBitmap)
    If $Error Then
        Return SetError(1, 0, 0)
    EndIf
    $tInt = DllStructCreate('int')
    $tPos = DllStructCreate('ushort;ushort', DllStructGetPtr($tInt))
    DllStructSetData($tInt, 1, $Ret)
    Dim $aPos[2]
    For $i = 0 To 1
        $aPos[$i] = DllStructGetData($tPos, $i + 1)
    Next
    Return $aPos
EndFunc   ;==>_PixelSearchEx

Func _PrintWindow($hWnd, $hMemDC, $iFlag = 0)
    $aRet = DllCall("User32.dll", "int", "PrintWindow", _
                                         "hwnd", $hWnd, _
                                         "hwnd", $hMemDC, _
                                         "int", $iFlag)
    Return $aRet[0]
EndFunc   ;==>_PrintWindow

credit to thread: https://www.autoitscript.com/forum/topic/136373-pixel-search-bitmap-stored-within-memory/ 

 

 

Link to comment
Share on other sites

I can' seem to find the edit post button; so perhaps its unavailable to me. I'll post an update on my research below;

1. Solution 1. I'm not sure if any of these work. I'm going to do some testing. 

 

2. Solution 2. The below is more confusing. I have to parse through and read it as I'm not familiar with autoit language / coding.

I'm thinking this might also work;

#RequireAdmin
#include <ScreenCapture.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Func CaptureWindow($iLeft = 0, $iTop = 0, $iWidth = -1, $iHeight = -1, Const $bSaveImage = False, Const $hWnd = WinGetHandle("Photo"))
    Local $rect_window = WinGetPos($hWnd)
    Local $hDC = _WinAPI_GetWindowDC($hWnd)
    Local $hDestDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $rect_window[2] - 16, $rect_window[3] - 38)
    Local $hDestSv = _WinAPI_SelectObject($hDestDC, $hBitmap)
    Local $hSrcDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBmp = _WinAPI_CreateCompatibleBitmap($hDC, $rect_window[2] - 16, $rect_window[3] - 38)
    Local $hSrcSv = _WinAPI_SelectObject($hSrcDC, $hBmp)

    _WinAPI_PrintWindow($hWnd, $hSrcDC, True)

    ScreenToClient($hWnd, $iLeft, $iTop)

    If ($iWidth > 0 and $iHeight > 0) Then
        _WinAPI_BitBlt($hDestDC, $iLeft, $iTop, $iWidth, $iHeight, $hSrcDC, $iLeft, $iTop, $MERGECOPY)
    Else
        _WinAPI_BitBlt($hDestDC, $iLeft, $iTop, $rect_window[2] - 16 - $iLeft, $rect_window[3] - 38 - $iTop, $hSrcDC, $iLeft, $iTop, $MERGECOPY)
    EndIf

    _WinAPI_SelectObject($hDestDC, $hDestSv)
    _WinAPI_SelectObject($hSrcDC, $hSrcSv)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    _WinAPI_DeleteDC($hDestDC)
    _WinAPI_DeleteDC($hSrcDC)
    _WinAPI_DeleteObject($hBmp)

;I dont need tolerance, might just remove.
    If ($image_search_tolerance < 50) Then $image_search_tolerance += 50
    If ($image_search_tolerance > 80) Then $image_search_tolerance = 80

;not sure how the brackets around the $bsaveimage work
    If ($bSaveImage) Then _ScreenCapture_SaveImage(@ScriptDir & "\CaptureWindow.jpg", $hBitmap, False)

    Return $hBitmap
EndFunc   ;==>CaptureWindow

DllCall("ImageSearchDll.dll", "str", "ImageSearchExt", "int", $iLeft, "int", $iTop, "int", $iRight, "int", $iBottom, "int", $iTolerance, "ptr", $hHbitmapImageToFind, "ptr", $hHbitmapImageToSearch)


Func ScreenToClient(Const ByRef $hWnd, ByRef $iX, ByRef $iY)
    Local $tReturn = DLLStructCreate("int iX;int iY")
    DllStructSetData($tReturn, 1, $iX)
    DllStructSetData($tReturn, 2, $iY)
    
    DllCall("user32.dll", "int", "ScreenToClient", "hwnd", $hWnd, "ptr", DllStructGetPtr($tReturn))
    
    $iX = DllStructGetData($tReturn, 1)
    $iY = DllStructGetData($tReturn, 2)
    $tReturn = Null
EndFunc   ;==>ClientToScreen

Func ClientToScreen(Const ByRef $hWnd, ByRef $iX, ByRef $iY)
    Local $tReutrn = DllStructCreate("int iX;int iY")
    DllStructSetData($tReturn, 1, $iX)
    DllStructSetData($tReturn, 2, $iY)
    
    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($tReturn))
    
    $iX = DllStructGetData($tReturn, 1)
    $iY = DllStructGetData($tReturn, 2)
    $tReturn = Null
EndFunc   ;==>ClientToScreen

those are the core functions. then from there i can use the functions to search?

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...