-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
230 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package oidb | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/LagrangeDev/LagrangeGo/client/packets/pb/service/oidb" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func BuildImageOcrRequestPacket(url string) (*Packet, error) { | ||
body := &oidb.OidbSvcTrpcTcp0XE07_0{ | ||
Version: 1, | ||
Client: 0, | ||
Entrance: 1, | ||
OcrReqBody: &oidb.OcrReqBody{ | ||
ImageUrl: url, | ||
OriginMd5: "", | ||
AfterCompressMd5: "", | ||
AfterCompressFileSize: "", | ||
AfterCompressWeight: "", | ||
AfterCompressHeight: "", | ||
IsCut: false, | ||
}, | ||
} | ||
return BuildOidbPacket(0xE07, 0, body, false, true) | ||
} | ||
|
||
type ( | ||
OcrResponse struct { | ||
Texts []*TextDetection `json:"texts"` | ||
Language string `json:"language"` | ||
} | ||
TextDetection struct { | ||
Text string `json:"text"` | ||
Confidence int32 `json:"confidence"` | ||
Coordinates []*Coordinate `json:"coordinates"` | ||
} | ||
Coordinate struct { | ||
X int32 `json:"x"` | ||
Y int32 `json:"y"` | ||
} | ||
) | ||
|
||
func ParseImageOcrResp(data []byte) (*OcrResponse, error) { | ||
var rsp oidb.OidbSvcTrpcTcp0XE07_0_Response | ||
_, err := ParseOidbPacket(data, &rsp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if rsp.Wording != "" { | ||
if strings.Contains(rsp.Wording, "服务忙") { | ||
return nil, errors.New("未识别到文本") | ||
} | ||
return nil, errors.New(rsp.Wording) | ||
} | ||
if rsp.RetCode != 0 { | ||
return nil, errors.Errorf("server error, code: %v msg: %v", rsp.RetCode, rsp.ErrMsg) | ||
} | ||
texts := make([]*TextDetection, 0, len(rsp.OcrRspBody.TextDetections)) | ||
for _, text := range rsp.OcrRspBody.TextDetections { | ||
points := make([]*Coordinate, 0, len(text.Polygon.Coordinates)) | ||
for _, c := range text.Polygon.Coordinates { | ||
points = append(points, &Coordinate{ | ||
X: c.X, | ||
Y: c.Y, | ||
}) | ||
} | ||
texts = append(texts, &TextDetection{ | ||
Text: text.DetectedText, | ||
Confidence: text.Confidence, | ||
Coordinates: points, | ||
}) | ||
} | ||
return &OcrResponse{ | ||
Texts: texts, | ||
Language: rsp.OcrRspBody.Language, | ||
}, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
client/packets/pb/service/oidb/OidbSvcTrpcTcp0xE07_0.pb.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
client/packets/pb/service/oidb/OidbSvcTrpcTcp0xE07_0.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
syntax = "proto3"; | ||
|
||
option go_package = "github.com/LagrangeDev/LagrangeGo/client/packets/pb/service/oidb"; | ||
|
||
message OidbSvcTrpcTcp0xE07_0 { | ||
uint32 Version = 1; | ||
uint32 Client = 2; | ||
uint32 Entrance = 3; | ||
OcrReqBody OcrReqBody = 10; | ||
} | ||
|
||
message OcrReqBody { | ||
string ImageUrl = 1; | ||
uint32 LanguageType = 2; | ||
uint32 Scene = 3; | ||
string OriginMd5 = 10; | ||
string AfterCompressMd5 = 11; | ||
string AfterCompressFileSize = 12; | ||
string AfterCompressWeight = 13; | ||
string AfterCompressHeight = 14; | ||
bool IsCut = 15; | ||
} | ||
|
||
message OidbSvcTrpcTcp0xE07_0_Response { | ||
int32 RetCode = 1; | ||
string ErrMsg = 2; | ||
string Wording = 3; | ||
OcrRspBody OcrRspBody = 10; | ||
} | ||
|
||
message OcrRspBody { | ||
repeated TextDetection TextDetections = 1; | ||
string Language = 2; | ||
string RequestId = 3; | ||
repeated string OcrLanguageList = 101; | ||
repeated string DstTranslateLanguageList = 102; | ||
repeated Language LanguageList = 103; | ||
uint32 AfterCompressWeight = 111; | ||
uint32 AfterCompressHeight = 112; | ||
} | ||
|
||
message TextDetection { | ||
string DetectedText = 1; | ||
int32 Confidence = 2; | ||
Polygon Polygon = 3; | ||
string AdvancedInfo = 4; | ||
} | ||
|
||
message Polygon { | ||
repeated Coordinate Coordinates = 1; | ||
} | ||
|
||
message Coordinate { | ||
int32 X = 1; | ||
int32 Y = 2; | ||
} | ||
|
||
message Language { | ||
string LanguageCode = 1; | ||
string LanguageDesc = 2; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.