From 7fd3c82d9320295507628ee0b8fe08a2686342ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Sun, 3 Dec 2023 05:53:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BE=93=E5=87=BAtext?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/graphql.go | 19 ++++++++++++++++--- api/struct.go | 13 ++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/api/graphql.go b/api/graphql.go index ddd91e3..f319ff2 100644 --- a/api/graphql.go +++ b/api/graphql.go @@ -52,8 +52,17 @@ func NewSchema(config Config) (graphql.Schema, error) { }, }) - // 图像中的文字提取 - // text := graphql.NewObject(graphql.ObjectConfig{}) + // 图像中的文字提取 [{"text": "角色选择", "confidence": 0.8484202027320862, "coordinate": [[666.0, 66.0], [908.0, 81.0], [903.0, 174.0], [661.0, 160.0]]} + text := graphql.NewObject(graphql.ObjectConfig{ + Name: "Text", + Fields: graphql.Fields{ + "text": &graphql.Field{Type: graphql.String}, + "confidence": &graphql.Field{Type: graphql.Float}, + "coordinate": &graphql.Field{Type: &graphql.List{ + OfType: graphql.NewList(graphql.Float), + }}, + }, + }) // 图像的可选字段 image := graphql.NewObject(graphql.ObjectConfig{ @@ -67,7 +76,7 @@ func NewSchema(config Config) (graphql.Schema, error) { "description": &graphql.Field{Type: graphql.String}, "tags": &graphql.Field{Type: graphql.String}, "rank": &graphql.Field{Type: graphql.String}, - "text": &graphql.Field{Type: graphql.String}, + "text": &graphql.Field{Type: graphql.NewList(text)}, "comment_num": &graphql.Field{Type: graphql.Int}, "article_category_top_id": &graphql.Field{Type: graphql.Int}, "praise_count": &graphql.Field{Type: graphql.Int}, @@ -302,6 +311,10 @@ func NewSchema(config Config) (graphql.Schema, error) { return nil, err } + // 打印带缩进格式化的JSON + // str, _ := json.MarshalIndent(images, "", " ") + // fmt.Println("获取图像列表成功", string(str)) + // 获取用户信息(如果图像列表不为空且请求字段中包含user) if len(images) > 0 && strings.Contains(fields_str, "user_id") { // 取到所有的用户ID, 去除重复 diff --git a/api/struct.go b/api/struct.go index bcb1d0a..0ac851e 100644 --- a/api/struct.go +++ b/api/struct.go @@ -1,6 +1,7 @@ package api import ( + "encoding/json" "time" ) @@ -13,7 +14,6 @@ type Image struct { Description string `json:"description" db:"description"` Tags string `json:"tags" db:"tags"` Rank string `json:"rank" db:"rank"` - Text string `json:"text" db:"text"` CommentNum int `json:"comment_num" db:"comment_num"` ArticleCategoryTopId int `json:"article_category_top_id" db:"article_category_top_id"` PraiseCount int `json:"praise_count" db:"praise_count"` @@ -24,6 +24,17 @@ type Image struct { Article Article `json:"article" db:"-"` CreateTime time.Time `json:"create_time" db:"create_time"` UpdateTime time.Time `json:"update_time" db:"update_time"` + Text TextList `json:"text" db:"text"` +} + +type TextList []struct { + Text string `json:"text"` + Confidence float64 `json:"confidence"` + Coordinate [][]float64 `json:"coordinate"` +} + +func (a *TextList) Scan(value interface{}) error { + return json.Unmarshal(value.([]byte), a) } type User struct {