@@ -12,111 +12,111 @@
import time

def find_image(query):
"""Download full size images from Google image search.
Don't print or republish images without permission.
I used this to train a learning algorithm.
"""
path = 'tmp/scenes'
BASE_URL = 'https://ajax.googleapis.com/ajax/services/search/images?'\
'v=1.0&q=' + query + '+star+trek&start=%d&userip=69.91.178.167'
"""Download full size images from Google image search.
Don't print or republish images without permission.
I used this to train a learning algorithm.
"""
path = 'tmp/scenes'
BASE_URL = 'https://ajax.googleapis.com/ajax/services/search/images?'\
'v=1.0&q=' + query + '+star+trek&start=%d&userip=69.91.178.167'

BASE_PATH = path

BASE_PATH = path
if os.path.exists(BASE_PATH + '/' + query + '.jpg'):
print "Reusing cached image..."
return cv2.imread(BASE_PATH + '/' + query + '.jpg')
if not os.path.exists(BASE_PATH):
os.makedirs(BASE_PATH)

if os.path.exists(BASE_PATH + '/' + query + '.jpg'):
print "Reusing cached image..."
return cv2.imread(BASE_PATH + '/' + query + '.jpg')
if not os.path.exists(BASE_PATH):
os.makedirs(BASE_PATH)
start = 0 # Google's start query string parameter for pagination.
while True:
r = requests.get(BASE_URL % start)
time.sleep(1)
for image_info in json.loads(r.text)['responseData']['results']:
url = image_info['unescapedUrl']
try:
image_r = requests.get(url)
except ConnectionError, e:
print 'could not download %s' % url
continue

start = 0 # Google's start query string parameter for pagination.
while True:
r = requests.get(BASE_URL % start)
time.sleep(1)
for image_info in json.loads(r.text)['responseData']['results']:
url = image_info['unescapedUrl']
try:
image_r = requests.get(url)
except ConnectionError, e:
print 'could not download %s' % url
continue
# Remove file-system path characters from name.
title = image_info['titleNoFormatting'].replace('/', '').replace('\\', '')

# Remove file-system path characters from name.
title = image_info['titleNoFormatting'].replace('/', '').replace('\\', '')
file = open(os.path.join(BASE_PATH, '%s.jpg') % query, 'w')
try:
arr = np.asarray(bytearray(image_r.content), dtype=np.uint8)
img = cv2.imdecode(arr,-1) # 'load it as it is'
# save a copy of the image
Image.open(StringIO(image_r.content)).save(file)
return img
except IOError, e:
# Throw away some gifs...blegh.
print 'could not save %s' % url
continue
finally:
file.close()
start += 4

file = open(os.path.join(BASE_PATH, '%s.jpg') % query, 'w')
try:
arr = np.asarray(bytearray(image_r.content), dtype=np.uint8)
img = cv2.imdecode(arr,-1) # 'load it as it is'
# save a copy of the image
Image.open(StringIO(image_r.content)).save(file)
return img
except IOError, e:
# Throw away some gifs...blegh.
print 'could not save %s' % url
continue
finally:
file.close()
start += 4


character_lookup_keywords = ['', 'character', 'face', 'profile', 'head']
def find_character(query):
"""Download full size images from Google image search.
Don't print or republish images without permission.
I used this to train a learning algorithm.
"""
path = 'tmp/characters'
BASE_PATH = path
keywords_i = 0
"""Download full size images from Google image search.
Don't print or republish images without permission.
I used this to train a learning algorithm.
"""
path = 'tmp/characters'
BASE_PATH = path
keywords_i = 0

if os.path.exists(BASE_PATH + '/' + query + '.jpg'):
print "Reusing cached image..."
img = cv2.imread(BASE_PATH + '/' + query + '.jpg')
results = False
while not results:
results = fd.detect_face(img)
return (results, img)
if not os.path.exists(BASE_PATH):
os.makedirs(BASE_PATH)
if os.path.exists(BASE_PATH + '/' + query + '.jpg'):
print "Reusing cached image..."
img = cv2.imread(BASE_PATH + '/' + query + '.jpg')
results = False
while not results:
results = fd.detect_face(img)
return (results, img)
if not os.path.exists(BASE_PATH):
os.makedirs(BASE_PATH)

