Skip to content

Instantly share code, notes, and snippets.

@dkurt
Created February 28, 2020 07:45
Show Gist options
  • Save dkurt/3175fdc7283900b9f10d33e94038ec9e to your computer and use it in GitHub Desktop.
Save dkurt/3175fdc7283900b9f10d33e94038ec9e to your computer and use it in GitHub Desktop.
import numpy as np
from openvino.inference_engine import IENetwork, IECore
import cv2 as cv
PERSON_ID = 11
net = IENetwork('semantic-segmentation-adas-0001.xml', 'semantic-segmentation-adas-0001.bin')
ie = IECore()
exec_net = ie.load_network(net, 'CPU')
img = cv.imread('/home/dkurt/Pictures/samsung-neon.jpg')
bg = cv.imread('/home/dkurt/Pictures/127117.jpg')
bg = cv.resize(bg, (img.shape[1], img.shape[0]))
inp = cv.resize(img, (2048, 1024)).astype(np.float32).transpose(2, 0, 1).reshape(1, 3, 1024, 2048)
# inp /= 255
outputs = exec_net.infer({'data': inp})
mask = outputs['3976.1'].reshape(1024, 2048)
mask = (mask == PERSON_ID).astype(np.uint8)
mask = cv.resize(mask, (img.shape[1], img.shape[0]), interpolation=cv.INTER_NEAREST)
mask = mask.astype(np.bool)
img[~mask] = 0
bg[mask] = 0
img = img + bg
cv.imshow('mask', img)
cv.waitKey()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment