Ejemplo n.º 1
0
    def run(self, td):
        currgp, currlp = self.ambimin.gp, self.ambimin.lp
        n = 1

        while n <= self.ambimin.mincnt:
            amb, sen, trees = self.find_ambiguity(currgp, currlp)
            assert amb
            ambi_parse = AmbiParse.parse(self, trees)
            gp = os.path.join(td, "%s.acc" % n)
            lp = os.path.join(td, "%s.lex" % n)
            print "currgp: %s, gp: %s " % (currgp, gp)
            MiniUtils.write_cfg_lex(ambi_parse.min_cfg, gp, currlp, lp)
            self.write_stat(gp)

            currgp = gp
            currlp = lp
            n += 1

        # run ambidexter on the minimised grammar
        _gp = self.ambidxt.filter(currgp, str(self.ambimin.duration))
        self.write_stat(_gp)

        if _gp is not None:
            return _gp, currlp

        return currgp, currlp
Ejemplo n.º 2
0
    def run_accent(self, sen, gp, lp, td):
        """ build parser in td using gp+lp, and parse sentence sen."""

        parser = Accent.compile(gp, lp)
        out = Accent.run(parser, sen)
        ambiparse = AmbiParse.parse(self, out)
        _gp = tempfile.mktemp('.acc', dir=td)
        _lp = tempfile.mktemp('.lex', dir=td)
        MiniUtils.write_cfg_lex(ambiparse.min_cfg, _gp, lp, _lp)

        return _gp, _lp
Ejemplo n.º 3
0
    def run(self, td):
        currgp, currlp = self.ambimin.gp, self.ambimin.lp
        n = 1

        while n <= self.ambimin.mincnt:
            amb, sen, trees = self.find_ambiguity(currgp, currlp)
            assert amb
            ambi_parse = AmbiParse.parse(self, trees)
            gp = os.path.join(td, "%s.acc" % n)
            lp = os.path.join(td, "%s.lex" % n)
            print "currgp: %s, gp: %s " % (currgp, gp)
            MiniUtils.write_cfg_lex(ambi_parse.min_cfg, gp, currlp, lp)
            self.write_stat(gp)

            currgp = gp
            currlp = lp
            n += 1

        # run ambidexter on the minimised grammar
        fltr = '-%s' % self.ambimin.fltr
        fltr_outg = '-%s' % self.ambimin.fltr_cfg_outfmt
        opts = ['-h', fltr, fltr_outg]
        t1 = time.time()
        _gp = self.ambidxt.filter(currgp, str(self.ambimin.ambi_duration), opts)
        t2 = time.time()
        self.write_stat(_gp)

        if _gp is None:
            return currgp, currlp 

        print "=> filtered grammar : " , _gp
        tp = self.fix_sym_tokens_bug(currgp, _gp, td)

        # run ambidexter on the minimised grammar
        opts = ['-pg', '-ik', '0']
        t = self.ambimin.ambi_duration - (t2 - t1)
        print "time remaining: " , t
        ambisen, accsen = self.ambidxt.ambiguous(tp, currlp, str(t), opts)
        print "accsen: " , accsen
        if accsen is not None:
            __gp, __lp = self.run_accent(accsen, tp, currlp, td)
            self.write_stat(__gp)
            return __gp, __lp

        # AmbiDexter didn't find anything
        self.write_stat(None)
        return tp, currlp
Ejemplo n.º 4
0
    def run(self, td):
        gp, lp = self.ambimin.gp, self.ambimin.lp
        amb, sen, trees = self.find_ambiguity(gp, lp)
        assert amb
        ambi_parse = AmbiParse.parse(self, trees)
        # save the minimised cfg, lex to target files
        _gp = os.path.join(td, "%s.acc" % 0)
        _lp = os.path.join(td, "%s.lex" % 0)
        print "gp: %s, _gp: %s " % (gp, _gp)
        MiniUtils.write_cfg_lex(ambi_parse.min_cfg, _gp, lp, _lp)

        # run ambidexter on the minimised grammar
        sen, r = self.ambidexter(_gp)
        if r != 0:
            return _gp, _lp, None

        return _gp, _gp, sen
