Created
April 21, 2018 15:42
-
-
Save josue/ce9188b0beb5955714b13b3271ec4922 to your computer and use it in GitHub Desktop.
Golang API example using Kairos API with detect method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
) | |
var ( | |
SERVER_PORT = ":3000" | |
KAIROS_API_URL = "https://api.kairos.com/detect" | |
KAIROS_APP_ID = os.Getenv("APP_ID") | |
KAIROS_APP_KEY = os.Getenv("APP_KEY") | |
) | |
type RequestData struct { | |
Student string `json:"student"` | |
Image string `json:"image"` | |
} | |
type ResponseData struct { | |
Student string `json:"student"` | |
Image string `json:"image"` | |
Gender string `json:"gender"` | |
} | |
type KairosPayload struct { | |
Image string `json:"image"` | |
} | |
type KairosResponse struct { | |
Images []struct { | |
Status string `json:"status"` | |
Width int `json:"width"` | |
Height int `json:"height"` | |
File string `json:"file"` | |
Faces []struct { | |
TopLeftX int `json:"topLeftX"` | |
TopLeftY int `json:"topLeftY"` | |
ChinTipX int `json:"chinTipX"` | |
RightEyeCenterX int `json:"rightEyeCenterX"` | |
Yaw int `json:"yaw"` | |
ChinTipY int `json:"chinTipY"` | |
Confidence float64 `json:"confidence"` | |
Height int `json:"height"` | |
RightEyeCenterY int `json:"rightEyeCenterY"` | |
Width int `json:"width"` | |
LeftEyeCenterY int `json:"leftEyeCenterY"` | |
LeftEyeCenterX int `json:"leftEyeCenterX"` | |
Pitch int `json:"pitch"` | |
Attributes struct { | |
Lips string `json:"lips"` | |
Asian float64 `json:"asian"` | |
Gender struct { | |
Type string `json:"type"` | |
} `json:"gender"` | |
Age int `json:"age"` | |
Hispanic float64 `json:"hispanic"` | |
Other float64 `json:"other"` | |
Black float64 `json:"black"` | |
White float64 `json:"white"` | |
Glasses string `json:"glasses"` | |
} `json:"attributes"` | |
FaceID int `json:"face_id"` | |
Quality float64 `json:"quality"` | |
Roll int `json:"roll"` | |
} `json:"faces"` | |
} `json:"images"` | |
} | |
func HandleRequest(w http.ResponseWriter, r *http.Request) { | |
if r.Body == nil { | |
http.Error(w, "Please send a request JSON body", 400) | |
return | |
} | |
// capture your own API request | |
data := RequestData{} | |
err := json.NewDecoder(r.Body).Decode(&data) | |
if err != nil { | |
http.Error(w, err.Error(), 400) | |
return | |
} | |
log.Println(fmt.Sprintf("Received student request: %+v", data)) | |
// parepare Kairos payload | |
payload := KairosPayload{Image: data.Image} | |
payloadBytes := new(bytes.Buffer) | |
json.NewEncoder(payloadBytes).Encode(payload) | |
// send to Kairos API | |
client := &http.Client{} | |
req, err := http.NewRequest("POST", KAIROS_API_URL, payloadBytes) | |
if err != nil { | |
http.Error(w, "unable to send to kairos api", 400) | |
return | |
} | |
req.Header.Add("content-type", "application/json") | |
req.Header.Add("app_id", KAIROS_APP_ID) | |
req.Header.Add("app_key", KAIROS_APP_KEY) | |
kairosResponse, err := client.Do(req) | |
defer kairosResponse.Body.Close() | |
// parse the Kairos response | |
kairosJsonResponse := KairosResponse{} | |
json.NewDecoder(kairosResponse.Body).Decode(&kairosJsonResponse) | |
// my custom response | |
myResponse := ResponseData{ | |
Student: data.Student, | |
Image: data.Image, | |
} | |
// check the student's gender | |
if kairosJsonResponse.Images[0].Faces[0].Attributes.Gender.Type == "F" { | |
myResponse.Gender = "female" | |
} else { | |
myResponse.Gender = "male" | |
} | |
// prepare the JSON response | |
json, err := json.Marshal(myResponse) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
// output | |
w.Header().Set("Content-Type", "application/json") | |
w.Write(json) | |
} | |
func main() { | |
log.Println("Running api server on port " + SERVER_PORT) | |
http.HandleFunc("/", HandleRequest) | |
http.ListenAndServe(SERVER_PORT, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Build your own API with Golang and detect images with Kairos API
Note: make sure your Kairos API keys is initialized on your environment variables:
build the api server & run it:
usage example: