Skip to content

Commit

Permalink
Merge pull request #15 from PIGfaces/feat/video-sdk
Browse files Browse the repository at this point in the history
Feat/video sdk
  • Loading branch information
pauky authored Aug 4, 2021
2 parents 97b709a + 4b9eb52 commit 23f4b31
Show file tree
Hide file tree
Showing 17 changed files with 930 additions and 55 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
.DS_Store

.vscode/
go.mod
go.sum

test/img/

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Golang SDK for TUPU visual recognition service (v1.6.1)

## Changelogs

#### v1.9.0
- add video(async/sync) sdk and adjust speech async sdk

#### v1.8.0
- add testSync method and lib

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,36 @@ import (
"time"

"github.com/bitly/go-simplejson"
longSpch "github.com/tuputech/tupu-go-sdk/recognition/speech/longasync"
SPCHAS "github.com/tuputech/tupu-go-sdk/recognition/speech/speechasync"
)

func main() {

// step1. get your secretID
secretID := "your secretID"
privateKeyPath := "rsa_private_key.pem"
// empty string will using default server url
serverURL := ""
var (
// step1. get your secretID
secretID string = "your secretID"
// your rsa_private_key local path
privateKeyPath string = "rsa_private_key.pem"
// your receive recognition result server url
callbackUrl string = "your server url"
// your need to recogniton speech url
speechUrl string = "your speech url"
// empty string will using default server url
)

// step2. create speech handler
speechHandler, err := longSpch.NewSpeechHandler(privateKeyPath, serverURL)
speechHandler, err := SPCHAS.NewSpeechHandler(privateKeyPath)
if err != nil {
fmt.Println("-------- ERROR ----------")
return
}
// step3. create LongSpeech object
longSpeech := &longSpch.LongSpeech{
FileRemoteURL: "your speech url",
CallbackURL: "your callback url",
}

// step3. recognition
// start recognition and get result
result, statusCode, err := speechHandler.Perform(secretID, longSpeech, 0)
// WithXXXX function is optional for api request params
// e.g. simple to use
// result, statusCode, err := speechHandler.Perform(secretID, speechUrl, callbackUrl)
result, statusCode, err := speechHandler.Perform(secretID, speechUrl, callbackUrl, SPCHAS.WithCallbackRule(SPCHAS.CallbackRuleALL), SPCHAS.WithUserId("testId"))
printResult(result, statusCode, err)
}

