Identify NSFW Content in an Image in C/C++
1 min readAug 26, 2021
If you’re noticing inappropriate images sneaking into the content of your website, it may be time to integrate a system to recognize the ‘Not Safe for Work’ materials so you can eliminate any potentially damaging repercussions to your company’s reputation. In this short tutorial, we will show you how to use an API in C/C++ to automatically detect NSFW content from images and provide a NSFW score and classification.
Our first step is installing libcurl into our C/C++ project:
libcurl/7.75.0
Next, we can call the function with the following code:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/image/nsfw/classify");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "imageFile");
curl_mime_filedata(part, "/path/to/file");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);
And we’re done! Simple as that.