-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage_rec.py
More file actions
36 lines (29 loc) · 1.57 KB
/
image_rec.py
File metadata and controls
36 lines (29 loc) · 1.57 KB
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
from header import *
visual_recognition = VisualRecognitionV3(myVersion, iam_apikey=myIam_apikey)
myTweet = 'This is Beaux. He would like to box, but only if you go easy on him. Might hit you with a left hook or maybe a swift puppercut. 12/10 https://www.google.com https://pbs.twimg.com/media/D4ZMVZSUYAAH6nv.jpg https://images.pexels.com/photos/20787/pexels-photo.jpg?cs=srgb&dl=adorable-animal-cat-20787.jpg&fm=jpg'
dog_images = 0
cat_images = 0
# url_search = re.search("(?P<url>https?://[^\s]+)", myTweet) # Looks for image url in tweet
for url in re.findall("(?P<url>https?://[^\s]+)", myTweet):
# print(url)
# url = url.group("url") # grabs URL
print("Found URL, attempting to classify possible image...")
try:
classes_result = visual_recognition.classify(url=url).get_result() # classifies image
# Gets json data
classify_data = json.dumps(classes_result["images"][0]["classifiers"][0]["classes"], indent=2)
# Going through every dictionary in list of json date
for dict in json.loads(classify_data):
for key, value in dict.items():
if value == 'dog':
dog_images += 1
print("URL was a DOG image")
break # won't account for both dog or cat (whichever is bigger)
if value == 'cat':
cat_images += 1
print("URL was a CAT image")
break # won't account for both dog or cat (whichever is bigger)
except:
print("Not a valid image URL")
print(dog_images)
print(cat_images)