How to do it...

You need to complete these steps:

  1. Import the modules:
import cv2import numpy as np
  1. Import the Caffe model:
model = cv2.dnn.readNetFromCaffe('../data/fcn8s-heavy-pascal.prototxt',                                 '../data/fcn8s-heavy-pascal.caffemodel')
  1. Load the image and perform inference:
frame = cv2.imread('../data/scenetext01.jpg')blob = cv2.dnn.blobFromImage(frame, 1, (frame.shape[1],frame.shape[0]))model.setInput(blob)output = model.forward()
  1. Compute the image with per-pixel class labels:
labels = output[0].argmax(0)
  1. Visualize the results:
plt.figure(figsize=(14,10))plt.subplot(121)plt.axis('off')plt.title('original')plt.imshow(frame[:,:,[2,1,0]])plt.subplot(122)plt.axis('off')plt.title('segmentation')plt.imshow(labels)plt.tight_layout()plt.show() ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.