-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_img_metadata.py
181 lines (162 loc) · 5.6 KB
/
gen_img_metadata.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# -*- coding: utf-8 -*-
import commands
import string
import indicoio
import os
import json
from face_recog_local import recognize
from age_gender_estimation.get_age_gender import get_age_gender
from get_wiki_info import get_year_of_birth
from googletrans import Translator
from subprocess import Popen, PIPE, STDOUT
indicoio.config.api_key = 'your api key'
#gets the number of objects
def findThings(thePath):
#finds the objects in the photo
theObj=indicoio.image_recognition(thePath, top_n=3)
return theObj.keys()
#gets the filling from the photo
def findEmotion(thePath):
#finds the result of the emotions
theResult = indicoio.fer(thePath)
#finds the biggest emotion
maxI = max(theResult, key=theResult.get) # Just use 'min' instead of 'max' for minimum.
return maxI
def get_textual_desc(meta_data):
inp = "Persons: "
ordered_pds = []
for pd in meta_data['persons']:
if pd[0] != 'N/A':
ordered_pds.append(pd)
for pd in meta_data['persons']:
if pd[0] == 'N/A':
pd[0] = "*anonymous*"
ordered_pds.append(pd)
print ordered_pds
for pd in ordered_pds:
inp += pd[0] + " "
inp += "- "
inp += "Emotions: "
for pd in ordered_pds:
inp += pd[3] + " "
inp += "- "
inp += "Objects: "
for obj in meta_data['objects']:
inp += obj + " "
inp += "- "
print inp
#p = Popen(['java', '-jar', './textGen.jar'], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
#stdout, stderr = p.communicate(input=inp)
#print stdout
#return str(stdout)
cmd = 'java -jar ./textGen.jar ' + inp
print cmd
res = commands.getstatusoutput(cmd)
print "res"
print res
return " ".join(res[1].split("\n"))
def gen_metadata(img_path):
"""
generates image metadata: persons in the images and their emotions, and the objects in the image.
:param img_path:
:return: a dict:{'persons':[['<name>','<emotion>'],...], 'objects':['<obj1>',...]}
"""
model_path = "knn_train_1.p"
names, paths = recognize(img_path, model_path)
persons_md = []
avg_pic_year = 0
pic_year_count = 0
for name, path in zip(names, paths):
print name, path
emotion = findEmotion(path)
age, gender = get_age_gender(path, name = name)
persons_md.append([name, int(age[0]), gender[0], emotion])
person_yob = get_year_of_birth(name)
if name != "N/A":
avg_pic_year += person_yob + age
pic_year_count += 1
objects = findThings(img_path)
meta_data = {'persons':persons_md, 'objects':objects, 'year':int(avg_pic_year/float(pic_year_count))}
textual_desc = get_textual_desc(meta_data)
meta_data['desc'] = textual_desc
file_name = img_path if "/" not in img_path else img_path[img_path.rfind("/")+1:]
meta_data['imgName'] = file_name
return meta_data
ENG_TO_HEB_DICT = {
'golda':u"גולדה",
'rabin':u"רבין",
'begin':u"בגין",
'gurion':u"בן גוריון",
'dayan':u"משה דיין",
'Angry':u"כועס",
'Happy':u"שמח",
'Sad':u"עצוב",
'Neutral':u"ניטרלי",
'Surprise':u"מופתע",
'female':u"נקבה",
'male':u"זכר",
}
def translate_word_eng_to_heb(word):
if word in ENG_TO_HEB_DICT.keys():
return ENG_TO_HEB_DICT[word]
else:
return u"שגיאת תרגום"
def translate_sentence_eng_to_heb(sen):
translator = Translator(service_urls=[
'translate.google.com',
'translate.google.co.kr',
])
d = translator.translate(sen,src='en',dest='iw').text
# printable = set(string.printable)
# filter(lambda x: x not in printable, d)
d = ''.join([i if not(ord(i) < 128) else ' ' for i in d])
d = d.strip()
return d
def translate_mds_to_heb(mds):
for i in xrange(len(mds)):
md = mds[i]
for j in range(len(md['persons'])):
pd = md['persons'][j]
pd[0] = translate_word_eng_to_heb(pd[0])
pd[2] = translate_word_eng_to_heb(pd[2])
pd[3] = translate_word_eng_to_heb(pd[3])
md['persons'][j] = pd
md['desc'] = translate_sentence_eng_to_heb(md['desc'])
for j in range(len(md['objects'])):
od = md['objects'][j]
md['objects'][j] = translate_sentence_eng_to_heb(od)
return mds
def gen_json_metadata_for_dir(dir_path, json_dst_path):
mds = []
for img_path in os.listdir(dir_path):
mds.append(gen_metadata(img_path))
mds = translate_mds_to_heb(mds)
with open(json_dst_path,'w') as f:
f.write(json.dumps(mds))
if __name__ == "__main__":
#print translate_sentence_eng_to_heb("hello gadarugzaq man")
#exit()
gen_json_metadata_for_dir('tst_md_dir1_1','tst_md_dir1_4.json')
exit()
# inp = "Persons: biden *anonymous* - Emotions: Happy Happy - Objects: bow tie, bow-tie, bowtie Windsor tie groom, bridegroom suit, suit of clothes abaya - "
# cmd = 'java -jar ./textGen.jar ' + inp
# print cmd
# res = commands.getstatusoutput(cmd)
# #os.system(cmd + " > textGen.txt")
# #print open('textGen.txt','w').read()
# p = Popen(['java', '-jar', './textGen.jar', inp], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
# stdout, stderr = p.communicate(input=inp)
# for line in stdout:
# print line
# print stdout
# exit()
#md = gen_metadata("/home/itamar/PycharmProjects/facedetection/obama_and_biden.jpg")
md = gen_metadata("golda_test1.jpg")
print md
print "*****"
print "People in image:"
for pd in md['persons']:
print "person md:" , pd
print "Things in image:"
for od in md['objects']:
print od