Skip to content

Commit 1bfcb81

Browse files
committed
returning of image
1 parent 6834c6e commit 1bfcb81

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ Visualizes custom results of object detection or segmentation on an image.
150150
- **show_confidences** (*bool*): If true and show_class=True, confidences near class are visualized. Default is False.
151151
- **axis_off** (*bool*): If true, axis is turned off in the final visualization. Default is True.
152152
- **show_classes_list** (*list*): If empty, visualize all classes. Otherwise, visualize only classes in the list.
153-
153+
- **return_image_array** (*bool*): If True, the function returns the image (BGR np.array) instead of displaying it.
154+
Default is False.
154155

155156

156157
Example of using:

patched_yolo_infer/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ Visualizes custom results of object detection or segmentation on an image.
130130
- **show_confidences** (*bool*): If true and show_class=True, confidences near class are visualized. Default is False.
131131
- **axis_off** (*bool*): If true, axis is turned off in the final visualization. Default is True.
132132
- **show_classes_list** (*list*): If empty, visualize all classes. Otherwise, visualize only classes in the list.
133-
133+
- **return_image_array** (*bool*): If True, the function returns the image (BGR np.array) instead of displaying it.
134+
Default is False.
134135

135136

136137
Example of using:

patched_yolo_infer/functions_extra.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def visualize_results_usual_yolo_inference(
2626
random_object_colors=False,
2727
show_confidences=False,
2828
axis_off=True,
29-
show_classes_list=[]
29+
show_classes_list=[],
30+
return_image_array=False
3031
):
3132
"""
3233
Visualizes the results of usual YOLOv8 or YOLOv8-seg inference on an image
@@ -53,6 +54,8 @@ def visualize_results_usual_yolo_inference(
5354
show_confidences (bool): If True and show_class=True, confidences near class are visualized.
5455
axis_off (bool): If True, axis is turned off in the final visualization.
5556
show_classes_list (list): If empty, visualize all classes. Otherwise, visualize only classes in the list.
57+
return_image_array (bool): If True, the function returns the image bgr array instead of displaying it.
58+
Default is False.
5659
5760
Returns:
5861
None
@@ -153,6 +156,9 @@ def visualize_results_usual_yolo_inference(
153156
thickness=thickness,
154157
)
155158

159+
if return_image_array:
160+
return labeled_image
161+
else:
156162
# Display the final image with overlaid masks and labels
157163
plt.figure(figsize=(8, 8), dpi=dpi)
158164
labeled_image = cv2.cvtColor(labeled_image, cv2.COLOR_BGR2RGB)
@@ -273,7 +279,8 @@ def visualize_results(
273279
random_object_colors=False,
274280
show_confidences=False,
275281
axis_off=True,
276-
show_classes_list=[]
282+
show_classes_list=[],
283+
return_image_array=False
277284
):
278285
"""
279286
Visualizes custom results of object detection or segmentation on an image.
@@ -301,7 +308,9 @@ def visualize_results(
301308
show_confidences (bool): If true and show_class=True, confidences near class are visualized. Default is False.
302309
axis_off (bool): If true, axis is turned off in the final visualization. Default is True.
303310
show_classes_list (list): If empty, visualize all classes. Otherwise, visualize only classes in the list.
304-
311+
return_image_array (bool): If True, the function returns the image bgr array instead of displaying it.
312+
Default is False.
313+
305314
Returns:
306315
None
307316
"""
@@ -378,10 +387,13 @@ def visualize_results(
378387
thickness=thickness,
379388
)
380389

381-
# Display the final image with overlaid masks and labels
382-
plt.figure(figsize=(8, 8), dpi=dpi)
383-
labeled_image = cv2.cvtColor(labeled_image, cv2.COLOR_BGR2RGB)
384-
plt.imshow(labeled_image)
385-
if axis_off:
386-
plt.axis('off')
387-
plt.show()
390+
if return_image_array:
391+
return labeled_image
392+
else:
393+
# Display the final image with overlaid masks and labels
394+
plt.figure(figsize=(8, 8), dpi=dpi)
395+
labeled_image = cv2.cvtColor(labeled_image, cv2.COLOR_BGR2RGB)
396+
plt.imshow(labeled_image)
397+
if axis_off:
398+
plt.axis('off')
399+
plt.show()

0 commit comments

Comments
 (0)