136 lines
3.4 KiB
Python
136 lines
3.4 KiB
Python
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/<id>')
|
|
def serve_img(id):
|
|
id=int(id)
|
|
if (id<len(cards)):
|
|
img = Image.fromarray(np.uint8(cards[id]))
|
|
return serve_pil_image(img)
|
|
else:
|
|
return "Congratulations !!! You have won the game!"
|
|
|
|
@app.route('/pixels/<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/<id>', methods = ['GET','POST'])
|
|
@cross_origin()
|
|
def sendCards(id):
|
|
id=int(id)
|
|
if (id<len(cards)):
|
|
res=str(cards[id].tolist())
|
|
img = Image.fromarray(np.uint8(cards[id]))
|
|
return serve_pil_image(img)
|
|
else:
|
|
return "Congratulations you have won the game !!!"
|
|
|
|
def shutdown_server():
|
|
func = request.environ.get('werkzeug.server.shutdown')
|
|
if func is None:
|
|
raise RuntimeError('Not running with the Werkzeug Server')
|
|
func()
|
|
|
|
@app.route('/shutdown', methods =['POST'] )
|
|
def shutdown():
|
|
shutdown_server()
|
|
return 'Server shutting down...'
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0",port=50001)
|
|
|
|
|