Compare and Match Faces in Go

Cloudmersive
2 min readMar 21, 2022

--

Implement a smooth facial comparison & match feature to your Go project with your Cloudmersive API key, using ready-to-run code below from the Cloudmersive API Console. Please note that the input image can contain multiple faces, but the reference image (containing whichever face you are attempting to match) must only contain one face.

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/image/face/compare-and-match"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputImage",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
file, errFile2 := os.Open("/path/to/file")
defer file.Close()
part2,
errFile2 := writer.CreateFormFile("matchFace",filepath.Base("/path/to/file"))
_, errFile2 = io.Copy(part2, file)
if errFile2 != nil {
fmt.Println(errFile2)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

Take a look at a JSON response model for this API below:

{
"Successful": true,
"Faces": [
{
"LeftX": 0,
"TopY": 0,
"RightX": 0,
"BottomY": 0,
"HighConfidenceMatch": true,
"MatchScore": 0
}
],
"FaceCount": 0,
"ErrorDetails": "string"
}

First time using a Cloudmersive API? Welcome aboard. Create a Cloudmersive Account on our website (www.cloudmersive.com) for free with an 800/month API call limit, or choose from one of our other available account options for more bandwidth.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet