Classifying Birds and Forests with the anotherjesse/fastai-bird Cognitive Actions

In the world of image classification, the ability to distinguish between different categories can unlock numerous possibilities for applications in wildlife monitoring, environmental studies, and more. The anotherjesse/fastai-bird Cognitive Actions provide a straightforward way to classify images into "bird" or "forest" categories using a pre-trained resnet18 model from the fastai course. This blog post will guide developers through the process of integrating this powerful classification action into their applications.
Prerequisites
Before you can start using the Cognitive Actions, ensure you have the following:
- An API key for the Cognitive Actions platform.
- Familiarity with making HTTP requests and handling JSON data.
For authentication, you'll typically pass your API key in the headers of your requests, allowing you to securely access the Cognitive Actions.
Cognitive Actions Overview
Classify Image as Bird or Forest
The Classify Image as Bird or Forest action leverages a resnet18 model to classify images as either a "bird" or "forest." This classification is based on a model trained with the fastai course and demonstrated in a Kaggle notebook.
Input
The input for this action consists of a single required field:
- imageUrl (string): A URI pointing to the image that needs to be classified. The image should be accessible and correctly formatted to ensure accurate classification.
Example Input:
{
"imageUrl": "https://replicate.delivery/pbxt/JozWvEjU8HJMutd7vXzvdCfhMV9G9mrzNKEyF5bawHYiUld4/bird.jpeg"
}
Output
Upon successful execution, this action returns a string indicating the classification result along with the probability score. For instance:
Example Output:
you sent a bird with prob of 0.9999674558639526
This output tells you not only the classification (in this case, "bird") but also how confident the model is in that classification (approximately 99.997%).
Conceptual Usage Example (Python)
Below is a conceptual Python code snippet demonstrating how you might call the Cognitive Actions execution endpoint for this classification action:
import requests
import json
# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint
action_id = "e5dc67e6-3461-4b1b-8f60-a117d897126d" # Action ID for Classify Image as Bird or Forest
# Construct the input payload based on the action's requirements
payload = {
"imageUrl": "https://replicate.delivery/pbxt/JozWvEjU8HJMutd7vXzvdCfhMV9G9mrzNKEyF5bawHYiUld4/bird.jpeg"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json"
}
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json={"action_id": action_id, "inputs": payload} # Hypothetical structure
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body: {e.response.text}")
In this example, you need to replace the placeholder for COGNITIVE_ACTIONS_API_KEY with your actual API key and adjust the endpoint URL as necessary. The payload is structured according to the required input schema, and the response is processed to provide feedback on the classification.
Conclusion
The anotherjesse/fastai-bird Cognitive Actions offer a powerful and easy-to-integrate solution for image classification tasks. By classifying images as either "bird" or "forest," developers can build applications that contribute to conservation efforts, enhance user experiences, and much more. Now that you have a foundational understanding of how to use these actions, consider exploring additional use cases or integrating them into your existing projects for improved functionality. Happy coding!