Skip to content

Commit

Permalink
bunch of improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
papalotis committed Mar 19, 2024
1 parent f94f102 commit 4816af2
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 63 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# drawing-to-fsd-layout

A small script that converts a hand drawn track layout to a track layout that can be used in in Formula Student Driverless Simulators
A tool that converts a hand drawn track layout to a track layout that can be used in in Formula Student Driverless Simulators

<!-- It will turn this image ![before](media/before.png) into this ![after](media/after.png) -->
<!-- Table with before and after images side by side -->
Expand All @@ -13,3 +13,23 @@ Clear hand-drawn tracks should also work. They do not have to be filled in.
There is now also a canvas option in the script.

The extracted track can be downloaded as a JSON file with x,y and color values are available for each cone, as well as an LYT file that can be used in Live for Speed.

The tool is hosted on Streamlit Cloud and can be accessed [here](https://drawing-to-fsd-layout.streamlit.app/).

## Installation

If you want to run the tool locally you can follow these steps:

```bash
git clone https://github.com/papalotis/drawing-to-fsd-layout.git

cd drawing-to-fsd-layout

# optional: create a virtual environment

pip install -r requirements.txt

streamlit run streamlit_app.py
```

The tool will be available at `http://localhost:8501`
25 changes: 18 additions & 7 deletions drawing_to_fsd_layout/canvas_image.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
from drawing_to_fsd_layout.image_processing import Image
from streamlit_drawable_canvas import st_canvas
import streamlit as st
import numpy as np
import streamlit as st
from streamlit_drawable_canvas import st_canvas

from drawing_to_fsd_layout.image_processing import Image


def show_canvas_warning():
st.warning("You need to draw a track in order to continue.")
st.stop()


def get_canvas_image() -> Image:

stroke_width = st.slider("Stroke width", 1, 25, 10)

should_erase = st.radio("Eraser", ["Off", "On"], horizontal=True)

stroke_color = "white" if should_erase == "On" else "black"

canvas_result = st_canvas(
stroke_width=stroke_width,
drawing_mode='freedraw',
stroke_color=stroke_color,
drawing_mode="freedraw",
key="canvas",
)

if canvas_result.image_data is None:
show_canvas_warning()

# by default canvas changes the alpha channel, not the rgb channels
raw_image_data = canvas_result.image_data
image_data = canvas_result.image_data[:, :, [-1]]
image_lightness = np.max(raw_image_data[:, :, :-1], axis=2) > 0

image_data[image_lightness] = 0

image_data = np.broadcast_to(image_data, image_data.shape[:-1] + (3,))
image_data = 255 - image_data

if np.all(image_data == 255):
show_canvas_warning()

return image_data
31 changes: 31 additions & 0 deletions drawing_to_fsd_layout/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
from pathlib import Path

import numpy as np

FloatArrayNx2 = np.typing.NDArray[np.float64]
IntArrayN = np.typing.NDArray[np.int64]
FloatArray2 = np.typing.NDArray[np.float64]


def find_github_link_of_repo() -> str:
"""
Find the github link of a repository by looking at the .git/config file
Args:
git_directory: The directory of the git repository
Returns:
The github link of the repository
"""

git_directory = Path(__file__).parent.parent

# The entire function was generated by github copilot
git_config = git_directory / ".git" / "config"
with open(git_config, "r") as f:
lines = f.readlines()

for i, line in enumerate(lines):
if "url = " in line:
url = line.split(" = ")[1].strip()
url = url.replace("git@", "https://")
url = url.replace(".git", "")
url = url.replace(".com:", ".com/")
return url

raise ValueError("Could not find the github link in the .git/config file")
18 changes: 0 additions & 18 deletions drawing_to_fsd_layout/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,3 @@ def cones_to_lyt(

bytes_to_write = _traces_to_lyt_bytes(cones_per_type, offset)
return bytes_to_write


# chrono json


def export_for_chrono_json_str(
cones_left: FloatArrayNx2,
cones_right: FloatArrayNx2,
) -> str:
all_left = cones_left.tolist()
all_right = cones_right.tolist()
return json.dumps(
{
"blue": all_left[1:],
"yellow": all_right[1:],
"orange_big": all_left[:1] + all_right[:1],
}
)
11 changes: 10 additions & 1 deletion drawing_to_fsd_layout/image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,17 @@ def extract_track_edges(
if len(best_clusters) == 2:
outer, inner = best_clusters
cc_outer, cc_inner = best_ccs
else:
elif len(best_clusters) == 4:
outer, _, inner, _ = best_clusters
cc_outer, _, cc_inner, _ = best_ccs
elif len(best_clusters) == 1:
st.error(
"There was an error extracting the two track edges. Have you drawn a closed track?"
)
st.stop()
elif len(best_clusters) == 0:
st.error("No track edges were found. Have you drawn a track?")
st.stop()

if show_steps:
plt.figure()
Expand All @@ -228,6 +236,7 @@ def extract_track_edges(

return outer_ordered, inner_ordered


@st.cache(show_spinner=False)
def fix_edges_orientation_and_scale_to_unit(
edge_a: FloatArrayNx2, edge_b: FloatArrayNx2
Expand Down
Binary file modified media/after.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4816af2

Please sign in to comment.