2024-08-05 13:41:50 -04:00
|
|
|
import json
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
|
|
|
from image_util import load_image
|
|
|
|
import unittest
|
|
|
|
import time
|
|
|
|
import accessible_output2.outputs.auto
|
|
|
|
import os
|
|
|
|
import base64
|
2024-08-06 12:41:40 -04:00
|
|
|
import test
|
2024-08-05 13:41:50 -04:00
|
|
|
po = test.PackageOutput("ff1_data.lud", "enemy_lookup.dat")
|
|
|
|
|
|
|
|
auto_obj = accessible_output2.outputs.auto.Auto()
|
|
|
|
|
|
|
|
def text_to_sound(string):
|
|
|
|
string = string.replace("\"", "'")
|
|
|
|
os.system("espeak \""+string+"\" --stdout > wave_out.wav")
|
|
|
|
data = base64.b64encode(open("wave_out.wav").read())
|
|
|
|
return data
|
|
|
|
|
2024-08-06 12:41:40 -04:00
|
|
|
class MainHandler(BaseHTTPRequestHandler):
|
2024-08-05 13:41:50 -04:00
|
|
|
def do_GET(self):
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "text/html")
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write("<html><head><title></title></head></html>")
|
|
|
|
self.wfile.write("<body>yo!</body></html>")
|
|
|
|
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
print("____")
|
2024-08-06 13:14:12 -04:00
|
|
|
query = urlparse(self.path).query
|
2024-08-05 13:41:50 -04:00
|
|
|
if query.strip():
|
|
|
|
query_components = dict(qc.split("=") for qc in query.split("&"))
|
|
|
|
else:
|
|
|
|
query_components = {}
|
2024-08-06 13:14:12 -04:00
|
|
|
content_length = int(self.headers.get('content-length', 0))
|
2024-08-05 13:41:50 -04:00
|
|
|
data = self.rfile.read(content_length);
|
|
|
|
doc = json.loads(data)
|
|
|
|
|
|
|
|
#print([self.request.body])
|
|
|
|
output = query_components.get("output","")
|
|
|
|
outputs = dict()
|
|
|
|
for value in output.split(","):
|
|
|
|
if value in ['sound', 'wav']:
|
|
|
|
outputs['sound'] = value
|
|
|
|
elif value in ['image', 'png-a', 'png']:
|
|
|
|
outputs['image'] = value
|
|
|
|
elif value in ['text']:
|
|
|
|
outputs['text'] = value
|
|
|
|
|
|
|
|
t_time = time.time()
|
|
|
|
strings = ""
|
|
|
|
presses = list()
|
|
|
|
|
|
|
|
if doc.get("image"):
|
|
|
|
im = load_image(doc['image'])
|
|
|
|
state = doc.get('state', {})
|
|
|
|
t_time = time.time()
|
|
|
|
strings, presses = po.run(im, state)
|
2024-08-06 12:41:40 -04:00
|
|
|
print(('aaa', time.time()-t_time))
|
2024-08-05 13:41:50 -04:00
|
|
|
words = " ".join(strings).replace(">", " ").split(" ")
|
|
|
|
nw = list()
|
|
|
|
for idx, word in enumerate(words):
|
|
|
|
if word.replace("G", "").replace("P", "").replace("O", "0").replace("o", "0").replace("DMG","").isdigit():
|
|
|
|
if len(words) > idx+1 and words[idx+1]=="to:":
|
|
|
|
nw.append(word)
|
|
|
|
else:
|
|
|
|
nw.append(word.replace("O", "0").replace("o", "0"))
|
|
|
|
else:
|
|
|
|
nw.append(word)
|
|
|
|
strings = " ".join(nw)
|
|
|
|
|
|
|
|
res = {"auto": "auto"}
|
|
|
|
if strings:
|
|
|
|
for key in outputs:
|
|
|
|
if key == 'text':
|
|
|
|
res['text'] = strings
|
|
|
|
elif key == 'sound':
|
|
|
|
res['sound'] = text_to_sound(strings)
|
|
|
|
elif key == 'image':
|
|
|
|
pass
|
|
|
|
#res['text'] = strings
|
|
|
|
#else:
|
|
|
|
# res['error'] = "No text found."
|
|
|
|
res['press'] = ",".join(presses)
|
|
|
|
print(presses)
|
|
|
|
print(strings)
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "text/html")
|
|
|
|
output = json.dumps(res)
|
2024-08-06 12:41:40 -04:00
|
|
|
print((len(output), time.time()-t_time))
|
2024-08-05 13:41:50 -04:00
|
|
|
self.send_header("Content-Length", len(output))
|
|
|
|
self.end_headers()
|
2024-08-06 13:14:12 -04:00
|
|
|
self.wfile.write(bytes(output, encoding='utf-8'))
|
2024-08-05 13:41:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
host = "localhost"
|
|
|
|
port = 4404
|
2024-08-06 12:41:40 -04:00
|
|
|
server_class = HTTPServer
|
2024-08-05 13:41:50 -04:00
|
|
|
httpd = server_class((host, port), MainHandler)
|
|
|
|
try:
|
|
|
|
httpd.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
httpd.server_close()
|
|
|
|
print('end')
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
main()
|
|
|
|
|