Exemplo n.º 1
0
class TrainPose():

    def __init__(self, id, nsamples, dst):
        self._id       = id
        self._nsamples = nsamples
        self._dst      = dst
        self._kinect   = Kinect()
        self._body     = BodyDetector()
        self._hand     = HandDetector()
        self._contour  = HandContourDetector()
        self._fdesc    = FourierDescriptors()
        self._train    = []

    def run(self):
        warmup = True
        for (depth8, depth, rgb) in self._kinect.get_data():
            contour = self._get_hand_contour(depth8, depth, rgb)
            if not contour:
                continue

            self._contour.draw()

            if warmup:
                key = cv2.waitKey(5)
                if key == GO:
                    warmup = False
                continue

            fd = self._fdesc.run(contour)
            self._train.append(fd)

            if len(self._train) == self._nsamples:
                self._save()
                break

            cv2.waitKey(5)

    def _get_hand_contour(self, depth8, depth, rgb):
        body = self._body.run(depth8)
        (hand, _) = self._hand.run(body)
        (cont, box, hc) = self._contour.run(hand)

        if self._contour.not_valid():
            return []

        (cont, _, _) = self._contour.run(rgb, True, box, hc, depth)

        return cont

    def _save(self):
        data = np.array(self._train)
        model = EmpiricalCovariance().fit(np.array(self._train))
        output = {'id': self._id, 'data': data,  'model': model}
        pickle.dump(output, open(self._dst, 'wb'))
Exemplo n.º 2
0
class TrainPose():
    def __init__(self, id, nsamples, dst):
        self._id = id
        self._nsamples = nsamples
        self._dst = dst
        self._kinect = Kinect()
        self._body = BodyDetector()
        self._hand = HandDetector()
        self._contour = HandContourDetector()
        self._fdesc = FourierDescriptors()
        self._train = []

    def run(self):
        warmup = True
        for (depth8, depth, rgb) in self._kinect.get_data():
            contour = self._get_hand_contour(depth8, depth, rgb)
            if not contour:
                continue

            self._contour.draw()

            if warmup:
                key = cv2.waitKey(5)
                if key == GO:
                    warmup = False
                continue

            fd = self._fdesc.run(contour)
            self._train.append(fd)

            if len(self._train) == self._nsamples:
                self._save()
                break

            cv2.waitKey(5)

    def _get_hand_contour(self, depth8, depth, rgb):
        body = self._body.run(depth8)
        (hand, _) = self._hand.run(body)
        (cont, box, hc) = self._contour.run(hand)

        if self._contour.not_valid():
            return []

        (cont, _, _) = self._contour.run(rgb, True, box, hc, depth)

        return cont

    def _save(self):
        data = np.array(self._train)
        model = EmpiricalCovariance().fit(np.array(self._train))
        output = {'id': self._id, 'data': data, 'model': model}
        pickle.dump(output, open(self._dst, 'wb'))
Exemplo n.º 3
0
class MultiPoseClassifier():
    def __init__(self, src):
        self._fourier_desc = FourierDescriptors()
        self._models = self._load_models(src)

    def run(self, contour):
        desc = self._fourier_desc.run(contour)
        dist = np.zeros(len(self._models))

        for i, model in enumerate(self._models):
            dist[i] = model['model'].mahalanobis([desc])

        pose = self._models[np.argsort(dist)[0]]['id']

        return pose

    def _load_models(self, src):
        models = []
        for s in src:
            data = pickle.load(open(s, 'rb'))
            models.append({'id': data['id'], 'model': data['model']})

        return models
Exemplo n.º 4
0
class MultiPoseClassifier():

    def __init__(self, src):
        self._fourier_desc = FourierDescriptors()
        self._models = self._load_models(src)

    def run(self, contour):
        desc = self._fourier_desc.run(contour)
        dist = np.zeros(len(self._models))

        for i, model in enumerate(self._models):
            dist[i] = model['model'].mahalanobis([desc])

        pose = self._models[np.argsort(dist)[0]]['id']

        return pose

    def _load_models(self, src):
        models = []
        for s in src:
            data = pickle.load(open(s, 'rb'))
            models.append({'id': data['id'], 'model': data['model']})

        return models