12from pphumanseg
import PPHumanSeg
15 if v.lower()
in [
'on',
'yes',
'true',
'y',
't']:
17 elif v.lower()
in [
'off',
'no',
'false',
'n',
'f']:
20 raise NotImplementedError
22backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
23targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
24help_msg_backends =
"Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
25help_msg_targets =
"Chose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
27 backends += [cv.dnn.DNN_BACKEND_TIMVX]
28 targets += [cv.dnn.DNN_TARGET_NPU]
29 help_msg_backends +=
"; {:d}: TIMVX"
30 help_msg_targets +=
"; {:d}: NPU"
32 print(
'This version of OpenCV does not support TIM-VX and NPU. Visit https://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')
34parser = argparse.ArgumentParser(description=
'PPHumanSeg (https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.2/contrib/PP-HumanSeg)')
35parser.add_argument(
'--input',
'-i', type=str, help=
'Path to the input image. Omit for using default camera.')
36parser.add_argument(
'--model',
'-m', type=str, default=
'human_segmentation_pphumanseg_2021oct.onnx', help=
'Path to the model.')
37parser.add_argument(
'--backend',
'-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
38parser.add_argument(
'--target',
'-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
39parser.add_argument(
'--save',
'-s', type=str, default=
False, help=
'Set true to save results. This flag is invalid when using camera.')
40parser.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.')
41args = parser.parse_args()
45 Returns the color map for visualizing the segmentation mask,
46 which can support arbitrary number of classes.
49 num_classes (int): Number of classes.
52 (list). The color map.
56 color_map = num_classes * [0, 0, 0]
57 for i
in range(0, num_classes):
61 color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j))
62 color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j))
63 color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j))
66 color_map = color_map[3:]
71 Convert predict result to color image, and save added image.
74 image (str): The input image.
75 result (np.ndarray): The predict result of image.
76 weight (float): The image weight of visual image, and the result weight is (1 - weight). Default: 0.6
77 fps (str): The FPS to be drawn on the input image.
80 vis_result (np.ndarray): The visualized result.
83 color_map = [color_map[i:i + 3]
for i
in range(0, len(color_map), 3)]
84 color_map = np.array(color_map).astype(np.uint8)
86 c1 = cv.LUT(result, color_map[:, 0])
87 c2 = cv.LUT(result, color_map[:, 1])
88 c3 = cv.LUT(result, color_map[:, 2])
89 pseudo_img = np.dstack((c1, c2, c3))
91 vis_result = cv.addWeighted(image, weight, pseudo_img, 1 - weight, 0)
94 cv.putText(vis_result,
'FPS: {:.2f}'.format(fps), (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
99if __name__ ==
'__main__':
101 model =
PPHumanSeg(modelPath=args.model, backendId=args.backend, targetId=args.target)
103 if args.input
is not None:
105 image = cv.imread(args.input)
106 h, w, _ = image.shape
107 image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
108 _image = cv.resize(image, dsize=(192, 192))
111 result = model.infer(_image)
112 result = cv.resize(result[0, :, :], dsize=(w, h), interpolation=cv.INTER_NEAREST)
119 print(
'Results saved to result.jpg\n')
120 cv.imwrite(
'result.jpg', image)
124 cv.namedWindow(args.input, cv.WINDOW_AUTOSIZE)
125 cv.imshow(args.input, image)
129 cap = cv.VideoCapture(deviceId)
130 w =
int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
131 h =
int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
134 while cv.waitKey(1) < 0:
135 hasFrame, frame = cap.read()
137 print(
'No frames grabbed!')
140 _frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
141 _frame = cv.resize(_frame, dsize=(192, 192))
145 result = model.infer(_frame)
147 result = cv.resize(result[0, :, :], dsize=(w, h), interpolation=cv.INTER_NEAREST)
150 frame =
visualize(frame, result, fps=tm.getFPS())
153 cv.imshow(
'PPHumanSeg Demo', frame)
visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps=None)
get_color_map_list(num_classes)