Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to invoke web service
#61
php code:
How to use PHP's base64 function? PHP code calls are much simpler  Tongue

 
PHP Code: Select All
 
$api 
'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=xxxxxxx';
$header = ['Content-Length: multipart/form-data'];

$file '/data/image/a.png'// Local image path
$image base64_encode(file_get_contents($file));
$post = [ 
    
'image' => $image,
];

$res request_post($api$post$header); 


The garbled problem of unicode characters is still not solved  Sad

Can someone provide some methods? thanks
#62
@redbull2k   @Kevin

I solved the problem,
Tongue

Do not use ResponseText

If you use the ResponseBody, there is no garbled

can make the code Shorter?
Smile

Macro Macro8
Code:
Copy      Help
typelib WinHttp {662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1
WinHttp.WinHttpRequest r._create
r.Open("POST" F"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic")
r.setRequestHeader("content-type", "application/x-www-form-urlencoded")
r.SetRequestHeader("cache-control" , "no-cache")

str access= "access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image="
str sFile.getfile("$desktop$\3.png");; change to file location
_s.encrypt(4 sFile "" 2)
_s.escape(9)
r.Send(F"{access}{_s}")

ARRAY(byte) a=r.ResponseBody
str s.fromn(&a[0] a.len)
out s

IXml x=JsonToXml(s)
ARRAY(IXmlNode) a1; x.Path("root/words_result/item/words" a1 1)
int i; for(i 0 a1.len) out a1[i].Value
#63
sometimes all it takes is a little longer patience and will you will find the answer.
#64
make code shorter no i don't think so, make it less lines yes .

for example
Code:
Copy      Help
typelib WinHttp {662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1; WinHttp.WinHttpRequest r._create; r.Open("POST" F"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"); r.setRequestHeader("content-type", "application/x-www-form-urlencoded"); r.SetRequestHeader("cache-control" , "no-cache")
str access= "access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image="
str sFile.getfile("$desktop$\3.png");; change to file location
_s.encrypt(4 sFile "" 2); _s.escape(9); r.Send(F"{access}{_s}")
ARRAY(byte) a=r.ResponseBody; str s.fromn(&a[0] a.len)
IXml x=JsonToXml(s); ARRAY(IXmlNode) a1; x.Path("root/words_result/item/words" a1 1)
int i; for(i 0 a1.len) out a1[i].Value
 could be even less lines but there really is no point. Code could be all in 1 line if you really wanted but like i said what's the point.
#65
and here is my crack at making it shorter

Function Function18
Code:
Copy      Help
typelib WinHttp {662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1
WinHttp.WinHttpRequest r._create
r.Open("POST" "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329")
r.setRequestHeader("content-type", "application/x-www-form-urlencoded") ;r.SetRequestHeader("language_type" , "CHN_ENG")
str sFile.getfile("$desktop$\3.png");; change to file location
str base64.encrypt(4 sFile "" 2) ;base64.escape(9)
r.Send(F"&image={base64}") ;ARRAY(byte) by= r.ResponseBody
str json.fromn(&by[0] by.len) ;out json
#66
Wow!
I think, is the code shorter and it runs faster? 
Maybe what I think is wrong. Big Grin

I think using external components may make the code run longer.

I am experiencing a problem with VBS code output

Wscript.Echo r.ResponseBody    All are garbled
Wscript.Echo r.ResponseText    Unicode characters are garbled

 
Code:
Copy      Help
 
Set r = CreateObject("WinHttp.WinHttpRequest.5.1") 
r.Open "POST", "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" 
r.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
r.Send "access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image=iVBORw0KGgoAAAANSUhEUgAAAScAAACCCAYAAAAE%2FA6oAAAK40lEQVR4nO3dQU%2FbyBvH8Ser%2F0uA8gbISmHpGwhiK3EjvVRVleve0ko9wGVvK7VIvXEJB7Qlt71G24rLhhtSi%2BAN0CXSJm%2BAwnvIf2bsUEjsxE4m8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qlcfzKiNew%2BnnJz7XNltupPnHmEbLYAm5ujYVleXi3PZpnEz22VxMf9jG7r9HbVeCfj0%2B21RKR1JfGdHIbqpIH6L602Usj%2BeVVl7DKWgUrcjGWffhP2xdW2OD4PWu2afoW7PuNvZKXV%2FbQAb7bLsLtJ7sxh4%2Fd0E2G1Le%2BstrJ8GdNy2pmfNpaDC5DcLDXVnfUjpQNo%2FnlUIF29nJ6xqHerYGhnsC28dEmAP1cKl7PWcn%2BX%2Bc4d7DrtextKRXOh7uyWuUZ9Jomcd9vifm2EpUX6Sxxy%2Bi53dMb%2FPRPcSbUh3qOT3N55zFMZr2WlhM%2FsMJmJfYcMIioEEc%2BRXercNiIpyQW0k7iCKfCCfkUvA0DIkdFoP8o80JysU0UCd5BA9yjXACoBLVOgAqEU4AVCKcAKhEOAFQiXACoBLhBEAlwgmASoQTAJUIJwAqEU4AVCKcAKhEOAFQiXACoBLhBEAlwgmASoQTAJUIJwAqEU4AVCKcAKhEOAFQiXACoBLhBEAlwgmASoQTAJUIJwAqEU4AVCKcAKhEOAFQiXACoBLhBEAlwgmASoQTAJUIJwAqEU4AVCKcAKj0f0KuyhM4KAWmAAAAAElFTkSuQmCC&language_type=CHN_ENG&probability=true&detect_direction=true" 
Wscript.Echo r.ResponseBody
'Wscript.Echo r.ResponseText
#67
The code itself is fast enough. What varies is the server response time. Which you have no control of. Code for me takes maybe 1 second or less to get a response.
#68
this so far has no error protection so when it errors it fails. the idea is to make it robust and catch potential errors
something like this
Function Function18
Code:
Copy      Help
typelib WinHttp {662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1
WinHttp.WinHttpRequest r._create
r.Open("POST" "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329")
r.setRequestHeader("content-type", "application/x-www-form-urlencoded") ;r.SetRequestHeader("language_type" , "CHN_ENG")
str sFile.getfile("$desktop$\100jhkjghjkg0.png");; change to file location
err goto endof
str base64.encrypt(4 sFile "" 2) ;base64.escape(9)
r.Send(F"&image={base64}") 
err goto endof
ARRAY(byte) by= r.ResponseBody
out r.Status
sel r.Status
,case 200 ;;The request completed successfully.
,str json.fromn(&by[0] by.len) ;out json
,case 404 ;;The server has not found anything that matches the requested URI.
,out "error"
,case else out "Fail"
ret
;endof
out _error.description
ret
#69
good idea
#70
one tip use a jpg instead of a png the documentation recommends jpg over other types
#71
I think the best is PNG, because JPG is sometimes very blurry, affecting the recognition effect, I sent a code (screenshot + text recognition)

http://www.quickmacros.com/forum/showthr...4#pid33604
#72
from the api documentation

   
#73
JPG ? Why not PNG?  For text, I think GIF is the best, small, and very clear.

This is currently the most popular free OCR software in China, it integrates many OCR engines, it is open source

There are many useful features, such as translation, barcode recognition, etc.


https://github.com/AnyListen/tianruoocr
#74
@kevin

In VBS, using the following com object, the output will not be garbled, but I did not find it in QM's class library.

Set r = CreateObject("MSXML2.ServerXMLHTTP") 
Wscript.Echo r.ResponseText
#75
How does QM create an MSXML2.ServerXMLHTTP object?  Huh
#76
you can find a lot of useful info in tools> com libraries
Function Function21
Code:
Copy      Help
typelib MSXML2 {F5078F18-C551-11D3-89B9-0000F81FE221} 3.0
MSXML2.ServerXMLHTTP b._create
#77
Thank you for your help, what method did you find for it?
#78
i Just found it for you. All typelib can be found in Tools>Com Libraries, once your there just find microsoft xml find the newest version which will be v6.0. double click it to isert it in current function/macro there are also controls that you can attach to dialog.
#79
This is my new code, it is better understood Smile

Macro Macro8
Code:
Copy      Help
typelib MSXML2 {F5078F18-C551-11D3-89B9-0000F81FE221} 3.0
MSXML2.ServerXMLHTTP r._create
r.Open("POST" F"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic")
r.setRequestHeader("content-type", "application/x-www-form-urlencoded")
r.SetRequestHeader("cache-control" , "no-cache")

str access= "access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image="
str sFile.getfile("$desktop$\1.png");; change to file location
_s.encrypt(4 sFile "" 2)
_s.escape(9)
r.Send(F"{access}{_s}")

IXml x=JsonToXml(r.ResponseText)
ARRAY(IXmlNode) a
x.Path("root/words_result/item/words" a 1)

str d
for(int'i 0 a.len)
,d.addline(a[i].Value)
mes d.trim

Thank you for your help, I just learned that it is an xml component.  Wink

@Kevin

I feel using the current components(MSXML2.ServerXMLHTTP), the response is faster  Tongue


Attached Files Image(s)
   
#80
test for yourself
Function Function21
Code:
Copy      Help
long t1=perf
typelib MSXML2 {F5078F18-C551-11D3-89B9-0000F81FE221} 3.0
MSXML2.ServerXMLHTTP r._create
r.Open("POST" F"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic")
r.setRequestHeader("content-type", "application/x-www-form-urlencoded")
r.SetRequestHeader("cache-control" , "no-cache")
str access= "access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image="
str sFile.getfile("$desktop$\2.png");; change to file location
_s.encrypt(4 sFile "" 2)
_s.escape(9)
r.Send(F"{access}{_s}")
long t2=perf ;;get time after
out t2-t1 ;;display the difference
IXml x=JsonToXml(r.ResponseText)
ARRAY(IXmlNode) a
x.Path("root/words_result/item/words" a 1)
str d
for(int'i 0 a.len)
,d.addline(a[i].Value)
mes d.trim
#81
127648
Can this value measure the response speed?

In my case, OCR recognition is too fast, there is almost no delay, it may be my reason in China, closer to Baidu's server.
#82
that value is in milliseconds its just like a stopwatch,we tell it to start we tell it to end. its just telling you the result from start to finish.
response times kevin has already told you, it all depends on server location as well as many other factors. the only way i think you can improve is by having the server in your kitchen or something.
#83
indeed so  Big Grin
#84
It can also be implemented using powershell code, see the link below

http://www.quickmacros.com/forum/showthr...1#pid33661


Forum Jump:


Users browsing this thread: 1 Guest(s)