Describe an Image in Natural Language using Go
Our Image API has a way with words: specifically, the Natural Language description iteration will take an input image file and return an English-language text description of the image in sentence string format. This is a great tool for image processing, captioning and labeling at scale. Ready-to-run code is included in this article for a custom-code connection in Go:
package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)func main() {url := "https://api.cloudmersive.com/image/recognize/describe"
method := "POST"payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("imageFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
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))
}
Your API response will return the following JSON information:
{
"Successful": true,
"Highconfidence": true,
"BestOutcome": {
"ConfidenceScore": 0,
"Description": "string"
},
"RunnerUpOutcome": {
"ConfidenceScore": 0,
"Description": "string"
}
}
Want to try it out first? Head to the Cloudmersive API Console page and use the “try it out” button to quickly test the API on a sample file.
If you end up being a fan, head to our website (www.cloudmersive.com) and create an account to receive your very own API key, which will provide access to hundreds of Cloudmersive APIs available on the console page. A free-tier account option is available with a limit of 800 API calls per month.