File size: 1,481 Bytes
178a652 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import requests
import os
# Function to download images from the internet
def download_images(urls, folder):
os.makedirs(folder, exist_ok=True)
for i, url in enumerate(urls):
response = requests.get(url)
with open(f"{folder}/image_{i}.jpg", "wb") as f:
f.write(response.content)
# Function to identify bears in images using Clarifai API
def identify_bears(images_folder):
# Replace 'YOUR_API_KEY' with your actual Clarifai API key
API_KEY = 'YOUR_API_KEY'
url = "https://api.clarifai.com/v2/models/c0c0ac362b03416da06ab3fa36fb58e3/outputs"
headers = {
"Authorization": f"Key {API_KEY}",
"Content-Type": "application/json",
}
image_files = os.listdir(images_folder)
for image_file in image_files:
with open(f"{images_folder}/{image_file}", "rb") as f:
response = requests.post(url, headers=headers, json={"inputs": [{"data": {"image": {"base64": f.read().hex()}}}]})
data = response.json()
concepts = data["outputs"][0]["data"]["concepts"]
for concept in concepts:
if concept["name"] in ["grizzly bear", "black bear"]:
print(f"Image {image_file}: {concept['name']} - Probability: {concept['value']}")
# Example usage
if __name__ == "__main__":
urls = [
"URL_TO_BEAR_IMAGE_1",
"URL_TO_BEAR_IMAGE_2",
# Add more URLs as needed
]
download_images(urls, "images")
identify_bears("images")
|