Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to invoke web service
#31
Have you tested it? The base64 value contained in the body is different from the base64 value generated directly by the QM Base64 function.

Macro Macro8
Code:
Copy      Help
str ss.getfile("$desktop$\1.png");; change to file location
_s.encrypt(4 ss "" 2)

str body=
F
;access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image={_s}&language_type=CHN_ENG&probability=true&detect_direction=true

out body
#32
even if that is the case it's not the base64 function its something else
#33
You compare the values ​​of base64 in 1.txt and 2.txt

Macro Macro8
Code:
Copy      Help
str ss.getfile("$desktop$\1.png");; change to file location
_s.encrypt(4 ss "" 2)
_s.setfile("$desktop$\1.txt")

str body=
F
;access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image={_s}&language_type=CHN_ENG&probability=true&detect_direction=true

body.setfile("$desktop$\2.txt")

The value of base64 in 1.txt is correct.
#34
as i said in previous post its something else not the encryption function that is causing it to change.
#35
I don't understand what you are describing, my English is not very good, use Google Translate  Shy

How to solve this problem? Smile
#36
something happens when you add the base64 encoded image to another string. I cannot help . Will need to wait for Gintaras to reply
#37
here you go

Function Function17
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")
r.SetRequestHeader("postman-token" , "7d83ad28-08ce-c5fa-7d4c-f0aa6c48b966")
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.replacerx("\+" "%2B")
r.Send(F"{access}{_s}")
out r.ResponseText
#38
@redbull2k

Thanks for your help, Heart
now the code can run successfully, the following code Why should I add it?


r.SetRequestHeader("cache-control" , "no-cache")
r.SetRequestHeader("postman-token" , "7d83ad28-08ce-c5fa-7d4c-f0aa6c48b966")
#39
there is no need, was for my trial and error only. you should look into chilkat it helps with json results

Function Function17
Code:
Copy      Help
typelib WinHttp {662901FC-6951-4854-9EB2-D9A2570F2B2E} 5.1
WinHttp.WinHttpRequest r._create
typelib Chilkat_v9_5_0 "$desktop$\ChilkatAx-9.5.0-win32.dll"
Chilkat_v9_5_0.ChilkatJsonObject js._create
Chilkat_v9_5_0.ChilkatJsonArray j._create
Chilkat_v9_5_0.ChilkatJsonObject jr._create
j=0;jr=0
r.Open("POST" F"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic")
r.setRequestHeader("content-type", "application/x-www-form-urlencoded")
str sFile.getfile("$desktop$\1.png");; change to file location
_s.encrypt(4 sFile "" 2)
_s.replacerx("\+" "%2B")
r.Send(F"access_token=24.b305c9822131e65eda05e29c157dd719.2592000.1559103982.282335-16139329&image={_s}&language_type=CHN_ENG&probability=true&detect_direction=true")
js.load(r.ResponseText)
;out js.Emit
j=js.ArrayOf("words_result")
;out js.StringOf("log_id")
;out js.StringOf("direction")
;out js.StringOf("words_result_num")
for int'i 0 j.Size
,jr=j.ObjectAt(i)
,str words_result=jr.StringOf("words")
,out words_result
#40
Thank you for your help  Heart

@kevin

2.png is a multi-line text image, get the result using the following code,  ann The following code OK, But need ChilkatAx-9.5.0-win32.dll

j=js.ArrayOf("words_result")
 out js.StringOf("log_id")
 out js.StringOf("direction")
 out js.StringOf("words_result_num")
for int'i 0 j.Size
    jr=j.ObjectAt(i)
    str words_result=jr.StringOf("words")
    out words_result


Macro Macro3
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$\2.png");; change to file location
_s.encrypt(4 sFile "" 2)
_s.replacerx("\+" "%2B")
r.Send(F"{access}{_s}")
;out r.ResponseText

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

IXml x=JsonToXml(s 1)
ARRAY(IXmlNode) a1
x.Path("root/words_result").GetAll(0 a1)
int i
for i 0 a1.len
,out "%s %s "a1[i].Name a1[i].Value


Attached Files
.zip   2.png.zip (Size: 1.01 KB / Downloads: 318)
#41
@redbull2k

Thank you for sharing, Chilkat's json component is free, and it works very well. I saw that the initial value is set. Is this necessary?


Attached Files Image(s)
   
#42
@kevin

Using the code below, the output will have a blank line, how to delete

Macro Macro4
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$\2.png");; change to file location
_s.encrypt(4 sFile "" 2)
_s.replacerx("\+" "%2B")
r.Send(F"{access}{_s}")

IXml x=JsonToXml(r.ResponseText 1)
ARRAY(IXmlNode) a1
x.Path("root/words_result").GetAll(0 a1)
int i
for i 0 a1.len
,out "%s" a1[i].Value


Attached Files Image(s)
   

.zip   2.png.zip (Size: 1.01 KB / Downloads: 292)
#43
I have set it because at times I could get compile error
#44
compile error?  I deleted it and it can run successfully
#45
Sorry at times unknown identifier.
#46
this is first time I used the chilkat component, it feels easier to understand,  Smile
the component is relatively large, 9M size, Shy
thank you again for sharing Heart
#47
It has a lot of stuff in it not just json some u will need a license for. But there is a 30 day free trial to use everything
#48
Thank you for your reminder, I am trying Tongue
#49
Code:
Copy      Help
Xml x=JsonToXml(r.ResponseText 1)
ARRAY(IXmlNode) a1
x.Path("root/words_result").GetAll(0 a1)
int i
for i 0 a1.len
,if(!empty(a1[i].Value))
,,out "%s" a1[i].Value
#50
Running successfully, thanks

