Skip to content

Commit

Permalink
feat: image ocr, proto gen (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
icarus-ai authored Nov 13, 2024
1 parent 63ccad5 commit 31e2113
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 6 deletions.
16 changes: 16 additions & 0 deletions client/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,3 +1264,19 @@ func (c *QQClient) GetGroupMessages(groupUin, startSeq, endSeq uint32) ([]*messa

return ret, nil
}

// ImageOcr 图片识别 有些域名的图可能无法识别,需要重新上传到tx服务器并获取图片下载链接
func (c *QQClient) ImageOcr(url string) (*oidb2.OcrResponse, error) {
if url != "" {
pkt, err := oidb2.BuildImageOcrRequestPacket(url)
if err != nil {
return nil, err
}
resp, err := c.sendOidbPacketAndWait(pkt)
if err != nil {
return nil, err
}
return oidb2.ParseImageOcrResp(resp)
}
return nil, errors.New("image error")
}
4 changes: 2 additions & 2 deletions client/packets/oidb/group_fs_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func BuildGroupFSDownloadReq(groupUin uint32, fileID string) (*Packet, error) {
body := &oidb.OidbSvcTrpcTcp0X6D6{
Download: &oidb.OidbSvcTrpcTcp0X6D6Download{
GroupUin: groupUin,
AppID: 7,
BusID: 102,
AppId: 7,
BusId: 102,
FileId: fileID,
},
}
Expand Down
79 changes: 79 additions & 0 deletions client/packets/oidb/image_ocr.go
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
}
2 changes: 1 addition & 1 deletion client/packets/pb/message/element.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions client/packets/pb/service/oidb/OidbSvcTrpcTcp0x6D6.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions 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.

61 changes: 61 additions & 0 deletions client/packets/pb/service/oidb/OidbSvcTrpcTcp0xE07_0.proto
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;
}
2 changes: 1 addition & 1 deletion client/packets/pb/service/oidb/OidbSvcTrpcTcp0xFE1_2.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 31e2113

Please sign in to comment.