Exemplo n.º 1
0
def test_camera():
    """ Simple test srcipt; requires /dev/video0 """
    app = ObjectDetectorTF(gpu=False, cmap={1: 'person'})

    cam = cv2.VideoCapture(0)

    fps = []

    while True:
        ret, img = cam.read()
        if not ret:
            print('camera capture failed')
            break
        t0 = time.time()
        res = app(img)
        t1 = time.time()
        fps.append(1.0 / (t1 - t0 + 1e-9))
        msk = (res['score'] > 0.5)

        cls = res['class'][msk]
        box = res['box'][msk]
        score = res['score'][msk]
        for box_, cls_ in zip(box, cls):
            #ry0,rx0,ry1,rx1 = box_ # relative
            draw_bbox(img, box_, str(cls_))
        cv2.imshow('win', img)
        k = cv2.waitKey(1)
        if k in [ord('q'), 27]:
            print('quitting...')
            break
        print('average fps: {}'.format(np.mean(fps[-100:])))
Exemplo n.º 2
0
    def data_cb(self, src, img, stamp):
        # TODO : save data in cam_ and run data_cb in step()
        # instead of inside the callback. May run into strange race conditions.
        now = rospy.Time.now()
        if (now - stamp).to_sec() > 0.5:
            rospy.loginfo_throttle(
                10.0, 'incoming data too old: [{}] @ {}'.format(src, stamp))
            # too old
            return

        if not (src in self.cam_):
            # should usually not reach here - error
            return
        cam = self.cam_[src]

        if not cam.has_info_:
            # should usually not reach here, maybe the first few frames
            return

        if self.dbg_:
            # continuous detection
            rsp = self.detect_cb(
                DetectRequest(source=src,
                              track=False,
                              cid=DetectRequest.CID_NULL))
            if rsp.success:
                box = BoxUtils.convert(t.box_, BoxUtils.FMT_NCCWH,
                                       BoxUtils.FMT_NYXYX)
                draw_bbox(img, box, cls=None)
            cv2.imshow('win', img)
            cv2.waitKey(1)
Exemplo n.º 3
0
def test_image(img='/tmp/image1.jpg'):
    """
    Simple test script; requires /tmp/image1.jpg
    """
    app = ObjectDetectorTF(model='model')
    img = cv2.imread(img)
    res = app(img)
    msk = (res['score'] > 0.5)

    cls = res['class'][msk]
    box = res['box'][msk]
    score = res['score'][msk]
    print('score', score)

    for box_, cls_ in zip(box, cls):
        #ry0,rx0,ry1,rx1 = box_ # relative
        draw_bbox(img, box_, str(cls_))

    cv2.imshow('win', img)
    cv2.waitKey(0)
Exemplo n.º 4
0
def test_images(imgdir, recursive=True, is_root=True, shuffle=True, viz=True):
    """
    Simple test script; operating on a directory
    """
    #app = ObjectDetectorTF()
    app = ObjectDetectorTF(
        gpu=0.5,
        #model='model2-drone-300x300',
        #model='model4-drone-300x300',
        model='model',
        cmap={
            1: 'drone',
            2: 'person'
        },
        threshold=0.5,
        threshold2=(0.375, 0.7))

    if is_root and viz:
        cv2.namedWindow('win', cv2.WINDOW_NORMAL)

    fs = os.listdir(imgdir)
    full = False
    if shuffle:
        np.random.shuffle(fs)

    for f in fs:
        f = os.path.join(imgdir, f)
        if os.path.isdir(f):
            if not recursive:
                continue
            if not test_images(
                    f, recursive, is_root=False, shuffle=shuffle, viz=viz):
                break

        img = cv2.imread(f)
        if img is None:
            continue

        #res = app(img[..., ::-1])
        res = app.detect(
            img,
            is_bgr=True,
        )

        #msk = (res['score'] > 0.5)
        #if np.count_nonzero(msk) <= 0:
        #    continue

        cls = res['class']
        box = res['box']
        score = res['score']
        print('scores', score)
        if viz:
            for box_, cls_, val_ in zip(box, cls, score):
                draw_bbox(img, box_, '{}:{}'.format(cls_, val_))
            cv2.imshow('win', img)
            k = cv2.waitKey(0)
            if k in [27, ord('q')]:
                break
    else:
        # went through all images without interruption
        full = True

    if is_root and viz:
        cv2.destroyWindow('win')
        cv2.destroyAllWindows()

    return full
Exemplo n.º 5
0
    def process(self, src, img, stamp):
        # source validation
        cam = self.cam_[src]
        if not cam.has_info_:
            # should usually not reach here, maybe the first few frames
            return

        obs_new = []

        # remove stale requests
        self.reqs_ = self.filter_reqs(stamp, self.reqs_)

        if len(self.reqs_) > 0:
            print(len(self.reqs_))

        req_msk = np.full(len(self.reqs_), True, dtype=np.bool)

        if len(self.reqs_) > 0:
            # TODO : looking out into the future with multiple parallel detectors ...
            # d_res = []
            # for d_cid, det in self.det_:
            #    if not np.any(d_cid in self.reqs_):
            #        continue
            #    d_res.append( det.detect(img) )
            d_res = self.det_.detect(img, is_bgr=True)
            for d in zip(d_res['class'], d_res['box'], d_res['score']):
                d_add = False  # flag to add detection to observations
                for i_r, r in enumerate(self.reqs_):
                    if not req_msk[i_r]:
                        # already matched request
                        continue
                    if self.match_detection(r, d):
                        req_msk[i_r] = False
                        d_add = True
                        break

                if not d_add:
                    continue

                obs_new.append(
                    TrackData(
                        src=src,
                        cid=d[0],
                        img=img,
                        box=BoxUtils.convert(d[1], BoxUtils.FMT_NYXYX,
                                             BoxUtils.FMT_NCCWH),
                        stamp=stamp.to_sec(),
                        meta=
                        None  # << will be populated by TrackManager() if the observation is accepted
                    ))

        self.reqs_ = [r for (r, m) in zip(self.reqs_, req_msk) if m]

        # append!
        self.mgr_.append(obs_new)
        self.mgr_.process(src, img, stamp.to_sec())

        for t in self.mgr_.get_tracks():
            if (t.src_ != src):
                continue
            draw_bbox(img, t.box_, fmt=BoxUtils.FMT_NCCWH, cls=None)

            iw, ih = cam.model_.width, cam.model_.height
            cx, cy = np.multiply(t.box_[:2], [iw, ih])
            ray3d = cam.model_.projectPixelTo3dRay([cx, cy])

            # self.pub_.publish(PointStamped(
            #    header=Header(frame_id=cam.model_.tfFrame(), stamp=stamp),
            #    point=Point(*ray3d)
            #    ))

        if self.dbg_:
            cv2.imshow('win', img)
            cv2.waitKey(1)