start = 0 # Google's start query string parameter for pagination.
while True:
if start > 8:
keywords_i += 1
start = 0
print "Searching for " + query + " " + character_lookup_keywords[keywords_i] + " " + str(start)
BASE_URL = 'https://ajax.googleapis.com/ajax/services/search/images?'\
'v=1.0&q=' + query + '+' + character_lookup_keywords[keywords_i] + '&start=%d'
r = requests.get(BASE_URL % start)
for image_info in json.loads(r.text)['responseData']['results']:
url = image_info['unescapedUrl']
try:
image_r = requests.get(url)
except ConnectionError, e:
print 'could not download %s' % url
continue
start = 0 # Google's start query string parameter for pagination.
while True:
if start > 8:
keywords_i += 1
start = 0
print "Searching for " + query + " " + character_lookup_keywords[keywords_i] + " " + str(start)
BASE_URL = 'https://ajax.googleapis.com/ajax/services/search/images?'\
'v=1.0&q=' + query + '+' + character_lookup_keywords[keywords_i] + '&start=%d'
r = requests.get(BASE_URL % start)
for image_info in json.loads(r.text)['responseData']['results']:
url = image_info['unescapedUrl']
try:
image_r = requests.get(url)
except ConnectionError, e:
print 'could not download %s' % url
continue

# Remove file-system path characters from name.
title = image_info['titleNoFormatting'].replace('/', '').replace('\\', '')
# Remove file-system path characters from name.
title = image_info['titleNoFormatting'].replace('/', '').replace('\\', '')

file = open(os.path.join(BASE_PATH, '%s.jpg') % query, 'w')
try:
arr = np.asarray(bytearray(image_r.content), dtype=np.uint8)
img = cv2.imdecode(arr,-1) # 'load it as it is'
# save a copy of the image
results = fd.detect_face(img)
print(results)
if not results:
continue
Image.open(StringIO(image_r.content)).save(file)
return (results, img)
except:
# Throw away some gifs...blegh.
print 'could not save %s' % url
continue
finally:
file.close()
start += 4
file = open(os.path.join(BASE_PATH, '%s.jpg') % query, 'w')
try:
arr = np.asarray(bytearray(image_r.content), dtype=np.uint8)
img = cv2.imdecode(arr,-1) # 'load it as it is'
# save a copy of the image
results = fd.detect_face(img)
print(results)
if not results:
continue
Image.open(StringIO(image_r.content)).save(file)
return (results, img)
except:
# Throw away some gifs...blegh.
print 'could not save %s' % url
continue
finally:
file.close()
start += 4

# Example use
# find_character('data')
@@ -3,7 +3,7 @@
from difflib import SequenceMatcher

def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
return SequenceMatcher(None, a, b).ratio()

class Dialog:
def __init__(self, character, text):
@@ -51,7 +51,7 @@ def __init__(self):
self.scenes = []
self.characters = dict()
self.settings = dict()

def addScene(self, scene):
self.scenes.append(scene)

@@ -91,7 +91,7 @@ def parse(path):
curchar = None
for line in actuallines:
tabs, line = line

if tabs == 1: # Stage direction
scene.addDirection(s.StageDirection(line))
elif tabs == 3: # Dialog
112 voice.py
@@ -8,7 +8,7 @@

# Voices:
# 0: British Male
# 1:
# 1:
#
#

@@ -22,65 +22,65 @@
mouth_images = dict()

def generate_mouths(voice_num, phones, fps=24, scale=1.0):
framelen = 1000.0 / fps # ms
totaloffset = 0
curframeend = 0

mouths = []

for phone in phones:
p, dur = phone
dur *= scale
if p in ph.phonemes:
face = ph.phonemes[p]
else:
face = ph.REST
print "Unknown phoneme", p

while totaloffset + dur >= curframeend:
mouths.append(face)
off = curframeend - totaloffset
totaloffset += off
curframeend += framelen
dur -= off
totaloffset += dur
return mouths
framelen = 1000.0 / fps # ms

totaloffset = 0
curframeend = 0

mouths = []

