forked from rossgoodwin/photosynthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
683 lines (562 loc) · 20.7 KB
/
__init__.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# word.camera image-to-text translator
# Copyright (C) 2015 Ross Goodwin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# You can contact Ross Goodwin at ross.goodwin@gmail.com or address
# physical correspondence to:
#
# Ross Goodwin c/o ITP
# 721 Broadway
# 4th Floor
# New York, NY 10003
from py.client import ClarifaiApi
import random
from random import choice as rc
from random import sample as rs
from random import randint as ri
#import en
import requests
import collections
from collections import defaultdict
import string
from string import Template
from conceptnet5.language.english import normalize
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, abort
from werkzeug import secure_filename
import os
import os.path
import subprocess
import PIL
from PIL import Image
import uuid
from base64 import decodestring
from flask.ext.mobility import Mobility
from flask.ext.mobility.decorators import mobilized
from pattern.en import referenced as a_or_an
from pattern.en import parsetree, UNIVERSAL, conjugate
import time
import hashids
from salty import saline
import exifread
import urlparse
from bs4 import BeautifulSoup
import stripe
from stripey import stripe_key
import lob
from lobby import lob_key
# from signal import signal, SIGPIPE, SIG_DFL
app = Flask(__name__)
Mobility(app)
app.config['UPLOAD_FOLDER'] = '/var/www/PhotoSyn/PhotoSyn/static/img'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
SYSPATH = '/var/www/PhotoSyn/PhotoSyn/static/img/'
AUDPATH = '/var/www/PhotoSyn/PhotoSyn/static/aud/'
APPPATH = '/var/www/PhotoSyn/PhotoSyn/'
BASEURL = 'http://word.camera'
MAX_GRAF_DENSITY = 6
lob.api_key = lob_key
stripe.api_key = stripe_key
api = ClarifaiApi() # Assumes environmental variables have been set
# signal(SIGPIPE,SIG_DFL) # Hope this works
@app.route("/")
def index():
if request.MOBILE:
return render_template("mobile.html")
else:
return render_template("index.html")
@app.errorhandler(404)
def page_not_found(e):
return render_template('fourohfour.html'), 404
@app.errorhandler(500)
def server_error(e):
return render_template('fivehundred.html'), 500
@app.route("/sitemap")
def sitemap():
return send_from_directory(APPPATH+'static/assets', 'sitemap.xml')
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/albums")
def albums():
return render_template("albums.html")
@app.route("/postcards")
def postcards():
return render_template("postcards.html")
@app.route("/thanks", methods=["POST"])
def thanks():
if request.method == "POST":
# Set your secret key: remember to change this to your live secret key in production
# See your keys here https://dashboard.stripe.com/account/apikeys
# Get the credit card details submitted by the form
token = request.form['stripeToken']
email = request.form['stripeEmail']
# Create the charge on Stripe's servers - this will charge the user's card
try:
customer = stripe.Customer.create(
source=token,
description="Postcard Customer"
)
charge = stripe.Charge.create(
amount=500, # amount in cents, again
currency="usd",
customer=customer.id,
receipt_email=email,
description="Lexograph Postcard"
)
except stripe.CardError, e:
# The card has been declined
return render_template("declined.html")
else:
lexText, imgUrl = fetchTextImg(request.form['lex-url'])
front_template = render_template("postcard_front.html", lexText=lexText, imgUrl=imgUrl)
back_template = render_template("postcard_back.html", msg=request.form['message'], imgUrl=imgUrl)
send_to = {
'name': request.form['name-recip'],
'address_line1': request.form['address1-recip'],
'address_line2': request.form['address2-recip'],
'address_city': request.form['city-recip'],
'address_state': request.form['state-recip'],
'address_zip': request.form['zip-recip'],
'address_country': request.form['country-recip']
}
send_from = {
'name': request.form['name-sender'],
'address_line1': request.form['address1-sender'],
'address_line2': request.form['address2-sender'],
'address_city': request.form['city-sender'],
'address_state': request.form['state-sender'],
'address_zip': request.form['zip-sender'],
'address_country': request.form['country-sender']
}
lob.Postcard.create(
to_address = send_to,
from_address = send_from,
full_bleed = 1,
template = 1,
front = front_template,
back = back_template
)
return render_template("thanks.html", email=email)
@app.route("/abm")
def abm():
params = urlparse.parse_qs(request.query_string)
if 'l' in params:
hashes = [url.split('/')[-1] for url in params['l']]
else:
abort(404)
if 't' in params:
title = params['t'][0]
else:
title = "Untitled Lexograph Album"
if 'a' in params:
author = params['a'][0]
else:
author = "anonymous"
abmInput = [(h, chTitle(h)) for h in hashes]
tocHtml = render_template("toc.html", albumInput=abmInput, title=title, author=author)
slug = url_hash()
tocFilePath = APPPATH+'static/tocs/'+slug+'.html'
with open(tocFilePath, 'w') as outfile:
outfile.write(tocHtml)
proc = subprocess.Popen([
"ebook-convert",
tocFilePath,
APPPATH+"static/epub/"+slug+".epub",
"--sr1-search=/static/img/",
"--sr1-replace="+APPPATH+"/static/img/",
"--sr2-search=#F2F1EF",
"--sr2-replace=#FFFFFF",
"--sr3-search=Tweet",
"--sr3-replace=",
"--cover="+APPPATH+"/static/assets/album_cover.jpg",
"--title="+title,
"--authors="+author,
"--publisher=word.camera"
])
proc.communicate()
return redirect(BASEURL+"/a/"+slug)
@app.route("/a/<slug>")
def album(slug):
if os.path.isfile(APPPATH+"static/epub/"+slug+".epub"):
tocFile = open(APPPATH+"static/tocs/"+slug+".html")
tocText = tocFile.read()
tocFile.close()
soup = BeautifulSoup(tocText)
try:
title = soup.find(id="title").string
byline = soup.find(id="byline").string
except:
title = "Untitled Lexograph Album"
byline = "by anonymous"
return render_template("a.html", s=slug, t=title, b=byline)
else:
abort(404)
@app.route("/i/<slug>")
def userpage(slug):
return send_from_directory(APPPATH+'static/output', slug+'.html')
@app.route("/img", methods=["GET", "POST"])
def img():
if request.method == 'POST':
newFilename = ""
try:
dtopUpload = request.form['IfYouScriptThisForm'] == 'GnomesWillEatYourLungs'
except:
dtopUpload = False
if request.MOBILE or dtopUpload:
f = request.files['file']
if f and allowed_file(f.filename):
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Resize image and generate unique filename
newFilename = resizeImage(filename)
else:
f = False
fstring = request.form['base64img']
newFilename = str(uuid.uuid4())+'.jpeg'
fh = open(SYSPATH+newFilename, "wb")
fh.write(fstring.decode('base64'))
fh.close()
if newFilename:
# Generate Text
photoText = main([newFilename])
# with open(APPPATH+"output/"+newFilename+".txt", "w") as outfile:
# outfile.write(photoText)
photoText = photoText.replace("\n", "<br />")
# oggName = filename.split(".")[0]+".ogg"
# mp3Name = filename.split(".")[0]+".mp3"
# proc = subprocess.Popen(["say", "-o", AUDPATH+oggName], stdin=subprocess.PIPE)
# proc.communicate(photoText)
# proc2 = subprocess.Popen(["ffmpeg", "-i", AUDPATH+oggName, "-acodec", "libmp3lame", AUDPATH+mp3Name])
# proc2.communicate()
imgPath = "/static/img/"+newFilename
# audioPath = "/static/aud/"+mp3Name
# return render_template("result.html", imgUrl=imgPath, imgText=photoText)
# NEW PART
html = render_template("result.html", imgUrl=imgPath, imgText=photoText)
slug = url_hash()
with open(APPPATH+"static/output/"+slug+".html", "w") as outfile:
outfile.write(html)
return redirect(BASEURL+"/i/"+slug)
else:
abort(500)
else:
abort(404)
def fetchTextImg(url):
htmlFile = open(APPPATH+'static/output/'+url.split('/')[-1]+'.html', 'r')
html = htmlFile.read()
htmlFile.close()
soup = BeautifulSoup(html)
text = max([i for i in soup.p.contents], key=len).replace("<br/>", "\n")
imgUrl = "https://word.camera" + soup.find_all('img')[0].get('src')
return text, imgUrl
def chTitle(hi):
htmlFile = open(APPPATH+'static/output/'+hi+'.html', 'r')
html = htmlFile.read()
htmlFile.close()
soup = BeautifulSoup(html)
text = "\n".join([unicode(i) for i in soup.p.contents]).replace("<br/>", "\n")
s = parsetree(text)
nounPhrases = []
for sentence in s:
for chunk in sentence.chunks:
if chunk.type == "NP":
nounPhrases.append(chunk.string)
selectNPs = rs([np for np in nounPhrases if not "&" in np], ri(1,2))
articles = ["a", "an", "the"]
nps = []
for np in selectNPs:
if startsWithCheck(np, articles):
nps.append(np)
else:
nps.append(a_or_an(np))
if len(selectNPs) == 1:
title = titlecase(nps[0])
elif len(selectNPs) == 2:
title = titlecase(" and ".join(nps))
# elif len(selectNPs) == 3:
# title = titlecase("%s, %s, and %s" % tuple(nps))
return title.encode('ascii', 'xmlcharrefreplace')
def titlecase(s):
words = s.split()
capwords = [string.capitalize(w) for w in words]
return " ".join(capwords)
def url_hash():
millis = int(round(time.time() * 1000))
hi = hashids.Hashids(salt=salty.saline)
return hi.encode(millis)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def resizeImage(fn):
longedge = 1000
orientDict = {
1: (0, 1),
2: (0, PIL.Image.FLIP_LEFT_RIGHT),
3: (-180, 1),
4: (0, PIL.Image.FLIP_TOP_BOTTOM),
5: (-90, PIL.Image.FLIP_LEFT_RIGHT),
6: (-90, 1),
7: (90, PIL.Image.FLIP_LEFT_RIGHT),
8: (90, 1)
}
imgOriList = []
try:
f = open(SYSPATH+fn, "rb")
exifTags = exifread.process_file(f, details=False, stop_tag='Image Orientation')
if 'Image Orientation' in exifTags:
imgOriList.extend(exifTags['Image Orientation'].values)
except:
pass
img = Image.open(SYSPATH+fn)
w, h = img.size
newName = str(uuid.uuid4())+'.jpeg'
if w >= h:
wpercent = (longedge/float(w))
hsize = int((float(h)*float(wpercent)))
img = img.resize((longedge,hsize), PIL.Image.ANTIALIAS)
else:
hpercent = (longedge/float(h))
wsize = int((float(w)*float(hpercent)))
img = img.resize((wsize,longedge), PIL.Image.ANTIALIAS)
for val in imgOriList:
if val in orientDict:
deg, flip = orientDict[val]
img = img.rotate(deg)
if flip != 1:
img = img.transpose(flip)
img.save(SYSPATH+newName, format='JPEG')
os.remove(SYSPATH+fn)
return newName
def extractTags(filenames):
imgFiles = [open(SYSPATH+fn) for fn in filenames]
data = api.tag_images(imgFiles)
for f in imgFiles:
f.close()
tags_probs = []
for r in data['results']:
try:
obj = r['result']['tag']
tags_probs.extend(zip(obj['classes'], obj['probs']))
except KeyError:
pass
return sorted(tags_probs, key=lambda x: x[1])
# def a_or_an(word):
# #return en.noun.article(word)
# if word[0].lower() in ['a', 'e', 'i', 'o', 'u']:
# return "an" + " " + word
# else:
# return "a" + " " + word
def uniqify(seq, idfun=None):
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
def chunks(l, n):
for i in xrange(0, len(l), n):
yield l[i:i+n]
def conceptNet(start):
endpt = "http://127.0.0.1:8084/data/5.3%s" % start
payload = {
"start": start,
"limit": 999
}
r = requests.get(endpt, params=payload)
try:
return r.json()
except ValueError:
return {}
def startsWithCheck(toCheck, wordList):
return any(toCheck.lower().startswith(word+" ") for word in wordList)
def verbConjugate(lemma, rel, aan):
relAvoid = ["/r/CapableOf", "/r/PartOf", "/r/MemberOf"
"/r/IsA", "/r/HasA", "/r/TranslationOf",
"/r/HasProperty"]
if not rel in relAvoid:
s = parsetree(lemma, relations=True)
try:
vb = s[0].verbs[0].words[0].string
result = lemma.replace(vb, conjugate(vb, "part"))
except:
result = lemma
else:
if vb in ["to", "can"]:
result = lemma
# if not aan:
# try:
# firstWord = s[0].chunks[0].words[0].string
# reconjugated = conjugate(firstWord, "part")
# result = lemma.replace(firstWord, reconjugated)
# except:
# result = lemma
else:
result = lemma
return result
def explodeTag(tag):
relDict = {
"/r/RelatedTo": ["evokes", False],
"/r/IsA": ["is", True],
"/r/PartOf": ["appertains to", True],
"/r/MemberOf": ["belongs to", True],
"/r/HasA": ["has", True],
"/r/UsedFor": ["is for", False],
"/r/CapableOf": ["may", False],
#"/r/AtLocation": [False, False],
"/r/Causes": ["causes", True],
"/r/HasSubevent": ["manifests", False],
"/r/HasFirstSubevent": ["began with", True],
"/r/HasLastSubevent": ["ends with", True],
"/r/HasPrerequisite": ["requires", True],
"/r/HasProperty": ["is", False],
"/r/MotivatedByGoal": ["dreams of", False],
"/r/ObstructedBy": ["struggles with", True],
"/r/Desires": ["yearns for", False],
"/r/CreatedBy": ["resulted from", True],
"/r/Synonym": ["is also known as", True],
"/r/Antonym": ["is not", True],
"/r/DerivedFrom": ["is made from", True],
"/r/TranslationOf": ["is known to some as", False],
"/r/DefinedAs": ["remains", True]
}
articleList = ["a", "an", "the"]
candidates = defaultdict(list)
normalizedTag = normalize(tag)
start = "/c/en/"+normalizedTag.replace(" ", "_")
try:
edges = conceptNet(start)["edges"]
except KeyError:
edges = []
for edge in edges:
endLemma = edge["end"].split("/")[-1].replace("_", " ")
rel = edge["rel"]
try:
verb, aan = relDict[rel]
except KeyError:
verb = False
else:
unmodified = endLemma
endLemma = verbConjugate(unmodified, rel, aan)
if verb and len(endLemma) > 1 and not unmodified in [tag, normalizedTag]:
if aan and not startsWithCheck(endLemma, articleList):
candidates[verb].append(a_or_an(endLemma))
else:
candidates[verb].append(endLemma)
return candidates
def open_template(x):
f = open(APPPATH+"tem/template_%s.txt" % str(x), 'r') # THERE IS A REASON FOR NOT USING %i
text = f.read()
templ = Template(text)
f.close()
return templ
def replacementDict(tc, dc, cc, tr):
MAX_PREDICATES = 3
repl = {}
for i in range(len(tc)):
repl["tag_%i"%i] = tc[i]
repl["tag_%i_aan"%i] = a_or_an(tc[i])
if dc[i]:
if len(dc[i].keys()) < MAX_PREDICATES:
relVerbs = dc[i].keys()
lemmas = [rc(dc[i][relVerb]) for relVerb in relVerbs]
for j in range(MAX_PREDICATES - len(relVerbs)):
useAgain = rc(dc[i].keys())
relVerbs.append(useAgain)
lemmas.append(rc(dc[i][useAgain]))
else:
relVerbs = rs(dc[i].keys(), MAX_PREDICATES)
lemmas = [rc(dc[i][relVerb]) for relVerb in relVerbs]
zipped = zip(relVerbs, lemmas)
for j in range(MAX_PREDICATES):
repl["tag_%i_predicate_%i"%(i,j)] = " ".join(zipped[j])
dump = []
for rv in dc[i].keys():
for lemma in dc[i][rv]:
dump.append("%s it %s %s." % (rc(tr), rv, lemma))
random.shuffle(dump)
repl["tag_%i_dump"%i] = dump
else:
for j in range(MAX_PREDICATES):
repl["tag_%i_predicate_%i"%(i,j)] = "remains unknown"
repl["tag_%i_dump"%i] = []
return repl
def grafBuilder(exploDictsConf):
MAX_TRANSITIONS = 7
tags, exploDicts, confs = zip(*exploDictsConf)
confMean = float(sum(confs))/len(confs)
grafs = []
templates = [open_template(n) for n in range(1,7)]
transFile = open(APPPATH+'lists/transitions.txt', 'r')
transitions = [l.strip() for l in transFile.readlines()]
transFile.close()
i = 0
for tagsChunk in chunks(tags, MAX_GRAF_DENSITY):
dictsChunk = exploDicts[i:i+len(tagsChunk)]
confsChunk = confs[i:i+len(tagsChunk)]
replDict = replacementDict(tagsChunk, dictsChunk, confsChunk, transitions)
transes = rs(transitions, MAX_TRANSITIONS)
for j in range(MAX_TRANSITIONS):
replDict["transition_%i"%j] = transes[j]
for j in range(len(tagsChunk)):
for k in range(2,5,2):
if len(replDict["tag_%i_dump"%j]) > k:
replDict["tag_%i_dump%i"%(j,k)] = " ".join(rs(replDict["tag_%i_dump"%j], k+1))
else:
replDict["tag_%i_dump%i"%(j,k)] = ""
templNo = len(tagsChunk)
grafs.append(templates[templNo-1].substitute(**replDict))
i+=len(tagsChunk)
return grafs
def main(filenames):
raw = extractTags(filenames)
concepts = uniqify(raw, idfun=lambda x: x[0]) # ascending order of confidence
exploDicts = [(tag, explodeTag(tag), conf) for tag, conf in concepts] # LIST OF DICTS
grafs = "\n\n".join(grafBuilder(exploDicts)).encode('ascii', 'xmlcharrefreplace')
return grafs
if __name__ == '__main__':
app.run()
# for i in range(6):
# print main([i])+"\n"
# def sceneDesc(numList):
# # Multiple images will result in one scene.
# # If multiple scenes desired, separate images into groups
# # for each scene & run through sceneDesc independently
# raw = extractTags(numList)
# concepts = uniqify(raw, idfun=lambda x: x[0]) # ascending order of confidence
# paragraphs = []
# conf = [y for x, y in concepts]
# if conf:
# confMean = float(sum(conf))/len(conf)
# i = 0
# for tag in concepts[::-1]:
# if conf[i] > confMean:
# itIs = "It is %s." % a_or_an(tag)
# else:
# itIs = "Is it %s?" % a_or_an(tag)
# paragraphs.append(itIs + " " + explodeTag(tag))
# i += 1
# return paragraphs
# else:
# return ""
# for i in range(1,34):
# printable = u"\n\n\n".join(sceneDesc([i])).encode('utf-8')
# outfile = open("%i-final.txt" % i, 'w')
# outfile.write(printable)
# outfile.close()
# print printable