Check if a picture contains a dog with Python
Monday, July 28, 2025This is a funny Python function to check if a picture contains a dog :D
Important: You will require to have a Google Cloud Vision API key.
import requests
import base64
def is_a_dog():
try:
image_path = "./dog.jpg"
url = f"https://vision.googleapis.com/v1/images:annotate?key=YOUR_GOOGLE_CLOUD_VISION_API_KEY"
with open(image_path, "rb") as image_file:
image_content = image_file.read()
payload = {
"requests": [
{
"image": {"content": base64.b64encode(image_content).decode()},
"features": [{"type": "LABEL_DETECTION", "maxResults": 10}],
}
]
}
response = requests.post(url, json=payload)
labels = response.json()["responses"][0]["labelAnnotations"]
return any("dog" in label["description"].lower() for label in labels)
except Exception as e:
print(e)
if __name__ == "__main__":
is_dog = is_a_dog()
if is_dog:
print("IS A DOG!")
else:
print("NOT A DOG!")