def __init__(self, arguments):
        self.arguments = arguments

        # Frame window dim
        self.winWidth = 640
        self.winHeight = 480

        actionPredictor_params.__init__(self)

        self.fps_time = 0
        #self.step = 15
        self.mode = {
            'Pose Estimation': 'estimation',
            'Tracking': 'tracking',
            'Action Recognition': 'recognition'
        }

        w, h = model_wh(self.arguments.resize)
        if w > 0 and h > 0:
            self.estimator = TfPoseEstimator(get_graph_path(
                self.arguments.model),
                                             target_size=(w, h))
        else:
            self.estimator = TfPoseEstimator(get_graph_path(
                self.arguments.model),
                                             target_size=(432, 368))

        self.cam = cv2.VideoCapture(self.arguments.camera)

        # Tracker based on Sort
        self.sort_max_age = 20
        self.sort_min_hit = 3
        self.tracker = Sort(self.sort_max_age, self.sort_min_hit)
Пример #2
0
def cal_eigenvalue(cov):
    """calculate eigenvalue of `cov` matrix"""
    eigenvalues, eigenvectors = np.linalg.eig(cov)
    eigen_dict = {value: idx for idx, value in enumerate(eigenvalues)}
    sort_handler = Sort()
    sort_eigenvalues = sort_handler.quick(eigenvalues)
    sort_eigenvalues = sort_eigenvalues[::-1]
    sort_eigenvectors = [eigenvectors[eigen_dict[sort_value]] for sort_value in sort_eigenvalues]
    return sort_eigenvalues, sort_eigenvectors
Пример #3
0
 def __init__(self, parent=None):
     super(Ui_MainWindow, self).__init__(parent)
     self.tracker = Sort(settings.sort_max_age, settings.sort_min_hit)
     self.timer_camera = QtCore.QTimer()
     self.cap = cv2.VideoCapture()
     self.CAM_NUM = 0
     self.set_ui()
     self.slot_init()
     self.__flag_mode = 0
     self.fps = 0.00
     self.data = {}
     self.memory = {}
     self.joints = []
     self.current = []
     self.previous = []
Пример #4
0
def get_reverse_seq_num(reverse_lst):
    """get the reverse order number"""
    num = len(reverse_lst)
    # map lst to minist range
    sort_handler = Sort()
    sort_lst = sort_handler.quick(reverse_lst.copy())
    sort_lst_dct = {item: i for i, item in enumerate(sort_lst)}
    insert_lst = [sort_lst_dct[item] for item in reverse_lst]
    # count
    new_lst = [0] * num
    reverse_num = 0
    for item in insert_lst:
        reverse_num += new_lst[item]
        for i in range(item):
            new_lst[i] += 1
    return reverse_num
Пример #5
0
def main():
    print("[INFO] sampling frames...")
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(
        'model/shape_predictor_5_face_landmarks.dat')
    time.sleep(2.0)
    stream = WebcamVideoStream(args['src']).start()
    fps = FPS().start()
    start = time.time()
    mot_tracker = Sort()
    grabbed, frame = stream.read()
    while grabbed:
        frame = cv2.resize(frame, (1280, 720))
        if fps._numFrames % 3 == 0:
            rects = detector(frame, 0)
        dets = np.array([get_pos_from_rect(rect) for rect in rects])
        ages = np.empty((len(dets)))
        genders = np.empty((len(dets)))
        if len(rects) > 0:
            shapes = dlib.full_object_detections()
            for rect in rects:
                shapes.append(predictor(frame, rect))
            faces = dlib.get_face_chips(frame, shapes, size=96, padding=0.4)
            faces = np.array(faces)
            faces = model.prep_image(faces)
            result = get_result(faces)
            genders, ages = model.decode_prediction(result)

        mot_tracker.update(dets, genders, ages)
        for tracker in mot_tracker.trackers:
            (left, top, right, bottom) = convert_x_to_bbox(
                tracker.kf.x[:4, :]).astype('int').flatten()
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
            age = tracker.smooth_age()
            gender = 'M' if tracker.smooth_gender() == 1 else 'F'
            cv2.putText(frame, "id: {} {} {}".format(tracker.id, gender, age),
                        (left - 10, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
        cv2.putText(frame, "{:.1f} FPS".format(fps.fps()), (1100, 50),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
        cv2.namedWindow("Frame", cv2.WINDOW_NORMAL)
        cv2.imshow("Frame", frame)
        key = cv2.waitKey(1) & 0xFF
        fps.update()
        grabbed, frame = stream.read()
    fps.stop()
    stream.release()
    cv2.destroyAllWindows()
Пример #6
0
 def __init__(self, parent=None):
     super(Ui_MainWindow, self).__init__(parent)
     self.tracker = Sort(settings.sort_max_age, settings.sort_min_hit)
     self.timer_camera = QtCore.QTimer()
     # self.cap = cv2.VideoCapture()
     # self.cap = cv2.VideoCapture()
     self.CAM_NUM = 0
     self.set_ui()
     self.slot_init()
     self.__flag_mode = 0
     self.fps = 0.00
     self.data = {}
     self.memory = {}
     self.joints = []
     self.current = []
     self.previous = []
     self.fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Be sure to use lower case
     self.out = cv2.VideoWriter('x.mp4', self.fourcc, 1, (432, 368))
Пример #7
0
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__(parent)
        self.tracker = Sort(settings.sort_max_age, settings.sort_min_hit)
        self.timer_camera = QtCore.QTimer()
        self.cap = cv2.VideoCapture()
        self.CAM_NUM = 0
        self.set_ui()
        self.slot_init()
        self.__flag_mode = 0
        self.fps = 0.00
        self.data = {}
        self.memory = {}
        self.joints = []
        self.current = []
        self.previous = []

        self.frames_count = 0  #用于计数每一次预测前摄入的帧数
        self.pred_num_frames = 16  #设置多少帧预测一次
        self.pred_data = {}  #用于存储每一次预测用的数据
        self.one_frame = []
def test_image_instances():
    true_data = get_img_points()
    instance_generator = InstanceGenerator()
    paths = instance_generator.get_paths()
    model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
    model.eval()
    seq_mot_tracker = Sort()
    sequence_path = os.path.join(CARRADA, SEQ_NAME)
    img_paths = sorted(
        glob.glob(os.path.join(sequence_path, 'camera_images', '*.jpg')))
    # img_path = img_paths[100]
    n_random_points = 0
    save_instances_masks = False
    for img_path in img_paths[72:80]:
        img_name = img_path.split('/')[-1].split('.')[0]
        # real_img_points = real_points[seq_name][img_name] # ['001114'][0]
        image = ImageInstances(img_path, model, paths, seq_mot_tracker,
                               n_random_points, save_instances_masks)
        image.update_instances()
        predicted_points = image.get_points()[img_name]
        if predicted_points != {}:
            assert list(predicted_points['000001']
                        [0]) == true_data[img_name]['001114'][0]
Пример #9
0
def kalman_filter_tracking(det_bb, video_n_frames, model_type):
    """
    This function assigns a track value to an object by using kalman filters.
    It also adjust bounding box coordinates based on tracking information.
    """
    bb_id_updated = []
    tracker = Sort(model_type=model_type)
    for frame_num in range(1, video_n_frames + 1):
        #Get only bb of current frame
        dets_all_info = list(filter(lambda x: x[0] == frame_num, det_bb))
        dets = np.array([[bb[3], bb[4], bb[5], bb[6]]
                         for bb in dets_all_info])  #[[x1,y1,x2,y2]]
        #Apply kalman filtering
        trackers = tracker.update(dets)
        #Obtain id and bb in correct format
        for bb_dets, bb_update in zip(dets_all_info, trackers):
            bb_id_updated.append([
                bb_dets[0], bb_dets[1],
                int(bb_update[4]), bb_update[0], bb_update[1], bb_update[2],
                bb_update[3], bb_dets[7]
            ])
            # bb_id_updated.append([bb_dets[0], bb_dets[1], int(bb_update[4]), bb_dets[3],bb_dets[4], bb_dets[5], bb_dets[6], bb_dets[7]])
    return bb_id_updated
Пример #10
0
 def _process_images(self, seq_name, n_random_points=False, save_points=True, save_boxes=False,
                     save_masks=False, save_labels=False, save_instances_masks=True):
     """Process the entire sequence to segment images and save projected centroid"""
     sequence_path = os.path.join(self.paths['carrada'], seq_name)
     seq_mot_tracker = Sort()
     points = dict()
     boxes = dict()
     masks = dict()
     labels = dict()
     points[seq_name] = dict()
     boxes[seq_name] = dict()
     masks[seq_name] = dict()
     labels[seq_name] = dict()
     img_paths = sorted(glob.glob(os.path.join(sequence_path, 'camera_images', '*.jpg')))
     print('*** Processing Sequence:  %s ***' % seq_name)
     for img_path in img_paths:
         image = ImageInstances(img_path, self.model, self.paths,
                                seq_mot_tracker, n_random_points,
                                save_instances_masks)
         image.update_instances()
         if isinstance(n_random_points, int):
             points[seq_name].update(image.get_points())
         if save_boxes:
             boxes[seq_name].update(image.get_boxes())
         if save_masks:
             masks[seq_name].update(image.get_masks())
         if save_labels:
             labels[seq_name].update(image.get_labels())
     self._set_points(points)
     if save_points:
         self._save_data(points, seq_name, 'points.json')
     if save_boxes:
         self._save_data(boxes, seq_name, 'boxes.json')
     if save_masks:
         self._save_data(masks, seq_name, 'masks.json')
     if save_labels:
         self._save_data(labels, seq_name, 'labels.json')
def main():

    # Ground truth file path:

    if INPUT == 'gt_txt':

        gt_file = os.path.join(ROOT_DIR, 'data', 'AICity_data', 'train', 'S03',
                               'c010', 'gt', 'gt.txt')

        # Get BBox detection from list
        df = ut.get_bboxes_from_MOTChallenge(gt_file)

    elif INPUT == 'yolo3':

        gt_file = os.path.join(ROOT_DIR, 'data', 'AICity_data', 'train', 'S03',
                               'c010', 'det', 'det_yolo3.txt')

        # Get BBox detection from list
        df = ut.get_bboxes_from_MOTChallenge(gt_file)

    elif INPUT == 'gt_xml':

        gt_file = os.path.join(ROOT_DIR, 'data', 'AICity_data', 'train', 'S03',
                               'c010', 'gt', 'm6-full_annotation_crop.xml')

        # Get BBox detection from list
        df = ut_xml.get_bboxes_from_aicity_file(gt_file)

        # Adapt GT to final metric calculation (add bbox and track_id columns):
        df.loc[:, 'track_id'] = df['id'].values.tolist()
        df.loc[:, 'img_id'] = df['frame'].values.tolist()

        boxes = []
        for index, row in df.iterrows():
            #boxes.append([row['ytl'], row['xtl'], row['ybr'], row['xbr']])
            boxes.append([row['xtl'], row['ytl'], row['xbr'], row['ybr']])

        df['boxes'] = boxes

    elif INPUT == 'cnn_out':

        #gt_file = os.path.join(ROOT_DIR,
        #                      'data', 'AICity_data', 'train', 'S03',
        #                      'c010', 'det', 'results_cnn.csv')

        gt_file = os.path.join(ROOT_DIR, 'data', 'AICity_data', 'train', 'S03',
                               'c010', 'det', 'results_for_map.csv')

        # Get BBox detection from list
        df = pd.read_csv(gt_file)

        # Adapt GT to final metric calculation (add bbox and track_id columns):

        boxes = []
        for index, row in df.iterrows():
            #boxes.append([row['ytl'], row['xtl'], row['ybr'], row['xbr']])
            #boxes.append([row['xtl'], row['ytl'], row['xbr'], row['ybr']])  # Total output from Cnn GOOD ONE!
            boxes.append([row['xbr'], row['ybr'], row['xtl'],
                          row['ytl']])  # Total output from Cnn TEST
        df['boxes'] = boxes

    # Display data:
    colours = np.random.rand(32, 3)  # Used only for display

    # Sort and group bbox by frame:
    df.sort_values(by=['frame'])
    df_grouped = df.groupby('frame')

    total_time = 0.0
    total_frames = 0
    out = []

    if display:
        plt.ion()  # for iterative display
        fig, ax = plt.subplots(1, 2, figsize=(20, 20))

    # Create instance of the SORT tracker
    mot_tracker = Sort()

    for f, df_group in df_grouped:

        frame = int(df_group['frame'].values[0])
        print(frame)
        if frame > 220:
            break

        if INPUT == 'gt_txt' or INPUT == 'yolo3':
            df_gt = df_group[['ymin', 'xmin', 'ymax', 'xmax']].values.tolist()

        elif INPUT == 'gt_xml':
            df_gt = df_group[['ytl', 'xtl', 'ybr', 'xbr']].values.tolist()

        elif INPUT == 'cnn_out':
            #ToDo: When we correct the order of CNN output CHANGE this order to the same as gt_xml!!
            #df_gt = df_group[['ybr', 'xtl', 'ytl', 'xbr']].values.tolist()
            df_gt = df_group[['ybr', 'xbr', 'ytl', 'xtl']].values.tolist()

        # Reshape GT format for Kalman tracking algorithm:
        # [x1,y1,x2,y2] format for the tracker input:

        df_gt = np.asarray(df_gt)
        dets = np.stack([df_gt[:, 1], df_gt[:, 0], df_gt[:, 3], df_gt[:, 2]],
                        axis=1)
        dets = np.reshape(dets, (len(dets), -1))
        dets = np.asarray(dets, dtype=np.float64, order='C')

        if (display):
            fn = frames_dir + '/frame_%03d.jpg' % (frame)  # read the frame
            im = io.imread(fn)
            ax[0].imshow(im)
            ax[0].axis('off')
            ax[0].set_title('Original R-CNN detections (untracked)')
            for j in range(np.shape(dets)[0]):
                color = 'r'
                coords = (dets[j, 0].astype(np.float),
                          dets[j, 1].astype(np.float)), dets[j, 2].astype(
                              np.float) - dets[j, 0].astype(
                                  np.float), dets[j, 3].astype(
                                      np.float) - dets[j, 1].astype(np.float)
                ax[0].add_patch(
                    plt.Rectangle(*coords, fill=False, edgecolor=color, lw=3))

        total_frames += 1

        if (display):
            ax[1].imshow(im)
            ax[1].axis('off')
            ax[1].set_title('Tracked Targets')

        start_time = time.time()
        trackers = mot_tracker.update(dets)
        cycle_time = time.time() - start_time
        total_time += cycle_time

        out.append([frame, trackers])

        for d in trackers:
            if (display):
                d = d.astype(np.uint32)
                ax[1].add_patch(
                    patches.Rectangle((d[0], d[1]),
                                      d[2] - d[0],
                                      d[3] - d[1],
                                      fill=False,
                                      lw=3,
                                      ec=colours[d[4] % 32, :]))
                ax[1].set_adjustable('box-forced')

        if (save):
            plt.savefig(
                os.path.join(results_dir,
                             'video_kalman_' + str(frame) + '.png'))

        if (display):
            dp.clear_output(wait=True)
            dp.display(plt.gcf())
            time.sleep(0.000001)
            ax[0].cla()
            ax[1].cla()

    plt.show()

    print("Total Tracking took: %.3f for %d frames or %.1f FPS" %
          (total_time, total_frames, total_frames / total_time))

    ############################################################################################################

    # Result of Kalman tracking (pandas format):

    df_kalman = ut.kalman_out_to_pandas_for_map(out)

    if INPUT == 'gt_txt' or INPUT == 'yolo3':
        df_gt = ut.get_bboxes_from_MOTChallenge_for_map(gt_file)

    elif INPUT == 'gt_xml':
        df_gt = df

    elif INPUT == 'cnn_out':
        df_gt = None

    # If ground truth was used, save ground truth adapted to map_metric.py:

    if df_gt is not None:

        ut.save_pkl(df_gt, os.path.join(results_dir, INPUT + '_gt_panda.pkl'))
        df_gt_corr = ut.panda_to_json_gt(df_gt)
        ut.save_json(
            df_gt_corr,
            os.path.join(results_dir, INPUT + '_gt_ground_truth_boxes.json'))

    # Save kalman filter output:

    ut.save_pkl(df_kalman,
                os.path.join(results_dir, INPUT + '_kalman_predictions.pkl'))
    df_pred_corr = ut.panda_to_json_predicted(df_kalman)
    ut.save_json(df_pred_corr,
                 os.path.join(results_dir, INPUT + '_predicted_boxes.json'))
Пример #12
0
def main():

    for CAM in cameras:
        # Set useful directories
        frames_dir = os.path.join(ROOT_DIR, 'train', SEQ, CAM, 'frames')

        results_dir = os.path.join(OUTPUT_DIR, WEEK, TASK)

        # Create folders if they don't exist
        if not os.path.isdir(results_dir):
            os.mkdir(results_dir)

        tstamp_path = os.path.join(ROOT_DIR, 'cam_timestamp', SEQ + '.txt')
        time_offset, fps = ut.obtain_timeoff_fps(tstamp_path, CAM)

        # Ground truth file path:

        gt_det = os.path.join(ROOT_DIR, 'train', SEQ, CAM, 'det',
                              INPUT + '.txt')

        # Save gt file as pkl:

        if CREATE_GT_PKL == True:
            gt_txt = os.path.join(ROOT_DIR, 'train', SEQ, CAM, 'gt', 'gt.txt')
            save_gt_pkl = os.path.join(ROOT_DIR, 'train', SEQ, CAM, 'gt',
                                       'gt.pkl')
            code_ut.getBBox_from_gt(gt_txt, save_in=save_gt_pkl)

        # Get BBox detection from list
        df = ut.get_bboxes_from_MOTChallenge(gt_det)
        df.loc[:, 'time_stamp'] = df['frame'].values.tolist()

        # Display data:
        colours = np.random.rand(32, 3)  # Used only for display

        # Sort and group bbox by frame:
        df.sort_values(by=['frame'])
        df_grouped = df.groupby('frame')

        total_time = 0.0
        total_frames = 0
        out = []

        if display:
            plt.ion()  # for iterative display
            fig, ax = plt.subplots(1, 2, figsize=(20, 20))

        # Create instance of the SORT tracker
        mot_tracker = Sort()

        for f, df_group in df_grouped:

            frame = int(df_group['frame'].values[0])

            time_stamp = ut.timestamp_calc(frame, time_offset, fps)

            df_gt = df_group[['ymin', 'xmin', 'ymax', 'xmax']].values.tolist()

            # Reshape GT format for Kalman tracking algorithm:
            # [x1,y1,x2,y2] format for the tracker input:

            df_gt = np.asarray(df_gt)
            dets = np.stack(
                [df_gt[:, 1], df_gt[:, 0], df_gt[:, 3], df_gt[:, 2]], axis=1)
            dets = np.reshape(dets, (len(dets), -1))
            dets = np.asarray(dets, dtype=np.float64, order='C')

            if (display):
                fn = frames_dir + '/frame_%04d.jpg' % (frame)  # read the frame
                im = io.imread(fn)
                ax[0].imshow(im)
                ax[0].axis('off')
                ax[0].set_title('Original R-CNN detections (untracked)')
                for j in range(np.shape(dets)[0]):
                    color = 'r'
                    coords = (dets[j, 0].astype(np.float), dets[j, 1].astype(
                        np.float)), dets[j, 2].astype(
                            np.float) - dets[j, 0].astype(
                                np.float), dets[j, 3].astype(
                                    np.float) - dets[j, 1].astype(np.float)
                    ax[0].add_patch(
                        plt.Rectangle(*coords,
                                      fill=False,
                                      edgecolor=color,
                                      lw=3))

            total_frames += 1

            if (display):
                ax[1].imshow(im)
                ax[1].axis('off')
                ax[1].set_title('Tracked Targets')

            start_time = time.time()
            trackers = mot_tracker.update(dets)
            cycle_time = time.time() - start_time
            total_time += cycle_time

            out.append([frame, trackers, time_stamp])

            for d in trackers:
                if (display):
                    d = d.astype(np.uint32)
                    ax[1].add_patch(
                        patches.Rectangle((d[0], d[1]),
                                          d[2] - d[0],
                                          d[3] - d[1],
                                          fill=False,
                                          lw=3,
                                          ec=colours[d[4] % 32, :]))
                    ax[1].set_adjustable('box-forced')

            if (save):
                plt.savefig(
                    os.path.join(results_dir,
                                 'video_kalman_' + str(frame) + '.png'))

            if (display):
                dp.clear_output(wait=True)
                dp.display(plt.gcf())
                time.sleep(0.000001)
                ax[0].cla()
                ax[1].cla()

        plt.show()

        print("Total Tracking took: %.3f for %d frames or %.1f FPS" %
              (total_time, total_frames, total_frames / total_time))

        ############################################################################################################

        # Result of Kalman tracking (pandas format):

        df_kalman = ut.kalman_out_to_pandas_for_map(out)

        # Save kalman filter output:

        ut.save_pkl(
            df_kalman,
            os.path.join(results_dir,
                         INPUT + SEQ + CAM + '_kalman_predictions.pkl'))
Пример #13
0
def main(cap_queue):

    yolo = YOLO(res="1024")
    sort = Sort(iou_threshold=0.05)
    whitelist = [
        "person",
        "bicycle",
        "car",
        "motorbike",
        "aeroplane",
        "bus",
        "truck",
        "boat",
        "fire hydrant",
        "stop sign",
        "parking meter",
        "bird",
        "cat",
        "dog",
        "backpack",
        "umbrella",
        "handbag",
        "suitcase",
    ]
    frames = 0
    while True:
        frame, original_dim = cap_queue.get(block=True)
        frames += 1
        outputs = yolo.get_results(frame)
        detections = []
        labels = []

        for output in outputs:
            label = yolo.classes[int(output[-1])]
            if not label in whitelist:
                del label
                continue

            tl = tuple(output[1:3].int())
            br = tuple(output[3:5].int())

            detections.append(
                np.array(
                    [tl[0].item(), tl[1].item(), br[0].item(), br[1].item()]))
            labels.append(label)

            del tl, br, label
        del outputs

        detections = np.array(detections)
        should_write = False

        if detections.shape[0] > 0:
            try:
                alive, _ = sort.update(detections, labels)
            except IndexError:
                del frame, detections, labels
                continue

            should_write = False
            for trk in alive:
                t = trk.get_state()[0]
                try:
                    bbox = [int(t[0]), int(t[1]), int(t[2]), int(t[3])]
                except ValueError:
                    continue
                cv2.rectangle(
                    frame,
                    (int(bbox[0]), int(bbox[1])),
                    (int(bbox[2]), int(bbox[3])),
                    trk.color,
                    2,
                )
                cv2.putText(
                    frame,
                    "{}:id {}".format(trk.get_label(), str(trk.id)),
                    (int(bbox[0]), int(bbox[1]) - 12),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.0005 * frame.shape[0],
                    trk.color,
                    2,
                )
                for location in trk.locations:
                    x1, y1, x2, y2 = location[1]
                    cv2.circle(frame, (((int(x1) + int(x2)) // 2), int(y2)), 3,
                               trk.color, -1)
                    del x1, y1, x2, y2

                pred_x, pred_y = predict_location(trk, amount_to_predict=10)
                center_x = (int(bbox[0]) + int(bbox[2])) // 2
                center_y = (int(bbox[1]) + int(bbox[3])) // 2
                cv2.line(
                    frame,
                    (int(pred_x), int(pred_y)),
                    (center_x, center_y),
                    trk.color,
                    8,
                )
                # text = "{}".format(
                #     distance_from_xy((center_x, center_y), (int(pred_x), int(pred_y)))
                # )
                # midpoint = ((center_x + pred_x) // 2, (center_y + pred_y) // 2)
                # cv2.putText(frame, text, (int(midpoint[0]), int(midpoint[1])), cv2.FONT_HERSHEY_SIMPLEX, 1, trk.color)
                if len(alive) > 1:
                    for trk2 in alive:
                        if trk.id == trk2.id:
                            continue
                    ttc = time_til_collision(trk, trk2)
                    ttc_thresh = 5
                    if ttc > 0 and ttc < ttc_thresh:

                        print(
                            "Potential accident between ID {} and {}.\nTTC:{}s"
                            .format(trk.id, trk2.id, ttc))
                        cv2.putText(
                            frame,
                            "ID {} and {} have TTC {}".format(
                                trk.id, trk2.id, ttc),
                            (0, 20),
                            cv2.FONT_HERSHEY_SIMPLEX,
                            1,
                            (0, 0, 255),
                        )
                        should_write = True
                    del ttc_thresh, ttc

                del t, bbox, pred_x, pred_y, center_x, center_y
        # write_queue.put(frame[0 : original_dim[0], 0 : original_dim[1]])
        cv2.imshow("Object Tracking", frame[0:original_dim[0],
                                            0:original_dim[1]])

        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            raise KeyboardInterrupt
        del should_write
Пример #14
0
def main(cap_queue):
    yolo = YOLO()
    sort = Sort(iou_threshold=0.05)
    whitelist = [
        "person",
        "bicycle",
        "car",
        "motorbike",
        "aeroplane",
        "bus",
        "truck",
        "boat",
        "fire hydrant",
        "stop sign",
        "parking meter",
        "bird",
        "cat",
        "dog",
        "backpack",
        "umbrella",
        "handbag",
        "suitcase",
    ]
    tally = 0
    while tally < 1332:
        frame = cap_queue.get(block=True)
        if type(frame) == int and frame == -1:
            return
        outputs = yolo.get_results(frame)
        detections = []
        labels = []

        for output in outputs:
            label = yolo.classes[int(output[-1])]
            if not label in whitelist:
                del label
                continue

            tl = tuple(output[1:3].int())
            br = tuple(output[3:5].int())

            detections.append(
                np.array(
                    [tl[0].item(), tl[1].item(), br[0].item(), br[1].item()]))
            labels.append(label)

            del tl, br, label
        del outputs

        detections = np.array(detections)
        if detections.shape[0] > 0:
            try:
                alive, _ = sort.update(detections, labels)
            except IndexError:
                del frame, detections, labels
                continue

            for trk in alive:
                t = trk.get_state()[0]
                try:
                    bbox = [int(t[0]), int(t[1]), int(t[2]), int(t[3])]
                except ValueError:
                    continue
                cv2.rectangle(
                    frame,
                    (int(bbox[0]), int(bbox[1])),
                    (int(bbox[2]), int(bbox[3])),
                    trk.color,
                    2,
                )
                cv2.putText(
                    frame,
                    "{}:id {}".format(trk.get_label(), str(trk.id)),
                    (int(bbox[0]), int(bbox[1]) - 12),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.0005 * frame.shape[0],
                    trk.color,
                    2,
                )
                for location in trk.locations:
                    x1, y1, x2, y2 = location[1]
                    cv2.circle(frame, (((int(x1) + int(x2)) // 2), int(y2)), 3,
                               trk.color, -1)
                    del x1, y1, x2, y2

                if len(alive) > 1:
                    for trk2 in alive:
                        if trk == trk2:
                            continue
                        t2 = trk2.get_state()[0]
                        try:
                            bbox2 = [
                                int(t2[0]),
                                int(t2[1]),
                                int(t2[2]),
                                int(t2[3])
                            ]
                        except ValueError:
                            continue

                        d = distance(bbox, bbox2)
                        color = (255, 255, 255) if d > 50 else (0, 255, 255)

                        cv2.line(
                            frame,
                            ((bbox[0] + bbox[2]) // 2, int(bbox[3])),
                            ((bbox2[0] + bbox2[2]) // 2, int(bbox2[3])),
                            color,
                            2,
                        )
                        cv2.putText(
                            frame,
                            str(d),
                            midpoint(bbox, bbox2),
                            cv2.FONT_HERSHEY_SIMPLEX,
                            0.0005 * frame.shape[0],
                            color,
                            2,
                        )
                        del d

                del t, bbox

        cv2.imshow("Object Tracking", frame)
        cv2.imwrite("./tracked/{}.png".format(tally), frame)
        tally += 1
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            raise KeyboardInterrupt
Пример #15
0
EYE_AR_THRESH = 0.3
EYE_AR_CONSEC_FRAMES = 3

# initialize the frame counters and the total number of blinks
COUNTER = 0
TOTAL = 0
print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])
# load our serialized face detector from disk
print("[INFO] loading face detector...")
protoPath = os.path.sep.join([args["detector"], "deploy.prototxt"])
modelPath = os.path.sep.join(
    [args["detector"], "res10_300x300_ssd_iter_140000.caffemodel"])
net = cv2.dnn.readNetFromCaffe(protoPath, modelPath)
ct = Sort()
# load the liveness detector model and label encoder from disk
print("[INFO] loading liveness detector...")
model = load_model(args["model"])
le = pickle.loads(open(args["le"], "rb").read())
svm = pickle.loads(open(args["svm"], "rb").read())
# grab the indexes of the facial landmarks for the left and
# right eye, respectively
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

eye_detect = defaultdict(int)
obj_label = defaultdict(str)
# initialize the video stream and allow the camera sensor to warmup
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()