import robot_util import requests import urllib.request import requests import random from video.ffmpeg_process import * import audio_util import networking import watchdog import subprocess import shlex import schedule import extended_command import atexit import os import sys import logging import time import signal import re styles = [] # Add your art styles urls here styles.append('https://cdn.britannica.com/78/43678-050-F4DC8D93/Starry-Night-canvas-Vincent-van-Gogh-New-1889.jpg') def textToImage(text): r = requests.post( "https://api.deepai.org/api/text2img", data={ 'text': text, }, headers={'api-key': 'YOUR API KEY HERE'} ) result = r.json() return result['output_url'] def styleTransfer(targetURL): index = random.randint(0, len(styles)-1) styleUrl =styles[index] r = requests.post( "https://api.deepai.org/api/fast-style-transfer", data={ 'content': targetURL, 'style': styleUrl, }, headers={'api-key': 'YOUR API KEY HERE'} ) result = r.json() return result['output_url'] def startFFMPEG(command, name, atExit, process): try: if sys.platform.startswith('linux') or sys.platform == "darwin": ffmpeg_process=subprocess.Popen(command, stderr=subprocess.PIPE, shell=True, preexec_fn=os.setsid) globals()[process] = ffmpeg_process except OSError: # Can't find / execute ffmpeg robot_util.terminate_controller() return() if ffmpeg_process != None: try: atexit.unregister(atExit) # Only python 3 except AttributeError: pass def generateImage(text, author): robot_util.sendChatMessage("Generating...") cleanText= re.sub('[^A-Za-z0-9]+', '', text) cleanUser = re.sub('[^A-Za-z0-9]+', '', author) targetURL = textToImage(text) resultURL = styleTransfer(targetURL) imgPath = "/home/pi/remotv/img/"+ str(int(time.time()))+ '-' + cleanText + '-' + cleanUser + ".png" urllib.request.urlretrieve(resultURL, imgPath) os.system("sudo killall ffmpeg") videoCommandLine = 'ffmpeg -loop 1 -i {PathToFile} -s 720x480 -f mpegts -codec:v mpeg1video -an -b:v 2500k -muxdelay 0.001 http://remo.tv:1567/transmit?name=-video' videoCommandLine = videoCommandLine.format(PathToFile=imgPath) startFFMPEG(videoCommandLine, 'Video', atExitVideoCapture, 'video_process') robot_util.sendChatMessage("Now showing: " + text + " by " + author) def atExitVideoCapture(): print('it dead!') def setup(robot_config): return def move(args, argsCustom): try: if args['button']['label'] == 'Generate': message = argsCustom['message'] user = argsCustom['sender'] generateImage(message,user) if args['button']['label'] == 'More Info': robot_util.sendChatMessage("Type in the chat and press 'Generate' to create an piece of AI art.") time.sleep(5) except: pass return