Exemple #1
0
class HandTracker:
    def __init__(self, model="data/model/gopro.pkl", nframe=150, display=False):
        self._hd = HandsDetector(model)
        self._nframe = nframe
        self._tracker = None
        self._vc = None
        self._display = display

    def run(self, vf):
        fr = FrameReader(vf)
        # assert os.path.exists(vf), '! video file...'
        # self._vc = cv2.VideoCapture(vf)
        state = State(self._nframe)
        strum = Strum()

        # while self._vc.isOpened():
        #    try:
        #        ret, frame = self._vc.read()
        #        assert frame.any()
        #    except AttributeError:
        #        break
        for frame in fr.next():

            frameg = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            if state.reinit():
                hands = self._hd.run(frame)
                pts = hands.get_points()
                self._tracker = PointTracker(frameg, pts)
            else:
                pts = self._tracker.run(frameg)

            strum.append(pts)
            state.update()

            if self._display:
                self.__show_points(frame, pts)
                key = cv2.waitKey(1)
                if key == ord("q"):
                    break
                elif key == ord("s"):
                    hands.save("hands.pck")

        return strum

    def __show_points(self, frame, pts):
        frame = np.array(frame)
        for pt in pts:
            pt = pt.astype(np.int32)
            cv2.circle(frame, tuple(pt), 8, (255, 0, 0), -1)
        cv2.imshow("pts", frame)
Exemple #2
0
    def run(self, vf):
        fr = FrameReader(vf)
        # assert os.path.exists(vf), '! video file...'
        # self._vc = cv2.VideoCapture(vf)
        state = State(self._nframe)
        strum = Strum()

        # while self._vc.isOpened():
        #    try:
        #        ret, frame = self._vc.read()
        #        assert frame.any()
        #    except AttributeError:
        #        break
        for frame in fr.next():

            frameg = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            if state.reinit():
                hands = self._hd.run(frame)
                pts = hands.get_points()
                self._tracker = PointTracker(frameg, pts)
            else:
                pts = self._tracker.run(frameg)

            strum.append(pts)
            state.update()

            if self._display:
                self.__show_points(frame, pts)
                key = cv2.waitKey(1)
                if key == ord("q"):
                    break
                elif key == ord("s"):
                    hands.save("hands.pck")

        return strum