Exemplo n.º 1
0
class MainLoop(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.camera = Camera()
        self.detector = Detector()
        self.stop_loop = False
        self.telemetry = Telemetry()
        self.position_calculator = PositionCalculator()
        self.frame = None

    def run(self):

        if Values.PRINT_FPS:
            last_time = time.time()
            ind = 0
        while True:
            try:
                if self.stop_loop:
                    break

                #self.frame = cv2.imread("images/pole_new.jpg")
                self.frame = self.camera.get_frame()

                #self.frame = cv2.resize(self.frame, (Values.CAMERA_WIDTH, Values.CAMERA_HEIGHT))
                if self.frame is None:
                    continue

                detections = self.detector.detect(self.frame)
                self.telemetry.update_telemetry()
                self.position_calculator.update_meters_per_pixel(
                    self.telemetry.altitude)
                self.position_calculator.calculate_max_meters_area()
                for d in detections:
                    lat, lon = self.position_calculator.calculate_point_lat_long(
                        d.middle_point, self.telemetry.latitude,
                        self.telemetry.longitude, self.telemetry.azimuth)
                    d.update_lat_lon(lat, lon)
                    d.area_m = self.position_calculator.calculate_area_in_meters_2(
                        d.area)
                    d.draw_detection(self.frame)

                cv2.imshow("frame", self.frame)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

                if Values.PRINT_FPS:
                    ind += 1
                    if time.time() - last_time > 1:
                        print("FPS:", ind)
                        ind = 0
                        last_time = time.time()

            except Exception as e:
                print("Some error accrued: ", str(e))
        self.close()

    def close(self):
        self.camera.close()
        self.stop_loop = True
Exemplo n.º 2
0
def yolo():
    image = cv2.imread(args["image"])
    weightsPath = get_yolo_path("yolov3.weights")
    configPath = get_yolo_path("yolov3.cfg")
    labelsPath = get_yolo_path("coco.names")
    LABELS = open(labelsPath).read().strip().split("\n")
    np.random.seed(42)
    COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8")

    # load our YOLO object detector trained on COCO dataset (80 classes)
    print("[INFO] loading YOLO from disk...")
    net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)

    detector = Detector(net, image, args['confidence'], args['threshold'],
                        LABELS, COLORS)
    img = detector.detect()

    cv2.imshow("Image", img)
    cv2.waitKey(0)
Exemplo n.º 3
0
def run(opt):

    # output dir
    if os.path.exists(opt.save_dir):
        shutil.rmtree(opt.save_dir)
    os.makedirs(opt.save_dir)

    # load dataset
    dataset = Dataloader(source=opt.source, imgsz=opt.img_size).dataset

    # load object detection model, and weights
    detector = Detector(detector_type=opt.detector_type,
                        cfg_file=opt.detector_cfg_file)
    detector.run_through_once(opt.img_size)  # 空跑一次

    # load object tracking model
    tracker = Tracker(tracker_type=opt.tracker_type,
                      cfg_file=opt.tracker_cfg_file)

    # load pose detection model
    poser = Poser(poser_type=opt.poser_type, cfg_file=opt.poser_cfg_file)

    # load classifier model
    clssifier = Classifier(classifier_type=opt.classifier_type,
                           cfg_file=opt.classifier_cfg_file)

    print(detector.device, detector.cfg)
    filt_with_txt = False  # 先分析一下status标注文件.txt,存在的才进行检测,这样能加快速度
    if filt_with_txt:
        from classifier.data_analyse import anaylise_label
        label_ret = anaylise_label()
        label_stems = [x[0] for x in label_ret]

    for img_idx, (path, img, im0s, vid_cap) in enumerate(dataset):
        # print(type(img), type(im0s))
        # print(type(im0s), im0s.shape)
        if dataset.is_camera:
            im0s = im0s[0]
            path = f'{path[0]}/{img_idx:0<6}.jpg'
        if filt_with_txt:
            fold_stem = path.split('/')[-2]
            idx = label_stems.index(fold_stem)
            # print(fold_stem, label_stems, idx)
            img_stem = Path(path).stem
            valid_stems = [Path(x).stem for x in label_ret[idx][-1]]
            in_it = f'track_{img_stem}' in valid_stems
            # print(path, in_it, label_ret[idx][-1][0])
            if not in_it:
                continue
        # img: [3, w, h], preprocess, inference, NMS,
        det_ret = detector.detect(
            path, img,
            im0s)  # detect result: nparray, [num_obj, 6] 6: xyxy,conf,cls
        # detector.imshow(im0s, det_ret)
        # track
        tra_ret = tracker.track(
            det_ret,
            im0s)  # track result: list, [num_obj, 7], 7: xyxy, cls, tid, trace
        # print(tra_ret[:, 5])
        # tracker.imshow(im0s, tra_ret, path)
        # pose detect
        pose_ret = poser.detect_pose(tra_ret, im0s, path, return_type='zzd')
        # zzd format: np.array(object): [num_obj, 10],10: xyxy cls tid trace keypoints kp_score proposal_score
        # print(pose_ret)
        poser.imshow(im0s, pose_ret, path, resize=(1280, 720))
        # classifier
        if opt.feature_save_dir is not None:  # 保存特征的
            clssifier.build_and_save_feature(pose_ret,
                                             path,
                                             save_dir=opt.feature_save_dir)
            print(f'\rsaving features: [{img_idx + 1:>3}/{len(dataset)}] ',
                  end='')
            continue

        # status_ret = clssifier.detect_status(pose_ret, path, is_camera=dataset.is_camera)
        # zzd format: np.array(object): [num_obj, 12], 12: 比10多了status_idx和status
        # clssifier.imshow(im0s, status_ret, show_name='x', resize=(1280, 720))
        # print(status_ret)

        if img_idx == 10:
            if cv2.waitKeyEx(0) == ord('q'):
                raise StopIteration
Exemplo n.º 4
0
            if bot > height:
                y_offset -= 5
                bot = height
            if top < 0:
                y_offset += 5
                top = 0
            if right > width:
                x_offset -= 5
                right = width
            if left < 0:
                x_offset += 5
                left = 0

            prepared_frame = frame[top:bot, left:right]
            print(left, right, top, bot, altitude)
            detections = detector.detect(prepared_frame)

            for d in detections:
                d.draw_detection(prepared_frame)

            cv2.imshow("frame", prepared_frame)
            key = cv2.waitKey(0)
            print(key)

            if key == 97:
                x_offset -= 5
            elif key == 100:
                x_offset += 5
            elif key == 119:
                y_offset -= 5
            elif key == 115: