Exemplo n.º 1
0
    def addTrackers(self,
                    boxes,
                    person_detection,
                    head_detection,
                    frame,
                    threshold=0.2):

        for i, p_det in enumerate(person_detection):
            # for p_det, h_det in zip(person_detection,head_detection):
            det_box = (p_det[1], p_det[0], p_det[3] - p_det[1],
                       p_det[2] - p_det[0])
            # det_hd  = (h_det[1], h_det[0], h_det[3] - h_det[1], h_det[2] - h_det[0])
            isnew = True
            for box in boxes:
                if utils.IoU(det_box, box) > threshold:
                    isnew = False
                    break

            if isnew:
                if head_detection is not None and head_detection[i] != []:
                    h_det = head_detection[i]
                    det_hd = (h_det[1], h_det[0], h_det[3] - h_det[1],
                              h_det[2] - h_det[0])
                else:
                    det_hd = None
                self.trackers.append(singleTracker(frame, det_box, det_hd))
                boxes.append(det_box)
Exemplo n.º 2
0
def gen_data(anno_file, data_dir, prefix):

    size = 12
    
    landmark_imgs_save_dir = os.path.join(data_dir,"12/landmark")
    if not os.path.exists(landmark_imgs_save_dir):
        os.makedirs(landmark_imgs_save_dir)

    anno_dir = config.ANNO_STORE_DIR
    if not os.path.exists(anno_dir):
        os.makedirs(anno_dir)

    landmark_anno_filename = config.PNET_LANDMARK_ANNO_FILENAME
    save_landmark_anno = os.path.join(anno_dir,landmark_anno_filename)

    f = open(save_landmark_anno, 'w')

    with open(anno_file, 'r') as f2:
        annotations = f2.readlines()

    num = len(annotations)
    print("%d total images" % num)

    l_idx =0
    idx = 0
    # image_path bbox landmark(5*2)
    for annotation in annotations:
        # print imgPath

        annotation = annotation.strip().split(' ')
        assert len(annotation)==15,"each line should have 15 element"
        im_path = os.path.join(prefix,annotation[0].replace("\\", "/"))

        gt_box = map(float, annotation[1:5])
        # the bounging box in original anno_file is [left, right, top, bottom]
        gt_box = [gt_box[0], gt_box[2], gt_box[1], gt_box[3]] #[left, top, right, bottom] 
        gt_box = np.array(gt_box, dtype=np.int32)
        
        landmark = map(float, annotation[5:])
        landmark = np.array(landmark, dtype=np.float)

        img = cv2.imread(im_path)
        assert (img is not None)

        height, width, channel = img.shape
        # crop_face = img[gt_box[1]:gt_box[3]+1, gt_box[0]:gt_box[2]+1]
        # crop_face = cv2.resize(crop_face,(size,size))

        idx = idx + 1
        if idx % 100 == 0:
            print("%d images done, landmark images: %d"%(idx,l_idx))

        x1, y1, x2, y2 = gt_box

        # gt's width
        w = x2 - x1
        # gt's height
        h = y2 - y1
        if max(w, h) < 40 or x1 < 0 or y1 < 0:
            continue
        # random shift
        for i in range(20):
            bbox_size = npr.randint(int(min(w, h) * 0.8), np.ceil(1.25 * max(w, h)))
            delta_x = npr.randint(-w * 0.2, w * 0.2)
            delta_y = npr.randint(-h * 0.2, h * 0.2)
            nx1 = max(x1 + w / 2 - bbox_size / 2 + delta_x, 0)
            ny1 = max(y1 + h / 2 - bbox_size / 2 + delta_y, 0)

            nx2 = nx1 + bbox_size
            ny2 = ny1 + bbox_size
            if nx2 > width or ny2 > height:
                continue
            crop_box = np.array([nx1, ny1, nx2, ny2])
            cropped_im = img[ny1:ny2 + 1, nx1:nx2 + 1, :]
            resized_im = cv2.resize(cropped_im, (size, size),interpolation=cv2.INTER_LINEAR)

            offset_x1 = (x1 - nx1) / float(bbox_size)
            offset_y1 = (y1 - ny1) / float(bbox_size)
            offset_x2 = (x2 - nx2) / float(bbox_size)
            offset_y2 = (y2 - ny2) / float(bbox_size)

            offset_left_eye_x = (landmark[0] - nx1) / float(bbox_size)
            offset_left_eye_y = (landmark[1] - ny1) / float(bbox_size)

            offset_right_eye_x = (landmark[2] - nx1) / float(bbox_size)
            offset_right_eye_y = (landmark[3] - ny1) / float(bbox_size)

            offset_nose_x = (landmark[4] - nx1) / float(bbox_size)
            offset_nose_y = (landmark[5] - ny1) / float(bbox_size)

            offset_left_mouth_x = (landmark[6] - nx1) / float(bbox_size)
            offset_left_mouth_y = (landmark[7] - ny1) / float(bbox_size)

            offset_right_mouth_x = (landmark[8] - nx1) / float(bbox_size)
            offset_right_mouth_y = (landmark[9] - ny1) / float(bbox_size)


            # cal iou
            iou = utils.IoU(crop_box.astype(np.float), np.expand_dims(gt_box.astype(np.float), 0))
            if iou > 0.65:
                save_file = os.path.join(landmark_imgs_save_dir, "%s.jpg" % l_idx)
                cv2.imwrite(save_file, resized_im)

                f.write(save_file + ' -2 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f \n' % \
                (offset_x1, offset_y1, offset_x2, offset_y2, \
                offset_left_eye_x,offset_left_eye_y,offset_right_eye_x,offset_right_eye_y,offset_nose_x,offset_nose_y,offset_left_mouth_x,offset_left_mouth_y,offset_right_mouth_x,offset_right_mouth_y))

                l_idx += 1

    f.close()
Exemplo n.º 3
0
    def processFrame(self, image, fusion_threshold=0.3):
        # Expand dimensions since the trained_model expects images to have shape: [1, None, None, 3]
        image_np_expanded = np.expand_dims(image, axis=0)
        # Actual detection.
        # start_time = time.time()
        (boxes, scores, classes, num_det) = self.sess.run(
            [
                self.detection_boxes, self.detection_scores,
                self.detection_classes, self.num_detections
            ],
            feed_dict={self.image_tensor: image_np_expanded})

        # end_time = time.time()
        # print("Elapsed Time:", end_time-start_time)
        im_height, im_width, _ = image.shape

        boxes = boxes[0, 0:int(num_det[0])]
        scores = scores[0, 0:int(num_det[0])]
        classes = classes[0, 0:int(num_det[0])]

        # Box format: startY, startX, endY, endX
        # Rect format: x, y, w, y

        scores = scores.tolist()
        classes = classes.tolist()

        boxes_list = []
        scores_list = []
        classes_list = []

        for i, box in enumerate(boxes):

            det_box = [
                int(box[0] * im_height),
                int(box[1] * im_width),
                int(box[2] * im_height),
                int(box[3] * im_width)
            ]
            newbox = True
            rect_det = utils.boxp1p2_to_rect(det_box)

            for j, b in enumerate(boxes_list):
                if classes[i] != classes_list[j]:
                    continue

                rect_list = utils.boxp1p2_to_rect(b)
                iou = utils.IoU(rect_list, rect_det)

                if iou > fusion_threshold:
                    x, y, w, h = utils.union(rect_list, rect_det)
                    boxes_list[j] = [y, x, y + h, x + w]
                    newbox = False
                    break

            if newbox:
                boxes_list.append(det_box)
                scores_list.append(scores[i])
                classes_list.append(classes[i])
        return boxes_list, scores_list, classes_list, len(boxes_list)

        boxes_list = [None for i in range(boxes.shape[1])]
        for i in range(boxes.shape[1]):
            boxes_list[i] = (int(boxes[0, i, 0] * im_height),
                             int(boxes[0, i, 1] * im_width),
                             int(boxes[0, i, 2] * im_height),
                             int(boxes[0, i, 3] * im_width))

        return boxes_list, scores[0].tolist(), [
            int(x) for x in classes[0].tolist()
        ], int(num_det[0])
Exemplo n.º 4
0
    def update(self,
               frame,
               person_detection=None,
               head_detection=None,
               threshold=0.6):
        # If not detection, update tracker
        if person_detection is None:
            success, box = self.tracker_body.update(frame)
            self.box_body = box
            self.body_status = success
            if not success:
                self.md += 1

            if self.box_head is not None:
                success_head, box_head = self.tracker_head.update(frame)
                self.box_head = box_head

        # Re-init tracker if detection is available
        else:
            box = self.box_body
            detected = False
            for i, det in enumerate(person_detection):
                det_box = (det[1], det[0], det[3] - det[1], det[2] - det[0])
                iou = utils.IoU(det_box, box)

                if iou < threshold and head_detection is not None and head_detection[
                        i] != [] and self.box_head is not None:
                    h_det = head_detection[i]
                    det_hd = (h_det[1], h_det[0], h_det[3] - h_det[1],
                              h_det[2] - h_det[0])
                    iou_head = utils.IoU(det_hd, self.box_head)
                else:
                    iou_head = 0

                # Reinitialize
                if iou > threshold or iou_head > threshold:
                    if head_detection is not None and head_detection[i] != []:
                        h_det = head_detection[i]
                        det_hd = (h_det[1], h_det[0], h_det[3] - h_det[1],
                                  h_det[2] - h_det[0])
                    elif self.box_head is not None:
                        success_head, box_head = self.tracker_head.update(
                            frame)
                        self.box_head = box_head
                        det_hd = self.box_head
                    else:
                        det_hd = None

                    self.tracker_body = self.createTrackerByName()
                    self.tracker_head = self.createTrackerByName()

                    ok = self.tracker_body.init(frame, det_box)
                    if det_hd is not None:
                        try:
                            ok = self.tracker_head.init(frame, det_hd)
                            self.box_head = det_hd
                        except:
                            self.box_head = None

                    self.md = 0
                    self.box_body = det_box
                    self.body_status = True

                    person_detection.remove(det)
                    detected = True
                    if head_detection is not None:
                        del head_detection[i]
                    break

            if not detected:
                self.md += 5

        return [self.body_status, self.box_body]