-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcat_checker.py
More file actions
26 lines (22 loc) · 770 Bytes
/
cat_checker.py
File metadata and controls
26 lines (22 loc) · 770 Bytes
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
from clarifai.rest import ClarifaiApp, client, Image
import settings
def img_has_cat(filename):
app = ClarifaiApp(api_key=settings.CLARIFAI_API_KEY)
model = app.models.get("general-v1.3")
try:
image = Image(file_obj=open(filename, 'rb'))
result = model.predict([image])
try:
items = result['outputs'][0]['data']['concepts']
for item in items:
if item['name'] == 'cat':
return True
else:
return False
except (IndexError):
return False
except (client.ApiError, FileNotFoundError):
return False
if __name__ == "__main__":
print(img_has_cat("downloads/notcat.jpg"))
print(img_has_cat("downloads/cat.jpg"))