Skip to content

Instantly share code, notes, and snippets.

@josue
Last active April 30, 2018 16:52
Show Gist options
  • Save josue/b9653127dfbee1a65b612b07367877db to your computer and use it in GitHub Desktop.
Save josue/b9653127dfbee1a65b612b07367877db to your computer and use it in GitHub Desktop.
Golang - Kairos API
package main
import (
"flag"
"time"
"fmt"
"strings"
"net/http"
"io/ioutil"
"encoding/json"
"encoding/base64"
)
// app defaults
const API_HOST = "https://api.kairos.com"
const APP_ID = "xxxxxxxxxxxx"
const APP_KEY = "xxxxxxxxxxx"
var (
defaultImageUrl = "https://i.imgur.com/C8MFfu1.jpg" // Ellen image
defaultGalleryName = "celebrities"
defaultSubjectId = "Ellen Degeneres"
)
// cli args
var (
res, source string
err error
encodeBase64Ptr = flag.Bool("base64", false, "encode base64")
filePtr = flag.String("file", "", "local file")
imageUrl = flag.String("imageurl", defaultImageUrl, "image url")
galleryName = flag.String("galleryname", defaultGalleryName, "gallery name")
subjectId = flag.String("subjectid", defaultSubjectId, "subject id")
)
func api(method string, endpoint string, data map[string]interface{}) (string, error){
if method == "" {
method = "GET"
}
url := API_HOST + "/" + endpoint
req, err := http.NewRequest(method, url, nil)
if method == "POST" {
jsonData, err := json.Marshal(data)
if err != nil {
return "", err
}
payload := strings.NewReader(string(jsonData))
req, err = http.NewRequest(method, url, payload)
}
if err != nil {
return "", err
}
if method == "POST" {
req.Header.Add("content-type", "application/json")
} else {
req.Header.Add("content-type", "application/x-www-form-urlencoded")
}
req.Header.Add("app_id", APP_ID)
req.Header.Add("app_key", APP_KEY)
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
var jsonResponse map[string] interface{}
if err := json.Unmarshal(body, &jsonResponse); err != nil {
return "", err
}
prettyJson, err := json.MarshalIndent(jsonResponse, "", " ")
if err != nil {
return "", err
}
return string(prettyJson), nil
}
func Enroll() {
displayTitle("Enroll")
res, err = api("POST", "enroll", map[string]interface{}{
"image": source,
"gallery_name": galleryName,
"subject_id": subjectId,
})
displayOutput(res, err)
}
func Verify() {
displayTitle("Verify")
res, err = api("POST", "verify", map[string]interface{}{
"image": source,
"gallery_name": galleryName,
"subject_id": subjectId,
})
displayOutput(res, err)
}
func Recognize() {
displayTitle("Recognize")
res, err = api("POST", "recognize", map[string]interface{}{
"image": source,
"gallery_name": galleryName,
})
displayOutput(res, err)
}
func Detect() {
displayTitle("Detect")
res, err = api("POST", "detect", map[string]interface{}{
"image": source,
})
displayOutput(res, err)
}
func GalleryListAll() {
displayTitle("Gallery/List_All")
res, err := api("POST", "gallery/list_all", map[string]interface{}{})
displayOutput(res, err)
}
func GalleryView() {
displayTitle("Gallery/View")
res, err = api("POST", "gallery/view", map[string]interface{}{
"gallery_name": galleryName,
})
displayOutput(res, err)
}
func GalleryViewSubject() {
displayTitle("Gallery/View_Subject")
res, err = api("POST", "gallery/view_subject", map[string]interface{}{
"gallery_name": galleryName,
"subject_id": subjectId,
})
displayOutput(res, err)
}
func GalleryRemove() {
displayTitle("Gallery/Remove")
res, err = api("POST", "gallery/remove", map[string]interface{}{
"gallery_name": galleryName,
})
displayOutput(res, err)
}
func GalleryRemoveSubject() {
displayTitle("Gallery/Remove_Subject")
res, err = api("POST", "gallery/remove_subject", map[string]interface{}{
"gallery_name": galleryName,
"subject_id": subjectId,
})
displayOutput(res, err)
}
func MediaPost(sourceUrl string) string {
displayTitle("Media - POST")
res, err = api("POST", "v2/media?source=" + sourceUrl, map[string]interface{}{})
displayOutput(res, err)
var jsonData map[string]interface{}
json.Unmarshal([]byte(res), &jsonData)
return jsonData["id"].(string)
}
func MediaGet(media_id string) {
displayTitle("Media - GET")
res, err = api("GET", "v2/media/" + media_id, map[string]interface{}{})
displayOutput(res, err)
}
func Analytics(media_id string) {
displayTitle("Analytics")
res, err = api("GET", "v2/analytics/" + media_id, map[string]interface{}{})
displayOutput(res, err)
}
func Delete(media_id string) {
displayTitle("Delete")
res, err = api("DELETE", "v2/media/" + media_id, map[string]interface{}{})
displayOutput(res, err)
}
func getBase64ContentFromFile(filePath string) (string, error) {
fileBytes, fileError := ioutil.ReadFile(filePath)
if fileError != nil {
return "", fileError
}
basee6String := base64.StdEncoding.EncodeToString(fileBytes)
return basee6String, nil
}
func displayOutput(data string, err error) {
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println(data)
}
}
func displayTitle(title string) {
fmt.Println(fmt.Sprintf("\n%s [%s] %s", strings.Repeat("-", 10), title, strings.Repeat("-", 20)))
}
func DisplayContentFrom() {
// check if sending by base64, else we use image url
if *encodeBase64Ptr == true && *filePtr != "" {
base64Data, _ := getBase64ContentFromFile(*filePtr)
source = base64Data
fmt.Println("Using base64 content from file: " + *filePtr)
} else {
source = *imageUrl
fmt.Println("Using image url: " + *imageUrl)
}
}
func main() {
// init
flag.Parse()
DisplayContentFrom()
displayTitle("<<< Face Recognition >>>")
// essential methods
Enroll()
Verify()
Recognize()
Detect()
// gallery methods
GalleryListAll()
GalleryView()
GalleryViewSubject()
GalleryRemoveSubject()
GalleryRemove()
displayTitle("<<< Emotion Analysis >>>")
sourceUrl := "https://media.kairos.com/demo/emotion/videos/video_1.mp4"
fmt.Println("\nUsing media source url: " + sourceUrl)
// v2
media_id := MediaPost(sourceUrl)
fmt.Println(media_id)
MediaGet(media_id)
Analytics(media_id)
fmt.Println("\n- Wait 5 seconds before deleting the media id")
time.Sleep(5 * time.Second)
Delete(media_id)
}
@josue
Copy link
Author

josue commented Oct 21, 2017

Download & Execute:

  1. Save script to localhost:
    curl -O https://gist.githubusercontent.com/josue/b9653127dfbee1a65b612b07367877db/raw/kairos-api.go

  2. Edit the Go script with your APP_ID & APP_KEY credentials:

    const APP_ID = "xxxxxxxxxxxx"
    const APP_KEY = "xxxxxxxxxxx"
  3. Run (or build) the Go script, with usage:

    use internal (hard-coded) image url:

    go run kairos-api.go

    use local file path and encode image as base64:

    go run kairos-api.go -base64 -file=/path/to/file.jpg

View Demo:

asciicast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment