Exemplo n.º 1
0
 def write_output_file(self, output, filenames):
     with open(os.path.join(self.working_directory, output),
               'a+') as fout, fileinput.input(filenames) as fin:
         for line in fin:
             fout.write(line)
     if os.path.exists(filenames[0]):
         if self.clean == True:
             logger.debug("Removed temp file: {}".format(filenames[0]))
             os.remove(filenames[0])
Exemplo n.º 2
0
    def execute_threads(self, split_sequences):
        output = multiprocessing.Queue()
        processes = []
        for ind in range(len(split_sequences)):
            process = multiprocessing.Process(target=self.prodigal_run,
                                              args=(
                                                  split_sequences[ind],
                                                  output,
                                              ))
            process.start()
            processes.append(process)
        results = [output.get() for process in processes]
        for process in processes:
            process.join()

        f_path, f_name = os.path.split(os.path.abspath(self.input_file))

        output_dna_orf = "{wd}/{tmp_name}.temp.contigToORF.fsa".format(
            wd=self.working_directory, tmp_name=f_name)
        output_prot_orf = "{wd}/{tmp_name}.temp.contig.fsa".format(
            wd=self.working_directory, tmp_name=f_name)
        output_draft = "{wd}/{tmp_name}.temp.draft".format(
            wd=self.working_directory, tmp_name=f_name)
        output_potential_genes = "{wd}/{tmp_name}.temp.potentialGenes".format(
            wd=self.working_directory, tmp_name=f_name)

        # combine results
        for i in results:
            for j in i:
                o_f_path, o_f_name = os.path.split(os.path.abspath(j))
                self.write_output_file(output_dna_orf, [
                    "{wd}/{tmp_name}.temp.contigToORF.fsa".format(
                        wd=self.working_directory, tmp_name=o_f_name)
                ])
                self.write_output_file(output_prot_orf, [
                    "{wd}/{tmp_name}.temp.contig.fsa".format(
                        wd=self.working_directory, tmp_name=o_f_name)
                ])
                self.write_output_file(output_draft, [
                    "{wd}/{tmp_name}.temp.draft".format(
                        wd=self.working_directory, tmp_name=o_f_name)
                ])
                self.write_output_file(output_potential_genes, [
                    "{wd}/{tmp_name}.temp.potentialGenes".format(
                        wd=self.working_directory, tmp_name=o_f_name)
                ])
        # remove temps directories
        tmp = os.path.join(self.working_directory,
                           "{}.temp.directory".format(f_name))
        if os.path.exists(tmp):
            if self.clean == True:
                logger.debug("Removed directory: {}".format(tmp))
                shutil.rmtree(tmp)
        return results
Exemplo n.º 3
0
def delete_tail_track(probe, batch_tracks, fids, refine=False, id=None):
    if not refine:
        id = int(get_trackid(probe[0]))
    else:
        assert id is not None
        id = str(id)
    while (len(probe) > 0 and float(get_score(probe[-1])) < 0):
        frameid = int(get_frameid(probe[-1])) - fids[0]
        logger.debug('refine trackid {} in frame {}'.format(
            id, int(get_frameid(probe[-1]))))
        del batch_tracks[frameid][id]
        del probe[-1]
Exemplo n.º 4
0
def compress_by_hog_feature(names, hs):
    id_choosen = [0]
    for i in range(len(hs) - 1):
        if np.linalg.norm(hs[i + 1] - hs[id_choosen[-1]]) > 5.0:
            id_choosen.append(i + 1)
    if (len(names) - 1) not in id_choosen:
        id_choosen.append(len(names) - 1)
    res = []
    for i, x in enumerate(names):
        if i in id_choosen:
            res.append(x)
    logger.debug('from: ', len(names), " to: ", len(id_choosen),
                 "with indices: ", id_choosen)
    return res
Exemplo n.º 5
0
 def update_track(self, frame, track_id, dbox):
     assert self.face_tracks[
         track_id].active, "Inactive track cannot be updated"
     if KALMAN:
         update = 1
         self.face_tracks[track_id].tracker.update(to_xywh(dbox))
     else:
         update = self.face_tracks[track_id].tracker.init(
             frame, to_xywh(dbox))
     self.face_tracks[track_id].last_frame = frame
     if not update:
         logger.debug(
             "cannot update tracker for obj [{:.1f}, {:.1f}, {:.1f}, {:.1f}] with id {}"
             .format(dbox[0], dbox[1], dbox[2], dbox[3], track_id))
     else:
         logger.debug(
             "update box - [{:.1f}, {:.1f}, {:.1f}, {:.1f}] with id {}".
             format(dbox[0], dbox[1], dbox[2], dbox[3], track_id))
         if self.face_tracks[track_id].life_length < 20:
             self.face_tracks[track_id].life_length += 1
Exemplo n.º 6
0
 def create_track(self, frame, bbox):
     self.track_count += 1
     roi = to_xywh(bbox)
     if KALMAN:
         tracker = KalmanTracker(roi)
         init = 1
     else:
         tracker = cv2.TrackerKCF_create()
         init = tracker.init(frame, roi)
     if not init:
         logger.debug(
             "cannot create tracker for obj [{:.1f}, {:.1f}, {:.1f}, {:.1f}] with id {}"
             .format(bbox[0], bbox[1], bbox[2], bbox[3], self.track_count))
     else:
         logger.debug(
             "create tracker for obj [{:.1f}, {:.1f}, {:.1f}, {:.1f}] with id {}"
             .format(bbox[0], bbox[1], bbox[2], bbox[3], self.track_count))
         self.n_active_tracks += 1
     self.face_tracks[self.track_count] = FaceTracks(
         self.track_count, roi, tracker, bbox[4], frame)
