def inference(self, image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), ): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' # image = np.copy(image) output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 # 归一化到0~1 image_exp = np.expand_dims(image_np, axis=0) y_bboxes_output, y_cls_output = keras_inference(self.model, image_exp) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(self.anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) # if draw_result: # if class_id == 0: # color = (0, 255, 0) # else: # color = (255, 0, 0) # cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) # cv2.putText(image, "%s: %.2f" % (self.id2class[class_id], conf), (xmin + 2, ymin - 2), # cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) # if show_result: # Image.fromarray(image).show() return output_info
def inference(net, image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, english=True): height, width, _ = image.shape blob = cv2.dnn.blobFromImage(image, scalefactor=1/255.0, size=target_shape) net.setInput(blob) y_bboxes_output, y_cls_output = net.forward(getOutputsNames(net)) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh) # keep_idxs = cv2.dnn.NMSBoxes(y_bboxes.tolist(), bbox_max_scores.tolist(), conf_thresh, iou_thresh)[:,0] tl = round(0.002 * (height + width) * 0.5) + 1 # line thickness for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: cv2.rectangle(image, (xmin, ymin), (xmax, ymax), colors[class_id], thickness=tl) if english: image = puttext_onscreen(image, id2chiclass[class_id], (xmin, ymin), colors[class_id]) ###Window TeXT else: cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, colors[class_id]) # pyglet.app.run() #Sound Alert return image
def inference(model, image, target_shape, conf_thresh=0.5, iou_thresh=0.4, mode=1): image = np.array(image)[:, :, ::-1].copy() image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 image_exp = np.expand_dims(image_np, axis=0) image_transposed = image_exp.transpose((0, 3, 1, 2)) y_bboxes_output, y_cls_output = pytorch_inference(model, image_transposed) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if class_id == 0: color = (0, 255, 0) else: if mode: color = (0, 0, 255) else: color = (255, 0, 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) return (output_info, image)
def inference(net, image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, chinese=False): global frame_cnt height, width, _ = image.shape blob = cv2.dnn.blobFromImage(image, scalefactor=1/255.0, size=target_shape) net.setInput(blob) y_bboxes_output, y_cls_output = net.forward(getOutputsNames(net)) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh) # keep_idxs = cv2.dnn.NMSBoxes(y_bboxes.tolist(), bbox_max_scores.tolist(), conf_thresh, iou_thresh)[:,0] tl = round(0.002 * (height + width) * 0.5) + 1 # line thickness sending_data = '' for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) label,color = id2class[class_id],colors[class_id] if len(keep_idxs) == 1: match = recognition(image,[ymin,xmax,ymax,xmin])[0] # print('recog:',match) if match: label = 'admin' color = colors[0] sending_data = 'admin:1' elif class_id == 1 and sending_data != 'admin:1': sending_data = 'mask:0' elif class_id == 0 and sending_data != 'admin:1' and sending_data != 'mask:0': sending_data = 'mask:1' # print('label:',label) # (left, top), (right, bottom) if draw_result: cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color) #, thickness=tl if chinese: image = puttext_chinese(image,label , (xmin, ymin),color) ###puttext_chinese else: cv2.putText(image, "%s" % (label), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) if frame_cnt % 16 == 0 and len(keep_idxs) > 0: udp_send.send_data(sending_data) return image
def inference(image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(260, 260), draw_result=True, show_result=True): global MASK output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 image_exp = np.expand_dims(image_np, axis=0) y_bboxes_output, y_cls_output = tf_inference(sess, graph, image_exp) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: if class_id == 0: color = (0, 255, 0) MASK = 1 else: color = (0, 0, 255) MASK = 0 cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) if show_result: Image.fromarray(image).show() return output_info
def inference(image, conf_thresh=0.5, iou_thresh=0.5, target_shape=(360, 360), draw_result=True, show_result=False): output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 image_exp = np.expand_dims(image_np, axis=0) image_transposed = image_exp.transpose((0, 3, 1, 2)) y_bboxes_output, y_cls_output = pytorch_inference(model, image_transposed) y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) if show_result: Image.fromarray(image).show() return output_info
def post_processing(image, height, width, y_bboxes_output, y_cls_output, draw_result=True): output_info = [] y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) # print(keep_idxs, 'keepid') for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) image = Image.open(BytesIO(image)) image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = Image.fromarray(image) buffered = BytesIO() image.save(buffered, format="JPEG") image = buffered.getvalue() # Image.fromarray(image).show() output_info.append([id2class[class_id], image]) return output_info
def postprocess(img, response_data, height, width, conf_thresh=0.5, iou_thresh=0.4, draw_result=True): output_info = [] y_cls_output = response_data[0]['data'] y_bboxes_output = response_data[1]['data'] y_bboxes_output = np.array(y_bboxes_output) y_cls_output = np.array(y_cls_output) y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) cv2.rectangle(img, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(img, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) img_output = Image.fromarray(img) return img_output, output_info
def inference(self, image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160)): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :return: ''' # image = np.copy(image) height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 # 归一化到0~1 image_exp = np.expand_dims(image_np, axis=0) y_bboxes_output, y_cls_output = tf_inference(self.sess, self.graph, image_exp) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(self.anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) resp = face_detection_pb2.FaceDetResponse() for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) det_obj = face_detection_pb2.DetectedObj(lx=xmin, ly=ymin, rx=xmax, ry=ymax, score=conf) resp.detObjs.append(det_obj) return resp
def inference(image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, show_result=True): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' # image = np.copy(image) image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 # 归一化到0~1 image_exp = np.expand_dims(image_np, axis=0) y_bboxes_output, y_cls_output = tf_inference(sess, graph, image_exp) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] if class_id == 0: #有口罩 return 0 return 1
def inference(image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, show_result=True): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' # image = np.copy(image) output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 # 归一化到0~1 image_exp = np.expand_dims(image_np, axis=0) image_transposed = image_exp.transpose((0, 3, 1, 2)) y_bboxes_output, y_cls_output = pytorch_inference(model, image_transposed) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) # AQUI ES DONDE PINTA EL CUADRO EN ROJO PORQUE NO LLEVA MASCARILLA imgTest = Image.fromarray(image).crop( (xmin - 20, ymin - 20, xmax + 20, ymax + 20)) #imgTest.show() current_milli_time = lambda: int(round(time.time() * 1000)) imgDL = imgTest.save("TMP/" + str(current_milli_time()) + ".jpg") # TIRAMOS FOTO ------------ cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) if show_result: #Image.fromarray(image).show() # muestra captura completa pass return output_info
def inference(image, conf_thresh=0.9, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, show_result=True): output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 image_exp = np.expand_dims(image_np, axis=0) y_bboxes_output, y_cls_output = model_inference(sess, graph, image_exp) y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: if class_id == 0: color = (0, 255, 0) if d.frame == 0: d.mask_count += 1 #nonlocal print("***********************" + str(d.mask_state)) if d.mask_state == False: #print("here"*1000) if not d.frame == 0: d.mask_count += 1 d.mask_state = True d.frame += 1 else: color = (255, 0, 0) #nonlocal mask_state if d.frame == 0: d.no_mask_count += 1 print("``````````````````````````" + str(d.mask_state)) if d.mask_state == True: d.no_mask_count += 1 d.mask_state = False d.frame += 1 value_people = "People : " + str(d.no_mask_count + d.mask_count) value_with_mask = "People wearing masks : " + str(d.mask_count) value_without_mask = "People without masks : " + str( d.no_mask_count) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) cv2.putText(image, value_people, (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 3) cv2.putText(image, value_with_mask, (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 3) cv2.putText(image, value_without_mask, (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 3) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) if show_result: Image.fromarray(image).show() return output_info
def inference(image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, show_result=True): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' # image = np.copy(image) output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 # 归一化到0~1 image_exp = np.expand_dims(image_np, axis=0) image_transposed = image_exp.transpose((0, 3, 1, 2)) y_bboxes_output, y_cls_output = pytorch_inference(model, image_transposed) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) if draw_result: if class_id == 1: # desktop notification if no mask toast = ToastNotifier() toast.show_toast("No Mask", "Person was detected without mask", duration=1) while toast.notification_active(): time.sleep(1) if show_result: Image.fromarray(image).show() return output_info
def infer(self, image): ''' Inference method for image :param image: :return: ''' answers = [] height, width, channel = image.shape _image = cv2.resize(image, self.target_shape) _image = _image / 255.0 _image = np.expand_dims(_image, axis=0) _image = _image.transpose((0, 3, 1, 2)) with torch.no_grad(): y_bboxes_output, y_cls_output, = self.model.forward( torch.tensor(_image).float().to(self.device)) if self.device == 'cuda': y_bboxes_output, y_cls_output = y_bboxes_output.cpu().detach( ).numpy(), y_cls_output.cpu().detach().numpy() torch.cuda.empty_cache() else: y_bboxes_output, y_cls_output = y_bboxes_output.detach().numpy( ), y_cls_output.detach().numpy() # remove the batch dimension, for batch is always 1 for inference y_bboxes = decode_bbox(self.anchor_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # to speed up, do single class NMS, not multiple classes NMS bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=self.conf_thresh, iou_thresh=self.iou_thresh) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary xmin, ymin = max(0, int(bbox[0] * width)), max( 0, int(bbox[1] * height)) xmax, ymax = min(int(bbox[2] * width), width), min(int(bbox[3] * height), height) if self.draw_result is True: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, '%s: %.2f' % (self.id2class[class_id], conf), (xmin + 4, ymin - 4), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color) answers.append([class_id, conf, xmin, ymin, xmax]) return answers, image
def inference(image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, show_result=True): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' # image = np.copy(image) output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 # 归一化到0~1 image_exp = np.expand_dims(image_np, axis=0) image_transposed = image_exp.transpose((0, 3, 1, 2)) y_bboxes_output, y_cls_output = pytorch_inference(model, image_transposed) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # margin for thumb img margin_time = 0.2 t_x1, t_y1, t_x2, t_y2 = bbox[0], bbox[1], bbox[2], bbox[3] t_w = t_x2 - t_x1 t_h = t_y2 - t_y1 t_margin_x = margin_time * t_w / 2 t_margin_y = margin_time * t_h / 2 t_x1 -= t_margin_x t_y1 -= t_margin_y t_x2 += t_margin_x t_y2 += t_margin_y # x_dis = int(np.maximum(x - margin_x, 0)) # y_dis = int(np.maximum(y - margin_y, 0)) # x_dis2 = int(np.minimum(x2 + margin_x, img_size[1])) # y_dis2 = int(np.minimum(y2 + margin_y, img_size[0])) # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(t_x1 * width)) ymin = max(0, int(t_y1 * height)) xmax = min(int(t_x2 * width), width) ymax = min(int(t_y2 * height), height) if draw_result: if class_id == 0: color = (255, 0, 0) else: color = (0, 255, 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) output_info.append([xmin, ymin, xmax, ymax, conf, class_id]) if show_result: Image.fromarray(image).show() return output_info
def reconocimientoFacial(image, max_distancia_centro, conf_thresh=0.5, iou_thresh=0.5, target_shape=(360, 360), draw_result=True, ): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. ''' #convertimos la imagen que llega image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 image_exp = np.expand_dims(image_np, axis=0) image_transposed = image_exp.transpose((0, 3, 1, 2)) y_bboxes_output, y_cls_output = pytorch_inference(model, image_transposed) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) #variables para calcular la cara del centro center_face_X = 0 center_face_Y = 0 distancia_al_centro = math.inf cara_detectada = False #inicilizamos a no mascarilla, que es true #mascarilla es false, al final invertimos el resultado class_id = True for idx in keep_idxs: cara_detectada = True conf = float(bbox_max_scores[idx]) bbox = y_bboxes[idx] #coordenadas minimas y maximas de la cara xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) #calculamos el centro de la ara concreta centerX = round((xmax + xmin) / 2) centerY = round((ymax + ymin) / 2) distancia = math.sqrt(((centerX-(width/2))**2)+((centerY-(height/2))**2)) #guardamos para quedarnos con la más cercana al centro if distancia < distancia_al_centro: distancia_al_centro = distancia center_face_X = centerX center_face_Y = centerY class_id = bbox_max_score_classes[idx] #dibujamos recuado en la cara detectada if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) #inicialiamos la variable que detecta si la cara esta en el circulo a false locked = False #dibujamos el circulo y el centro de la cara con distancia mas cercana al centro if distancia_al_centro != math.inf and draw_result: if distancia_al_centro < max_distancia_centro: locked = True color = (0, 255, 0) else: locked = False color = (255, 0, 0) cv2.rectangle(image,(center_face_X-10, center_face_Y), (center_face_X+10, center_face_Y), color, 2) cv2.rectangle(image,(center_face_X, center_face_Y-10), (center_face_X, center_face_Y+10), color, 2) cv2.circle(image, (int(width/2), int(height/2)), int(max_distancia_centro) , color, 2) center_face_X = center_face_X-(width/2) center_face_Y = center_face_Y-(height/2) #inverimos class id para que false sea no mascarilla return [cara_detectada, image, center_face_X, center_face_Y, locked, not class_id]
def inference(image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, show_result=True ): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' # image = np.copy(image) faces = [] output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 # 归一化到0~1 image_exp = np.expand_dims(image_np, axis=0) y_bboxes_output, y_cls_output = keras_inference(model, image_exp) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) face_a=xmin face_b=abs(xmax-xmin) y = round((Face[0]*FOCUS/(face_b)),2) if face_a + face_b/2 < 320: P = 640 - 2*(face_a+ face_b/2 ) # P_ = y * P /38 w = round((P * y / (FOCUS)),2) x = -w/2 elif face_a + face_b/2 > 320: P = 2*(face_a + face_b/2 ) - 640 # P_ = y * P /38 w = round((P *y / (FOCUS)),2) x=w/2 else: x = 0 faces.append((x,y)) if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) print(faces) # global warning warning=[] for face in faces: warning.append([]) if len(faces)>1: for a in range(len(faces)): for b in range(a + 1,len(faces)): dist = (faces[a][0] - faces[b][0])**2 + ( faces[a][1] - faces[b][1])**2 dist = round(math.sqrt(dist),5) if dist < 500: warning[a].append(b) warning[b].append(a) cv2.putText(image,"distance is "+str(dist),(50,50),cv2.FONT_HERSHEY_SIMPLEX,1,[0,0,255],2) str1 = '' n = 0 if len(faces)>0: if len(warning)>0: for war in range(len(warning)): # n = n+1 str1 += '\n' str1 += 'Person ' war3 = str(n) str1 += war3 str1 += ' near to : ' for war2 in warning[war]: str1 += str(war2) str1 += ' , ' n = n+1 y0, dy = 50, 20 for i, line in enumerate(str1.split('\n')): y1 = y0 + i*dy y1 = int(y1) cv2.putText(image, line , (0,y1) ,cv2.FONT_HERSHEY_SIMPLEX,0.5,[0,0,255],2) if show_result: Image.fromarray(image).show() return output_info
def inference(image, conf_thresh=0.5, iou_thresh=0.5, target_shape=(260, 260), draw_result=True, show_result=False ): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' ''' people=names name="" # image = np.copy(image) output_info = []''' height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 # 归一化到0~1 image_exp = np.expand_dims(image_np, axis=0) y_bboxes_output, y_cls_output = tf_inference(sess, graph, image_exp) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: loded_pic=[] if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) ''' if people_count!=len(keep_idxs): people_count=len(keep_idxs) for p in people_file: temp=face_recognition.load_image_file(p) loded_pic.append(face_recognition.face_encodings(temp)[0]) #face_encodings 여러개 되는지 체크하기 unknown_face_encoding = face_recognition.face_encodings(image[:,: ,::-1],known_face_locations=[(ymin,xmax,ymax,xmin)])[0] results = face_recognition.compare_faces(loded_pic, unknown_face_encoding) #print("len"+str(len(loded_pic))) for x,compare in enumerate(results): if compare==True : name=people[x] break''' cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) '''try: cv2.imshow('image', image[ymin:ymax,xmin:xmax ,::-1]) except: continue''' cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2),cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) #output_info.append([class_id, conf, xmin, ymin, xmax, ymax,name]) if show_result: Image.fromarray(image).show() return
def inference(image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, show_result=True): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' # image = np.copy(image) output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 image_exp = np.expand_dims(image_np, axis=0) y_bboxes_output, y_cls_output = keras_inference(model, image_exp) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) js = { "class_id": id2class[class_id], "conf": str(conf), "xmin": xmin, "ymin": ymin, "xmax": xmax, "ymax": ymax } output_info.append(js) jsonresult = {"data": output_info} if show_result: Image.fromarray(image).show() return jsonresult
def inference(image, conf_thresh=0.8, iou_thresh=0.2, target_shape=(160, 160), draw_result=True, show_result=True ): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' output_info = [] height, width, _ = image.shape interpreter = MNN.Interpreter(model_file) session = interpreter.createSession() input_tensor = interpreter.getSessionInput(session) # image = np.copy(image) image_f32 = np.float32(image) image_f32 = cv2.resize(image_f32, target_shape) image_f32 = image_f32.astype(float) image_f32 = image_f32 * (1/255.0, 1/255.0, 1/255.0) #preprocess it image_np = np.float32(image_f32) image_np = image_np.transpose((2, 0, 1)) tmp_input = MNN.Tensor((1, 3, 260, 260), MNN.Halide_Type_Float,\ image_np, MNN.Tensor_DimensionType_Caffe) #construct tensor from np.ndarray input_tensor.copyFrom(tmp_input) interpreter.runSession(session) y_bboxes_output = interpreter.getSessionOutput(session,'loc_branch_concat').getData() y_cls_output = interpreter.getSessionOutput(session,'cls_branch_concat').getData() # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0 , 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin-2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) if show_result: Image.fromarray(image).show() return output_info
).InferRequestedOutputTensor() loc_conv.name = "loc_branch_concat__0" cls_conv = grpc_service_v2_pb2.ModelInferRequest( ).InferRequestedOutputTensor() cls_conv.name = "cls_branch_concat__1" request.outputs.extend([loc_conv, cls_conv]) response = grpc_stub.ModelInfer(request) print("model infer:\n{}".format(response)) # Post process id2class = {0: 'Mask', 1: 'NoMask'} y_cls_output = get_array(response.outputs[0], np.float32) y_bboxes_output = get_array(response.outputs[1], np.float32) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=0.5, iou_thresh=0.5, ) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx]
def openvino_infer(self, image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(260, 260), draw_result=True, show_result=False): height, width, _ = image.shape # resize & adjust input image_resized = cv2.resize(image, target_shape) image_np = image_resized / 255.0 image_exp = np.expand_dims(image_np, axis=0) image_transposed = image_exp.transpose( (0, 3, 1, 2)) # Change data layout from HWC to CHW res = self._exec_net.infer(inputs={self._input_blob: image_transposed}) # result conversion y_cls_output = res['cls_branch_concat'] y_bboxes_output = res['loc_branch_concat'] # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # To speed up, do single class NMS, not multiple classes NMS. # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression( y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) # build detections for output (and draw if needed) output_info = [] for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) if show_result: Image.fromarray(image).show() return output_info
def inference(image, conf_thresh=0.5, iou_thresh=0.4, target_shape=(160, 160), draw_result=True, show_result=True, ): ''' Main function of detection inference :param image: 3D numpy array of image :param conf_thresh: the min threshold of classification probabity. :param iou_thresh: the IOU threshold of NMS :param target_shape: the model input size. :param draw_result: whether to daw bounding box to the image. :param show_result: whether to display the image. :return: ''' # image = np.copy(image) output_info = [] height, width, _ = image.shape image_resized = cv2.resize(image, target_shape) #print (image_resized.shape) image_np = image_resized / 255.0 # 归一化到0~1 image_exp = np.expand_dims(image_np, axis=0) y_bboxes_output, y_cls_output = tf_inference(sess, graph, image_exp) #print (y_bboxes_output) #print (y_cls_output) # remove the batch dimension, for batch is always 1 for inference. y_bboxes = decode_bbox(anchors_exp, y_bboxes_output)[0] y_cls = y_cls_output[0] # To speed up, do single class NMS, not multiple classes NMS. bbox_max_scores = np.max(y_cls, axis=1) bbox_max_score_classes = np.argmax(y_cls, axis=1) # keep_idx is the alive bounding box after nms. keep_idxs = single_class_non_max_suppression(y_bboxes, bbox_max_scores, conf_thresh=conf_thresh, iou_thresh=iou_thresh, ) print ("print keep index") #print (keep_idxs) for idx in keep_idxs: conf = float(bbox_max_scores[idx]) class_id = bbox_max_score_classes[idx] bbox = y_bboxes[idx] # clip the coordinate, avoid the value exceed the image boundary. xmin = max(0, int(bbox[0] * width)) ymin = max(0, int(bbox[1] * height)) xmax = min(int(bbox[2] * width), width) ymax = min(int(bbox[3] * height), height) print ("xmin %s" %xmin) result = np.array([xmin,ymin,xmax,ymax]) print (result) if draw_result: if class_id == 0: color = (0, 255, 0) else: color = (255, 0, 0) cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) writeText(image) print ("##### Predicted Class %s" %class_id) print (type(class_id)) print ("##### Confidence Level %s" %conf) if class_id == 1: print ("violation") #writeText(image) #print (result) #trackers = mot_tracker.update(result) #print (trackers) #TelegramBot.send_violation_text() output_info.append([class_id, conf, xmin, ymin, xmax, ymax]) if show_result: Image.fromarray(image).show() print (output_info) return output_info