I don't understand why this line of code can remove blank lines. Huh
if(!empty(a1[i].Value))
#51
That Statement Basically says if the Variable has a value . (Meaning is not empty or not "")
#52
still a little do not understand, Big Grin can not use the trim function?

Is there an easier way?
use Chilkat's json component code, I can understand, but it is too big  Shy
#53
@redbull2k
@Kevin

I just found the problem, if there are Chinese or uincode characters in the picture, QM's base64 encoding is wrong, have garbled characters in the recognition result.

I have prepared a picture 3.png in the attachment

Is there a solution?


This is the result of postman generation, no garbled, it is correct

 
Code:
Copy      Help
 
{
    "log_id": 8318947325956499430,
    "direction": 0,
    "words_result_num": 2,
    "words_result": [
        {
            "words": "这是中文",
            "probability": {
                "variance": 0.000001,
                "average": 0.999076,
                "min": 0.997459
            }
        },
        {
            "words": "this is english",
            "probability": {
                "variance": 0.000215,
                "average": 0.981307,
                "min": 0.96286
            }
        }
    ]
}


Attached Files
.zip   3.png.zip (Size: 2.68 KB / Downloads: 336)
#54
not sure what that problem is .Don't have the time to look into it either but here is a simplified code for just getting all the words

Code:
Copy      Help
IXml x=JsonToXml(r.ResponseText)
ARRAY(IXmlNode) a; x.Path("root/words_result/item/words" a 1)
int i; for(i 0 a.len) out a[i].Value        
#55
Wow, Good code!!

I have not found a better OCR solution at present, I can only use Baidu OCR.

I think it might be what needs to be modified in the unicode code.

Baidu OCR, supports many languages, Support for 50,000 requests per day for free, There are many free features, such as text-to-speech, translation, and image recognition. Smile

- CHN_ENG: mixed in Chinese and English;
- ENG: English;
- POR: Portuguese;
- FRE: French;
- GER: German;
- ITA: Italian;
- SPA: Spanish;
- RUS: Russian;
- JAP: Japanese;
- KOR: Korean
#56
also a better idea instead of using 

_s.replacerx("\+" "%2B")
use instead
_s.escape(9)

it's what its for.. It urlencodes the data. that is what the original problem was the data needed to be urlencoded

full code
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$\2.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)
int i; for(i 0 a.len) out a[i].Value
       
#57
wow!  Cool The code is shorter and easier to understand

Improvement is endless   Big Grin

It's a bit strange, why in postman can recognize unicode
#58
cause its probably programmed to just like its programmed to urlencode the image before it sends it
#59
C# interface code

The C# SDK is now open source!   https://github.com/Baidu-AIP/dotnet-sdk

This project is a basic OCR API test app as Visual Studio C# project. 

https://github.com/A9T9/Baidu-OCR-API
 
Code:
Copy      Help
 
public void GeneralBasicDemo() {
    var image = File.ReadAllBytes("Image file path");
    // Calling universal text recognition, the image parameters are local images, and may throw an exception such as the network. Please use try/catch to capture
    var result = client.GeneralBasic(image);
    Console.WriteLine(result);
    // If there are optional parameters
    var options = new Dictionary<string, object>{
        {"language_type", "CHN_ENG"},
        {"detect_direction", "true"},
        {"detect_language", "true"},
        {"probability", "true"}
    };
    // Calling general text recognition with parameters, picture parameters are local images
    result = client.GeneralBasic(image, options);
    Console.WriteLine(result);
}
public void GeneralBasicUrlDemo() {
    var url = "https//www.x.com/sample.jpg";

    // Calling universal text recognition, the image parameter is a remote url image, and may throw an exception such as the network. Please use try/catch to capture
    var result = client.GeneralBasicUrl(url);
    Console.WriteLine(result);
    // If there are optional parameters
    var options = new Dictionary<string, object>{
        {"language_type", "CHN_ENG"},
        {"detect_direction", "true"},
        {"detect_language", "true"},
        {"probability", "true"}
    };
    // Calling general text recognition with parameters, image parameters are remote url images
    result = client.GeneralBasicUrl(url, options);
    Console.WriteLine(result);
}


#60
VBS Base64 encoding , The following code can support Unicode
Code:
Copy      Help
 
Function FileBase64Enc(ByVal strFilePath)
    Dim fso,WshShell,TmpFilePath,oReadFile
    Const ForReading = 1
    Const MyBirthDay = 1228
    Const WINDOW_HIDE = 0
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set WshShell = CreateObject("Wscript.Shell")
    TmpFilePath = fso.GetSpecialFolder(2) & "\" & Fix(Rnd * MyBirthDay) & ".TMP"
    WshShell.Run "Certutil.EXE -ENCODE """ & strFilePath & """ """ & TmpFilePath & """" ,WINDOW_HIDE ,True
    Set oReadFile = fso.OpenTextFile(TmpFilePath,ForReading)
    oReadFile.ReadLine 'Skip the "----- BEGIN CERTIFICATE -----" flag on the first line
    Dim Str
    While Str <> "-----END CERTIFICATE-----"
        FileBase64Enc = FileBase64Enc & Str
        Str = oReadFile.ReadLine
    Wend
    oReadFile.Close
    fso.DeleteFile TmpFilePath
End Function


Forum Jump:


Users browsing this thread: 5 Guest(s)