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
Пример #2
0
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
Пример #3
0
            
            conf_thresh = 0.5
            iou_thresh = 0.4
            target_shape = (260, 260)
            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)
            cv2.imshow('image',image_exp)
            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,
                                                        )
        
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