Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#970 Update YoloNASQuickstart.md

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:bugfix/SG-000_fix_readme_yolonas_snippets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  1. import cv2
  2. import numpy as np
  3. import time
  4. from typing import Callable, Optional
  5. __all__ = ["WebcamStreaming"]
  6. class WebcamStreaming:
  7. """Stream video from a webcam. Press 'q' to quit the streaming.
  8. :param window_name: Name of the window to display the video stream.
  9. :param frame_processing_fn: Function to apply to each frame before displaying it.
  10. If None, frames are displayed as is.
  11. :param capture: ID of the video capture device to use.
  12. Default is cv2.CAP_ANY (which selects the first available device).
  13. :param fps_update_frequency: Minimum time (in seconds) between updates to the FPS counter.
  14. If None, the counter is updated every frame.
  15. """
  16. def __init__(
  17. self,
  18. window_name: str = "",
  19. frame_processing_fn: Optional[Callable[[np.ndarray], np.ndarray]] = None,
  20. capture: int = cv2.CAP_ANY,
  21. fps_update_frequency: Optional[float] = None,
  22. ):
  23. self.window_name = window_name
  24. self.frame_processing_fn = frame_processing_fn
  25. self.cap = cv2.VideoCapture(capture)
  26. if not self.cap.isOpened():
  27. raise ValueError("Could not open video capture device")
  28. self._fps_counter = FPSCounter(update_frequency=fps_update_frequency)
  29. def run(self) -> None:
  30. """Start streaming video from the webcam and displaying it in a window.
  31. Press 'q' to quit the streaming.
  32. """
  33. while not self._stop():
  34. self._display_single_frame()
  35. def _display_single_frame(self) -> None:
  36. """Read a single frame from the video capture device, apply any specified frame processing,
  37. and display the resulting frame in the window.
  38. Also updates the FPS counter and displays it in the frame.
  39. """
  40. _ret, frame = self.cap.read()
  41. if self.frame_processing_fn:
  42. frame = self.frame_processing_fn(frame)
  43. _write_fps_to_frame(frame, self.fps)
  44. cv2.imshow(self.window_name, frame)
  45. def _stop(self) -> bool:
  46. """Stopping condition for the streaming."""
  47. return cv2.waitKey(1) & 0xFF == ord("q")
  48. @property
  49. def fps(self) -> float:
  50. return self._fps_counter.fps
  51. def __del__(self):
  52. """Release the video capture device and close the window."""
  53. self.cap.release()
  54. cv2.destroyAllWindows()
  55. def _write_fps_to_frame(frame: np.ndarray, fps: float) -> None:
  56. """Write the current FPS value on the given frame.
  57. :param frame: Frame to write the FPS value on.
  58. :param fps: Current FPS value to write.
  59. """
  60. font = cv2.FONT_HERSHEY_SIMPLEX
  61. font_scale = 0.6
  62. font_color = (0, 255, 0)
  63. line_type = 2
  64. cv2.putText(frame, "FPS: {:.2f}".format(fps), (10, 30), font, font_scale, font_color, line_type)
  65. class FPSCounter:
  66. """Class for calculating the FPS of a video stream."""
  67. def __init__(self, update_frequency: Optional[float] = None):
  68. """Create a new FPSCounter object.
  69. :param update_frequency: Minimum time (in seconds) between updates to the FPS counter.
  70. If None, the counter is updated every frame.
  71. """
  72. self._update_frequency = update_frequency
  73. self._start_time = time.time()
  74. self._frame_count = 0
  75. self._fps = 0.0
  76. def _update_fps(self, elapsed_time, current_time) -> None:
  77. """Compute new value of FPS and reset the counter."""
  78. self._fps = self._frame_count / elapsed_time
  79. self._start_time = current_time
  80. self._frame_count = 0
  81. @property
  82. def fps(self) -> float:
  83. """Current FPS value."""
  84. self._frame_count += 1
  85. current_time, elapsed_time = time.time(), time.time() - self._start_time
  86. if self._update_frequency is None or elapsed_time > self._update_frequency:
  87. self._update_fps(elapsed_time=elapsed_time, current_time=current_time)
  88. return self._fps
Discard
Tip!

Press p or to see the previous file or, n or to see the next file