This repository was archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·126 lines (105 loc) · 3.68 KB
/
main.py
File metadata and controls
executable file
·126 lines (105 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#Gruppe 15
#Christian Frost s184140
#Mikkel Lindtner s205421
#Nikolai Stein s205469
#Oliver Christensen s176352
#Søren Andersen s182881
#Tobias Kristensen s195458
from image_processing.img_compare import compare
import cv2
import numpy as np
import ml_solitaire.cut_image
import image_solutions as solution
import algorithm.image_algorithm as alg
import communication_layer.ml_opencv as ml_opencv
import communication_layer.app_alg as app_comm
from flask_cors import CORS
from flask.json import jsonify
from flask import Flask, request
from flask.helpers import make_response
from algorithm.image_algorithm import Fountain, run_algorithm
app = Flask(__name__)
app.config['SECRET_KEY'] = 'back3nd_!cdio'
app.config["IMAGE_UPLOADS"] = "images"
CORS(app)
@app.route('/')
def hello():
return "CDIO API"
@app.route('/upload', methods=['POST'])
def calculate_solution():
imagefile = request.files['file'].read()
image = cv2.imdecode(np.frombuffer(imagefile, np.uint8), cv2.IMREAD_COLOR)
image_fractions = ml_solitaire.cut_image.cut_img_cut_three(image)
cv_res = solution.opencv_solution(image_fractions)
ml_res = solution.ml_solution(image_fractions)
solitaire_alg = ml_opencv.compare_results(cv_res, ml_res)
bestmove = alg.run_algorithm(solitaire_alg)
return app_comm.convert_alg_to_app_json(bestmove)
#udelukkende til test af index.html skal ikke ændres!
@app.route("/algtest", methods = ['POST'])
def algtestpost():
bestMove = run_algorithm(request.get_json())
if "won" in bestMove and bestMove["won"]:
res = make_response(jsonify({'message': "VANDT!", "firstcard": "won", "secondcard": "won", "movemessage": "", 'location': ""}), 200)
return res
move = bestMove["move"]
firstcard = ""
secondcard = ""
movemessage = ""
if type(move) != str and move.fromCard is not None:
firstcard = str(getCardCharFromNumberTest(move.fromCard.number)) + " " + str(getColourCharFromNumberTest(move.fromCard.suit))
if move.toCard is not None:
secondcard = str(getCardCharFromNumberTest(move.toCard.number)) + " " + str(getColourCharFromNumberTest(move.toCard.suit))
else:
secondcard = "empty"
movemessage = str(move.description)
if (bestMove["point"] < 20 and bestMove["numberOfMoves"] == 1) or (bestMove["point"] < 40 and bestMove["numberOfMoves"] == 2):
firstcard = "flip"
secondcard = "random"
else:
movemessage = str(move)
location = "tableau"
if move and type(move.toStack) is Fountain:
location = "fountain"
res = make_response(jsonify({'message': bestMove["move"].description, "firstcard": firstcard, "secondcard": secondcard, "movemessage": movemessage, 'location': location}), 200)
return res
def getCardCharFromNumber(number):
if number == 1:
return "a"
if number == 11:
return "j"
if number == 12:
return "q"
if number == 13:
return "k"
return number
def getCardCharFromNumberTest(number):
if number == 1:
return "A"
if number == 11:
return "J"
if number == 12:
return "Q"
if number == 13:
return "K"
return number
def getColourCharFromNumber(number):
if number.value == 1:
return "d"
if number.value == 2:
return "h"
if number.value == 3:
return "s"
if number.value == 4:
return "c"
def getColourCharFromNumberTest(number):
if number.value == 1:
return "diamond"
if number.value == 2:
return "heart"
if number.value == 3:
return "spade"
if number.value == 4:
return "club"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5005)