Exemplo n.º 7
0
    def match_detection_tracking_hungarian(self, frame, detected_bboxes):
        # TODO: refactor this
        logger.debug('Start Hungarian')
        iou_matrix = np.zeros((len(self.face_tracks), len(detected_bboxes)))
        trois = {track_id: None for track_id in self.face_tracks}
        oks = {track_id: None for track_id in self.face_tracks}
        stopped_tracks = []
        unmatched_tracks = []
        for track_id, track_obj in self.face_tracks.items():
            if not track_obj.active:
                continue
            if KALMAN:
                ok = 1
                troi = track_obj.tracker.predict()
            else:
                ok, troi = track_obj.tracker.update(frame)
            trois[track_id] = troi
            oks[track_id] = ok
            if not ok:
                stopped_tracks.append(track_id)
                # track is lost
                logger.debug(
                    "losing track for obj [{:.1f}, {:.1f}, {:.1f}, {:.1f}] with id {:d}"
                    .format(track_obj.troi[0], track_obj.troi[1],
                            track_obj.troi[2], track_obj.troi[3], track_id))
                # update track status
                self.face_tracks[track_id].active = False
                # self.remove_track(track_id)
            else:
                for box_id, detected_bbox in enumerate(detected_bboxes):
                    iou_matrix[track_id,
                               box_id] = cal_iou(to_xyxy(troi), detected_bbox)

        cost_matrix = 1 - iou_matrix
        row_inds, col_inds = linear_sum_assignment(cost_matrix)
        detected_index = np.zeros((iou_matrix.shape[1]))
        track_index = np.zeros((iou_matrix.shape[0]))
        for i, row_ind in enumerate(row_inds):
            if iou_matrix[row_ind, col_inds[i]] > IOU_THRES:
                self.face_tracks[row_ind].troi = to_xywh(
                    detected_bboxes[col_inds[i]])
                self.face_tracks[row_ind].score = detected_bboxes[
                    col_inds[i]][4]
                self.update_track(frame, row_ind, detected_bboxes[col_inds[i]])
                detected_index[col_inds[i]] = 1
                track_index[row_ind] = 1
            elif oks[row_ind]:
                unmatched_tracks.append(row_ind)

        unmatched_detections = np.where(detected_index == 0)[0]
        iou_matrix = np.zeros(
            (len(unmatched_tracks), len(unmatched_detections)))
        for i, idx_t in enumerate(unmatched_tracks):
            track_obj = self.face_tracks[idx_t]
            found, tbox = track_obj.cf_predict(frame)
            if found:
                for j, idx_d in enumerate(unmatched_detections):
                    iou_matrix[i][j] = cal_iou(to_xyxy(tbox),
                                               detected_bboxes[idx_d])
        cost_matrix = 1 - iou_matrix
        row_inds, col_inds = linear_sum_assignment(cost_matrix)
        for i, row_ind in enumerate(row_inds):
            if iou_matrix[row_ind, col_inds[i]] > IOU_THRES:
                logger.debug('unmatched track is resurgent {}'.format(
                    unmatched_tracks[row_ind]))
                trackid = unmatched_tracks[row_ind]
                detection_idx = unmatched_detections[col_inds[i]]
                # self.face_tracks[trackid].active = True
                self.face_tracks[trackid].troi = to_xywh(
                    detected_bboxes[detection_idx])
                self.face_tracks[trackid].score = detected_bboxes[
                    detection_idx][4]
                self.update_track(frame, trackid,
                                  detected_bboxes[detection_idx])
                detected_index[detection_idx] = 1
                track_index[trackid] = 1

        unmatched_detections = np.where(detected_index == 0)[0]
        iou_matrix = np.zeros((len(stopped_tracks), len(unmatched_detections)))
        for i, idx_t in enumerate(stopped_tracks):
            track_obj = self.face_tracks[idx_t]
            found, tbox = track_obj.cf_predict(frame)
            if found:
                for j, idx_d in enumerate(unmatched_detections):
                    iou_matrix[i][j] = cal_iou(to_xyxy(tbox),
                                               detected_bboxes[idx_d])
        cost_matrix = 1 - iou_matrix
        row_inds, col_inds = linear_sum_assignment(cost_matrix)
        for i, row_ind in enumerate(row_inds):
            if iou_matrix[row_ind, col_inds[i]] > IOU_THRES:
                logger.debug('stopped track is resurgent {}'.format(
                    stopped_tracks[row_ind]))
                trackid = stopped_tracks[row_ind]
                detection_idx = unmatched_detections[col_inds[i]]
                self.face_tracks[trackid].active = True
                self.face_tracks[trackid].troi = to_xywh(
                    detected_bboxes[detection_idx])
                self.face_tracks[trackid].score = detected_bboxes[
                    detection_idx][4]
                self.update_track(frame, trackid,
                                  detected_bboxes[detection_idx])
                detected_index[detection_idx] = 1
                track_index[trackid] = 1

        for track_id, track_obj in self.face_tracks.items():
            if track_index[track_id] == 0:
                if (not self.allow_live) or (
                        self.face_tracks[track_id].life_length < 0):
                    self.face_tracks[
                        track_id].active = False  # this mean we kill it right away
                    self.face_tracks[track_id].score = -1
                    # self.remove_track(track_id)
                else:
                    self.face_tracks[track_id].troi = trois[track_id]
                    self.face_tracks[track_id].score = -1
                    self.face_tracks[track_id].life_length -= 1
        for box_id, detected_bbox in enumerate(detected_bboxes):
            if detected_index[box_id] == 0:
                self.create_track(frame, detected_bbox)
