def is_img(img_cv, color): j = 0 if len(img_cv) != 0: print("---1312--------------") for i in range (len(img_cv)): im_cv_r = cv2.resize(img_cv[i], (1300, 414)) gray = cv2.cvtColor(im_cv_r, cv2.COLOR_BGR2GRAY) equ = cv2.equalizeHist(gray) gaussian = cv2.GaussianBlur(gray, (3, 3), 0, 0, cv2.BORDER_DEFAULT) median = cv2.medianBlur(gaussian, 3) original_image = median original_image_size = original_image.shape[:2] image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size]) image_data = image_data[np.newaxis, ...] data = json.dumps({"signature_name": "serving_default", "instances": image_data.tolist()}) headers = {"content-type": "application/json"} num_classes=65 json_response = requests.post( 'http://tf:port/v1/models/yolov3:predict', data=data, headers=headers) predictions = json.loads(json_response.text)['predictions'] pred_sbbox, pred_mbbox, pred_lbbox =predictions[0]['pred_sbbox'],predictions[0]['pred_mbbox'],predictions[0]['pred_lbbox'] pred_bbox = np.concatenate([np.reshape(pred_sbbox, (-1, 5 + num_classes)), np.reshape(pred_mbbox, (-1, 5 + num_classes)), np.reshape(pred_lbbox, (-1, 5 + num_classes))], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.3) bboxes = utils.nms(bboxes, 0.45, method='nms') if np.array(bboxes).shape[0] > 6: image = utils.draw_bbox(im_cv_r, bboxes) # print(image) name = color +'im' + str(i) + '.jpg' path = os.path.join("./pre_out/", name) cv2.imwrite(path,image) print("-------------")
def detect_image(self, image): if self.model_image_size != (None, None): assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required' assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required' boxed_image = image_preporcess(np.copy(image), tuple(reversed(self.model_image_size))) image_data = boxed_image out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model.input: image_data, self.input_image_shape: [image.shape[0], image.shape[1]]#,#[image.size[1], image.size[0]], #K.learning_phase(): 0 # Denne har jeg udkommeret!!!!!! }) #print('Found {} boxes for {}'.format(len(out_boxes), 'img')) thickness = (image.shape[0] + image.shape[1]) // 600 fontScale=1 ObjectsList = [] for i, c in reversed(list(enumerate(out_classes))): predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] label = '{} {:.2f}'.format(predicted_class, score) #label = '{}'.format(predicted_class) scores = '{:.2f}'.format(score) top, left, bottom, right = box top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(image.shape[0], np.floor(bottom + 0.5).astype('int32')) right = min(image.shape[1], np.floor(right + 0.5).astype('int32')) mid_h = (bottom-top)/2+top mid_v = (right-left)/2+left # put object rectangle cv2.rectangle(image, (left, top), (right, bottom), self.colors[c], thickness) # get text size (test_width, text_height), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, thickness/self.text_size, 1) # put text rectangle cv2.rectangle(image, (left, top), (left + test_width, top - text_height - baseline), self.colors[c], thickness=cv2.FILLED) # put text above rectangle cv2.putText(image, label, (left, top-2), cv2.FONT_HERSHEY_SIMPLEX, thickness/self.text_size, (0, 0, 0), 1) # add everything to list ObjectsList.append([top, left, bottom, right, mid_v, mid_h, label, scores]) return image, ObjectsList
def object_detect(input_path="./road.jpg", output_path='./demo.jpg'): img_size = 608 num_channels = 3 # image_path = "./docs/images/sample_computer.jpg" image_path = input_path # 调用图片,示例:"./docs/images/sample_computer.jpg" original_image = cv2.imread(image_path) # original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) image_data = utils.image_preporcess(np.copy(original_image), [img_size, img_size]) # 图片处理成608*608*3 # print(image_data.shape) plt.imshow(image_data) plt.show() yolov3_api = "http://localhost:8501/v1/models/yolov3:predict" # 刚刚产生的接口 image_data_yolo_list = image_data[np.newaxis, :].tolist() # 转化为多维数组矩阵 headers = {"Content-type": "application/json"} r = requests.post(yolov3_api, headers=headers, data=json.dumps({ "signature_name": "predict", "instances": image_data_yolo_list })).json() #post请求 # print('r',r) # 19, 19, 85 = 30685 # {'error': 'Input to reshape is a tensor with 18411 values, but the requested shape requires a multiple of 30685\n\t [[{{node pred_multi_scale/Reshape_2}}]]'} # 18411 的因子 [3, 17, 19, 51, 57, 323, 361, 969, 1083, 6137] output = np.array(r['predictions']) # print(output.shape) # (63, 19, 19, 85) reduction factor 注:衰减系数以及步长:32 608/32=19 85 = 80类+1可能性+4个坐标 # 416 x 416 则为 13*13 output = np.reshape( output, (-1, 85 )) # 这一步处理成 22743*85的维度(63*19*19 =22743, 85 = 80类+1可能性+4个坐标,根据自己数据集改) # print(output.shape) original_image_size = original_image.shape[:2] bboxes = utils.postprocess_boxes( output, original_image_size, img_size, 0.3) # 这一步是将所有可能的预测信息提取出来,主要是三类:类别,可能性,坐标值。 bboxes = utils.nms(bboxes, 0.45, method='nms') # 这一步是 将刚刚提取出来的信息进行筛选,返回最好的预测值,同样是三类。 image = utils.draw_bbox(original_image, bboxes) # 这一步是把结果画到新图上面。 image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) image = Image.fromarray(image) image.show() image.save(output_path) # 保存图片到本地
def detect_img(model_result, input_size, fp_src, fp_dst): original_image = cv2.imread(fp_src) original_image_draw = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) original_image_size = original_image.shape[:2] image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size]) image_data = np.array(image_data).astype(np.float32) bboxes = model_result.model_reponse(image_data, original_image_size) image = utils.draw_bbox(original_image_draw, bboxes) image = Image.fromarray(image) image.save(fp_dst)
def predict(original_image): ''' :param original_image: [H, W, 3] :return: (xmin, ymin, xmax, ymax, score, class) ''' image = utils.image_preporcess(np.copy(original_image), [cfg.TEST.INPUT_SIZE, cfg.TEST.INPUT_SIZE]) image = image[np.newaxis, ...] image_ = np.transpose(image, [0, 3, 1, 2]) image_ = np.copy(image_, order='C') pred = test_job(image_, anchors[0], anchors[1])[0, ...] original_image_size = original_image.shape[0:2] bboxes = utils.postprocess_boxes(pred, original_image_size, cfg.TEST.INPUT_SIZE, cfg.TEST.SCORE_THRESHOLD) bboxes = utils.nms(bboxes, cfg.TEST.IOU_THRESHOLD, method='nms') return bboxes
def __next__(self): with tf.device('/cpu:0'): batch_image = np.zeros( (self.batch_size, self.input_size, self.input_size, 3), dtype=np.float32) batch_label_sbbox = np.zeros( (self.batch_size, self.output_sizes[0], self.output_sizes[0], self.anchor_per_scale, 5 + self.num_classes), dtype=np.float32) batch_label_mbbox = np.zeros( (self.batch_size, self.output_sizes[1], self.output_sizes[1], self.anchor_per_scale, 5 + self.num_classes), dtype=np.float32) batch_label_lbbox = np.zeros( (self.batch_size, self.output_sizes[2], self.output_sizes[2], self.anchor_per_scale, 5 + self.num_classes), dtype=np.float32) batch_sbboxes = np.zeros( (self.batch_size, self.max_bbox_per_scale, 4), dtype=np.float32) batch_mbboxes = np.zeros( (self.batch_size, self.max_bbox_per_scale, 4), dtype=np.float32) batch_lbboxes = np.zeros( (self.batch_size, self.max_bbox_per_scale, 4), dtype=np.float32) num = 0 if self.batch_count < self.num_batchs: while num < self.batch_size: index = self.batch_count * self.batch_size + num if index >= self.num_samples: index -= self.num_samples bboxes = self.load_label(self.label_path[index]) image = self.load_image(self.image_path[index]) image, bboxes = utils.image_preporcess( np.copy(image), [self.input_size, self.input_size], np.copy(bboxes)) label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes = self.preprocess_true_boxes( bboxes) batch_image[num, :, :, :] = image batch_label_sbbox[num, :, :, :, :] = label_sbbox batch_label_mbbox[num, :, :, :, :] = label_mbbox batch_label_lbbox[num, :, :, :, :] = label_lbbox batch_sbboxes[num, :, :] = sbboxes batch_mbboxes[num, :, :] = mbboxes batch_lbboxes[num, :, :] = lbboxes num += 1 self.batch_count += 1 batch_smaller_target = batch_label_sbbox, batch_sbboxes batch_medium_target = batch_label_mbbox, batch_mbboxes batch_larger_target = batch_label_lbbox, batch_lbboxes return batch_image, (batch_smaller_target, batch_medium_target, batch_larger_target) else: self.batch_count = 0 raise StopIteration
def predict(self, img, classes, threshold, output_path): t1 = time.time() input_size = 608 num_classes = len(classes) original_image = cv2.imread(img) original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB) original_image_size = original_image.shape[:2] image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size]) images_data = [] images_data.append(image_data) images_data_np = np.array(images_data).astype(np.float32) tensor = tf.contrib.util.make_tensor_proto( images_data_np, shape=list(images_data_np.shape)) # 单张图片加载 # image_data = [image_data.tolist()] t2 = time.time() # print('图片预处理时间:{}'.format(t2-t1)) url = self.server options = [('grpc.max_send_message_length', 512 * 1024 * 1024), ('grpc.max_receive_message_length', 512 * 1024 * 1024)] channel = grpc.insecure_channel(url, options=options) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) r = predict_pb2.PredictRequest() r.model_spec.name = self.model r.model_spec.signature_name = 'predict' r.inputs['input'].CopyFrom(tensor) # res = stub.Predict(r, 10.0) result_future = stub.Predict.future(r, 10.0) # 10 secs timeout res = result_future.result() t3 = time.time() # print('GRPC 预测时间:{}'.format(t3-t2)) arr = tf.make_ndarray(res.outputs['output']) # print("arr", arr) # print("output shape", arr.shape) pred_bbox = np.reshape(arr, (-1, 5 + num_classes)) bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, threshold) t4 = time.time() # print(']图片 postprocess_boxes 处理时间:{}'.format(t4-t3)) bboxes = utils.nms(bboxes, 0.45, method='nms') t5 = time.time() # print('图片 nms 处理时间:{}'.format( t5-t4)) image = utils.draw_bbox(original_image, bboxes, classes, True) t6 = time.time() # # print("图片draw_bbox处理时间:", t6-t5) # image_np = cv2.cvtColor( image, cv2.COLOR_RGB2BGR ) # 转换一下通道 # cv2.imwrite(filepath, image_np) t7 = time.time() # print('图片写出处理时间:{}'.format( t7-t6)) image = Image.fromarray(image) image.show() image.save(output_path) # 保存图片到本地
] pb_file = "./yolov3_voc.pb" input_image_path = "./road.jpg" num_class = len(classes) strides = [8, 16, 32] input_size = (608, 608) graph = tf.Graph() # Output shapes expected by the post-processor output_shapes = [(1, 19, 19, (num_class + 5) * 3), (1, 38, 38, (num_class + 5) * 3), (1, 76, 76, (num_class + 5) * 3)] image = cv2.imread(input_image_path) image_size = image.shape[:2] image_data = np.expand_dims(utils.image_preporcess(image, input_size), 0) image_data = np.array(image_data, dtype=np.float32, order='C') return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements) with tf.Session(graph=graph) as sess: tf_outputs = sess.run( [return_tensors[1], return_tensors[2], return_tensors[3]], feed_dict={return_tensors[0]: image_data}) # tf_outputs = [output.reshape(shape) for output, shape in zip(tf_outputs, output_shapes)] pred_bboxes = [ utils.decode(tf_outputs[i], anchors[i], strides[i]) for i in range(3) ] pred_bboxes = np.concatenate( [np.reshape(pred_bbox, (-1, 5 + num_class)) for pred_bbox in pred_bboxes],
def run_processing(video_stream: cv2.VideoCapture): return_elements = [ "input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0", "pred_lbbox/concat_2:0" ] pb_file = "./model/model.pb" num_classes = 80 input_size = 416 graph = tf.Graph() return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements) counter = 0 classes = utils.read_class_names() with tf.Session(graph=graph) as sess: while True: return_value, frame = video_stream.read() if return_value: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) counter = 0 else: counter += 1 if counter > 5: return else: continue preproc_time_start = time.time() frame_size = frame.shape[:2] image_data = utils.image_preporcess(np.copy(frame), [input_size, input_size]) image_data = image_data[np.newaxis, ...] prev_time = time.time() preproc_time = prev_time - preproc_time_start info = "preprocessing time: %.2f ms" % (1000 * preproc_time) print(info) model_time = time.time() pred_sbbox, pred_mbbox, pred_lbbox = sess.run( [return_tensors[1], return_tensors[2], return_tensors[3]], feed_dict={return_tensors[0]: image_data}) end_model_time = time.time() proc_model_time = end_model_time - model_time info = "preprocessing model time: %.2f ms" % (1000 * proc_model_time) print(info) pred_bbox = np.concatenate([ np.reshape(pred_sbbox, (-1, 5 + num_classes)), np.reshape(pred_mbbox, (-1, 5 + num_classes)), np.reshape(pred_lbbox, (-1, 5 + num_classes)) ], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size, 0.7) bboxes = utils.nms(bboxes, 0.45, method='nms') result_image = utils.draw_bbox(frame, bboxes, classes) curr_time = time.time() exec_time = curr_time - prev_time info = "time: %.2f ms" % (1000 * exec_time) print(info) cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE) result = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR) cv2.imshow("result", result) if cv2.waitKey(1) & 0xFF == ord('q'): break