This repository has been archived by the owner on Jul 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_selector.py
128 lines (114 loc) · 4.16 KB
/
object_selector.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from clarifai.client import ClarifaiApi
import pdb
from collections import Counter
from collections import defaultdict
import subprocess
import operator
def createHashmap(results):
for x in xrange(0,len(results)):
resultsData = {}
for y in range(0, len(results[x]['result']['tag']['probs'])):
resultsData[str(results[x]['result']['tag']['classes'][y])] = results[x]['result']['tag']['probs'][y]
result = sorted(resultsData.items(), key=operator.itemgetter(1))[::-1]
print result
print "\n"
relevantData.append(result)
#print relevantData
def main():
clarifai_api = ClarifaiApi()
#ADD ANY MORE IMAGES HERE, PROGRAM SHOULD ADJUST
image_array = [open('output/rgb_img_' + str(x) + ".jpg", 'rb') for x in xrange(1,13)]
results_json = clarifai_api.tag_images(image_array)
results = results_json['results']
createHashmap(results)
find_objects()
text_results_string = str(namesOfObjects)
say(text_results_string)
# reads out output
def say(string):
subprocess.call(['say', string])
#iterates trhough relevantData to get our data output
def find_objects():
del prominentObjects[:]
del namesOfObjects[:]
del names[:]
for x in xrange(0 , len(relevantData)-1):
highestInfo = find_highest(x)
prominentObjects.append(highestInfo)
isDone = checkForPopular()
while(isDone == False):
find_objects()
pass
#checks to make sure there's not too many repeats
def checkForPopular():
D = defaultdict(list)
for i,item in enumerate(names):
D[item].append(i)
D = {k:v for k,v in D.items() if len(v)>1}
if len(D)!=0 and len(D.values())>2:
print D
for x in xrange(0, len(D)):
indices = D.itervalues().next()
for index in indices:
del relevantData[index][0]
return False
else:
return True
#this is the function that actually collects all the data (iterated over in find object), by analyzing image overlap
def find_highest(x):
hourRatio = ((2.0*x + 1.0)/2.0)/len(relevantData)
timeInHours = put_time_in_hours(hourRatio)
highestInfo = []
effectiveprobs = [a[1] for a in relevantData[x]] + [a[1] for a in relevantData[x+1]]
effectivenames = [a[0] for a in relevantData[x]] + [a[0] for a in relevantData[x+1]]
adjustedprobs = adjust_probs(effectiveprobs)
duplicate_pairs = find_duplicates(effectivenames)
adjustedprobs = add_duplicate_probs(duplicate_pairs, adjustedprobs)
indexOfHighest = find_most_likely(adjustedprobs)
highestInfo.append(effectivenames[indexOfHighest])
highestInfo.append(effectiveprobs[indexOfHighest])
names.append(str(effectivenames[indexOfHighest]))
namesOfObjects.append(str(effectivenames[indexOfHighest]) + " at " + str(int(timeInHours)) + " o'clock.")
return highestInfo
#convert degree ratio to hours
def put_time_in_hours(hourRatio):
hours = round(hourRatio*6, 0)
adjustedhours = 0
if hours>3 :
adjustedhours = hours - 3
else :
adjustedhours = hours + 9
return adjustedhours
#finds the highest prob. object
def find_most_likely(adjustedprobs):
index = 0
for x in xrange(0, len(adjustedprobs)):
if adjustedprobs[index] < adjustedprobs[x]:
index = x
return index
#lowers the impact of repeat
def adjust_probs(effectiveprobs):
for x in xrange(0,len(effectiveprobs)):
effectiveprobs[x] -= .85
return effectiveprobs
#finds the duplicate key: value pairs once two image results are merged
def find_duplicates(effectiveids):
D = defaultdict(list)
for i,item in enumerate(effectiveids):
D[item].append(i)
D = {k:v for k,v in D.items() if len(v)>1}
return D
#adds together two duplicate key-value pairs
def add_duplicate_probs(duplicate_pairs, adjustedprobs):
for x in xrange(0,len(duplicate_pairs)):
duplicate_pair = duplicate_pairs.itervalues().next()
index0 = duplicate_pair[0]
index1 = duplicate_pair[1]
adjustedprobs[index0] += adjustedprobs[index1]
adjustedprobs[index1] = 0
return adjustedprobs
relevantData = []
prominentObjects = []
namesOfObjects = []
names = []
main()