Image Operations
After acquiring your images as Frame
objects in the previous section, you can perform a variety of operations on them.
These operations are useful for pre-processing the images before running inferences on them, especially if your model was trained with images acquired in slightly different conditions from what you have when running inference.
For example, if your train dataset images were taken in a room with more light, you might want to adjust the brightness and contrast before running inference on your frames.
Below, you can see the list of useful operations that can be performed on a Frame
object.
Cropping
Cropping is the operation of extracting regions of interest from an image.
from landingai.pipeline.frameset import Frame
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.crop((16, 16, 112, 112)) # (1)!
frame.save_image("docs/images/cereal-ops/cereal-cropped.jpeg")
(16, 16, 112, 112)
is the bounding-box coordinates to crop, meaning that the cropped image will start at(16, 16)
and end at(112, 112)
.
Crop predictions
Appart from cropping arbitrary regions of a Frame
, you can also automatically crop detected objects from a Frame.
from landingai.pipeline.frameset import Frame
from landingai.predict import Predictor
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
predictor = Predictor( # (1)!
endpoint_id=ENDPOINT_ID,
api_key=API_KEY,
)
frame.run_predict(predictor) # (2)!
for i, cropped_frame in enumerate(frame.crop_predictions()): # (3)!
cropped_frame.resize(width=64) # (4)!
cropped_frame.save_image(f"docs/images/cereal-ops/cereal-crop-predictions-{i}.png") # (5)!
- Creates a predictor to run inference using a LandingLens model. See the Running Inferences section for more details on how to build and deploy a computer vision model.
- Runs inference on the frame.
- Iterate over the objects detected in the image. Each iteration will yield a new
Frame
object with just the detected object in it. - Resizes the cropped frame to 64 pixels width, keeping original aspect ratio.
- Save the cropped frame to a file.
For more details about running inferences, see the Running Inferences section.
Resizing
You can resize your image to any aspect ratio by passing both width and height:
from landingai.pipeline.frameset import Frame
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.resize(width=64, height=128)
frame.save_image("docs/images/cereal-ops/cereal-resized-both.jpeg")
You can also keep the original aspect ratio by passing only one of the dimensions:
from landingai.pipeline.frameset import Frame
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.resize(width=64)
frame.save_image("docs/images/cereal-ops/cereal-resized-width.jpeg")
Resizing images before running inference can speed up the inference process. But don't downscale too much, or you might lose important image details.
Color
You can adjust color intensity:
- No change: 1.0
- Less intensity: Less than 1.0
- More intensity: Greater than 1.0
See the following examples.
from landingai.pipeline.frameset import Frame
# Adjust color to 0.1
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_color(0.1)
frame.save_image("docs/images/cereal-ops/cereal-color-0.1.jpeg")
# Adjust color to 2.0
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_color(2.0)
frame.save_image("docs/images/cereal-ops/cereal-color-2.0.jpeg")
Contrast
You can adjust the contrast intensity:
- No change: 1.0
- Less intensity: Less than 1.0
- More intensity: Greater than 1.0
See the following examples.
from landingai.pipeline.frameset import Frame
# Adjust contrast to 0.1
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_contrast(0.1)
frame.save_image("docs/images/cereal-ops/cereal-contrast-0.1.jpeg")
# Adjust contrast to 2.0
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_contrast(2.0)
frame.save_image("docs/images/cereal-ops/cereal-contrast-2.0.jpeg")
Brightness
You can adjust the brightness intensity:
- No change: 1.0
- Less intensity: Less than 1.0
- More intensity: Greater than 1.0
See the following examples.
from landingai.pipeline.frameset import Frame
# Adjust brightness to 0.1
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_brightness(0.1)
frame.save_image("docs/images/cereal-ops/cereal-brightness-0.1.jpeg")
# Adjust brightness to 2.0
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_brightness(2.0)
frame.save_image("docs/images/cereal-ops/cereal-brightness-2.0.jpeg")
Sharpness
You can adjust the sharpness intensity:
- No change: 1.0
- Less intensity: Less than 1.0
- More intensity: Greater than 1.0
See the following examples.
from landingai.pipeline.frameset import Frame
# Adjust sharpness to 0.1
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_sharpness(0.1)
frame.save_image("docs/images/cereal-ops/cereal-sharpness-0.1.jpeg")
# Adjust sharpness to 2.0
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_sharpness(2.0)
frame.save_image("docs/images/cereal-ops/cereal-sharpness-2.0.jpeg")