landingai.pipeline
The vision pipeline abstraction helps chain image processing operations as
sequence of steps. Each step consumes and produces a FrameSet
which typically
contains a source image and derivative metadata and images.
The vision pipeline abstraction helps chain image processing operations as sequence of steps. Each step consumes and produces a FrameSet
which typically contains a source image and derivative metadata and images.
Frame
Bases: BaseModel
A Frame stores a main image, its metadata and potentially other derived images.
Source code in landingai/pipeline/frameset.py
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
|
frames: List[Frame]
property
Returns a list with a single frame
image: Image.Image
instance-attribute
Main image generated typically at the beginning of a pipeline
metadata: Dict[str, Any] = {}
class-attribute
instance-attribute
An optional collection of metadata
other_images: Dict[str, Image.Image] = {}
class-attribute
instance-attribute
Other derivative images associated with this the main image. For example: FrameSet.overlay_predictions
will store the resulting image on `Frame.other_images["overlay"]
predictions: PredictionList = PredictionList([])
class-attribute
instance-attribute
List of predictions for the main image
adjust_brightness(factor)
Adjust the brightness of the image
Parameters
factor: The enhancement factor
adjust_color(factor)
Adjust the color of the image
Parameters
factor: The enhancement factor
adjust_contrast(factor)
Adjust the contrast of the image
Parameters
factor: The enhancement factor
adjust_sharpness(factor)
Adjust the sharpness of the image
Parameters
factor: The enhancement factor
crop(bbox)
Crop the image based on the bounding box
Parameters
bbox: A tuple with the bounding box coordinates (xmin, ymin, xmax, ymax)
Source code in landingai/pipeline/frameset.py
crop_predictions()
Crops from this frame regions with predictions and returns a FrameSet with the the cropped Frames
Source code in landingai/pipeline/frameset.py
downsize(width=None, height=None)
Resize only if the image is larger than the expected dimensions, Parameters
width: The requested width in pixels. height: The requested width in pixels.
Source code in landingai/pipeline/frameset.py
from_array(array, is_bgr=True)
classmethod
Creates a Frame from a image encode as ndarray
Parameters
array : np.ndarray Image is_bgr : bool, optional Assume OpenCV's BGR channel ordering? Defaults to True
Returns
Frame
Source code in landingai/pipeline/frameset.py
from_image(uri, metadata={})
classmethod
Creates a Frame from an image file
Parameters
uri : URI to file (local or remote)
Returns
Frame : New Frame enclosing the image
Source code in landingai/pipeline/frameset.py
resize(width=None, height=None)
Resizes the frame to the given dimensions. If width or height is missing the resize will preserve the aspect ratio. Parameters
width: The requested width in pixels. height: The requested width in pixels.
Source code in landingai/pipeline/frameset.py
run_predict(predictor, reuse_session=True, **kwargs)
Run a cloud inference model Parameters
predictor: the model to be invoked.
reuse_session
Whether to reuse the HTTPS session for sending multiple inference requests. By default, the session is reused to improve the performance on high latency networks (e.g. fewer SSL negotiations). If you are sending requests from multiple threads, set this to False.
kwargs: keyword arguments to forward to predictor
.
Source code in landingai/pipeline/frameset.py
save_image(path, format='png', *, include_predictions=False)
Save the image to path
Parameters
path: File path for the output image format: File format for the output image. Defaults to "png" include_predictions: If the image has predictions, should it be overlaid on top of the image?
Source code in landingai/pipeline/frameset.py
show_image(image_src='', clear_nb_cell=False, *, include_predictions=False)
Open a window and display all the images. Parameters
image_src (deprecated): if empty the source image will be displayed. Otherwise the image will be selected from other_images
include_predictions: If the image has predictions, should it be overlaid on top of the image?
Source code in landingai/pipeline/frameset.py
to_numpy_array(image_src='', *, include_predictions=False)
Return a numpy array using RGB channel ordering. If this array is passed to OpenCV, you will need to convert it to BGR
Parameters
image_src (deprecated): if empty the source image will be displayed. Otherwise the image will be selected from other_images
include_predictions: If the image has predictions, should it be overlaid on top of the image?
Source code in landingai/pipeline/frameset.py
FrameSet
Bases: BaseModel
A FrameSet is a collection of frames (in order). Typically a FrameSet will include a single image but there are circumstances where other images will be extracted from the initial one. For example: we may want to identify vehicles on an initial image and then extract sub-images for each of the vehicles.
Source code in landingai/pipeline/frameset.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 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 516 517 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 629 630 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 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
|
predictions: PredictionList
property
Returns the predictions from all the frames in the FrameSet
adjust_brightness(factor)
Adjust the brightness of the image
Parameters
factor: The enhancement factor
adjust_color(factor)
Adjust the color of the image
Parameters
factor: The enhancement factor
adjust_contrast(factor)
Adjust the contrast of the image
Parameters
factor: The enhancement factor
adjust_sharpness(factor)
Adjust the sharpness of the image
Parameters
factor: The enhancement factor
append(fr)
Add a Frame into this FrameSet
Parameters
fr : Frame Frame to be added at the end of the current one
Returns
FrameSet
Source code in landingai/pipeline/frameset.py
apply(function=lambda f: f)
Apply a function to all frames
Parameters
function: lambda function that takes individual frames and returned an updated frame
Source code in landingai/pipeline/frameset.py
copy(*args, **kwargs)
Returns a copy of this FrameSet, with all the frames copied
Source code in landingai/pipeline/frameset.py
crop(bbox)
Crop the images based on the bounding box
Parameters
bbox: A tuple with the bounding box coordinates (xmin, ymin, xmax, ymax)
Source code in landingai/pipeline/frameset.py
downsize(width=None, height=None)
Resize only if the image is larger than the expected dimensions, Parameters
width: The requested width in pixels. height: The requested width in pixels.
Source code in landingai/pipeline/frameset.py
extend(frs)
Add a all the Frames from frs
into this FrameSet
Parameters
frs : FrameSet Framerset to be added at the end of the current one
Returns
FrameSet
Source code in landingai/pipeline/frameset.py
filter(function=lambda f: True)
Evaluate a function on every frame and keep or remove
Parameters
function : lambda function that gets invoked on every Frame. If it returns False, the Frame will be deleted
Source code in landingai/pipeline/frameset.py
from_array(array, is_bgr=True)
classmethod
Creates a FrameSet from a image encode as ndarray
Parameters
array : np.ndarray Image is_bgr : bool, optional Assume OpenCV's BGR channel ordering? Defaults to True
Returns
FrameSet
Source code in landingai/pipeline/frameset.py
from_image(uri, metadata={})
classmethod
Creates a FrameSet from an image file
Parameters
uri : URI to file (local or remote)
Returns
FrameSet : New FrameSet containing a single image
Source code in landingai/pipeline/frameset.py
is_empty()
Check if the FrameSet is empty Returns
bool True if the are no Frames on the FrameSet
resize(width=None, height=None)
Returns a resized copy of this image. If width or height is missing the resize will preserve the aspect ratio Parameters
width: The requested width in pixels. height: The requested width in pixels.
Source code in landingai/pipeline/frameset.py
run_predict(predictor, num_workers=1)
Run a cloud inference model Parameters
predictor: the model to be invoked. num_workers: By default a single worker will request predictions sequentially. Parallel requests can help reduce the impact of fixed costs (e.g. network latency, transfer time, etc) but will consume more resources on the client and server side. The number of workers should typically be under 5. A large number of workers when using cloud inference will be rate limited and produce no improvement.
Source code in landingai/pipeline/frameset.py
save_image(filename_prefix, image_src='', format='png', *, include_predictions=False)
Save all the images on the FrameSet to disk (as PNG)
Parameters
filename_prefix : path and name prefix for the image file
image_src: (deprecated) if empty the source image will be saved. Otherwise the image will be selected from other_images
include_predictions: If the image has predictions, should it be overlaid on top of the image?
Source code in landingai/pipeline/frameset.py
save_video(video_file_path, video_fps=None, video_length_sec=None, image_src='', include_predictions=False)
Save the FrameSet as an mp4 video file. The following example, shows to use save_video to save a clip from a live RTSP source.
video_len_sec=10
fps=4
img_src = NetworkedCamera(stream_url, fps=fps)
frs = FrameSet()
for i,frame in enumerate(img_src):
if i>=video_len_sec*fps: # Limit capture time
break
frs.extend(frame)
frs.save_video("sample_images/test.mp4",video_fps=fps)
Parameters
video_file_path : str
Path and filename with extension of the video file
video_fps : Optional[int]
The number of frames per second for the output video file.
Either the video_fps
or video_length_sec
should be provided to assemble the video. if none of the two are provided, the method will try to set a "reasonable" value.
video_length_sec : Optional[float]
The total number of seconds for the output video file.
Either the video_fps
or video_length_sec
should be provided to assemble the video. if none of the two are provided, the method will try to set a "reasonable" value.
image_src : str, optional
if empty the source image will be used. Otherwise the image will be selected from other_images
Source code in landingai/pipeline/frameset.py
626 627 628 629 630 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 |
|
show_image(image_src='', clear_nb_cell=False, *, include_predictions=False)
Open a window and display all the images. Parameters
image_src (deprecated): if empty the source image will be displayed. Otherwise the image will be selected from other_images
include_predictions: If the image has predictions, should it be overlaid on top of the image?
Source code in landingai/pipeline/frameset.py
PredictionList
Bases: List[Union[ClassificationPrediction, OcrPrediction]]
A list of predictions from LandingLens, with some helper methods to filter and check prediction results.
This class inherits from list
, so it can be used as a list. For example, you can iterate over the predictions, or use len()
to get the number of predictions.
Some operations are overriten to make it easier to work with predictions. For example, you can use in
operator to check if a label is in the prediction list:
"label-that-exists" in frameset.predictions True "not-found-label" in frameset.predictions False
Source code in landingai/pipeline/frameset.py
filter_label(label)
Return a new PredictionList with only the predictions that have the specified label
Parameters
label: The label name to filter for Returns
PredictionList : A new instance of PredictionList containing only the filtered labels
Source code in landingai/pipeline/frameset.py
filter_threshold(min_score)
Return a new PredictionList with only the predictions that have a score greater than the threshold
Parameters
min_score: The threshold to filter predictions out
Returns
PredictionList : A new instance of PredictionList containing only predictions above min_score
Source code in landingai/pipeline/frameset.py
A module that provides a set of abstractions and APIs for reading images from different sources.
ImageFolder
Bases: ImageSourceBase
The ImageFolder
class is an image source that reads images from a folder path.
Example 1:
folder = ImageFolder("/home/user/images")
for image_batch in folder:
print(image_batch[0].image.size)
Example 2:
# Read all jpg files in the folder (including nested files)
folder = ImageFolder(glob_pattern="/home/user/images/**/*.jpg")
for image_batch in folder:
print(image_batch[0].image.size)
Source code in landingai/pipeline/image_source.py
image_paths: List[str]
property
Returns a list of image paths.
__init__(source=None, glob_pattern=None)
Constructor for ImageFolder.
Parameters
source
A list of file paths or the path to the folder path that contains the images.
A folder path can be either an absolute or relative folder path, in str
or Path
type. E.g. "/home/user/images".
If you provide a folder path, all the files directly within the folder will be read (including non-image files).
Nested files and sub-directories will be ignored.
Consider using glob_pattern
if you need to:
1. filter out unwanted files, e.g. your folder has both image and non-image files
2. read nested image files, e.g. /home/user/images/**/*.jpg
.
The ordering of images is based on the file name alphabetically if source is a folder path.
If source is a list of files, the order of the input files is preserved.
Currently only local file paths are supported.
glob_pattern
One or more python glob pattern(s) to grab only wanted files in the folder. E.g. "/home/user/images/*.jpg".
NOTE: If glob_pattern
is provided, the source
parameter is ignored.
For more information about glob pattern, see https://docs.python.org/3/library/glob.html
Source code in landingai/pipeline/image_source.py
ImageSourceBase
Bases: Iterator
The base class for all image sources.
Source code in landingai/pipeline/image_source.py
NetworkedCamera
Bases: BaseModel
, ImageSourceBase
The NetworkCamera class can connect to RTSP and other live video sources in order to grab frames. The main concern is to be able to consume frames at the source speed and drop them as needed to ensure the application allday gets the lastes frame
Source code in landingai/pipeline/image_source.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
|
__init__(stream_url, motion_detection_threshold=0, capture_interval=None, fps=None)
Parameters
stream_url : url to video source, or a number indicating the webcam index motion_detection_threshold : If set to zero then motion detections is disabled. Any other value (0-100) will make the camera drop all images that don't have significant changes capture_interval : Number of seconds to wait in between frames. If set to None, the NetworkedCamera will acquire images as fast as the source permits. fps: Capture speed in frames per second. If set to None, the NetworkedCamera will acquire images as fast as the source permits.
Source code in landingai/pipeline/image_source.py
get_latest_frame()
Return the most up to date frame by dropping all by the latest frame. This function is blocking
Source code in landingai/pipeline/image_source.py
Screenshot
Bases: ImageSourceBase
Take a screenshot from the screen as an image source.
The screenshot will be taken at each iteration when looping over this object. For example:
for frameset in Screenshot():
# `frameset` will contain a single frame with the screenshot here
time.sleep(1)
Source code in landingai/pipeline/image_source.py
VideoFile
Bases: ImageSourceBase
The VideoFile
class is an image source that samples frames from a video file.
Example:
import landingai.pipeline as pl
img_src = pl.image_source.VideoFile("sample_images/surfers.mp4", samples_per_second=1)
frs = pl.FrameSet()
for i,frame in enumerate(img_src):
if i>=3: # Fetch only 3 frames
break
frs.extend(
frame.run_predict(predictor=surfer_model)
.overlay_predictions()
)
print(pl.postprocessing.get_class_counts(frs))
Source code in landingai/pipeline/image_source.py
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
__init__(uri, samples_per_second=1)
Constructor for VideoFile.
Parameters
uri : str URI to the video file. This could be a local file or a URL that serves the video file in bytes. samples_per_second : float, optional The number of images to sample per second (by default 1). If set to zero, it disables sampling
Source code in landingai/pipeline/image_source.py
properties()
Return properties of the of the source file and the resulting FrameSet
Returns
Tuple[float, int, float, int] Properties: 0. Source file FPS (frames per second) 1. Source file total number of frames 2. Resulting FPS after applying sampling rate 3. Number of frames after applying sampling rate
Source code in landingai/pipeline/image_source.py
Webcam
Bases: NetworkedCamera
The Webcam class can connect to a local webcam in order to iterate over captured frames.
This leverages the NetworkedCamera implementations, with the constructor
receiving the webcam ID to be sent to OpenCV (instead of a stream URL).
Note that it doesn't work with Collab or remote Jupyter notebooks (yet). In this case,
use landingai.image_source_ops.take_photo_from_webcam()
instead.
Source code in landingai/pipeline/image_source.py
get_class_counts(frs, add_id_to_classname=False)
This method returns the number of occurrences of each detected class in the FrameSet.
Parameters
add_id_to_classname : bool, optional By default, detections with the same class names and different defect id will be counted as the same. Set to True if you want to count them separately
Returns
Dict[str, int] A dictionary with the counts