Spaces:
No application file
No application file
Download src/sim_match_analysis/utils.py from metasoccer/collab: direct link, hf CLI and curl.
- Browser
- Download file 12.9 kB
-
https://huggingface.co/spaces/metasoccer/collab/resolve/main/src/sim_match_analysis/utils.py
- Command line
-
hf download hf://spaces/metasoccer/collab/src/sim_match_analysis/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/metasoccer/collab/resolve/main/src/sim_match_analysis/utils.py
12.9 kB
| import random | |
| import string | |
| def render_pitch_html(coordinates): | |
| id = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) | |
| return f""" | |
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | |
| <script type="text/javascript"> | |
| function buildFootballRenderer(anchor) {{ | |
| var X_FIELD_SCALE = 54.4; | |
| var Y_FIELD_SCALE = 83.6; | |
| var Z_FIELD_SCALE = 1; | |
| var FOOTBALL_BALL_RADIUS = 0.11 / X_FIELD_SCALE; | |
| var FOOTBALL_CENTER_CIRCLE_RADIUS = 9.15 / X_FIELD_SCALE; | |
| var FOOTBALL_CORNER_ARC_RADIUS = 1 / X_FIELD_SCALE; | |
| var FOOTBALL_PITCH_HALF_WIDTH = 55 / X_FIELD_SCALE; // ~1 | |
| var FOOTBALL_PITCH_HALF_HEIGHT = 36 / Y_FIELD_SCALE; // ~0.42 | |
| var FOOTBALL_PITCH_RIM_X = 5 / X_FIELD_SCALE; | |
| var FOOTBALL_PITCH_RIM_Y = 4 / Y_FIELD_SCALE; | |
| var FOOTBALL_GOAL_AREA_WIDTH = FOOTBALL_PITCH_HALF_WIDTH * (1 - 0.92); // https://github.com/ephere-football/football/blob/939cad3f3b037502a91a383b132b98a6d017299b/third_party/gfootball_engine/src/onthepitch/referee.cpp#L183 | |
| var FOOTBALL_GOAL_HEIGHT = 3.7 * 2 / Y_FIELD_SCALE; | |
| var FOOTBALL_GOAL_WIDTH = 2.55 / X_FIELD_SCALE; | |
| var FOOTBALL_GOAL_Z_LENGTH = 2.5; // Height in 3D of the Goal | |
| var FOOTBALL_PENALTY_X = 11 / X_FIELD_SCALE; // https://github.com/ephere-football/football/blob/939cad3f3b037502a91a383b132b98a6d017299b/third_party/gfootball_engine/src/onthepitch/referee.cpp#L593-L595 | |
| var FOOTBALL_GOAL_AREA_ARC_RADIUS = 9.15 / X_FIELD_SCALE; | |
| var FOOTBALL_PENALTY_AREA_HEIGHT = 40.3 / Y_FIELD_SCALE; | |
| var FOOTBALL_PENALTY_AREA_WIDTH = 16.5 / X_FIELD_SCALE; | |
| // Renderer constants | |
| var BALL_ALTITUDE_EXPANSION_FACTOR = 1 / FOOTBALL_GOAL_Z_LENGTH; | |
| var ZOOM = 2; | |
| function adjustContext(context) {{ | |
| var {{ width, height }} = context.canvas; | |
| var paddingXRatio = FOOTBALL_PITCH_RIM_X / FOOTBALL_PITCH_HALF_WIDTH; | |
| var paddingYRatio = FOOTBALL_PITCH_RIM_Y / FOOTBALL_PITCH_HALF_HEIGHT; | |
| context.maxWidth = width * (1 - paddingXRatio); | |
| context.maxHeight = height * (1 - paddingYRatio); | |
| }} | |
| function adjustPosition(context, x, y) {{ | |
| return [ | |
| x * (context.maxWidth / 2) / FOOTBALL_PITCH_HALF_WIDTH, | |
| y * (context.maxHeight / 2) / FOOTBALL_PITCH_HALF_HEIGHT | |
| ]; | |
| }} | |
| function adjustSizeH(context, size) {{ | |
| return size * (context.maxHeight / 2) / FOOTBALL_PITCH_HALF_HEIGHT; | |
| }} | |
| function adjustSizeW(context, size, zoom) {{ | |
| return size * (context.maxWidth / 2) / FOOTBALL_PITCH_HALF_WIDTH * (zoom ?? 1); | |
| }} | |
| function createContext(canvas, devicePixelRatio) {{ | |
| // Get the device pixel ratio, falling back to 1. | |
| var dpr = devicePixelRatio || 1; | |
| // Get the size of the canvas in CSS pixels. | |
| var rect = canvas.getBoundingClientRect(); | |
| // Give the canvas pixel dimensions of their CSS | |
| // size * the device pixel ratio. | |
| canvas.width = rect.width * dpr; | |
| canvas.height = rect.height * dpr; | |
| var ctx = canvas.getContext('2d'); | |
| ctx.imageSmoothingEnabled = true; | |
| // Scale all drawing operations by the dpr, so you | |
| // don't have to worry about the difference. | |
| ctx.scale(dpr, dpr); | |
| // 0,0 will be the point of reference | |
| ctx.translate(canvas.width / 2, canvas.height / 2); | |
| return ctx; | |
| }} | |
| function drawBall(context, state) {{ | |
| var [x, y, z] = state; | |
| var [centerX, centerY] = adjustPosition(context, x, y); | |
| var radius = adjustSizeW(context, FOOTBALL_BALL_RADIUS, ZOOM) * (1 + z * BALL_ALTITUDE_EXPANSION_FACTOR); | |
| context.beginPath(); | |
| context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); | |
| context.fillStyle = 'white'; | |
| context.fill(); | |
| context.lineWidth = 1; | |
| context.strokeStyle = 'black'; | |
| context.stroke(); | |
| }} | |
| function drawMatchInfo(context, state) {{ | |
| var padding = 12; | |
| context.font = "12px serif"; | |
| context.textBaseline = "top"; | |
| context.fillText("STEPS: " + state.steps + "; SCORE: " + state.score.join(" - "), -context.maxWidth / 2 + padding, -context.maxHeight / 2 + padding); | |
| }} | |
| // README: https://en.wikipedia.org/wiki/Football_pitch | |
| function drawPitch(context) {{ | |
| // Clear | |
| context.clearRect(-context.canvas.width / 2, -context.canvas.height / 2, context.canvas.width, context.canvas.height); | |
| // Options | |
| context.fillStyle = "white"; | |
| context.lineWidth = 1; | |
| context.strokeStyle = 'white'; | |
| // Touch line | |
| context.beginPath(); | |
| context.rect(-context.maxWidth / 2, -context.maxHeight / 2, context.maxWidth, context.maxHeight); | |
| context.stroke(); | |
| // Half-way line | |
| context.moveTo(0, -context.maxHeight / 2); | |
| context.lineTo(0, context.maxHeight / 2); | |
| context.stroke(); | |
| // Centre circle | |
| context.beginPath(); | |
| context.arc(0, 0, adjustSizeW(context, FOOTBALL_CENTER_CIRCLE_RADIUS), 0, 2 * Math.PI); | |
| context.stroke(); | |
| context.beginPath(); | |
| context.arc(0, 0, adjustSizeW(context, FOOTBALL_BALL_RADIUS), 0, 2 * Math.PI, true); | |
| context.fill(); | |
| context.stroke(); | |
| // Corner arc | |
| context.moveTo(-context.maxWidth / 2, -context.maxHeight / 2); | |
| context.arc(-context.maxWidth / 2, -context.maxHeight / 2, adjustSizeW(context, FOOTBALL_CORNER_ARC_RADIUS), 0, Math.PI / 2); | |
| context.moveTo(-context.maxWidth / 2, context.maxHeight / 2); | |
| context.arc(-context.maxWidth / 2, context.maxHeight / 2, adjustSizeW(context, FOOTBALL_CORNER_ARC_RADIUS), -Math.PI / 2, 0); | |
| context.moveTo(context.maxWidth / 2, -context.maxHeight / 2); | |
| context.arc(context.maxWidth / 2, -context.maxHeight / 2, adjustSizeW(context, FOOTBALL_CORNER_ARC_RADIUS), Math.PI / 2, Math.PI); | |
| context.moveTo(context.maxWidth / 2, context.maxHeight / 2); | |
| context.arc(context.maxWidth / 2, context.maxHeight / 2, adjustSizeW(context, FOOTBALL_CORNER_ARC_RADIUS), -Math.PI, -Math.PI / 2); | |
| context.stroke(); | |
| // Goal area | |
| var goalAreaWidth = adjustSizeW(context, FOOTBALL_GOAL_AREA_WIDTH); | |
| var goalAreaHeight = goalAreaWidth / (FOOTBALL_PENALTY_AREA_WIDTH / FOOTBALL_PENALTY_AREA_HEIGHT) * 1.3; | |
| context.beginPath(); | |
| context.rect( | |
| -(context.maxWidth / 2), | |
| -goalAreaHeight / 2, | |
| goalAreaWidth, | |
| goalAreaHeight | |
| ); | |
| context.rect( | |
| (context.maxWidth / 2) - goalAreaWidth, | |
| -goalAreaHeight / 2, | |
| goalAreaWidth, | |
| goalAreaHeight | |
| ); | |
| context.stroke(); | |
| // Penalty area | |
| var penaltyAreaHeight = adjustSizeH(context, FOOTBALL_PENALTY_AREA_HEIGHT); | |
| var penaltyAreaWidth = adjustSizeW(context, FOOTBALL_PENALTY_AREA_WIDTH); | |
| context.beginPath(); | |
| context.rect( | |
| -(context.maxWidth / 2), | |
| -penaltyAreaHeight / 2, | |
| penaltyAreaWidth, | |
| penaltyAreaHeight | |
| ); | |
| context.rect( | |
| (context.maxWidth / 2) - penaltyAreaWidth, | |
| -penaltyAreaHeight / 2, | |
| penaltyAreaWidth, | |
| penaltyAreaHeight | |
| ); | |
| context.stroke(); | |
| // Penalty mark | |
| var [penaltyX] = adjustPosition(context, FOOTBALL_PENALTY_X, 0); | |
| context.beginPath(); | |
| context.arc(-(context.maxWidth / 2) + penaltyX, 0, adjustSizeW(context, FOOTBALL_BALL_RADIUS), 0, 2 * Math.PI, true); | |
| context.fill(); | |
| context.stroke(); | |
| context.beginPath(); | |
| context.arc((context.maxWidth / 2) - penaltyX, 0, adjustSizeW(context, FOOTBALL_BALL_RADIUS), 0, 2 * Math.PI, true); | |
| context.fill(); | |
| context.stroke(); | |
| // Penalty arc | |
| context.save(); | |
| context.rect(-(context.maxWidth / 2) + penaltyAreaWidth, -penaltyAreaHeight / 2, context.maxWidth - penaltyAreaWidth * 2, penaltyAreaHeight); | |
| context.clip(); | |
| context.beginPath(); | |
| context.arc(-(context.maxWidth / 2) + penaltyX, 0, adjustSizeW(context, FOOTBALL_GOAL_AREA_ARC_RADIUS), 0, 2 * Math.PI); | |
| context.stroke(); | |
| context.beginPath(); | |
| context.arc((context.maxWidth / 2) - penaltyX, 0, adjustSizeW(context, FOOTBALL_GOAL_AREA_ARC_RADIUS), 0, 2 * Math.PI); | |
| context.stroke(); | |
| context.restore(); | |
| // Goal | |
| var goalHeight = adjustSizeH(context, FOOTBALL_GOAL_HEIGHT); | |
| var goalWidth = adjustSizeW(context, FOOTBALL_GOAL_WIDTH) / 2; | |
| context.beginPath(); | |
| context.rect( | |
| -(context.maxWidth / 2) - goalWidth, | |
| -goalHeight / 2, | |
| goalWidth, | |
| goalHeight | |
| ); | |
| context.rect( | |
| (context.maxWidth / 2), | |
| -goalHeight / 2, | |
| goalWidth, | |
| goalHeight | |
| ); | |
| context.closePath(); | |
| context.strokeStyle = 'magenta'; | |
| context.stroke(); | |
| }} | |
| function drawPlayer(context, state, color) {{ | |
| var [x, y] = state; | |
| var [centerX, centerY] = adjustPosition(context, x, y); | |
| var radius = adjustSizeW(context, FOOTBALL_BALL_RADIUS * 3, ZOOM); | |
| context.beginPath(); | |
| context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); | |
| context.fillStyle = color; | |
| context.fill(); | |
| context.lineWidth = 1; | |
| context.strokeStyle = 'black'; | |
| context.stroke(); | |
| var caption = [x.toFixed(2), y.toFixed(2)].join(","); | |
| context.textBaseline = "top"; | |
| context.fillText(caption, centerX - (context.measureText(caption).width / 2), centerY + 12); | |
| }} | |
| function drawPoint(context, state, color, notes) {{ | |
| var [x, y] = state; | |
| var [centerX, centerY] = adjustPosition(context, x, y); | |
| var radius = adjustSizeW(context, FOOTBALL_BALL_RADIUS, ZOOM); | |
| context.beginPath(); | |
| context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); | |
| context.fillStyle = color; | |
| context.fill(); | |
| context.lineWidth = 1; | |
| context.strokeStyle = 'black'; | |
| context.stroke(); | |
| var caption = [x.toFixed(2), y.toFixed(2)].join(",") + " (" + notes + ")"; | |
| context.textBaseline = "top"; | |
| context.fillText(caption, centerX - (context.measureText(caption).width / 2), centerY + 12); | |
| }} | |
| function drawPreMatch(context) {{ | |
| adjustContext(context); | |
| drawPitch(context); | |
| }} | |
| var canvasCtx = createContext(anchor); | |
| return {{ | |
| drawPoint: (position, color, notes) => drawPoint(canvasCtx, position, color, notes), | |
| drawPreMatch: () => drawPreMatch(canvasCtx) | |
| }}; | |
| }} | |
| </script> | |
| </head> | |
| <body> | |
| <div class="ratio ratio-21x9"> | |
| <div class="w-100 h-100"> | |
| <canvas id="{id}" class="w-100 h-100 rounded" style="background: black;"> | |
| </canvas> | |
| </div> | |
| </div> | |
| <script> | |
| (() => {{ | |
| const r = buildFootballRenderer(document.querySelector("#{id}")); | |
| r.drawPreMatch(); | |
| {' '.join([f'r.drawPoint([{x}, {y}], "{color}", "{notes}");' for [x, y, color, notes] in coordinates])} | |
| }})(); | |
| </script> | |
| </body> | |
| </html> | |
| """ | |