Exemplo n.º 8
0
    def start(self):
        print('Start process')

        if self.ofs.is_done():
            print('Processed! skip')
            return

        buffer = Buffer()
        batch = Batch(BATCH_SIZE)
        flag = 0
        batch_tracks = []
        dt_time = Timer()
        m_time = Timer()
        tt_time = Timer()

        self.ofs.init()
        for (frame_id, frame) in self.ifs.get_iter():
            height, width, _ = frame.shape
            batch.update(frame, frame_id, height, width)
            dt_time.tic()
            detection = self.face_detector.infer(
                frame, path=False)  # xmin, ymin, xmax, ymax
            logger.debug('Frame {} have {} detections'.format(
                frame_id, len(detection)))
            detection = np.clip(detection, a_min=0, a_max=None)
            self.face_tracker.match_detection_tracking_hungarian(
                frame, detection)
            results = self.face_tracker.get_active_track()
            list_tracks = {}
            dt_time.toc()
            # print('dt time: {}'.format(dt_time.average_time))
            for box in results['geometries']:
                id = box['id']
                xmin = box['xmin']
                ymin = box['ymin']
                width = box['width']
                height = box['height']
                score = box['score']
                list_tracks[id] = (xmin, ymin, width, height, score)
            batch_tracks.append(list_tracks)
            if batch.count % BATCH_SIZE == 0:
                if MATCHING:
                    if batch.count % (BATCH_SIZE * 2) == 0:
                        flag = 0
                        print('Start matching')
                        batch_tracks = buffer.temp_batchtracks + batch_tracks

                        fids = list(buffer.temp_fids) + list(batch.fids)
                        bitmaps = list(buffer.temp_bitmaps) + list(
                            batch.bitmaps)
                        m_time.tic()
                        main(self.matcher, batch_tracks, bitmaps, fids)
                        m_time.toc()
                        print('m time: {}'.format(m_time.average_time))
                        tt_time.tic()
                        for idx, tracks in zip(buffer.out_fids,
                                               buffer.out_batchtracks):
                            self.ofs.add(TrackResult(self.ifs.get(idx),
                                                     tracks))
                        self.ofs.flush()
                        buffer.out_batchtracks += batch_tracks[BATCH_SIZE:]
                        buffer.out_fids += fids[BATCH_SIZE:]
                        buffer.out_bitmaps += bitmaps[BATCH_SIZE:]
                    else:
                        flag = 1
                        print('Save batch')
                        buffer.temp_batchtracks = deepcopy(
                            batch_tracks)  # test this without deepcopy
                        buffer.temp_fids = deepcopy(list(batch.fids))
                        buffer.temp_bitmaps = deepcopy(list(batch.bitmaps))

                        print('Save out')
                        buffer.out_batchtracks += buffer.temp_batchtracks
                        buffer.out_fids += buffer.temp_fids
                        buffer.out_bitmaps += buffer.temp_bitmaps
                else:
                    for idx, tracks in zip(batch.fids, batch_tracks):
                        self.ofs.add(TrackResult(self.ifs.get(idx), tracks))
                    self.ofs.flush()
                batch_tracks = []

        # flush the queue -> process the remaining frames
        num_last = batch.count % BATCH_SIZE
        if num_last > 0:
            last_ids = list(islice(batch.fids, 0, BATCH_SIZE))[-num_last:]
            last_bitmaps = list(islice(batch.bitmaps, 0,
                                       BATCH_SIZE))[-num_last:]
            last_tracks = batch_tracks[-num_last:]
            if MATCHING:
                if flag == 0:
                    main(self.matcher, last_tracks, last_bitmaps, last_ids)
                    for idx, tracks in zip([
                            buffer.out_fids[i]
                            for i in range(BATCH_SIZE, BATCH_SIZE * 2)
                    ], [
                            buffer.out_batchtracks[i]
                            for i in range(BATCH_SIZE, BATCH_SIZE * 2)
                    ]):
                        self.ofs.add(TrackResult(self.ifs.get(idx), tracks))

                    for idx, tracks in zip(last_ids, last_tracks):
                        self.ofs.add(TrackResult(self.ifs.get(idx), tracks))
                    self.ofs.flush()
                else:
                    last_ids = list(buffer.temp_fids) + last_ids
                    last_bitmaps = list(buffer.temp_bitmaps) + last_bitmaps
                    last_tracks = buffer.temp_batchtracks + last_tracks
                    main(self.matcher, last_tracks, last_bitmaps, last_ids)

                    for idx, tracks in zip(
                        [buffer.out_fids[i] for i in range(0, BATCH_SIZE)], [
                            buffer.out_batchtracks[i]
                            for i in range(0, BATCH_SIZE)
                        ]):
                        self.ofs.add(TrackResult(self.ifs.get(idx), tracks))

                    for idx, tracks in zip(last_ids, last_tracks):
                        self.ofs.add(TrackResult(self.ifs.get(idx), tracks))
                    self.ofs.flush()
            else:
                for idx, tracks in zip(last_ids, last_tracks):
                    self.ofs.add(TrackResult(self.ifs.get(idx), tracks))
                self.ofs.flush()
        self.ofs.release()
