Jump to content
Sign in to follow this  
corgano

Search google for terms based on image.

Recommended Posts

corgano

I am trying to write a macro to auto-tag images in my library. I am looking for a way to search google images, but NOT trying to get images - I instead want to search an image and get possible words for it.

Things I've tried:

  • I've searched the forums, but almost every hit was searching a phrase to get images - I want to do the opposite.
  • I found two threads on it, One had no answer, and the other used the winhttp udf - which I can never get to work properly :/
  • I could try using IE functions, but it's messy and there's no way to do it completely in the background, which is much preferred :/

Does anyone have any suggestions? Or should I suck it up and use IE?


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites
corgano

I know how much you guys love code. I found a pics,it example on the forum, and tried changing it to work for the google search-by-image url / request. This is what I got

#include <WinHTTP.au3>
#include <zlib.au3>

;~ http://pics.lt/index.php?lang=english

Global $sBoundary = '--' & _GenerateAlphaNumericRandomString ( 50 )

$sImagePath = @ScriptDir&"\0.jpg"
If Not FileExists ( $sImagePath ) Then Exit 1

$sDirectLink = _PicsItUpload ( $sImagePath )
ConsoleWrite ( 'Direct Link : ' & $sDirectLink & @Crlf )
If $sDirectLink Then ShellExecute ( $sDirectLink )

Func _PicsItUpload ( $sImagePath )
    If FileGetSize ( $sImagePath ) > 10*1024*1024 Then Return -1 ; max 10 MB
    Local $ReceivedData = Binary ( '' ), $chunk, $iExtended


    Local $hOpen = _WinHttpOpen ()
    Local $hConnect = _WinHttpConnect ( $hOpen, 'images.google.ca' )
    Local $hRequest = _WinHttpOpenRequest ( $hConnect, 'POST', '/searchbyimage/upload', 'HTTP/1.1' )
    _WinHttpAddRequestHeaders ( $hRequest, 'Referer: http://images.google.ca/' )
    _WinHttpAddRequestHeaders ( $hRequest, 'Accept-Encoding: gzip, deflate' )
    Local $sDataToSend = _PicsItSetDataToSend ( $sImagePath )
    If Not @error Then
        _WinHttpAddRequestHeaders ( $hRequest, 'Content-Length: ' & StringLen ( $sDataToSend ) )
        Local $sContentType = 'Content-Type: multipart/form-data; boundary=' & StringTrimLeft ( $sBoundary, 2 )
        _WinHttpSendRequest ( $hRequest, $sContentType, $WINHTTP_NO_REQUEST_DATA, StringLen ( $sDataToSend ) )
        _UploadProgress ( $hRequest, $sImagePath, $sDataToSend )
        If Not @error Then
            _WinHttpReceiveResponse ( $hRequest )
            If _WinHttpQueryDataAvailable ( $hRequest ) Then

                Local $sHeader = _WinHttpQueryHeaders($hRequest) ; ...get full header
                While 1
                    $chunk = _WinHttpReadData ( $hRequest, 2, 1024 )
                    If Not @extended Then ExitLoop
                    $ReceivedData &= $chunk
                WEnd
                $ReceivedData = BinaryToString ( _ZLIB_GZUncompress ( $ReceivedData ), 1 )
            EndIf
        EndIf
    EndIf
    _WinHttpCloseHandle ( $hRequest )
    _WinHttpCloseHandle ( $hConnect )
    _WinHttpCloseHandle ( $hOpen )
    If $sHeader Then
        ConsoleWrite("Recieved"&@CRLF&$sHeader&@CRLF)
;~         Local $aRet = StringRegExp ( $ReceivedData, '(?s)(?i)codedirect" value="(.*?)"', 3 )
;~         If Not @error Then Return $aRet[0]
    EndIf
EndFunc ;==> _PicsItUpload ()

Func _PicsItSetDataToSend ( $sFilePath )
    If Not FileExists ( $sFilePath ) Then Return SetError ( -1 )
    Local $hFile = FileOpen ( $sFilePath )
    If $hFile = -1 Then Return SetError ( 1, 0, 0 )
    Local $sData = FileRead ( $hFile )
    FileClose ( $hFile )
    Return $sBoundary & @CRLF & _
        'Content-Disposition: form-data; name="fileName[]"' & @CRLF & @CRLF & $sFilePath & @CRLF & $sBoundary & @CRLF & _
        'Content-Disposition: form-data; name="file[]"; filename="' & _FileGetFullNameByFullPath ( $sFilePath ) & '"' & @CRLF & _
        'Content-Type: image/jpeg' & @CRLF & @CRLF & $sData & @CRLF & $sBoundary & @CRLF & _
        'Content-Disposition: form-data; name="alt[]"' & @CRLF & @CRLF & _FileGetNameWithoutExtByFullPath ( $sFilePath ) & @CRLF & $sBoundary & @CRLF & _
        'Content-Disposition: form-data; name="submit"' & @CRLF & @CRLF & 'Upload' & @CRLF & $sBoundary
