|
|
|
|
|
|
|
|
|
|
|
|
|
import argparse |
|
|
|
import numpy as np |
|
import cv2 as cv |
|
|
|
from pphumanseg import PPHumanSeg |
|
|
|
def str2bool(v): |
|
if v.lower() in ['on', 'yes', 'true', 'y', 't']: |
|
return True |
|
elif v.lower() in ['off', 'no', 'false', 'n', 'f']: |
|
return False |
|
else: |
|
raise NotImplementedError |
|
|
|
parser = argparse.ArgumentParser(description='PPHumanSeg (https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.2/contrib/PP-HumanSeg)') |
|
parser.add_argument('--input', '-i', type=str, help='Path to the input image. Omit for using default camera.') |
|
parser.add_argument('--model', '-m', type=str, default='human_segmentation_pphumanseg_2021oct.onnx', help='Path to the model.') |
|
parser.add_argument('--save', '-s', type=str, default=False, help='Set true to save results. This flag is invalid when using camera.') |
|
parser.add_argument('--vis', '-v', type=str2bool, default=True, help='Set true to open a window for result visualization. This flag is invalid when using camera.') |
|
args = parser.parse_args() |
|
|
|
def get_color_map_list(num_classes): |
|
""" |
|
Returns the color map for visualizing the segmentation mask, |
|
which can support arbitrary number of classes. |
|
|
|
Args: |
|
num_classes (int): Number of classes. |
|
|
|
Returns: |
|
(list). The color map. |
|
""" |
|
|
|
num_classes += 1 |
|
color_map = num_classes * [0, 0, 0] |
|
for i in range(0, num_classes): |
|
j = 0 |
|
lab = i |
|
while lab: |
|
color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j)) |
|
color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j)) |
|
color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j)) |
|
j += 1 |
|
lab >>= 3 |
|
color_map = color_map[3:] |
|
return color_map |
|
|
|
def visualize(image, result, weight=0.6, fps=None): |
|
""" |
|
Convert predict result to color image, and save added image. |
|
|
|
Args: |
|
image (str): The input image. |
|
result (np.ndarray): The predict result of image. |
|
weight (float): The image weight of visual image, and the result weight is (1 - weight). Default: 0.6 |
|
fps (str): The FPS to be drawn on the input image. |
|
|
|
Returns: |
|
vis_result (np.ndarray): The visualized result. |
|
""" |
|
color_map = get_color_map_list(256) |
|
color_map = [color_map[i:i + 3] for i in range(0, len(color_map), 3)] |
|
color_map = np.array(color_map).astype(np.uint8) |
|
|
|
c1 = cv.LUT(result, color_map[:, 0]) |
|
c2 = cv.LUT(result, color_map[:, 1]) |
|
c3 = cv.LUT(result, color_map[:, 2]) |
|
pseudo_img = np.dstack((c1, c2, c3)) |
|
|
|
vis_result = cv.addWeighted(image, weight, pseudo_img, 1 - weight, 0) |
|
|
|
if fps is not None: |
|
cv.putText(vis_result, 'FPS: {:.2f}'.format(fps), (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0)) |
|
|
|
return vis_result |
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
model = PPHumanSeg(modelPath=args.model) |
|
|
|
if args.input is not None: |
|
|
|
image = cv.imread(args.input) |
|
h, w, _ = image.shape |
|
image = cv.cvtColor(image, cv.COLOR_BGR2RGB) |
|
_image = cv.resize(image, dsize=(192, 192)) |
|
|
|
|
|
result = model.infer(_image) |
|
result = cv.resize(result[0, :, :], dsize=(w, h), interpolation=cv.INTER_NEAREST) |
|
|
|
|
|
image = visualize(image, result) |
|
|
|
|
|
if args.save: |
|
print('Results saved to result.jpg\n') |
|
cv.imwrite('result.jpg', image) |
|
|
|
|
|
if args.vis: |
|
cv.namedWindow(args.input, cv.WINDOW_AUTOSIZE) |
|
cv.imshow(args.input, image) |
|
cv.waitKey(0) |
|
else: |
|
deviceId = 0 |
|
cap = cv.VideoCapture(deviceId) |
|
w = int(cap.get(cv.CAP_PROP_FRAME_WIDTH)) |
|
h = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)) |
|
|
|
tm = cv.TickMeter() |
|
while cv.waitKey(1) < 0: |
|
hasFrame, frame = cap.read() |
|
if not hasFrame: |
|
print('No frames grabbed!') |
|
break |
|
|
|
_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB) |
|
_frame = cv.resize(_frame, dsize=(192, 192)) |
|
|
|
|
|
tm.start() |
|
result = model.infer(_frame) |
|
tm.stop() |
|
result = cv.resize(result[0, :, :], dsize=(w, h), interpolation=cv.INTER_NEAREST) |
|
|
|
|
|
frame = visualize(frame, result, fps=tm.getFPS()) |
|
|
|
|
|
cv.imshow('PPHumanSeg Demo', frame) |
|
|
|
tm.reset() |