for phone in phones:
p, dur = phone
dur *= scale
if p in ph.phonemes:
face = ph.phonemes[p]
else:
face = ph.REST
print "Unknown phoneme", p

while totaloffset + dur >= curframeend:
mouths.append(face)
off = curframeend - totaloffset
totaloffset += off
curframeend += framelen
dur -= off
totaloffset += dur
return mouths

# Given a voice number and a line of dialog, returns an array of tuples phoneme, length (ms)
def generate_line(voice_num, line, scale=1.0):
voice, pitch, volume = voices[voice_num]
phonemes = subprocess.check_output(["./voice.sh", voice, pitch, volume, pipes.quote(line)])
lines = phonemes.split("\n")
phones = []
for i in range(len(lines)):
phon = lines[i].split("\t")
if len(phon) < 2:
continue
p, time = phon[0], phon[1]
phones.append((p, int(time)))

mouths = generate_mouths(voice_num, phones, scale=scale)
mouth_img_list = []
for mouth in mouths:
if not mouth in mouth_images:
path = "mouths/" + mouth
mouth_images[mouth] = v.load_image(path)
mouth_img_list.append(mouth_images[mouth])
return mouth_img_list
voice, pitch, volume = voices[voice_num]
phonemes = subprocess.check_output(["./voice.sh", voice, pitch, volume, pipes.quote(line)])

lines = phonemes.split("\n")
phones = []
for i in range(len(lines)):
phon = lines[i].split("\t")
if len(phon) < 2:
continue
p, time = phon[0], phon[1]
phones.append((p, int(time)))

mouths = generate_mouths(voice_num, phones, scale=scale)
mouth_img_list = []
for mouth in mouths:
if not mouth in mouth_images:
path = "mouths/" + mouth
mouth_images[mouth] = v.load_image(path)
mouth_img_list.append(mouth_images[mouth])
return mouth_img_list

if __name__ == "__main__":
# text = "Data says I am incapable of any feeling. I do not think that that is a correct statement, but the matter will require further analysis. Now I am simply talking for a long time, because I do not care if I experience emotion. I am rather more interested in whether or not my lips sync properly with my audio, since I do try to act human if possible."

text = "Space, the final frontier. These are the voyages of the starship enterprise. Its ongoing mission: to explore strange new worlds. To seek out new life, and new civilizations. To boldly go where no one has gone before. Dooooo dooooo.... doooo, dooo dooo dooo doooooooooooooooooooo... dooooo, dooooo.... dooooo, dooo doooo doooo dooooooooooo..... dooooo doooooooo, dooooo dooooooo, dooo dooo dooo dooo doooooooo..... dooooooo..... dooo doooo, doooooooooo....... dooo, dooo, dooo, dooooooooooooooooooooooooooooooo"
au = a.OutputAudio()
mouth_images = generate_line(0, text)
au.addAudio("tmp/tmp.wav", 0)
pipe = subprocess.Popen(v.ffmpeg_create_video_command, stdin = subprocess.PIPE)
for m in mouth_images:
pipe.stdin.write(v.as_background_image(m).tostring())
pipe.stdin.close()

au.combineWith("tmp/out.mp4", "final.mkv")
text = "Space, the final frontier. These are the voyages of the starship enterprise. Its ongoing mission: to explore strange new worlds. To seek out new life, and new civilizations. To boldly go where no one has gone before. Dooooo dooooo.... doooo, dooo dooo dooo doooooooooooooooooooo... dooooo, dooooo.... dooooo, dooo doooo doooo dooooooooooo..... dooooo doooooooo, dooooo dooooooo, dooo dooo dooo dooo doooooooo..... dooooooo..... dooo doooo, doooooooooo....... dooo, dooo, dooo, dooooooooooooooooooooooooooooooo"
au = a.OutputAudio()
mouth_images = generate_line(0, text)
au.addAudio("tmp/tmp.wav", 0)

pipe = subprocess.Popen(v.ffmpeg_create_video_command, stdin = subprocess.PIPE)
for m in mouth_images:
pipe.stdin.write(v.as_background_image(m).tostring())
pipe.stdin.close()

au.combineWith("tmp/out.mp4", "final.mkv")