20200627 2318 

 

00 개요

Ocr 서비스*를 해주는 사이트가 있다. 이 사이트를 직접 이용하려면 불편하다. API를 이용해서 자동화 해보자.

* 이미지(png, jpg, pdf)를 읽어서 결과를 텍스트 형태로 제공하거나, searchable PDF로 제공

 

99 참고자료

"https://www.autohotkey.com/boards/viewtopic.php?t=24884&p=117844"

 

01 사이트

"https://ocr.space"

→ Ui Vision과 관계있는 듯..

 

02 API key

여기 등록하면 얻을 수 있단다.

"https://us11.list-manage.com/subscribe?u=ce17e59f5b68a2fd3542801fd&id=252aee70a1"

* 무료는 한달에 25000건(하루 1000건 정도), 파일크기 1메가 이하, 건당 3페이지 이하, Searchable PDF에 워터마크 있다. 좋은 점은 상업적으로 사용해도 된단다!

 
(note)API 아니고, 웹사이트에서 사용하면 건당 3페이지 제한이 적용되지 않는 듯

03 코드

 

1/사용

oForm := { apikey: "f533427abcdefg43188957"
         , language: "kor"
		 , isOverlayRequired: "true"
		 ,isCreateSearchablePdf: "true"
		 ,isSearchablePdfHideTextLayer: "true"
;         , cache: "false"
;         , contentType: "false"
;         , processData: "false"
;         , type: "json"
         , file: ["zTest.pdf"] }

CreateFormData(PostData, ContentType, oForm)
url := "https://api.ocr.space/Parse/Image"
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", url, true)
whr.SetRequestHeader("Content-Type", ContentType)
whr.SetRequestHeader("Referer", "https://api.ocr.space/")
whr.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko")
whr.Option(6) := False ; No auto redirect
whr.Send(PostData)
whr.WaitForResponse()
json_str:= whr.ResponseText
clipboard:= json_str
;MsgBox, % json_str
MsgBox, see the clipboard. `nProcess Ended
return

file: ["zTest.pdf"] → 현재 작업 중인 폴더에 OCR적용할 파일을 두면 된다.

isCreateSearchablePdf → Searchable PDF를 출력

isSearchablePdfHideTextLayer → Overlay를 숨김

(note) Searchable PDF는 웹에서 생성되고, 해당 URL을 제공해준다.

 

1/함수정의

CreateFormData(ByRef retData, ByRef retHeader, objParam) {
	New CreateFormData(retData, retHeader, objParam)
}

Class CreateFormData {

	__New(ByRef retData, ByRef retHeader, objParam) {

		Local CRLF := "`r`n", i, k, v, str, pvData
		; Create a random Boundary
		Local Boundary := this.RandomBoundary()
		Local BoundaryLine := "------------------------------" . Boundary

    this.Len := 0 ; GMEM_ZEROINIT|GMEM_FIXED = 0x40
    this.Ptr := DllCall( "GlobalAlloc", "UInt",0x40, "UInt",1, "Ptr"  )          ; allocate global memory

		; Loop input paramters
		For k, v in objParam
		{
			If IsObject(v) {
				For i, FileName in v
				{
					str := BoundaryLine . CRLF
					     . "Content-Disposition: form-data; name=""" . k . """; filename=""" . FileName . """" . CRLF
					     . "Content-Type: " . this.MimeType(FileName) . CRLF . CRLF
          this.StrPutUTF8( str )
          this.LoadFromFile( Filename )
          this.StrPutUTF8( CRLF )
				}
			} Else {
				str := BoundaryLine . CRLF
				     . "Content-Disposition: form-data; name=""" . k """" . CRLF . CRLF
				     . v . CRLF
        this.StrPutUTF8( str )
			}
		}

		this.StrPutUTF8( BoundaryLine . "--" . CRLF )

    ; Create a bytearray and copy data in to it.
    retData := ComObjArray( 0x11, this.Len ) ; Create SAFEARRAY = VT_ARRAY|VT_UI1
    pvData  := NumGet( ComObjValue( retData ) + 8 + A_PtrSize )
    DllCall( "RtlMoveMemory", "Ptr",pvData, "Ptr",this.Ptr, "Ptr",this.Len )

    this.Ptr := DllCall( "GlobalFree", "Ptr",this.Ptr, "Ptr" )                   ; free global memory 

    retHeader := "multipart/form-data; boundary=----------------------------" . Boundary
	}

  StrPutUTF8( str ) {
    Local ReqSz := StrPut( str, "utf-8" ) - 1
    this.Len += ReqSz                                  ; GMEM_ZEROINIT|GMEM_MOVEABLE = 0x42
    this.Ptr := DllCall( "GlobalReAlloc", "Ptr",this.Ptr, "UInt",this.len + 1, "UInt", 0x42 )   
    StrPut( str, this.Ptr + this.len - ReqSz, ReqSz, "utf-8" )
  }
  
  LoadFromFile( Filename ) {
    Local objFile := FileOpen( FileName, "r" )
    this.Len += objFile.Length                     ; GMEM_ZEROINIT|GMEM_MOVEABLE = 0x42 
    this.Ptr := DllCall( "GlobalReAlloc", "Ptr",this.Ptr, "UInt",this.len, "UInt", 0x42 )
    objFile.RawRead( this.Ptr + this.Len - objFile.length, objFile.length )
    objFile.Close()       
  }

	RandomBoundary() {
		str := "0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z"
		Sort, str, D| Random
		str := StrReplace(str, "|")
		Return SubStr(str, 1, 12)
	}

	MimeType(FileName) {
		n := FileOpen(FileName, "r").ReadUInt()
		Return (n        = 0x474E5089) ? "image/png"
		     : (n        = 0x38464947) ? "image/gif"
		     : (n&0xFFFF = 0x4D42    ) ? "image/bmp"
		     : (n&0xFFFF = 0xD8FF    ) ? "image/jpeg"
		     : (n&0xFFFF = 0x4949    ) ? "image/tiff"
		     : (n&0xFFFF = 0x4D4D    ) ? "image/tiff"
		     : "application/octet-stream"
	}

}
Posted by Weneedu
,


출처: https://privatedevelopnote.tistory.com/81 [개인노트]