2024-11-21 01:26:40 +08:00
|
|
|
package catface
|
|
|
|
|
|
|
|
import (
|
|
|
|
"catface/app/global/variable"
|
|
|
|
"catface/app/utils/micro_service"
|
|
|
|
"context"
|
2024-11-23 01:30:13 +08:00
|
|
|
"fmt"
|
|
|
|
|
2024-11-21 01:26:40 +08:00
|
|
|
"github.com/carlmjohnson/requests"
|
|
|
|
)
|
|
|
|
|
2024-11-23 01:30:13 +08:00
|
|
|
type FaceData struct {
|
|
|
|
FaceBreed string `json:"face_breed"`
|
2024-11-21 01:26:40 +08:00
|
|
|
Cats []struct {
|
|
|
|
Id int64 `json:"id"`
|
2024-11-23 01:30:13 +08:00
|
|
|
Conf float64 `json:"conf"`
|
2024-11-21 01:26:40 +08:00
|
|
|
} `json:"cats"`
|
|
|
|
}
|
|
|
|
|
2024-11-23 01:30:13 +08:00
|
|
|
type CatFaceRes struct {
|
|
|
|
Status int `json:"status"`
|
|
|
|
Data FaceData `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCatfaceResult(filePath string) (*FaceData, error) {
|
2024-11-21 01:26:40 +08:00
|
|
|
body := map[string]interface{}{
|
|
|
|
"file_path": filePath,
|
|
|
|
}
|
2024-11-23 01:30:13 +08:00
|
|
|
var res CatFaceRes
|
2024-11-21 01:26:40 +08:00
|
|
|
err := requests.URL(micro_service.FetchPythonServiceUrl("cnn/detect_cat")).
|
|
|
|
BodyJSON(&body).
|
|
|
|
ToJSON(&res).
|
|
|
|
Fetch(context.Background())
|
2024-11-23 01:30:13 +08:00
|
|
|
|
2024-11-21 01:26:40 +08:00
|
|
|
if err != nil {
|
|
|
|
variable.ZapLog.Error("获取cat face结果集失败: " + err.Error())
|
2024-11-23 01:30:13 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Status != 200 {
|
|
|
|
return nil, fmt.Errorf("请求状态码错误: %d", res.Status)
|
2024-11-21 01:26:40 +08:00
|
|
|
}
|
|
|
|
|
2024-11-23 01:30:13 +08:00
|
|
|
return &res.Data, nil
|
2024-11-21 01:26:40 +08:00
|
|
|
}
|