1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| import cv2 import numpy as np # 引用 paddle inference 预测库 import paddle.inference as paddle_infer from PIL import Image# 创建 config config = paddle_infer.Config("./models/mobilenet_ssd/mobilenet-ssd-model", "./models/mobilenet_ssd/mobilenet-ssd-params") # 根据 config 创建 predictor predictor = paddle_infer.create_predictor(config) # 获取输入 Tensor input_names = predictor.get_input_names() input_tensor = predictor.get_input_handle(input_names[0]) print(input_names) print(input_tensor) # 从 CPU 获取数据,设置到 Tensor 内部 imgPath = './frame/15.jpg' cap = cv2.VideoCapture('./models/mobilenet_ssd/sample.mp4') frame = [] while True: ret, frame = cap.read() if ret == False: break # cv2.imwrite("./frame/frame.jpg", frame) # imgPath = './frame/frame.jpg' im = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))# Image.open(imgPath) im = im.resize((300, 300), Image.BICUBIC) # img = cv2.cvtColor(numpy.asarray(im.resize((320, 240), Image.BICUBIC)), cv2.COLOR_RGB2BGR)#cv2.cvtColor(, cv2.COLOR_RGB2BGR) # 转换代码 # cv2.imshow('img', img) # opencv显示 # cv2.waitKey() im = np.array(im, dtype=np.float32).transpose(2, 0, 1).reshape((1, 3, 300, 300)) mean = [121.636447, 125.696666, 124.807721] # mean = [127.5, 127.5, 127.5] scale = [0.007843, 0.007843, 0.007843] std = [ 69.480526, 66.625320, 50.616609] for ri in range(0, 300): im[0][0][ri] = (im[0][0][ri] / 255)# - (scale[0] - mean[0] / 255) for gi in range(0, 300): im[0][1][gi] = (im[0][1][gi] / 255)# - (scale[1] - mean[1] / 255) for bi in range(0, 300): im[0][2][bi] = (im[0][2][bi] / 255)# - (scale[2] - mean[2] / 255) fake_input = im input_tensor1 = predictor.get_input_handle(input_names[0]) input_tensor1.copy_from_cpu(im) # # im = np.asarray([[320.0/240.0, 320.0/320.0]], dtype=np.float32) # input_tensor2 = predictor.get_input_handle(input_names[1]) # input_tensor2.copy_from_cpu(im) # # im = np.asarray([[608, 608]], dtype=np.float32) # input_tensor2 = predictor.get_input_handle(input_names[2]) # input_tensor2.copy_from_cpu(im) # 执行预测 predictor.run() # 获取输出 Tensor output_names = predictor.get_output_names() output_tensor = predictor.get_output_handle(output_names[0]) # 从 Tensor 中获取数据到 CPU output_data = output_tensor.copy_to_cpu() # 获取 Tensor 的维度信息 output_shape = output_tensor.shape() # 获取 Tensor 的数据类型 output_type = output_tensor.type() results = [] i = 0 img = frame label_list = ["background", "cone", "bridge", "pig", "tractor", "corn", "bump", "crosswalk", " "] label_list = ["background", "cone", "granary", "bridge", "tractor", "corn", "pig", "crosswalk", "bump"] img = cv2.resize(img, dsize=(300, 300), fx=1, fy=1, interpolation=cv2.INTER_LINEAR) if len(output_data) > 1: for data in output_data: tmp = {} if data[1] > 0.9: tmp['type'] = data[0] tmp['score'] = data[1] tmp['x'] = data[2] * 300 tmp['y'] = data[3] * 300 tmp['width'] = data[4] * 300 - tmp['x'] tmp['height'] = data[5] * 300 - tmp['y'] results.insert(i, tmp) # print(tmp) i = i + 1 for i in results: x = int(i['x']) xmax = int(i['x'] + i['width']) y = int(i['y']) ymax = int(i['y'] + i['height']) # print((x, y), (xmax, ymax)) cv2.rectangle(img, (x, y), (xmax, ymax), (0, 0, 255), 2) # cv2.rectangle(img, (x, ymax), (xmax, y), (255, 0, 0), 2) cv2.putText(img, str(label_list[int(i['type'])]), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, int(i['type']) * 30, 255), 2) img = cv2.resize(img, dsize=(320, 240), fx=1, fy=1, interpolation=cv2.INTER_LINEAR) cv2.imshow('src', img) cv2.waitKey(1) # 释放中间 Tensor predictor.clear_intermediate_tensor() # 释放内存池中的所有临时 Tensor predictor.try_shrink_memory()
|