This commit is contained in:
unknown 2024-01-06 17:24:34 +01:00
commit 41380fbfcf
6 changed files with 208 additions and 33 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "react_bs_web", "name": "react_bs_web",
"private": true, "private": true,
"proxy": "http://localhost:5173", "proxy": "http://127.0.0.1:5000/",
"version": "0.0.0", "version": "0.0.0",
"type": "module", "type": "module",
"scripts": { "scripts": {

View File

@ -10,7 +10,7 @@ const Hero = () => {
const toggleDropdown = () => setIsOpen(!isOpen); const toggleDropdown = () => setIsOpen(!isOpen);
const handleMinuteChange = (e) => setGameMinute(e.target.value); const handleMinuteChange = (e) => setGameMinute(e.target.value);
const [data,setData] = useState([{}]) const [data, setData] = useState("");
//zmienne globalne //zmienne globalne
//zmienna mówiąca który przycisk jest aktywny, jest ona wykorzystywana //zmienna mówiąca który przycisk jest aktywny, jest ona wykorzystywana
//przez listener nanoszący zawodników na boisko. 1 - strzelec , 2 - bramkarz , 3 - obronca, 4 - napasnik. zmienna przez aktywacje przycskówk bb1, bb2, bb3 i bb4 //przez listener nanoszący zawodników na boisko. 1 - strzelec , 2 - bramkarz , 3 - obronca, 4 - napasnik. zmienna przez aktywacje przycskówk bb1, bb2, bb3 i bb4
@ -108,10 +108,10 @@ const Hero = () => {
var pName = "Bramkarz" var pName = "Bramkarz"
var pColor = "#03e7fc" var pColor = "#03e7fc"
}else if(possition == 2){ }else if(possition == 2){
var pName = "Napastnik" var pName = "Obrońca"
var pColor = "#0324fc" var pColor = "#0324fc"
}else if (possition ==3){ }else if (possition ==3){
var pName = "Obrońca" var pName = "Napastnik"
var pColor = "#fc6703" var pColor = "#fc6703"
} }
var player = document.createElement('div'); var player = document.createElement('div');
@ -189,6 +189,8 @@ const Hero = () => {
ball.style.left = x + "%" ball.style.left = x + "%"
ball.style.top = y + "%" ball.style.top = y + "%"
} }
//var shooterX = konwerturX(ball.style.left)
//var shooterY = konwetujY(ball.style.top)
posytion.innerHTML = "x:" + konwerturX(ball.style.left) + "m " + "y:" + konwetujY(ball.style.top) + "m"; posytion.innerHTML = "x:" + konwerturX(ball.style.left) + "m " + "y:" + konwetujY(ball.style.top) + "m";
bojo.addEventListener("mouseup", function(){ bojo.addEventListener("mouseup", function(){
ball.style.background = pColor ball.style.background = pColor
@ -200,22 +202,38 @@ const Hero = () => {
}) })
} }
// // Wyłanie zapytania do serwera
// Wyłanie zapytania do serwera
function sentQuestion() { function sentQuestion() {
if (numer_od_shooters > 0) { var ball = document.querySelector('.football');
fetch("http://localhost:5173/members").then
(res => res.json()).then(res => { if (ball) {
let token = res.token; // Użyj backticksów zamiast zwykłych cudzysłowów
console.log("token: ", token); //console.log('Wysyłanie wartości: ', ball);
}) shooterX = konwerturX(ball.style.left)
eX = data.response shooterY = konwetujY(ball.style.top)
} else { console.log('Wysyłanie wartości: ', shooterX, shooterY);
alert('Piłka nie jest obecnie na boisku.'); fetch(`http://127.0.0.1:5000/get_model?x=${shooterX}&y=${shooterY}`).then(
} res => res.json()
).then(
data => {
setData(data);
console.log(data);
// Przenieś tę linię do środka bloku .then(), aby uniknąć błędów
let eX = data.response;
document.getElementById("ex").innerHTML = "Współczynnik xG: " + eX;
}
).catch(error => {
console.error('Błąd:', error);
});
} else {
alert('Piłka nie jest obecnie na boisku.');
}
} }
/* Funkcja dodająca listener do boiska*/ /* Funkcja dodająca listener do boiska*/
function boiskoListener(ev){ function boiskoListener(ev){
ev.preventDefault() ev.preventDefault()

View File

@ -3,9 +3,9 @@ import pandas as pd
from math import sqrt from math import sqrt
# Funkcja zwraca prawdopodobieństwo zdobycia gola # Funkcja zwraca prawdopodobieństwo zdobycia gola
def LogisticRegression_predict_proba(position_x, position_y, angle, match_minute, Number_Intervening_Opponents, Number_Intervening_Teammates, isFoot, isHead): def LogisticRegression_predict_proba(position_x, position_y, distance_to_goalM, angle, match_minute, Number_Intervening_Opponents, Number_Intervening_Teammates, isFoot, isHead):
distance_to_goalM = sqrt(( (position_x**2) + (position_y**2))) # distance_to_goalM = sqrt(( (position_x**2) + (position_y**2)))
model = load('regresja_logistyczna.joblib') model = load('regresja_logistyczna.joblib')
X_new = pd.DataFrame(columns=['position_x', 'position_y', 'distance_to_goalM', 'angle','match_minute', 'Number_Intervening_Opponents','Number_Intervening_Teammates', 'isFoot', 'isHead']) X_new = pd.DataFrame(columns=['position_x', 'position_y', 'distance_to_goalM', 'angle','match_minute', 'Number_Intervening_Opponents','Number_Intervening_Teammates', 'isFoot', 'isHead'])
@ -13,3 +13,80 @@ def LogisticRegression_predict_proba(position_x, position_y, angle, match_minute
return model.predict_proba(X_new)[0][1].round(2) return model.predict_proba(X_new)[0][1].round(2)
#kolejne modele #kolejne modele
def xgboost_predict_proba(minute, position_name, shot_body_part_name, shot_technique_name,
shot_type_name, shot_first_time, shot_one_on_one,
shot_aerial_won, shot_deflected, shot_open_goal,
shot_follows_dribble, shot_redirect, x1, y1,
number_of_players_opponents, number_of_players_teammates,
angle, distance, x_player_opponent_Goalkeeper,
x_player_opponent_8, x_player_opponent_1, x_player_opponent_2,
x_player_opponent_3, x_player_teammate_1, x_player_opponent_4,
x_player_opponent_5, x_player_opponent_6, x_player_teammate_2,
x_player_opponent_9, x_player_opponent_10, x_player_opponent_11,
x_player_teammate_3, x_player_teammate_4, x_player_teammate_5,
x_player_teammate_6, x_player_teammate_7, x_player_teammate_8,
x_player_teammate_9, x_player_teammate_10,
y_player_opponent_Goalkeeper, y_player_opponent_8,
y_player_opponent_1, y_player_opponent_2, y_player_opponent_3,
y_player_teammate_1, y_player_opponent_4, y_player_opponent_5,
y_player_opponent_6, y_player_teammate_2, y_player_opponent_9,
y_player_opponent_10, y_player_opponent_11, y_player_teammate_3,
y_player_teammate_4, y_player_teammate_5, y_player_teammate_6,
y_player_teammate_7, y_player_teammate_8, y_player_teammate_9,
y_player_teammate_10, x_player_opponent_7, y_player_opponent_7,
x_player_teammate_Goalkeeper, y_player_teammate_Goalkeeper,
shot_kick_off):
model = load('xgboost.joblib')
X_new = pd.DataFrame(columns=['minute', 'position_name', 'shot_body_part_name', 'shot_technique_name',
'shot_type_name', 'shot_first_time', 'shot_one_on_one',
'shot_aerial_won', 'shot_deflected', 'shot_open_goal',
'shot_follows_dribble', 'shot_redirect', 'x1', 'y1',
'number_of_players_opponents', 'number_of_players_teammates',
'angle', 'distance', 'x_player_opponent_Goalkeeper',
'x_player_opponent_8', 'x_player_opponent_1', 'x_player_opponent_2',
'x_player_opponent_3', 'x_player_teammate_1', 'x_player_opponent_4',
'x_player_opponent_5', 'x_player_opponent_6', 'x_player_teammate_2',
'x_player_opponent_9', 'x_player_opponent_10', 'x_player_opponent_11',
'x_player_teammate_3', 'x_player_teammate_4', 'x_player_teammate_5',
'x_player_teammate_6', 'x_player_teammate_7', 'x_player_teammate_8',
'x_player_teammate_9', 'x_player_teammate_10',
'y_player_opponent_Goalkeeper', 'y_player_opponent_8',
'y_player_opponent_1', 'y_player_opponent_2', 'y_player_opponent_3',
'y_player_teammate_1', 'y_player_opponent_4', 'y_player_opponent_5',
'y_player_opponent_6', 'y_player_teammate_2', 'y_player_opponent_9',
'y_player_opponent_10', 'y_player_opponent_11', 'y_player_teammate_3',
'y_player_teammate_4', 'y_player_teammate_5', 'y_player_teammate_6',
'y_player_teammate_7', 'y_player_teammate_8', 'y_player_teammate_9',
'y_player_teammate_10', 'x_player_opponent_7', 'y_player_opponent_7',
'x_player_teammate_Goalkeeper', 'y_player_teammate_Goalkeeper',
'shot_kick_off'])
X_new.loc[len(X_new.index)] = [minute, position_name, shot_body_part_name, shot_technique_name,
shot_type_name, shot_first_time, shot_one_on_one,
shot_aerial_won, shot_deflected, shot_open_goal,
shot_follows_dribble, shot_redirect, x1, y1,
number_of_players_opponents, number_of_players_teammates,
angle, distance, x_player_opponent_Goalkeeper,
x_player_opponent_8, x_player_opponent_1, x_player_opponent_2,
x_player_opponent_3, x_player_teammate_1, x_player_opponent_4,
x_player_opponent_5, x_player_opponent_6, x_player_teammate_2,
x_player_opponent_9, x_player_opponent_10, x_player_opponent_11,
x_player_teammate_3, x_player_teammate_4, x_player_teammate_5,
x_player_teammate_6, x_player_teammate_7, x_player_teammate_8,
x_player_teammate_9, x_player_teammate_10,
y_player_opponent_Goalkeeper, y_player_opponent_8,
y_player_opponent_1, y_player_opponent_2, y_player_opponent_3,
y_player_teammate_1, y_player_opponent_4, y_player_opponent_5,
y_player_opponent_6, y_player_teammate_2, y_player_opponent_9,
y_player_opponent_10, y_player_opponent_11, y_player_teammate_3,
y_player_teammate_4, y_player_teammate_5, y_player_teammate_6,
y_player_teammate_7, y_player_teammate_8, y_player_teammate_9,
y_player_teammate_10, x_player_opponent_7, y_player_opponent_7,
x_player_teammate_Goalkeeper, y_player_teammate_Goalkeeper,
shot_kick_off]
return model.predict_proba(X_new)[0][1].round(2)

View File

@ -1,36 +1,116 @@
from flask import Flask from flask import Flask, request, jsonify
from flask_cors import CORS from flask_cors import CORS
from modele.modele import LogisticRegression_predict_proba from modele.modele import LogisticRegression_predict_proba, xgboost_predict_proba
import math
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
def loc2angle(x, y):
rads = math.atan(7.32 * x / (x**2 + (y - 34)**2 - (7.32/2)**2))
rads = math.pi + rads if rads < 0 else rads
deg = math.degrees(rads)
return deg
def loc2distance(x, y):
return math.sqrt(x**2 + (y - 34)**2)
def loc2locdistance(x1, y1, x2, y2):
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
# model Api # model Api
<<<<<<< HEAD
@app.route("/members") @app.route("/members")
def members(): def members():
return "dnjss" return "dnjss"
=======
# @app.route("/members")
# def members():
# return{"members":["Member1","Member2","Member3"]}
>>>>>>> 0b5e2681184a215a830217fb15fc4be882e06e8c
# zapytanie o regresje logistyczną na podstawie pozycji piłki # zapytanie o regresje logistyczną na podstawie pozycji piłki
@app.route("/LRegresion<x>&<y>") # @app.route("/LRegresion<x>&<y>")
def LRegresion(x,y): @app.route("/get_model", methods = ['GET'])
# def get_model():
x = int(x[0:2]) # # x = int(x[0:2])
y = int(y[0:2]) # # y = int(y[0:2])
# x = request.args.get('x', type=float)
# y = request.args.get('y', type=float)
# ## change model on xgboost
# ## add angle, match minutes and number of players
# angle = loc2angle(x = x, y = y)
# dist = loc2distance(x = x, y = y)
# if y is None and x is None:
# return jsonify({"error": "Brak wymaganych parametrów"}), 400
# response = xgboost_predict_proba(minute = 20, position_name, shot_body_part_name, shot_technique_name,
# shot_type_name, shot_first_time, shot_one_on_one,
# shot_aerial_won, shot_deflected, shot_open_goal,
# shot_follows_dribble, shot_redirect, x1 = x, y1 = y,
# number_of_players_opponents, number_of_players_teammates,
# angle = angle, distance = dist, x_player_opponent_Goalkeeper,
# x_player_opponent_8, x_player_opponent_1, x_player_opponent_2,
# x_player_opponent_3, x_player_teammate_1, x_player_opponent_4,
# x_player_opponent_5, x_player_opponent_6, x_player_teammate_2,
# x_player_opponent_9, x_player_opponent_10, x_player_opponent_11,
# x_player_teammate_3, x_player_teammate_4, x_player_teammate_5,
# x_player_teammate_6, x_player_teammate_7, x_player_teammate_8,
# x_player_teammate_9, x_player_teammate_10,
# y_player_opponent_Goalkeeper, y_player_opponent_8,
# y_player_opponent_1, y_player_opponent_2, y_player_opponent_3,
# y_player_teammate_1, y_player_opponent_4, y_player_opponent_5,
# y_player_opponent_6, y_player_teammate_2, y_player_opponent_9,
# y_player_opponent_10, y_player_opponent_11, y_player_teammate_3,
# y_player_teammate_4, y_player_teammate_5, y_player_teammate_6,
# y_player_teammate_7, y_player_teammate_8, y_player_teammate_9,
# y_player_teammate_10, x_player_opponent_7, y_player_opponent_7,
# x_player_teammate_Goalkeeper, y_player_teammate_Goalkeeper,
# shot_kick_off)
# #print(x)
# #print(y)
# #print(response)
# res = str(response)
# #return {"response":res}
# return jsonify({"response":res})
def get_model():
# x = int(x[0:2])
# y = int(y[0:2])
x = request.args.get('x', type=float)
y = request.args.get('y', type=float)
## change model on xgboost
## add angle, match minutes and number of players
angle = loc2angle(x = x, y = y)
dist = loc2distance(x = x, y = y)
if y is None and x is None:
return jsonify({"error": "Brak wymaganych parametrów"}), 400
response = LogisticRegression_predict_proba(position_x=x, response = LogisticRegression_predict_proba(position_x=x,
position_y=y, position_y=y,
angle = 13.67, distance_to_goalM = dist,
angle = angle,
match_minute=13, match_minute=13,
Number_Intervening_Opponents=3, Number_Intervening_Opponents=3,
Number_Intervening_Teammates=0, Number_Intervening_Teammates=0,
isFoot=1, isFoot=1,
isHead=0) isHead=0)
print(x) #print(x)
print(y) #print(y)
print(response) #print(response)
return {"response":[str(response)]} res = str(response)
#return {"response":res}
return jsonify({"response":res})
# uruchomienie serwera # uruchomienie serwera
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -114,7 +114,7 @@ loc2angle <- function(x, y) {
# distance to goal # distance to goal
loc2distance <- function(x, y) { loc2distance <- function(x, y) {
sqrt(x^2 + y^2) sqrt(x^2 + (y - 34)^2)
} }
# distance between two points on the pitch # distance between two points on the pitch
@ -345,5 +345,5 @@ cols <- names(data3_final)[grepl(pattern, names(data3_final))]
data_final <- data3_final %>% unnest(all_of(cols)) data_final <- data3_final %>% unnest(all_of(cols))
skimr::skim(data_final) skimr::skim(data_final)
write_csv(data_final, file = "data/final_data.csv") write_csv(data_final, file = "data/final_data.csv")
# df_test <- read.csv("data/final_data.csv", nrows = 100) df_test <- read.csv("data/final_data.csv", nrows = 1000)
##################### The fourth dataset ############################## ##################### The fourth dataset ##############################