Exemplo n.º 9
0
    def detect_track_match_frames(self, all_frames):
        print('Start process')
        buffer = Buffer()
        batch = Batch(BATCH_SIZE)
        flag = 0

        fourcc = cv2.VideoWriter_fourcc(*'MJPG')
        out_video = 'output/office3.mp4'
        height, width, _ = all_frames[0][1].shape
        writer = cv2.VideoWriter(out_video, fourcc, 30, (width, height), True)
        batch_tracks = []
        for (frame_id, frame) in all_frames:
            height, width, _ = frame.shape
            batch.update(frame, frame_id, height, width)
            detection = self.face_detector.infer(
                frame, path=False)  # xmin, ymin, xmax, ymax

            logger.debug('Frame {} have {} detections'.format(
                frame_id, len(detection)))
            detection = np.clip(detection, a_min=0, a_max=None)
            self.face_tracker.match_detection_tracking_hungarian(
                frame, detection)
            results = self.face_tracker.get_active_track()
            list_tracks = {}
            for box in results['geometries']:
                id = box['id']
                xmin = box['xmin']
                ymin = box['ymin']
                width = box['width']
                height = box['height']
                score = box['score']
                list_tracks[id] = (xmin, ymin, width, height, score)
            batch_tracks.append(list_tracks)
            if batch.count % BATCH_SIZE == 0:

                if MATCHING:
                    if batch.count % (BATCH_SIZE * 2) == 0:
                        flag = 0
                        print('Start matching')
                        batch_tracks = buffer.temp_batchtracks + batch_tracks

                        fids = list(buffer.temp_fids) + list(batch.fids)
                        bitmaps = list(buffer.temp_bitmaps) + list(
                            batch.bitmaps)
                        main(self.matcher, batch_tracks, bitmaps, fids)

                        for idx, tracks in zip(buffer.out_fids,
                                               buffer.out_batchtracks):
                            self.draw_frame(all_frames, idx, tracks, writer)

                        buffer.out_batchtracks += batch_tracks[BATCH_SIZE:]
                        buffer.out_fids += fids[BATCH_SIZE:]
                        buffer.out_bitmaps += bitmaps[BATCH_SIZE:]
                    else:
                        flag = 1
                        print('Save batch')
                        buffer.temp_batchtracks = deepcopy(
                            batch_tracks)  # test this without deepcopy
                        buffer.temp_fids = deepcopy(list(batch.fids))
                        buffer.temp_bitmaps = deepcopy(list(batch.bitmaps))

                        print('Save out')
                        buffer.out_batchtracks += buffer.temp_batchtracks
                        buffer.out_fids += buffer.temp_fids
                        buffer.out_bitmaps += buffer.temp_bitmaps
                else:
                    for idx, tracks in zip(batch.fids, batch_tracks):
                        self.draw_frame(all_frames, idx, tracks, writer)

                batch_tracks = []

        # flush the queue -> process the remaining frames
        num_last = batch.count % BATCH_SIZE
        if num_last > 0:
            last_ids = list(islice(batch.fids, 0, BATCH_SIZE))[-num_last:]
            last_bitmaps = list(islice(batch.bitmaps, 0,
                                       BATCH_SIZE))[-num_last:]
            last_tracks = batch_tracks[-num_last:]
            if MATCHING:
                if flag == 0:
                    main(self.matcher, last_tracks, last_bitmaps, last_ids)
                    for idx, tracks in zip([
                            buffer.out_fids[i]
                            for i in range(BATCH_SIZE, BATCH_SIZE * 2)
                    ], [
                            buffer.out_batchtracks[i]
                            for i in range(BATCH_SIZE, BATCH_SIZE * 2)
                    ]):
                        self.draw_frame(all_frames, idx, tracks, writer)

                    for idx, tracks in zip(last_ids, last_tracks):
                        self.draw_frame(all_frames, idx, tracks, writer)
                else:
                    last_ids = list(buffer.temp_fids) + last_ids
                    last_bitmaps = list(buffer.temp_bitmaps) + last_bitmaps
                    last_tracks = buffer.temp_batchtracks + last_tracks
                    main(self.matcher, last_tracks, last_bitmaps, last_ids)

                    for idx, tracks in zip(
                        [buffer.out_fids[i] for i in range(0, BATCH_SIZE)], [
                            buffer.out_batchtracks[i]
                            for i in range(0, BATCH_SIZE)
                        ]):
                        self.draw_frame(all_frames, idx, tracks, writer)

                    for idx, tracks in zip(last_ids, last_tracks):
                        self.draw_frame(all_frames, idx, tracks, writer)
            else:
                for idx, tracks in zip(last_ids, last_tracks):
                    self.draw_frame(all_frames, idx, tracks, writer)
        writer.release()