forked from akelleh/clarifai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_main.py
executable file
·81 lines (58 loc) · 1.89 KB
/
sample_main.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
"""
sample python script for using Clarifai Python Client API
usage: sample_main.py [-h] [-t] [-c]
optional arguments:
-h, --help show this help message and exit
-t, --tag tag images
-c, --color color images
Examples:
Run tag api on an image url or image on disk
python sample_main.py <url|filename>
python sample_main.py -t|--tag <url|filename>
Run color api on an image url or image on disk
python sample_main.py -c|--color <url|filename>
"""
import os
import sys
import json
import glob
import argparse
from clarifai.client import ClarifaiApi
def tag_images(api, imageurl):
if imageurl.startswith('http'):
response = api.tag_image_urls(imageurl)
elif os.path.isfile(imageurl):
with open(imageurl,'rb') as image_file:
response = api.tag_images(image_file)
return response
def color_images(api, imageurl):
if imageurl.startswith('http'):
response = api.color_urls(imageurl)
elif os.path.isfile(imageurl):
with open(imageurl,'rb') as image_file:
response = api.color(image_file)
return response
def main(argv):
# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--tag", help="tag images", action='store_true')
parser.add_argument("-c", "--color", help="color images", action='store_true')
parser.add_argument("-u", "--usage", help="usage", action='store_true')
args, argv = parser.parse_known_args()
if len(argv) == 1:
imageurl = argv[0]
else:
imageurl = 'http://clarifai-img.s3.amazonaws.com/test/toddler-flowers.jpeg'
api = ClarifaiApi()
if args.usage:
response = api.get_usage()
elif not args.color:
response = tag_images(api, imageurl)
elif args.color and not args.tag:
response = color_images(api, imageurl)
else:
raise Exception('call with --tag or --color for the image')
print(json.dumps(response))
if __name__ == '__main__':
main(sys.argv)