Using Laravel and Sightengine’s Artificial Intelligence as a Service to detect and filter inappropriate images
Introduction: Artificial Intelligence as a Service (AIaaS)
Artificial Intelligence as a service, or AIaaS, is the on-demand delivery and use of AI and Deep learning capabilities towards an individual or business objective.
AIaaS provides cognitive computing capabilities such as language detection, image analysis and detection, text recognition, Natural language processing, facial expression, sentiment analysis etc.
Consuming AI as a service means that businesses and developers do not need to have in-house expertise to engage in research, development and training of AI models, instead they can harness the computing advantages of an AI and deep learning system that is already trained in a particular skill set.
For this article, we’re going to present one way in which we can solve for unwanted or inappropriate image display a web or mobile platform using an AIaaS service called Sightengine for image detection.
what is Sightengine?
Sightengine is an Artificial Intelligence company that uses proprietary state-of-the-art Deep Learning systems to provide powerful image and video analysis through simple and clean APIs. They provide different services in the field of image recognition like face, scam, nudity, celebrity, minors, weapons, alcohol, drugs, image quality, offensive and hate signs, and symbols, and artificial text recognition.
Why use Sightengine for image detection over other public cloud providers?
Currently, Sightengine offers the most accurate image detection and classification with a 97.1% accuracy as demonstrated in a benchmark analysis across AWS Rekognition, Google Cloud Vision and Microsoft Cognitive Service.
Additionally, Sightengine has the following advantages:
- Highest image detection accuracy and adaptability across other providers
- Easy setup and adoption. Getting started is simple that with less than 3 lines of code, you can get the image analysis you want.
- Simple, concise and clear documentation is a rare thing to come by. Lucky for us, Sightengine does it well. Like seriously, check it out.
- Generous free tier offer. The free tier plan provides 2000 operations a month, with a cap of 500 a day.
Breathtaking simplicity in the adoption of AIaaS
Sightengine has brilliantly reduced the complexity of an AI detection system down to 2 lines of code. The first one initializes the engine, the second checks the image based on the parameter passed, in our case, we’ll focus on the nudity detection model.
The Nudity Detection Model determines if an image contains some level of nudity along with a description of the “level” of nudity.
These levels are based on the following criteria:
- Raw nudity (X-rated material such as genitals, bare breasts…)
Partial nudity due to the presence of women in bikini
- Partial nudity due to the presence of bare-chested males. Partial nudity due to the presence of suggestive cleavages
- No nudity (safe content)
Probability analysis and meaning
The probability score of each of these criteria is between 0 and 1, with 1 having the highest probability level while 0 having the lowest.
With a “raw” score of 0.01, this means that the image has the lowest probability of containing any nudity.
With a score close to 1, this “safe” score tells us that the image is safe.
“partial” : 0.01
A partial score of 0.01 means that there is the lowest probability of having implied/partial nudity on the image.
Use Case: Profile image upload verification
When a user uploads a new profile picture, we’ll analyze the image and either reject or accept the upload based on our acceptance criteria. So something like this:
Here are the 4 objectives we’re going to cover:
- Create a free SightEngine account an grab the API credentials
- Set up our Laravel application to use this API
- Create the routes and controller files for our integration logic
- Test and adjust the response retrieved to match our acceptance ratio
1. Getting started with Sightengine
Head on over to Sightengine and create a free account. The dashboard section will display all the calls made with your API key while the My Account section will contain your API credentials and other details related to your account.
2. Integrating with Laravel
Laravel is a php web application framework with an expressive, elegant syntax that follows the model–view–controller architectural pattern.
Here’s what we need to do:
- Install the package
- Set up the .env file, views, route file and controller
- Initializing the SightEngine client
First, we’ll install Sightengine using composer or you can update your composer.json file.
composer require sightengine/client-php
Update .env file
Create the 2 property entries on your .env file with the API info retrieved from Sightengine.
SIGHTENGINEUSER=your api user
SIGHTENGINEKEY=your api key
3. Creating the Views, Routes and Controller Files
Create form for the avatar upload and set the POST destination to “/user/avatar/update” in the action tag as shown below.
Next, we’ll set the route file that will direct the form submission action to the controller which will house our logic. In the example below, we’ve created an “UploadsController” with a function called “updateAvatar”.
Lastly, create the UploadsController using the php artisan command below:
php artisan make:controller UploadsController
Our UploadsController will handle the following elements of our image verification logic:
1.Handle the user form request submission (lines 16–26)
2. Validate the upload file is an acceptable image format (lines 28–30)
3. Save the file locally in our storage directory (lines 33–44)
4. Analyze the local image using Sightengine (lines 51–54)
5. Set an acceptance threshold for the image (lines 61–62)
6.if the image is not acceptable, we will reject the upload with an error message to the user. (lines 64–75)
7. If image is acceptable, we will upload it to amazon s3 storage and remove the file from our temporary storage directory and send a response back to the user (lines 77–93)
4. Testing: Determining an acceptance ratio
Establishing an acceptance criterion that is consistent with your business terms and end-users experience is required to ensure you meet your objectives and intended results.
Sightengine algorithms instantly analyzes the contents of the image including the pose, body parts, clothing, etc and from this provides an output.
To determine an acceptable threshold based on the raw, partial and safe probability scores, I’d recommend playing with the demo provided on the Sightengine page. The demo lets you upload or link to your image and provides a score association of each image.
Alternatively, you can use the dieDump (dd) helper function to see the data associated with our ImageCheck variable as shown in line 13.
Test Analysis and Results
For our application testing, when we run the analysis on our profile image, we will have the JSON output shown below.
A raw score of 0.01, means that our image has the least probability of containing raw nudity. So when determining an acceptance ratio, you can use the raw score to set this. In the UploadsController example above, in line, 62, we set an acceptance ratio of 0.60 and determined that any image that is greater than this score should be rejected.
With a safe score of 0.98, means that the image has the highest probability of not containing any nudity. This is inversely related to the raw score from above. You can use the “safe score” to set your acceptance ratio such that we will only accept an image upload if the safe score is greater than 0.60
The partial score of 0.01 means that our image has the least probability of containing partial or implied nudity. This range anywhere from cleavage to implied nudity. Lastly, when determining an acceptance ratio, you can set it to 0.70 and determined that any image that is greater than this score should be rejected.
How we’re using it
At rugged I/O, we’ve built an ecosystem of applications that leverage different aspects of AI. For the example above with the image upload, this is an active feature of RuggedProjects, a project management tool built to make your workflow, fun and productive. Check it out
Thanks to the democratization of AI and Machine Learning capabilities, businesses and individuals to have unprecedented opportunities to bring their ideas to life. From payments processing to fraud detection, safer online communities to genomic research and analysis, these avenues will keep increasing as different AI companies focus on particular niches and audiences.
What are some use cases that image detection can be applied to? Let me know in the comments.
Thank you for reading my article. 🖐