Ejemplo n.º 5
0
def checkPositionDepthAndRGB(rgb, depth_transformed, depth):

    imgConts, conts = MiniUtils.getContours(rgb, depth_transformed)
    for i in conts:
        x, y, w, h = i[3]
        #print('RGB:%d DepthTrans: %d', np.size(rgb), np.size(depth_transformed))
        print(depth_transformed[x][y])
        '''
Ejemplo n.º 6
0
    def prune_cfg(self, cfg, lex):
        """ Given a grammar (gp), prune the unreachable rules.
            The cfg is directly manipulated.
        """
        unreachable = MiniUtils.unreachable_rules(cfg)
        if len(unreachable) > 0:
            print "=> unreachable: ", unreachable
            _rules = []
            for rule in cfg.rules:
                if rule.name not in unreachable:
                    _rules.append(rule)

            return CFG.CFG(lex, cfg.sym_tokens, _rules)

        return cfg
Ejemplo n.º 7
0
def main():
    k4a = PyK4A(
        Config(
            color_resolution=pyk4a.ColorResolution.RES_720P,
            color_format=pyk4a.ImageFormat.COLOR_BGRA32,
            depth_mode=pyk4a.DepthMode.NFOV_UNBINNED,
            synchronized_images_only=True,
        ))

    k4a.start()
    # getters and setters directly get and set on device
    k4a.whitebalance = 4500
    assert k4a.whitebalance == 4500
    k4a.whitebalance = 4510
    assert k4a.whitebalance == 4510
    windowID = ''  #Give id to the windows to know which to destroy when not needed
    BODY_PARTS = {
        "Nose": 0,
        "Neck": 1,
        "RShoulder": 2,
        "RElbow": 3,
        "RWrist": 4,
        "LShoulder": 5,
        "LElbow": 6,
        "LWrist": 7,
        "RHip": 8,
        "RKnee": 9,
        "RAnkle": 10,
        "LHip": 11,
        "LKnee": 12,
        "LAnkle": 13,
        "REye": 14,
        "LEye": 15,
        "REar": 16,
        "LEar": 17,
        "Background": 18
    }

    POSE_PAIRS = [["Neck", "RShoulder"], ["Neck", "LShoulder"],
                  ["RShoulder", "RElbow"], ["RElbow", "RWrist"],
                  ["LShoulder", "LElbow"], ["LElbow", "LWrist"],
                  ["Neck", "RHip"], ["RHip", "RKnee"], ["RKnee", "RAnkle"],
                  ["Neck", "LHip"], ["LHip", "LKnee"], ["LKnee", "LAnkle"],
                  ["Neck", "Nose"], ["Nose", "REye"], ["REye", "REar"],
                  ["Nose", "LEye"], ["LEye", "LEar"]]

    net = cv2.dnn.readNetFromTensorflow("graph_opt.pb")  ##weights
    inWidth = 654
    inHeight = 368
    thr = 0.2
    while 1:
        capture = k4a.get_capture()

        if np.any(capture.color) and np.any(capture.depth):
            checker, frame, length = bodyTracking(capture.color, net, inWidth,
                                                  inHeight, BODY_PARTS,
                                                  POSE_PAIRS, thr)
            frame_depth = np.clip(capture.depth, 0, 2**10 - 1)
            frame_depth >>= 2
            num_fingers, img_draw = handEstimator(frame_depth)
            imgConts, conts = MiniUtils.getContours(capture.color,
                                                    capture.transformed_depth,
                                                    filter=4)
            cv2.imshow('Hand', img_draw)
            cv2.imshow('rgb', capture.color)
            '''if checker:
                if length < 11:
                    if windowID != '01' and windowID != '':
                        cv2.destroyAllWindows()
                    cv2.imshow('Hand', img_draw)
                    windowID = '01'
                else:
                    if windowID != '10' and windowID != '':
                        cv2.destroyAllWindows()
                    cv2.imshow('Body', frame)
                    windowID = '10'
            else:
                cv2.imshow('Conts', imgConts)
                if windowID != '11' and windowID != '':
                    cv2.destroyAllWindows()
                windowID = '11'
                '''
            key = cv2.waitKey(1)
            if key == ord('q'):
                cv2.destroyAllWindows()
                break
    k4a.stop()
Ejemplo n.º 8
0
    def valid_cfg(self, cfg):
        if MiniUtils.cyclic(cfg):
            return False

        return True
Ejemplo n.º 9
0
def main():
    '''cv2.namedWindow("Trackbars")

    cv2.createTrackbar("L-H", "Trackbars", 0, 255, nothing)
    cv2.createTrackbar("L-S", "Trackbars", 0, 255, nothing)
    cv2.createTrackbar("L-V", "Trackbars", 0, 255, nothing)
    cv2.createTrackbar("U-H", "Trackbars", 0, 255, nothing)
    cv2.createTrackbar("U-S", "Trackbars", 0, 255, nothing)
    cv2.createTrackbar("U-V", "Trackbars", 0, 255, nothing)'''

    wP = 210
    hP = 297
    scale = 3

    img = cv2.imread('colorimg.bmp')
    depth = cv2.imread('depth.bmp')
    #cv2.rectangle(img, (489, 269), (612, 344), (0, 255, 0), 2)
    slot0 = [[489, 269], [612, 269], [489, 344], [612, 344]] #slot0
    slot0_mP = [550.5, 306.5]
    #cv2.rectangle(img, (645, 273), (780, 350), (0, 255, 0), 2)
    slot1 = [[645, 273], [780, 273], [645, 350], [780, 350]]#slot1
    slot1_mP = [712.5, 311.5]
    #cv2.rectangle(img, (805, 271), (920, 346), (0, 255, 0), 2)
    slot2= [[805, 271], [920, 271], [805, 346], [920, 346]]#slot2
    slot2_mP = [862.5, 308.5]
    #cv2.rectangle(img, (483, 377), (599, 470), (0, 255, 0), 2)
    slot3 = [[483, 377], [599, 377], [483, 470], [599, 470]]#slot3
    slot3_mP = [541, 423.5]
    #cv2.rectangle(img, (645, 377), (792, 474), (0, 255, 0), 2)
    slot4 = [[645, 377], [792, 377], [645, 474], [792, 474]]#slot4
    slot4_mP = [718.5, 425.5]
    #cv2.rectangle(img, (825, 382), (949, 475), (0, 255, 0), 2)
    slot5 = [[825, 382], [949, 382], [825, 475], [949, 475]]#slot5
    slot5_mP = [887, 428.5]
    slots = [slot0, slot1, slot2, slot3, slot4, slot5]
    midpointsSlots = [slot0_mP, slot1_mP, slot2_mP, slot3_mP, slot4_mP, slot5_mP]
    imgConts, conts = MiniUtils.getContours(img, depth, filter=4, showCanny=True)
    midpointsConts = MiniUtils.midPoints(conts)
    '''frame_depth = np.clip(capture.depth, 0, 2 ** 10 - 1)
    frame_depth >>= 2
    num_fingers, img_draw = handEstimator(frame_depth)
    if len(conts) > 0:
        switcher = {
            0: goalPosition[0] = [midpointsSlots[0], midpointsConts.pop(0)]
            1: goalPosition[1] = [midpointsSlots[1], midpointsConts.pop(0)]
            2: goalPosition[2] = [midpointsSlots[2], midpointsConts.pop(0)]
            3: goalPosition[3] = [midpointsSlots[3], midpointsConts.pop(0)]
            4: goalPosition[4] = [midpointsSlots[4], midpointsConts.pop(0)]
            5: goalPosition[5] = [midpointsSlots[5], midpointsConts.pop(0)]
            }
        switcher.get(num_fingers, 'Error')
        
    '''

    #print(len(conts))
    while 1:

        if len(conts) != 0:
            for obj in conts:
                cv2.polylines(imgConts, [obj[2]], True, (0, 255, 0), 2)
        #cv2.imshow('contss', imgConts)
        for slot in slots:
            cv2.rectangle(imgConts, (slot[0][0], slot[0][1]), (slot[3][0], slot[3][1]), (0, 255, 0), 2)
        cv2.imshow('img', img)
        key = cv2.waitKey(1)
        if key == ord('q'):
            cv2.destroyAllWindows()
            break