Expand Down Expand Up @@ -70,5 +75,5 @@ func printResult(result string, statusCode int, err error) {
for k, v := range task {
fmt.Printf("- Result: [%v]\n\t%v\n", k, v)
}
fmt.Println("----------------------\n")
fmt.Println("----------------------")
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ import (
"time"

"github.com/bitly/go-simplejson"
spch "github.com/tuputech/tupu-go-sdk/recognition/speech/shortsync"
spch "github.com/tuputech/tupu-go-sdk/recognition/speech/speechsync"
)

func main() {

// step1. get your secretID
secretID := "your secretID"
privateKeyPath := "rsa_private_key.pem"
serverURL := ""
// step2. create speech handler
speechHandler, err := spch.NewShortSpeechHandler(privateKeyPath, serverURL)
speechHandler, err := spch.NewSyncHandler(privateKeyPath)
if err != nil {
fmt.Println("-------- ERROR ----------")
return
Expand All @@ -34,7 +33,7 @@ func main() {
testSpeechAPIWithBinary(secretID, speechHandler)
}

func testSpeechAPIWithBinary(secretID string, speechHandler *spch.ShortSpeechHandler) {
func testSpeechAPIWithBinary(secretID string, speechHandler *spch.SyncHandler) {
//Using local file or binary data
filePath := "your speech filePath"
fileBytes, e2 := ioutil.ReadFile(filePath)
Expand All @@ -47,25 +46,25 @@ func testSpeechAPIWithBinary(secretID string, speechHandler *spch.ShortSpeechHan
filepath.Base(filePath): fileBytes,
}

printResult(speechHandler.PerformWithBinary(secretID, speechSlice, 0))
printResult(speechHandler.PerformWithBinary(secretID, speechSlice))
}

func testSpeechAPIWithPath(secretID string, speechHandler *spch.ShortSpeechHandler) {
func testSpeechAPIWithPath(secretID string, speechHandler *spch.SyncHandler) {
// step1. get speech file path
speechPaths := []string{
"your speech filePath",
}

// step2. get result of speech recognition API
printResult(speechHandler.PerformWithPath(secretID, speechPaths, 0))
printResult(speechHandler.PerformWithPath(secretID, speechPaths))
}

func testSpeechAPIWithURL(secretID string, speechHandler *spch.ShortSpeechHandler) {
func testSpeechAPIWithURL(secretID string, speechHandler *spch.SyncHandler) {
// step1. get speech file url
speechURLs := []string{
"your speech url",
}
printResult(speechHandler.PerformWithURL(secretID, speechURLs, 0))
printResult(speechHandler.PerformWithURL(secretID, speechURLs))
}

func printResult(result string, statusCode int, err error) {
Expand Down Expand Up @@ -95,6 +94,7 @@ func printResult(result string, statusCode int, err error) {
message, e = rlt.Get("message").String()
timestamp, e = rlt.Get("timestamp").Int64()
timestamp = int64(float64(timestamp) / 1000)
task, e = rlt.Map()
// parse vulgar speech
// task, e = rlt.Get("5c8213b9bc807806aab0a574").Map()
// if e != nil {
Expand All @@ -103,8 +103,8 @@ func printResult(result string, statusCode int, err error) {
// }

fmt.Printf("- Code: %v %v\n- Time: %v\n", code, message, time.Unix(timestamp, 0))
// for k, v := range task {
// fmt.Printf("- Task: [%v]\n%v\n", k, v)
// }
fmt.Println("----------------------\n")
for k, v := range task {
fmt.Printf("- Task: [%v]\n%v\n", k, v)
}
fmt.Println("----------------------")
}
114 changes: 114 additions & 0 deletions example/videodemo/async/video.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"fmt"
"time"

"github.com/bitly/go-simplejson"
VDASHdler "github.com/tuputech/tupu-go-sdk/recognition/video/videoasync"
)

func main() {

// step1. get your secretID
var (
// your secretId
secretID string = "your sid"
// your rsa_private_key local path
privateKeyPath string = "your rsa private key"
// your video url
videoUrl string = "your need to recognition video url"
// your receive recognition result server url
callbackUrl string = "your server url"
// you will get a videoId after request tupu-video-api
videoId string
// json string
result string
// video handler
vdasHdler *VDASHdler.AsyncHandler
err error
statusCode int
rlt *simplejson.Json
)

// step2. create speech handler
vdasHdler, err = VDASHdler.NewVideoAsyncHandler(privateKeyPath)
if err != nil {
fmt.Println("-------- ERROR ----------")
return
}
// step3. create LongSpeech object
callbackRules := make(map[string][]VDASHdler.TaskCallbackRule)
callbackRules["54bcfc6c329af61034f7c2fc"] = []VDASHdler.TaskCallbackRule{
{
Label: 1,
Review: true,
},
}

// start recognition and get result
// WithXXXX function is optional for api request params
// e.g. simple to use
// result, statusCode, err = vdasHdler.Perform(secretID, videoUrl, callbackUrl)
result, statusCode, err = vdasHdler.Perform(secretID, videoUrl, callbackUrl, VDASHdler.WithCallbackRules(callbackRules))
printResult(result, statusCode, err)

// (optional) query recognition result ()
rlt, err = simplejson.NewJson([]byte(result))

// get recogniton videoId from Perform func
videoId, err = rlt.Get("videoId").String()
if err != nil {
fmt.Println("start recognition failed, result:", result)
return
}

// (optional) query videoId recognition result
result, statusCode, err = vdasHdler.QueryRecognitionResult(secretID, videoId)
printResult(result, statusCode, err)

// (optional) stop videoId recognition task ()
result, statusCode, err = vdasHdler.CloseRecognitionTask(secretID, videoId)
printResult(result, statusCode, err)

}

func printResult(result string, statusCode int, err error) {
if err != nil {
fmt.Printf("Failed: %v\n", err)
return
}

fmt.Println("-------- v1.0 --------")
fmt.Printf("Status-Code: %v\n-----\n", statusCode)

// Example of parsing json string using simplejson
var (
rlt, e = simplejson.NewJson([]byte(result))
task map[string]interface{}
code, message string
timestamp int64
)
if e != nil {
fmt.Println("[ERROR] params error")
return
}
// fmt.Println(result)

// Get the value corresponding to the key in json
code, e = rlt.Get("code").String()
message, e = rlt.Get("message").String()
timestamp, e = rlt.Get("timestamp").Int64()
timestamp = int64(float64(timestamp) / 1000)
task, e = rlt.Map()
if e != nil {
fmt.Println("decode error")
return
}

fmt.Printf("- Code: %v %v\n- Time: %v\n", code, message, time.Unix(timestamp, 0))
for k, v := range task {
fmt.Printf("- Result: [%v]\n\t%v\n", k, v)
}
fmt.Println("----------------------")
}
Loading

0 comments on commit 23f4b31

Please sign in to comment.