achouffe commited on
Commit
7b4f324
·
verified ·
1 Parent(s): f6ad7e6

feat: add other video examples

Browse files
app.py CHANGED
@@ -1,10 +1,11 @@
1
  """
2
- Gradio app to showcase the pyronear model for early forest fire detection.
3
  """
4
 
5
  from collections import Counter
6
  from pathlib import Path
7
  from typing import Any, Tuple
 
8
 
9
  import gradio as gr
10
  import numpy as np
@@ -18,6 +19,18 @@ def bgr_to_rgb(a: np.ndarray) -> np.ndarray:
18
  """
19
  return a[:, :, ::-1]
20
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def analyze_predictions(yolo_predictions) -> dict[str, Any]:
23
  """
@@ -43,14 +56,14 @@ def analyze_predictions(yolo_predictions) -> dict[str, Any]:
43
  names = yolo_predictions[0].names
44
  ids = set()
45
  for prediction in yolo_predictions:
46
- if prediction.boxes.id:
47
  for id in prediction.boxes.id.numpy().astype("int"):
48
  ids.add(id.item())
49
  detected_species = {}
50
  for id in ids:
51
  counter = Counter()
52
  for prediction in yolo_predictions:
53
- if prediction.boxes.id:
54
  for idd, klass in zip(
55
  prediction.boxes.id.numpy().astype("int"),
56
  prediction.boxes.cls.numpy().astype("int"),
@@ -88,7 +101,7 @@ def prediction_to_str(yolo_predictions) -> str:
88
  return f"Detected {len(ids)} salmons in the video clip with ids {ids}:\n{summary_str}"
89
 
90
 
91
- def predict(model: YOLO, video_filepath: Path) -> Tuple[Path, str]:
92
  """
93
  Main interface function that runs the model on the provided pil_image and
94
  returns the exepected tuple to populate the gradio interface.
@@ -161,7 +174,7 @@ with gr.Blocks() as demo:
161
  )
162
  output_raw = gr.Text(label="raw prediction")
163
 
164
- fn = lambda video_filepath: predict(
165
  model=model, video_filepath=Path(video_filepath)
166
  )
167
  gr.Interface(
 
1
  """
2
+ Gradio app to showcase the pyronear model for salmon vision.
3
  """
4
 
5
  from collections import Counter
6
  from pathlib import Path
7
  from typing import Any, Tuple
8
+ import torch
9
 
10
  import gradio as gr
11
  import numpy as np
 
19
  """
20
  return a[:, :, ::-1]
21
 
22
+ def has_values(maybe_tensor: torch.Tensor | None) -> bool:
23
+ """
24
+ Check whether the `maybe_tensor` contains items.
25
+ """
26
+ if maybe_tensor is None:
27
+ return False
28
+ elif isinstance(maybe_tensor, torch.Tensor):
29
+ if len(maybe_tensor) == 0:
30
+ return False
31
+ else:
32
+ return True
33
+
34
 
35
  def analyze_predictions(yolo_predictions) -> dict[str, Any]:
36
  """
 
56
  names = yolo_predictions[0].names
57
  ids = set()
58
  for prediction in yolo_predictions:
59
+ if has_values(prediction.boxes.id):
60
  for id in prediction.boxes.id.numpy().astype("int"):
61
  ids.add(id.item())
62
  detected_species = {}
63
  for id in ids:
64
  counter = Counter()
65
  for prediction in yolo_predictions:
66
+ if has_values(prediction.boxes.id):
67
  for idd, klass in zip(
68
  prediction.boxes.id.numpy().astype("int"),
69
  prediction.boxes.cls.numpy().astype("int"),
 
101
  return f"Detected {len(ids)} salmons in the video clip with ids {ids}:\n{summary_str}"
102
 
103
 
104
+ def interface_fn(model: YOLO, video_filepath: Path) -> Tuple[Path, str]:
105
  """
106
  Main interface function that runs the model on the provided pil_image and
107
  returns the exepected tuple to populate the gradio interface.
 
174
  )
175
  output_raw = gr.Text(label="raw prediction")
176
 
177
+ fn = lambda video_filepath: interface_fn(
178
  model=model, video_filepath=Path(video_filepath)
179
  )
180
  gr.Interface(
data/videos/{video1-clip.mp4 → video1-clip-fps-20.mp4} RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:3fb22eeeb3be60b9cb2a65d54a2d6c5379e9e65c47d7f2ce88eab0986f069167
3
- size 2966504
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:be2171afaba6acd9acd68138aa6d02334b5f2e711103383dcc984d05dd87232b
3
+ size 1753568
data/videos/video2-clip-fps-20.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bcc2bf599356da9d9db57f05d17f72f2366e0c7f63c0bc562fe5a34a7ac757c6
3
+ size 1078207
data/videos/video3-clip-fps-20.mp4 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:808290484ba8088aae55b4af4faf48f45b6efe9524b2d0b3c85d26d6a95ad59b
3
+ size 3196588