计算机视觉工具 (CV Utils)¶
sahi.utils.cv
¶
Computer vision utilities for image processing and visualization.
Classes¶
Colors
¶
Color palette for visualization.
Source code in sahi/utils/cv.py
Methods:¶
__call__(ind, bgr=False)
¶
Convert an index to a color code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ind
¶ |
int
|
The index to convert. |
required |
bgr
¶ |
bool
|
Whether to return the color code in BGR format. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[int, int, int]
|
The color code in RGB or BGR format, depending on the value of |
Source code in sahi/utils/cv.py
__init__()
¶
Initialize the color palette from hex color codes.
Source code in sahi/utils/cv.py
hex_to_rgb(hex_code)
staticmethod
¶
Converts a hexadecimal color code to RGB format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hex_code
¶ |
str
|
The hexadecimal color code to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[int, int, int]
|
A tuple representing the RGB values in the order (R, G, B). |
Source code in sahi/utils/cv.py
Functions:¶
apply_color_mask(image, color)
¶
Applies color mask to given input image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
The input image to apply the color mask to. |
required |
|
tuple
|
The RGB color tuple to use for the mask. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The resulting image with the applied color mask. |
Source code in sahi/utils/cv.py
convert_image_to(read_path, extension='jpg', grayscale=False)
¶
Reads an image from the given path and saves it with the specified extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The path to the image file. |
required |
|
str
|
The desired file extension for the saved image. Defaults to "jpg". |
'jpg'
|
|
bool
|
Whether to convert the image to grayscale. Defaults to False. |
False
|
Source code in sahi/utils/cv.py
crop_object_predictions(image, object_prediction_list, output_dir='', file_name='prediction_visual', export_format='png')
¶
Crops bounding boxes over the source image and exports it to the output folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
The source image to crop bounding boxes from. |
required |
|
list
|
A list of object predictions. |
required |
|
str
|
The directory where the resulting visualizations will be exported. Defaults to an empty string. |
''
|
|
str
|
The name of the exported file. The exported file will be saved as |
'prediction_visual'
|
|
str
|
The format of the exported file. Can be specified as 'jpg' or 'png'. Defaults to "png". |
'png'
|
Source code in sahi/utils/cv.py
get_bbox_from_bool_mask(bool_mask)
¶
Generate VOC bounding box [xmin, ymin, xmax, ymax] from given boolean mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
2D boolean mask. |
required |
Returns:
| Type | Description |
|---|---|
list[int] | None
|
Optional[List[int]]: VOC bounding box [xmin, ymin, xmax, ymax] or None if no bounding box is found. |
Source code in sahi/utils/cv.py
get_bbox_from_coco_segmentation(coco_segmentation)
¶
Generate voc box ([xmin, ymin, xmax, ymax]) from given coco segmentation.
Source code in sahi/utils/cv.py
get_bool_mask_from_coco_segmentation(coco_segmentation, width, height)
¶
Convert COCO segmentation to a 2D boolean mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[list[float]]
|
List of polygons representing the COCO segmentation. |
required |
|
int
|
Width of the boolean mask. |
required |
|
int
|
Height of the boolean mask. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
2D boolean mask of size (height, width). |
Source code in sahi/utils/cv.py
get_coco_segmentation_from_bool_mask(bool_mask)
¶
Convert boolean mask to COCO segmentation format.
Converts a 2D boolean mask to COCO polygon format: [ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ].
Source code in sahi/utils/cv.py
get_coco_segmentation_from_obb_points(obb_points)
¶
Convert OBB (Oriented Bounding Box) points to COCO polygon format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
np.ndarray OBB points tensor from ultralytics.engine.results.OBB Shape: (4, 2) containing 4 points with (x,y) coordinates each |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
List[List[float]]: Polygon points in COCO format [[x1, y1, x2, y2, x3, y3, x4, y4], [...], ...] |
Source code in sahi/utils/cv.py
get_video_reader(source, save_dir, frame_skip_interval, export_visual=False, view_visual=False)
¶
Creates OpenCV video capture object from given video file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Video file path |
required |
|
str
|
Video export directory |
required |
|
int
|
Frame skip interval |
required |
|
bool
|
Set True if you want to export visuals |
False
|
|
bool
|
Set True if you want to render visual |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
iterator |
Generator[Image]
|
Pillow Image |
video_writer |
VideoWriter | None
|
cv2.VideoWriter |
video_file_name |
str
|
video name with extension |
Source code in sahi/utils/cv.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
ipython_display(image)
¶
Displays numpy image in notebook.
If input image is in range 0..1, please first multiply img by 255 Assumes image is ndarray of shape [height, width, channels] where channels can be 1, 3 or 4
Source code in sahi/utils/cv.py
normalize_numpy_image(image)
¶
read_image(image_path)
¶
Loads image as a numpy array from the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The path to the image file. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy.ndarray: The loaded image as a numpy array. |
Source code in sahi/utils/cv.py
read_image_as_pil(image, exif_fix=True, return_arr=False)
¶
Loads an image as PIL.Image.Image (or np.ndarray when return_arr=True).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Union[Image, str, PathLike, ndarray]
|
The image to be loaded. It can be an image path (str or path-like) or URL (str), a numpy image (np.ndarray), or a PIL.Image object. |
required |
|
bool
|
Whether to apply the EXIF orientation to the image. Defaults to True. |
True
|
|
bool
|
When True, return an HWC RGB ndarray. Local paths decode straight to one; other inputs convert from PIL before returning. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Image | ndarray
|
PIL.Image.Image | np.ndarray: The loaded image. |
Source code in sahi/utils/cv.py
read_image_size(image, exif_fix=True)
¶
Return the (width, height) read_image_as_pil would produce, without decoding it.
For a local path only the header is read. Decoding a gigapixel scan just to ask for its dimensions costs gigabytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Image | str | PathLike | ndarray
|
The image to size. An image path or URL (str), a numpy image (np.ndarray), or a PIL.Image object. |
required |
|
bool
|
Whether the caller will apply the EXIF orientation, as |
True
|
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
The image size as (width, height). |
Example
from sahi.utils.cv import read_image_size read_image_size("tests/data/small-vehicles1.jpeg") (1068, 580)
Source code in sahi/utils/cv.py
read_large_image(image_path)
¶
Reads a large image from the specified image path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The path to the image file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[ndarray, bool]
|
A tuple containing the image data and a flag indicating whether cv2 was used to read the image. The image data is a numpy array representing the image in RGB format. The flag is True if cv2 was used, False otherwise. |
Source code in sahi/utils/cv.py
select_random_color()
¶
Selects a random color from a predefined list of colors.
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[int]
|
A list representing the RGB values of the selected color. |
Source code in sahi/utils/cv.py
visualize_object_predictions(image, object_prediction_list, rect_th=None, text_size=None, text_th=None, color=None, hide_labels=False, hide_conf=False, output_dir=None, file_name='prediction_visual', export_format='png')
¶
Visualize object predictions with bounding boxes and category names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
Input image as numpy array. |
required |
|
list
|
List of prediction.ObjectPrediction instances. |
required |
|
int | None
|
rectangle thickness |
None
|
|
float | None
|
size of the category name over box |
None
|
|
int | None
|
text thickness |
None
|
|
tuple | None
|
annotation color in the form: (0, 255, 0) |
None
|
|
bool
|
hide labels |
False
|
|
bool
|
hide confidence |
False
|
|
str | None
|
directory for resulting visualization to be exported |
None
|
|
str | None
|
exported file will be saved as: output_dir+file_name+".png" |
'prediction_visual'
|
|
str | None
|
can be specified as 'jpg' or 'png' |
'png'
|
Source code in sahi/utils/cv.py
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 | |
visualize_prediction(image, boxes, classes, masks=None, rect_th=None, text_size=None, text_th=None, color=None, hide_labels=False, output_dir=None, file_name='prediction_visual')
¶
Visualizes prediction classes, bounding boxes over the source image and exports it to output folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ndarray
|
The source image. |
required |
|
List[List]
|
List of bounding boxes coordinates. |
required |
|
List[str]
|
List of class labels corresponding to each bounding box. |
required |
|
Optional[List[ndarray]]
|
List of masks corresponding to each bounding box. Defaults to None. |
None
|
|
int
|
Thickness of the bounding box rectangle. Defaults to None. |
None
|
|
float
|
Size of the text for class labels. Defaults to None. |
None
|
|
int
|
Thickness of the text for class labels. Defaults to None. |
None
|
|
tuple
|
Color of the bounding box and text. Defaults to None. |
None
|
|
bool
|
Whether to hide the class labels. Defaults to False. |
False
|
|
Optional[str]
|
Output directory to save the visualization. Defaults to None. |
None
|
|
Optional[str]
|
File name for the saved visualization. Defaults to "prediction_visual". |
'prediction_visual'
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary containing the visualized image and the elapsed time for the visualization process. |
Source code in sahi/utils/cv.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
yolo_bbox_to_voc_bbox(yolo_bbox, image_width, image_height)
¶
Convert YOLO format bounding box to VOC format.
Converts normalized YOLO format [x_center, y_center, width, height] to absolute VOC format [xmin, ymin, xmax, ymax] pixel coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[float]
|
list of [x_center, y_center, width, height] |
required |
|
int
|
width of the image |
required |
|
int
|
height of the image |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
list of [xmin, ymin, xmax, ymax] |