EndFunc ;==> _PicsItSetDataToSend ()

Func _UploadProgress ( $hRequest, $sPicPath, $sDataToSend )
    If Not FileExists ( $sPicPath ) Then Return SetError ( -1 )
    Local $sDatas = StringToBinary ( $sDataToSend )
    Local $iNumberOfBytesToSend = 1024
    Local $Bin, $iStart = 1, $iExtended = 0
    While 1
        $Bin = BinaryMid ( $sDatas, $iStart, $iNumberOfBytesToSend )
        If BinaryLen ( $Bin ) = 0 Then Return
        _WinHTTPWriteData ( $hRequest, $Bin, 1 )
        If Not @error Then
            $iExtended += @extended
            ConsoleWrite ( '+ $iPercent : ' & Round ( ( $iExtended  ) / ( StringLen ( $sDataToSend ) ) * 100, 2 ) & @Crlf )
            $iStart += $iNumberOfBytesToSend
        Else
            $Bin = 0
            Return SetError ( 1 )
        EndIf
    WEnd
EndFunc ;==> _UploadProgress ()

Func _FileGetNameWithoutExtByFullPath ( $sFullPath )
    Local $aPathSplit = StringSplit ( $sFullPath, '\' )
    If Not @error Then Return StringLeft ( $aPathSplit[UBound($aPathSplit)-1], StringInStr ( $aPathSplit[UBound($aPathSplit)-1], '.', 0, -1 ) - 1 )
EndFunc ;==> _FileGetNameWithoutExtByFullPath ()

Func _FileGetFullNameByFullPath ( $sFullPath )
    Local $aFileName = StringSplit ( $sFullPath, '\' )
    If Not @error Then Return $aFileName[$aFileName[0]]
EndFunc ;==> _FileGetFullNameByFullPath ()

Func _GenerateAlphaNumericRandomString ( $iLen )
    Local $sString = ''
    Do
        Switch Random ( 1, 3, 1 )
            Case 1
                $sString &= Chr ( Random ( 48, 57, 1 ) )  ; 0 to 9
            Case 2
                $sString &= Chr ( Random ( 65, 90, 1 ) )  ; A to Z
            Case 3
                $sString &= Chr ( Random ( 97, 122, 1 ) ) ; a to z
        EndSwitch
    Until StringLen ( $sString ) = $iLen
    Return $sString
EndFunc ;==> _GenerateAlphaNumericRandomString ()

It appears to work. When I look at  it gives me

HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Date: Mon, 07 Jul 2014 07:51:09 GMT
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1
Expires: -1
Server: gws
Set-Cookie: PREF=ID=c64ec6a73aa70b5d:U=9a78a2518fc3dcb0:FF=0:TM=1404719469:LM=1404719469:S=dndXYXDxpnyGV3G1; expires=Wed, 06-Jul-2016 07:51:09 GMT; path=/; domain=.google.ca
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic

when what I should get is

Alternate-Protocol:80:quic
Cache-Control:public, max-age=21600
Content-Length:1759
Content-Type:text/html; charset=UTF-8
Date:Mon, 07 Jul 2014 07:14:32 GMT
Expires:Mon, 07 Jul 2014 13:14:32 GMT
Location:http://images.google.ca/search?tbs=sbi:AMhZZitHkEabunchofcraphere
Server:quimby_frontend
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1; mode=block 

What I need that I'm missing is the Location:http://images.google.ca/search?tbs=sbi:AMhZZitHkEabunchofcraphere, because that tells me where to go next. I'm thinking my request must not be right, but I can't tell hoe. Any ideas?

EDIT: Further investigation makes me think it's not returning the location, because the upload isnt working, because something wrong with how I formatted the request payload / content, so I'm trying to make it look exactly how chrome tells me the request looks.See next post

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites
corgano

I've been messing around the wth the payload data of the request, trying to get it to match what chrome shows is sent, but I hit a snag at 'Content-Type: application/octet-stream'.

What I'm supposed to be sending is this:

 

but when I fileread() the image, all I get is this:

ÿØÿà

But it FileGetSize says the size is indeed 2933. How do I read the image, and what do I do to the format to get it to look like the above example?

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites
corgano

Revisiting old posts, and this is still something I'm wondering how to do. Does anyone know a way to search for tags for an image (using google or something else?)


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites
SadBunny

Well, about your fileread issue: you'll need to specify binary file reading mode when reading a file you know/expect to be binary:

#include <FileConstants.au3>

Local $hFileOpen = FileOpen("c:\temp\test.png", $FO_READ + $FO_BINARY)
Local $sFileRead = FileRead($hFileOpen)
MsgBox(0, "", "Contents of the file:" & @CRLF & $sFileRead)
FileClose($hFileOpen)

No idea about those other things. Good luck :)


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • Xandy
      By Xandy
      MapIt is a tile world editor.  MapIt was built around the concept of reversing Dragon Warrior map images.  MapIt can take image input and produce a tile and world array.
      MapIt features Unity style dragable labels that adjust property values.

      MapParser is a C++ project that scans images for unique tiles.  MapParser is very fast.  Due to hard drive failure, many bugs were restored b/c I had to rewind many years.  Frustrated with the design, I wrote a new version from the ground up.  This New Version:  AutoIt Front-end, command line controls, and shared with the world; so that I can't lose it again.
      You can toggle the C++ MapParser off to see the difference in speeds between the MapParser CPP verse AutoIt function.  Function is named Scan_Tiles() in AutoIt.  You can also chose to download without MapParser.exe.
      At the moment Scanning a image resets the arrays, but you can add tiles after scanning.
      Images can be added as tiles without scan image at all.  Then configure settings to give your world parameters and manually fill the world data with tile indexes.
      Using the settings you can change tile size after a scan.  Example: you wanted to replace a map with different sized tiles.
      Changing and replacing tile / world data is easy.  B/c tile world editor.

      Hotkeys, I use CTRL+R in image above to signal replace tile action and I use "G" to Get the tile under mouse.
      Hotkeys are not saved to disk and thus are set to default between sessions.
      I might draw the world to pre-rendered surfaces and use them as multi-layer someday.  I do that in my AutoIt, DragonWarrior Remake but I could spend forever unsure what features are important for this.  The DW_Remake has a method of replacing a tile with a tile on two layers.  So you could replace a tree on the first layer with a grass, and a tree in the second layer.  This is all getting very confusing.
      I attempted to write the good code.  If something could be better, please advise.
      Fifth release.  Enjoy.
      For download, videos, and example of created world file data; please visit the MapIt webpage: http://songersoft.com/programming/mapit/mapit_about.phtml
      Special thanks: @AdmiralAlkex, @Melba23, @MrCreatoR
      Main AutoIt source file: Will not run without other Includes and SDL DLLs.
      Last Update: 5/26/2018 3:45 PM EST
      REMOVED CODE BLOCK:  I was informed the this page loaded very slowly, one solution so far has been to remove the 2k lines in the code block.
      When I recieve more feedback from the User I may reduce image size or remove images.
      Next Version Added: $eSETTING_TILE_LAST_PATH I have the weekend, I want to write world layers with aBoard surfaces.  
    • lenclstr746
      By lenclstr746
      HELLO GUYS
      I'm a work on a background see and click bot project 
      I can complete it if your help me
      (using imagesearch , gdi+ and  fastfind)
    • dadalt95
      By dadalt95
      I would like to know if it's possible to pass an image recognition (captcha) system.
       
      What are the ways to achieve this?
      Just the references or links is enough for me by now.
       
      Thanks by now!
       
      Thanks!
    • Ascer
      By Ascer
      1. Description.
      oAuth 2.0 is security system implemented by Google a few years ago. You are able to connect into your Google accounts and manage documents. In this UDF i show you how to pass first authorization process., this allow you to automate most of functions using API interface. 2. Requirements.
      Google account. oAuth.au3 Download 3. Possibilities
      ;============================================================================================================ ; Date: 2018-02-10, 14:21 ; ; Description: UDF for authorize your app with oAuth 2.0 Google. ; ; Function(s): ; oAuth2GetAuthorizationCode() -> Get Code for "grant". ; oAuth2GetAccessToken() -> Get "access_token" and "refresh_token" first time. ; oAuth2RefreshAccessToken() -> Get current "access_token" using "refresh_token". ; ; Author(s): Ascer ;============================================================================================================ 4. Enable your Google API.
          4.1. Video Tutorial not mine!
       YouTube     4.2 Screenshots from authorization process (Polish language) 
      Go to https://console.developers.google.com/apis/dashboard and accept current rules.  

       
      Next create an new project  

       
      Enter name of you new project and click Create  

       
      Google will working now, please wait until finish. Next go to enable your API interface, we make if for Google  

       
      Take "Gmail" in search input and after click in found result.  

       
      Click Enable interface, Google will working now.  

       
      Create your login credentials  

       
      Select Windows Interface (combobox), User credentials (radio) and click button what is need bla bla  

       
      Type name of a new client id for oAuth 2.0 and click Create a new Client ID.  

       
      Next configure screen aplication, type some name and click Next. Google will working now.  

       
      Last step on this website is download source with your credentials in *Json format.  

       
      Now you received a file named client_id.json, it's how it look in Sublime Text:  

       
      5. Coding.
      Now we need to call a some function to get access code.  
      #include <oAuth.au3> Local $sClientId = "167204758184-vpeues0uk6b0g4jrnv0ipq5fapoig2v8.apps.googleusercontent.com" Local $sRedirectUri = "http://localhost" oAuth2GetAuthorizationCode($sClientId, $sRedirectUri)  
      Function will execute default browser for ask you to permission.  

       
      Next Google ask you to permission for access to your personal details by application Autoit   

       
      Now you can thing is something wrong but all is ok, you need to copy all after  code= . It your access code.  

       
      Let's now ask Google about our Access Token and Refresh Token  
      #include <oAuth.au3> Local $sClientId = "167204758184-vpeues0uk6b0g4jrnv0ipq5fapoig2v8.apps.googleusercontent.com" Local $sClientSecret = "cWalvFr3WxiE6cjUkdmKEPo8" Local $sAuthorizationCode = "4/AAAPXJOZ-Tz0s6mrx7JbV6nthXSfcxaszFh_aH0azVqHkSHkfiwE8uamcabn4eMbEWg1eAuUw7AU0PQ0XeWUFRo#" Local $sRedirectUri = "http://localhost" Local $aRet = oAuth2GetAccessToken($sClientId, $sClientSecret, $sAuthorizationCode, $sRedirectUri) If Ubound($aRet) <> 4 then ConsoleWrite("+++ Something wrong with reading ResponseText." & @CRLF) Exit EndIf ConsoleWrite("Successfully received data from Google." & @CRLF) ConsoleWrite("access_token: " & $aRet[0] & @CRLF) ConsoleWrite("expires_in: " & $aRet[1] & @CRLF) ConsoleWrite("refresh_token: " & $aRet[2] & @CRLF) ConsoleWrite("token_type: " & $aRet[3] & @CRLF)  
      Important! When you received error 400 and output says: Invalid grant it means that your previous generated access_code lost validity and you need to generate new calling previus code. When everything is fine you should received a 4 informations about your: access_token, expires_in, refresh_token and token_type. Access_Token time is a little short so you need to know fuction possible to refresh it (tell Google that he should generate a new Token for you)  
      #include <oAuth.au3> Local $sRefreshToken = "1/ba8JpW7TjQH3-UI1BvPaXhSf-oTQ4BmZAbBfhcKgKfY" Local $sClientId = "167204758184-vpeues0uk6b0g4jrnv0ipq5fapoig2v8.apps.googleusercontent.com" Local $sClientSecret = "cWalvFr3WxiE6cjUkdmKEPo8" Local $sRedirectUri = "http://localhost" Local $aRet = oAuth2RefreshAccessToken($sRefreshToken, $sClientId, $sClientSecret) If Ubound($aRet) <> 3 then ConsoleWrite("+++ Something wrong with reading ResponseText." & @CRLF) Exit EndIf ConsoleWrite("Successfully received data from Google." & @CRLF) ConsoleWrite("access_token: " & $aRet[0] & @CRLF) ConsoleWrite("expires_in: " & $aRet[1] & @CRLF) ConsoleWrite("token_type: " & $aRet[2] & @CRLF)  
      6. Finish words
      If you followed all this above steps im sure that you received all informations required for coding your Google API (Gmail, Dropbox, YouTube, Calender etc. See next thread: [UDF] Gmail API - Email automation with AutoIt!
×