Detects object and get result as json object Introduction
This article explains how to detect objects in an existing image.
The bounds method parameters :
name (string, required): image name. Currently, 3 image formats are supported: bmp, jpeg, and jpeg2000
method (string, optional, [“ssd”], default “ssd”): object detection method
threshold (number, optional, [0 - 100], default 50): minimum objects' probability in percents that will be included in the result
includeLabel (boolean, optional, default false): whether to include detected object labels in the response
includeScore (boolean, optional, default false): whether to include detected object probabilities in the response
folder (string, optional): folder
storage (string, optional): storage
You can detect objects an image using one of the following two APIs:
The first API expects you to first upload an image to Cloud Storage then pass its name in the API URL. After the object detection, the API returns the result as a json object in the response.
On the other hand, with the second API, you can directly pass the image in the request body. It also lets you save the loaded image on the Cloud Storage by specifying the outPath parameter value. The response contains the result as a json object.
Resource URI
Aspose.Imaging Cloud APIs Swagger UI lets you call REST APIs directly from the browser. The description of the API and its parameters are also given there.
cURL Example
Request
Copy
curl - v "https://api.aspose.cloud/connect/token" \
- X POST \
- d ' grant_type = client_credentials & client_id = xxxx & client_secret = xxxx ' \
- H "Content-Type: application/x-www-form-urlencoded" \
- H "Accept: application/json"
curl - v "https://api.aspose.cloud/v3/imaging/ai/object_detection_image.jpg/bounds?method=ssd&threshold=50&includeLabel=true&includeScore=true" \
- X GET \
- H "Content-Type: application/json" \
- H "Accept: multipart/form-data" \
- H "Authorization: Bearer <jwt token>" \
Response
Copy
{
"detectedObjects" : [
{
"score" : 0 . 599537253 ,
"label" : "dog" ,
"bounds" : {
"x" : 33 . 0 ,
"y" : - 1 . 0 ,
"width" : 510 . 0 ,
"height" : 352 . 0
}
},
{
"score" : 0 . 66614604 ,
"label" : "cat" ,
"bounds" : {
"x" : 37 . 0 ,
"y" : 3 . 0 ,
"width" : 493 . 0 ,
"height" : 344 . 0
}
}
]
}
Request
Copy
curl - v "https://api.aspose.cloud/connect/token" \
- X POST \
- d ' grant_type = client_credentials & client_id = xxxx & client_secret = xxxx ' \
- H "Content-Type: application/x-www-form-urlencoded" \
- H "Accept: application/json"
curl - v "https://api.aspose.cloud/v3/imaging/ai/bounds?method=ssd&threshold=50&includeLabel=true&includeScore=true" \
- X POST \
- T object_detection_image . jpg \
- H "Content-Type: application/json" \
- H "Accept: multipart/form-data" \
- H "Authorization: Bearer <jwt token>" \
Response
Copy
{
"detectedObjects" : [
{
"score" : 0 . 599537253 ,
"label" : "dog" ,
"bounds" : {
"x" : 33 . 0 ,
"y" : - 1 . 0 ,
"width" : 510 . 0 ,
"height" : 352 . 0
}
},
{
"score" : 0 . 66614604 ,
"label" : "cat" ,
"bounds" : {
"x" : 37 . 0 ,
"y" : 3 . 0 ,
"width" : 493 . 0 ,
"height" : 344 . 0
}
}
]
}
SDKs
Using an SDK (API client) is the quickest way for a developer to speed up the development. An SDK takes care of a lot of low-level details of making requests and handling responses and lets you focus on writing code specific to your particular project. Check out our GitHub repository for a complete list of Aspose.Imaging Cloud SDKs along with working examples, to get you started in no time. Please check Available SDKs article to learn how to add an SDK to your project.
SDK Examples
.NET
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
// For complete examples and data files, please go to aspose-imaging-cloud-dotnet/Examples/AsposeImagingCloudSdkExamples
public void BoundsAnImageInCloud()
{
// Input formats could be one of the following:
// bmp, jpeg, and jpeg2000
string sampleImageFileName = "object_detection_example.jpg";
// Upload local image to Cloud Storage
using (var localInputImage = File.OpenRead(Path.Combine(ExampleImagesFolder, sampleImageFileName)))
{
var uploadFileRequest = new UploadFileRequest(Path.Combine(CloudPath, imageName), image);
var result = ImagingApi.UploadFile(uploadFileRequest);
}
string method = "ssd";
int threshold = 50;
bool includeLabel = true;
bool includeScore = true;
string folder = CloudPath; // Input file is saved at the Examples folder in the storage
string storage = null; // We are using default Cloud Storage
var request = new GetObjectBoundsRequest(sampleImageFileName, method, threshold, includeLabel, includeScore, folder, storage);
var detectedObjectList = this.ImagingApi.GetObjectBounds(request);
}
Java
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
public void BoundsAnImageInCloud() throws Exception {
String fileName = "object_detection_example.jpg";
String method = "ssd";
int threshold = 50;
Boolean includeLabel = true;
Boolean includeScore = true;
String folder = CloudPath; // Input file is saved at the Examples folder in the storage
String storage = null; // We are using default Cloud Storage
GetObjectBoundsRequest request = new GetObjectBoundsRequest(getSampleImageFileName(), method, threshold, includeLabel, includeScore, folder, storage);
DetectedObjectList detectedObjectsList = ImagingApi.getObjectBounds(request);
}
.NET
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
// For complete examples and data files, please go to aspose-imaging-cloud-dotnet/Examples/AsposeImagingCloudSdkExamples
public void BoundsAnImageFromRequestBody()
{
// Input formats could be one of the following:
// bmp, jpeg, and jpeg2000
string sampleImageFileName = "object_detection_example.jpg";
using (FileStream inputImageStream = File.OpenRead(Path.Combine(ExampleImagesFolder, sampleImageFileName)))
{
string method = "ssd";
int threshold = 50;
bool includeLabel = true;
bool includeScore = true;
string outPath = null;
string storage = null; // We are using default Cloud Storage
var request = new CreateObjectBoundsRequest(inputImageStream, method, threshold, includeLabel, includeScore, outPath, storage);
DetectedObjectList detectedObjectList = this.ImagingApi.CreateObjectBounds(request);
}
}
Java
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
public void BoundsAnImageInRequestBody() throws Exception {
String fileName = "object_detection_example.jpg";
String method = "ssd";
int threshold = 50;
Boolean includeLabel = true;
Boolean includeScore = true;
String outPath = null;
String storage = null; // We are using default Cloud Storage
byte[] inputStream = Files.readAllBytes(Paths.get(ExampleImagesFolder, getSampleImageFileName()));
CreateObjectBoundsRequest request = new CreateObjectBoundsRequest(inputStream, method, threshold, includeLabel, includeScore, outPath, storage);
DetectedObjectList detectedObjectsList = ImagingApi.createObjectBounds(request);
}