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