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

test_solutions.py 5.6 KB

You have to be logged in to leave a comment. Sign In
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. # Tests Ultralytics Solutions: https://docs.ultralytics.com/solutions/,
  3. # including every solution excluding DistanceCalculation and Security Alarm System.
  4. import cv2
  5. import pytest
  6. from tests import MODEL, TMP
  7. from ultralytics import solutions
  8. from ultralytics.utils import ASSETS_URL, IS_RASPBERRYPI, LINUX, checks
  9. from ultralytics.utils.downloads import safe_download
  10. # Pre-defined arguments values
  11. SHOW = False
  12. DEMO_VIDEO = "solutions_ci_demo.mp4" # for all the solutions, except workout, object cropping and parking management
  13. CROP_VIDEO = "decelera_landscape_min.mov" # for object cropping solution
  14. POSE_VIDEO = "solution_ci_pose_demo.mp4" # only for workouts monitoring solution
  15. PARKING_VIDEO = "solution_ci_parking_demo.mp4" # only for parking management solution
  16. PARKING_AREAS_JSON = "solution_ci_parking_areas.json" # only for parking management solution
  17. PARKING_MODEL = "solutions_ci_parking_model.pt" # only for parking management solution
  18. REGION = [(10, 200), (540, 200), (540, 180), (10, 180)] # for object counting, speed estimation and queue management
  19. # Test configs for each solution : (name, class, needs_frame_count, video, kwargs)
  20. SOLUTIONS = [
  21. (
  22. "ObjectCounter",
  23. solutions.ObjectCounter,
  24. False,
  25. DEMO_VIDEO,
  26. {"region": REGION, "model": MODEL, "show": SHOW},
  27. ),
  28. (
  29. "Heatmap",
  30. solutions.Heatmap,
  31. False,
  32. DEMO_VIDEO,
  33. {"colormap": cv2.COLORMAP_PARULA, "model": MODEL, "show": SHOW, "region": None},
  34. ),
  35. (
  36. "HeatmapWithRegion",
  37. solutions.Heatmap,
  38. False,
  39. DEMO_VIDEO,
  40. {"colormap": cv2.COLORMAP_PARULA, "region": REGION, "model": MODEL, "show": SHOW},
  41. ),
  42. (
  43. "SpeedEstimator",
  44. solutions.SpeedEstimator,
  45. False,
  46. DEMO_VIDEO,
  47. {"region": REGION, "model": MODEL, "show": SHOW},
  48. ),
  49. (
  50. "QueueManager",
  51. solutions.QueueManager,
  52. False,
  53. DEMO_VIDEO,
  54. {"region": REGION, "model": MODEL, "show": SHOW},
  55. ),
  56. (
  57. "LineAnalytics",
  58. solutions.Analytics,
  59. True,
  60. DEMO_VIDEO,
  61. {"analytics_type": "line", "model": MODEL, "show": SHOW},
  62. ),
  63. (
  64. "PieAnalytics",
  65. solutions.Analytics,
  66. True,
  67. DEMO_VIDEO,
  68. {"analytics_type": "pie", "model": MODEL, "show": SHOW},
  69. ),
  70. (
  71. "BarAnalytics",
  72. solutions.Analytics,
  73. True,
  74. DEMO_VIDEO,
  75. {"analytics_type": "bar", "model": MODEL, "show": SHOW},
  76. ),
  77. (
  78. "AreaAnalytics",
  79. solutions.Analytics,
  80. True,
  81. DEMO_VIDEO,
  82. {"analytics_type": "area", "model": MODEL, "show": SHOW},
  83. ),
  84. ("TrackZone", solutions.TrackZone, False, DEMO_VIDEO, {"region": REGION, "model": MODEL, "show": SHOW}),
  85. (
  86. "ObjectCropper",
  87. solutions.ObjectCropper,
  88. False,
  89. CROP_VIDEO,
  90. {"crop_dir": str(TMP / "cropped-detections"), "model": MODEL, "show": SHOW},
  91. ),
  92. (
  93. "ObjectBlurrer",
  94. solutions.ObjectBlurrer,
  95. False,
  96. DEMO_VIDEO,
  97. {"blur_ratio": 0.5, "model": MODEL, "show": SHOW},
  98. ),
  99. (
  100. "InstanceSegmentation",
  101. solutions.InstanceSegmentation,
  102. False,
  103. DEMO_VIDEO,
  104. {"model": "yolo11n-seg.pt", "show": SHOW},
  105. ),
  106. ("VisionEye", solutions.VisionEye, False, DEMO_VIDEO, {"model": MODEL, "show": SHOW}),
  107. (
  108. "RegionCounter",
  109. solutions.RegionCounter,
  110. False,
  111. DEMO_VIDEO,
  112. {"region": REGION, "model": MODEL, "show": SHOW},
  113. ),
  114. ("AIGym", solutions.AIGym, False, POSE_VIDEO, {"kpts": [6, 8, 10], "show": SHOW}),
  115. (
  116. "ParkingManager",
  117. solutions.ParkingManagement,
  118. False,
  119. PARKING_VIDEO,
  120. {"model": str(TMP / PARKING_MODEL), "show": SHOW, "json_file": str(TMP / PARKING_AREAS_JSON)},
  121. ),
  122. (
  123. "StreamlitInference",
  124. solutions.Inference,
  125. False,
  126. None, # streamlit application don't require video file
  127. {}, # streamlit application don't accept arguments
  128. ),
  129. ]
  130. def process_video(solution, video_path, needs_frame_count=False):
  131. """Process video with solution, feeding frames and optional frame count."""
  132. cap = cv2.VideoCapture(video_path)
  133. assert cap.isOpened(), f"Error reading video file {video_path}"
  134. frame_count = 0
  135. while cap.isOpened():
  136. success, im0 = cap.read()
  137. if not success:
  138. break
  139. frame_count += 1
  140. im_copy = im0.copy()
  141. args = [im_copy, frame_count] if needs_frame_count else [im_copy]
  142. _ = solution(*args)
  143. cap.release()
  144. @pytest.mark.skipif(
  145. (LINUX and checks.IS_PYTHON_3_11) or IS_RASPBERRYPI,
  146. reason="Disabled for testing due to --slow test errors after YOLOE PR.",
  147. )
  148. @pytest.mark.parametrize("name, solution_class, needs_frame_count, video, kwargs", SOLUTIONS)
  149. def test_solution(name, solution_class, needs_frame_count, video, kwargs):
  150. """Test individual Ultralytics solution."""
  151. if video:
  152. safe_download(url=f"{ASSETS_URL}/{video}", dir=TMP)
  153. if name == "ParkingManager":
  154. safe_download(url=f"{ASSETS_URL}/{PARKING_AREAS_JSON}", dir=TMP)
  155. safe_download(url=f"{ASSETS_URL}/{PARKING_MODEL}", dir=TMP)
  156. elif name == "StreamlitInference":
  157. if checks.check_imshow(): # do not merge with elif above
  158. solution_class(**kwargs).inference() # requires interactive GUI environment
  159. return
  160. process_video(
  161. solution=solution_class(**kwargs),
  162. video_path=str(TMP / video),
  163. needs_frame_count=needs_frame_count,
  164. )
Tip!

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

Comments

Loading...