detecto.visualize

detecto.visualize.detect_live(model, score_filter=0.6)

Displays in a window the given model’s predictions on the current computer’s live webcam feed. To stop the webcam, press ‘q’ or the ESC key. Note that if the given model is not running on a GPU, the webcam framerate could very well be under 1 FPS. Also note that you should not call this function on Google Colab or other services running on virtual machines as they may not have access to the webcam.

Parameters:
  • model (detecto.core.Model) – The trained model with which to run object detection.
  • score_filter (float) – (Optional) Minimum score required to show a prediction. Defaults to 0.6.

Example:

>>> from detecto.core import Model
>>> from detecto.visualize import detect_live

>>> model = Model()
>>> detect_live(model, score_filter=0.7)
detecto.visualize.detect_video(model, input_file, output_file, fps=30, score_filter=0.6)

Takes in a video and produces an output video with object detection run on it (i.e. displays boxes around detected objects in real-time). Output videos should have the .avi file extension. Note: some apps, such as macOS’s QuickTime Player, have difficulty viewing these output videos. It’s recommended that you download and use VLC if this occurs.

Parameters:
  • model (detecto.core.Model) – The trained model with which to run object detection.
  • input_file (str) – The path to the input video.
  • output_file (str) – The name of the output file. Should have a .avi file extension.
  • fps (int) – (Optional) Frames per second of the output video. Defaults to 30.
  • score_filter (float) – (Optional) Minimum score required to show a prediction. Defaults to 0.6.

Example:

>>> from detecto.core import Model
>>> from detecto.visualize import detect_video

>>> model = Model.load('model_weights.pth', ['tick', 'gate'])
>>> detect_video(model, 'input_vid.mp4', 'output_vid.avi', score_filter=0.7)
detecto.visualize.plot_prediction_grid(model, images, dim=None, figsize=None, score_filter=0.6)

Plots a grid of images with boxes drawn around predicted objects.

Parameters:
  • model (detecto.core.Model) – The trained model with which to run object detection.
  • images (iterable) – An iterable of images to plot. If the images are normalized torch.Tensor images, they will automatically be reverse-normalized and converted to PIL images for plotting.
  • dim (tuple or None) – (Optional) The dimensions of the grid in the format (rows, cols). If no value is given, the grid is of the shape (len(images), 1). rows * cols must match the number of given images, or a ValueError is raised. Defaults to None.
  • figsize (tuple or None) – (Optional) The size of the entire grid in the format (width, height). Defaults to None.
  • score_filter (float) – (Optional) Minimum score required to show a prediction. Defaults to 0.6.

Example:

>>> from detecto.core import Model
>>> from detecto.utils import read_image
>>> from detecto.visualize import plot_prediction_grid

>>> model = Model.load('model_weights.pth', ['tick', 'gate'])
>>> images = []
>>> for i in range(4):
>>>     image = read_image('image{}.jpg'.format(i))
>>>     images.append(image)
>>> plot_prediction_grid(model, images, dim=(2, 2), figsize=(8, 8))
detecto.visualize.show_labeled_image(image, boxes, labels=None)

Show the image along with the specified boxes around detected objects. Also displays each box’s label if a list of labels is provided.

Parameters:
  • image (numpy.ndarray or torch.Tensor) – The image to plot. If the image is a normalized torch.Tensor object, it will automatically be reverse-normalized and converted to a PIL image for plotting.
  • boxes (torch.Tensor) – A torch tensor of size (N, 4) where N is the number of boxes to plot, or simply size 4 if N is 1.
  • labels (torch.Tensor or None) – (Optional) A list of size N giving the labels of each box (labels[i] corresponds to boxes[i]). Defaults to None.

Example:

>>> from detecto.core import Model
>>> from detecto.utils import read_image
>>> from detecto.visualize import show_labeled_image

>>> model = Model.load('model_weights.pth', ['tick', 'gate'])
>>> image = read_image('image.jpg')
>>> labels, boxes, scores = model.predict(image)
>>> show_labeled_image(image, boxes, labels)