This how-to-guide shows you how to easily detect objects in an image with Clarifai's General Detector. The General Detector is second only to our General model in popularity and can be used in an extremely wide range of use cases.
For example, the General detector might identify the following objects in the image above:
And then will offer predictions for each detected object. For example, this region:
Might receive the following predictions:
Concept | Prediction |
no person | 0.99 |
laptop |
0.99 |
computer
|
0.99 |
internet
|
0.98 |
technology
|
0.97 |
Getting Started
First things first. You will need to set up a Clarifai account and create an application. This how-to article shows you how to analyze color with Clarifai API and through Portal. If you would like to analyze color via API, you will also need to generate an API key.
Detecting objects with Clarifai Portal
You can do almost anything that Clarifai can do with Clarifai Portal, and we work hard to make Portal the world's easiest interface for using AI. Classifying images with Portal is as simple as uploading your data, and choosing the right base workflow.
Create your application and choose your base workflow
Simply log in to Clarifai Portal and create a new application. Select "General Detection" as your base workflow.
View your image in Explorer view
Upload an image to your application and view predictions in the righthand sidebar under the tab that says "App Workflow". Here, you will be able to view each detected object as well as predictions made for each region of the image.
Detect objects in a local image with the API
Use the following Python snippet as an example of how to detect objects an image hosted on your local computer. For more details and information on working with predictions in our other client languages, please refer to our API documentation.
from clarifai_grpc.grpc.api import service_pb2, resources_pb2 from clarifai_grpc.grpc.api.status import status_code_pb2 # This is how you authenticate. metadata = (('authorization', 'Key {{YOUR_CLARIFAI_API_KEY}}'),) with open("{YOUR_IMAGE_FILE_LOCATION}", "rb") as f: file_bytes = f.read() request = service_pb2.PostModelOutputsRequest( model_id='cb649131aa72f86911815b0fe98dee55', inputs=[ resources_pb2.Input( data=resources_pb2.Data( image=resources_pb2.Image( base64=file_bytes ) ) ) ]) response = stub.PostModelOutputs(request, metadata=metadata) if response.status.code != status_code_pb2.SUCCESS: raise Exception("Request failed, status code: " + str(response.status.code)) for concept in response.outputs[0].data.concepts: print('%12s: %.2f' % (concept.name, concept.value))
Detect objects in a remote image with the API
Here is an example of how to detect objects an image that is hosted on a URL. This snippet is in Python, but we offer support for many other client languages. Please refer to our API documentation for additional information.
from clarifai_grpc.grpc.api import service_pb2, resources_pb2 from clarifai_grpc.grpc.api.status import status_code_pb2 # This is how you authenticate. metadata = (('authorization', 'Key {{YOUR_CLARIFAI_API_KEY}}'),) request = service_pb2.PostModelOutputsRequest( model_id='cb649131aa72f86911815b0fe98dee55', inputs=[ resources_pb2.Input(data=resources_pb2.Data(image=resources_pb2.Image(url='{{YOUR_IMAGE_URL}}'))) ]) response = stub.PostModelOutputs(request, metadata=metadata) if response.status.code != status_code_pb2.SUCCESS: raise Exception("Request failed, status code: " + str(response.status.code)) for concept in response.outputs[0].data.concepts: print('%12s: %.2f' % (concept.name, concept.value))