Skip to content

Commit

Permalink
Merge pull request tuputech#8 from tuputech/feat/add-task-param
Browse files Browse the repository at this point in the history
feat: add task param to image recognition
  • Loading branch information
pauky authored Apr 14, 2021
2 parents c8d9dd3 + a1df2b8 commit c527c34
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Golang SDK for TUPU visual recognition service (v1.6.1)
<https://www.tuputech.com>

## Changelogs

#### v1.7.2
- add task param to image recognition
#### v1.7.1
- refactor speech sdk

Expand Down
15 changes: 10 additions & 5 deletions example/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ func main() {
fmt.Printf("Failed: %v\n", e)
return
}
//Optional Step: set identity of sub-user if necessary
handler.UID = "bucket-of-jackbauer"

//Optional Step: using http-client created by your own
// tr := &http.Transport{
Expand All @@ -31,10 +29,17 @@ func main() {
url1 := "your speech url1"
url2 := "your speech url2"
images1 := []string{url1, url2}

//No tag for images
printResult(handler.PerformWithURL(secretID, images1, nil))
printResult(handler.PerformWithURL(secretID, images1, nil, nil))

//Number of tags less than number of images, the rest images will use the last tag
printResult(handler.PerformWithURL(secretID, images1, []string{"Remote Image"}))
tags := []string{"Remote Image"}
printResult(handler.PerformWithURL(secretID, images1, tags, nil))

// run by appoint task
tasks := []string{"54bcfc6c329af61034f7c2fc"}
printResult(handler.PerformWithURL(secretID, images1, nil, tasks))

//Using local file or binary data
fileBytes, e2 := ioutil.ReadFile("your speech filePath")
Expand All @@ -45,7 +50,7 @@ func main() {
imgBinary := rcn.NewBinaryImage(fileBytes, "1.jpg")
defer imgBinary.ClearBuffer()
images2 := []*rcn.Image{rcn.NewLocalImage("your speech filePath"), imgBinary}
printResult(handler.Perform(secretID, images2, []string{"Local Image", "Using Buffer"}))
printResult(handler.Perform(secretID, images2, []string{"Local Image", "Using Buffer"}, nil))
}

func printResult(result string, statusCode int, e error) {
Expand Down
10 changes: 7 additions & 3 deletions lib/controller/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (hdler *Handler) RecognizeWithJSON(jsonStr, secretID string) (result string
}

// Recognize is the major method for initiating a recognition request
func (hdler *Handler) Recognize(secretID string, dataInfoSlice []*tupumodel.DataInfo) (result string, statusCode int, e error) {
func (hdler *Handler) Recognize(secretID string, dataInfoSlice []*tupumodel.DataInfo, tasks []string) (result string, statusCode int, e error) {
// Only 10 data can be carried in one request
if len(dataInfoSlice) > 10 || tupuerrorlib.StringIsEmpty(secretID) {
result = ""
Expand All @@ -193,7 +193,7 @@ func (hdler *Handler) Recognize(secretID string, dataInfoSlice []*tupumodel.Data
return
}

if req, e = hdler.request(&url, &params, dataInfoSlice); e != nil {
if req, e = hdler.request(&url, &params, dataInfoSlice, tasks); e != nil {
//log.Fatal(e)
return
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func (hdler *Handler) verify(message []byte, sig string) error {
return nil
}

func (hdler *Handler) request(url *string, params *map[string]string, dataInfoSlice []*tupumodel.DataInfo) (req *http.Request, e error) {
func (hdler *Handler) request(url *string, params *map[string]string, dataInfoSlice []*tupumodel.DataInfo, tasks []string) (req *http.Request, e error) {
// verify legatity params
if tupuerrorlib.PtrIsNil(url, params, dataInfoSlice) {
return nil, fmt.Errorf("%s, %s", tupuerrorlib.ErrorParamsIsEmpty, tupuerrorlib.GetCallerFuncName())
Expand All @@ -279,6 +279,10 @@ func (hdler *Handler) request(url *string, params *map[string]string, dataInfoSl
_ = writer.WriteField(key, val)
}

for _, task := range tasks {
_ = writer.WriteField("task", task)
}

// write binary data to request body
for index, dataInfoItem := range dataInfoSlice {
if e = addDataInfoField(writer, dataInfoItem, index); e == nil {
Expand Down
12 changes: 6 additions & 6 deletions recognition/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ func NewHandlerWithURL(privateKeyPath, url string) (h *Handler, e error) {
}

// PerformWithURL is a shortcut for initiating a recognition request with URLs of images
func (h *Handler) PerformWithURL(secretID string, imageURLs []string, tags []string) (result string, statusCode int, e error) {
func (h *Handler) PerformWithURL(secretID string, imageURLs []string, tags []string, tasks []string) (result string, statusCode int, e error) {
var images []*Image
for _, val := range imageURLs {
images = append(images, NewRemoteImage(val))
}
return h.Perform(secretID, images, tags)
return h.Perform(secretID, images, tags, tasks)
}

// PerformWithPath is a shortcut for initiating a recognition request with paths of images
func (h *Handler) PerformWithPath(secretID string, imagePaths []string, tags []string) (result string, statusCode int, e error) {
func (h *Handler) PerformWithPath(secretID string, imagePaths []string, tags []string, tasks []string) (result string, statusCode int, e error) {
var images []*Image
for _, val := range imagePaths {
images = append(images, NewLocalImage(val))
}
return h.Perform(secretID, images, tags)
return h.Perform(secretID, images, tags, tasks)
}

// Perform is the major method for initiating a recognition request
func (h *Handler) Perform(secretID string, images []*Image, tags []string) (result string, statusCode int, e error) {
func (h *Handler) Perform(secretID string, images []*Image, tags []string, tasks []string) (result string, statusCode int, e error) {
// verify legatity params
if tupuerror.PtrIsNil(images) || tupuerror.StringIsEmpty(secretID) {
e = fmt.Errorf("%s, %s", tupuerror.ErrorParamsIsEmpty, tupuerror.GetCallerFuncName())
Expand All @@ -93,6 +93,6 @@ func (h *Handler) Perform(secretID string, images []*Image, tags []string) (resu
dataInfoSlice = append(dataInfoSlice, images[i].dataInfo)
}

return h.hdler.Recognize(secretID, dataInfoSlice)
return h.hdler.Recognize(secretID, dataInfoSlice, tasks)

}

0 comments on commit c527c34

Please sign in to comment.