Faraphel commited on
Commit
9b4dba2
·
1 Parent(s): 180f728

Initial commit

Browse files
Files changed (5) hide show
  1. .gitignore +2 -0
  2. README.md +2 -4
  3. app.py +74 -0
  4. environment.yml +11 -0
  5. runtime.txt +1 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .idea
2
+ runs
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: MarathonBibDetector
3
- emoji: 📚
4
  colorFrom: blue
5
  colorTo: red
6
  sdk: gradio
@@ -8,7 +8,5 @@ sdk_version: 6.3.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
- short_description: 'A detector for the bib wore by a marathon''s participants '
12
  ---
13
-
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: MarathonBibDetector
3
+ emoji: 🏃
4
  colorFrom: blue
5
  colorTo: red
6
  sdk: gradio
 
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
+ short_description: 'A detector for the bib wore by a marathon''s participants'
12
  ---
 
 
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ import gradio
4
+ import ultralytics
5
+ import cv2
6
+ import huggingface_hub
7
+
8
+
9
+ project_directory = Path(huggingface_hub.snapshot_download(repo_id="Faraphel/MarathonBibDetector"))
10
+ weights_directory = project_directory / "weights"
11
+
12
+
13
+
14
+ DETECTION_COLOR: tuple[int, int, int] = (64, 255, 64)
15
+
16
+ MODELS: dict[str, Path] = {
17
+ "nano": weights_directory / "bib-detector-nano.pt",
18
+ "medium": weights_directory / "bib-detector-medium.pt",
19
+ }
20
+ LOADED_MODELS: dict[str, ultralytics.YOLO] = {}
21
+
22
+
23
+ def get_model(model_type: str = "nano") -> ultralytics.YOLO:
24
+ """
25
+ Get a YOLO model instance from the given model type.
26
+ :param model_type: the model type to load
27
+ :return: the YOLO model instance
28
+ """
29
+
30
+ # check if the model type is valid
31
+ if model_type not in MODELS:
32
+ raise ValueError(f"Invalid model type: {model_type}")
33
+
34
+ # check if the model is already loaded
35
+ if model_type in LOADED_MODELS:
36
+ return LOADED_MODELS[model_type]
37
+
38
+ # otherwise load the model
39
+ LOADED_MODELS[model_type] = ultralytics.YOLO(MODELS[model_type])
40
+
41
+ return LOADED_MODELS[model_type]
42
+
43
+
44
+ def infer(image, model_type: str = "nano"):
45
+ model = get_model(model_type)
46
+
47
+ # run the model
48
+ results = model(image)[0]
49
+
50
+ # draw the detected bounding boxes
51
+ for box in results.boxes:
52
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
53
+ conf = float(box.conf[0])
54
+ cls = int(box.cls[0])
55
+ label = model.names[cls]
56
+
57
+ # draw the bounding box
58
+ cv2.rectangle(image, (x1, y1), (x2, y2), DETECTION_COLOR, 4)
59
+ # draw the annotation
60
+ cv2.putText(image, f"{label} {conf:.2f}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, DETECTION_COLOR, 2)
61
+
62
+ return image
63
+
64
+
65
+ application = gradio.Interface(
66
+ fn=infer,
67
+ inputs=[
68
+ gradio.Image(label="Image", type="numpy"),
69
+ gradio.Dropdown(label="Model Type", choices=list(MODELS.keys()), value="nano")
70
+ ],
71
+ outputs="image",
72
+ flagging_mode="never"
73
+ )
74
+ application.launch()
environment.yml ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: marathon-bib-detector
2
+ channels:
3
+ - conda-forge
4
+ dependencies:
5
+ - python=3.12
6
+ - pip
7
+ - pip:
8
+ - gradio>=5.49.1
9
+ - ultralytics>=8.4.0
10
+ - opencv-python-headless>=4.12.0
11
+ - huggingface_hub>=0.34.4
runtime.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ python-3.12