From c01c7ff2c213328b3ca433e93368e8d2c21c4a52 Mon Sep 17 00:00:00 2001 From: lalami Date: Thu, 20 Jan 2022 12:24:19 +0100 Subject: [PATCH] server side and game instance --- game_instance/game.py | 136 ++++++++++++++++++++++++++++++++++++++ server_side/Dockerfile | 7 ++ server_side/mainserver.py | 54 +++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 game_instance/game.py create mode 100644 server_side/Dockerfile create mode 100644 server_side/mainserver.py diff --git a/game_instance/game.py b/game_instance/game.py new file mode 100644 index 0000000..216e383 --- /dev/null +++ b/game_instance/game.py @@ -0,0 +1,136 @@ +from flask import Flask, jsonify, request,send_file +from flask_cors import CORS, cross_origin +import docker +import socket +import requests +import numpy as np +import ast +from ast import literal_eval +import json +from PIL import Image +from matplotlib import cm +from io import StringIO, BytesIO + +app = Flask(__name__) +cors=CORS(app) +app.config["CORS_HEADERS"]='Content-Type' + +discoveryservice_url = "192.168.37.137:50005" + +cardManager_url = requests.get(url = "http://" + discoveryservice_url + "/CardManager").text +cardstext = requests.get(url = "http://" + cardManager_url + "/recupCarte/14/2").text +cards=np.array(json.loads(cardstext)) +print(len(cards)) +carre=np.uint8(cards) +print(type(carre[0][0][0][0])) +#res=str(cards[0]) +#print(res) +#print(type(res)) +i=0 +list_ai=[] +true_box=[] +# intelligence artificielle +ai_url=requests.get(url = "http://" + discoveryservice_url + "/IA").text +for j in range(len(cards)): + response=requests.post("http://"+ai_url+"/prediction", json={"card" : cards[i].tolist()}) + list_ai.append(response.json()) +for k in range(len(list_ai)-1): + json_obj1=list_ai[k] + json_obj2=list_ai[k+1] + lab_obj1=json_obj1["label"] + lab_obj2=json_obj2["label"] + common =list(set(lab_obj1).intersection(set(lab_obj2))) + index=lab_obj1.index(common[0]) + coordonne=json_obj1["coord"][index] + true_box.append(coordonne) +print(true_box) +print(response.json()) + + +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') + +@app.route('/route/') +def serve_img(id): + id=int(id) + if (id', methods = ['POST']) +@cross_origin() +def pixels(id): + id=int(id) + resultat = request.get_json(force=True) + print("µµµµµµµ") + print(resultat["X"]) + X=resultat["X"] + print("********") + print(resultat["Y"]) + Y=resultat["Y"] + print("µµµµµµµ") + print(true_box[id]) + if(X>= true_box[id][0] and X<= true_box[id][1] and Y>= true_box[id][2] and Y<= true_box[id][3]): + if id==len(true_box)-1: + return "FINI" + else: + return "OK" + else: + return "Fail" + +@app.route('/pools', methods = ['GET','POST']) +def verify_res(): + resultat = request.get_json(force=True) + print("µµµµµµµ") + print(resultat["X"]) + print("********") + print(resultat["Y"]) + print("µµµµµµµ") + return "Bonjour" + + +@app.route('/time', methods = ['GET','POST']) +def getTime(): + global i + res0 = str(cards[0].tolist()) + res1 = str(cards[1].tolist()) + i = 2 + return jsonify( + message= "this is the first two cards", + first_card = res0 , + second_card = res1 +) + + +@app.route('/cards/', methods = ['GET','POST']) +@cross_origin() +def sendCards(id): + id=int(id) + if (id