projet-integrateur/IPreoject-CLientSide/main.py
2022-01-20 12:04:51 +01:00

128 lines
2.9 KiB
Python

import ast
from flask import Flask, render_template, jsonify, Response, request, send_file
import time
import requests
import json
from PIL import Image
from ast import literal_eval
import ast
from io import StringIO, BytesIO
import numpy as np
import io
import cv2
import base64
app = Flask(__name__)
@app.route('/')
def index():
return render_template('main.html')
@app.route("/choose-game", methods=['POST', 'GET'])
def choose_game():
forward_message = "Moving Forward"
return render_template('menu.html', forward_message=forward_message)
@app.route("/add-images", methods=['POST'])
def add_images():
return None
# TO-DO
@app.route("/rules", methods=['POST', 'GET'])
def rules():
return render_template('rules.html')
@app.route("/join-game", methods=['POST', 'GET'])
def join_game():
return None
# TO-DO
API_URL1 = "http://192.168.37.69:50000"
API_URL2 = "http://192.168.37.69:50000/port"
#------------------------------------------ Cards -----------------------------------------
def serve_pil_image(pil_img):
img_io = BytesIO()
pil_img.save(img_io, 'JPEG', quality=70)
img_io.seek(0)
return send_file(img_io, mimetype='image/jpeg')
def stringToRGB(base64_string):
imgdata = base64.b64decode(str(base64_string))
image = Image.open(io.BytesIO(imgdata))
return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
API_CARD = "http://192.168.37.69:50001/time"
@app.route("/new-game", methods=['POST', 'GET'])
def getCards():
res = requests.get(API_CARD).json()
card1 = res.get('first_card') # our port
card2 = res.get('second_card')
card1_array = np.array(json.loads(card1))
card2_array = np.array(json.loads(card2))
img1 = Image.fromarray(np.uint8(card1_array))
img2 = Image.fromarray(np.uint8(card2_array))
pil_img1 = serve_pil_image(img1)
pil_img2 = serve_pil_image(img2)
pil_img1.direct_passthrough = False
pil_img2.direct_passthrough = False
print(type(pil_img1.data))
imageStream1 = io.BytesIO(pil_img1.data)
imageFile1 = Image.open(imageStream1)
imageStream2 = io.BytesIO(pil_img2.data)
imageFile2 = Image.open(imageStream2)
buffer = io.BytesIO()
imageFile1.save(buffer, format='JPEG')
imageFile2.save(buffer, format='JPEG')
imageFile1.save("./static/images/temp/card1.jpg")
imageFile2.save("./static/images/temp/card2.jpg")
return render_template('new-game.html')
@app.route("/apitest", methods=['POST', 'GET'])
def apitest():
# reqParms = request
# return render_template('test.html', param=reqParms)
x = request.get_json(silent=True)
# print(json.loads(request.data.decode("utf-8")))
# log the user out
return x
@app.route("/getPixels", methods=['GET'])
def getPixels():
return render_template('test.html')
if __name__ == "__main__":
app.run(debug = True)