def extract_dynamic_texture(colors, states):
    """
    extract dynamic texture of given frame.
    three frames are given, (i-1)-th, i-th, (i+1)-th, and they are 3ch color image.
    convert them into gray-scale first, then extract dynamic texture.
    It returns feature of i-th frame.


    :param colors: 3ch color image
    :param states: states == step
    :return: float64 image [-1,1]
    """
    grays = []
    for i in range(states):
        gray = cv2.cvtColor(colors[i], cv2.COLOR_RGB2GRAY)
        grays.append(gray)

    size = grays[0].size
    shape = grays[0].shape
    length = len(grays)

    data = np.zeros((size, length))  # size of image, number of images

    for i in range(len(grays)):
        data[:, i] = grays[i].reshape(-1)

    lds = LinearDS(states, False, False)  # number of data
    lds.suboptimalSysID(data)

    for i in range(states):
        a = lds._Chat[:, i].reshape(shape)
        grays.append(a)

    return lds._Chat[:, 1].reshape(shape)
Beispiel #2
0
def test_ldsMartinDistance():
    """Test Martin distance computation for LDS's (5 states).

    Config: 5 states (observation centering - default).
    """

    fileA = os.path.join(TESTBASE, "data/data1.txt")
    fileB = os.path.join(TESTBASE, "data/data2.txt")

    # load data files
    dataA, _ = loadDataFromASCIIFile(fileA)
    dataB, _ = loadDataFromASCIIFile(fileB)

    ldsA = LinearDS(5, False, False)
    ldsB = LinearDS(5, False, False)

    # estimate LDS's
    ldsA.suboptimalSysID(dataA)
    ldsB.suboptimalSysID(dataB)

    # compute distances A<->A, A<->B
    dAA = ldsMartinDistance(ldsA, ldsA, 20)
    dAB = ldsMartinDistance(ldsA, ldsB, 20)

    truth = np.genfromtxt(os.path.join(TESTBASE, "data/ldsMartinDistanceData1Data2.txt"))
    np.testing.assert_almost_equal(dAB, truth, 2)
    np.testing.assert_almost_equal(dAA, 0, 2)
Beispiel #3
0
def test_LinearDS_suboptimalSysID(): 
    """Test LinearDS system identification.
    """
    
    dataFile = os.path.join(TESTBASE, "data/data1.txt")
    data, _ = loadDataFromASCIIFile(dataFile)
     
    lds = LinearDS(5, False, False)
    lds.suboptimalSysID(data)
     
    baseLDSFile = os.path.join(TESTBASE, "data/data1-dt-5c-center.pkl")
    baseLDS = pickle.load(open(baseLDSFile))
     
    _, err = LinearDS.stateSpaceMap(baseLDS, lds)
    assert np.allclose(err, 0.0) == True
Beispiel #4
0
def test_LinearDS_suboptimalSysID():
    """Test LinearDS system identification.
    """

    dataFile = os.path.join(TESTBASE, "data/data1.txt")
    data, _ = loadDataFromASCIIFile(dataFile)

    lds = LinearDS(5, False, False)
    lds.suboptimalSysID(data)

    baseLDSFile = os.path.join(TESTBASE, "data/data1-dt-5c-center.pkl")
    baseLDS = pickle.load(open(baseLDSFile))

    err = ldsMartinDistance(lds, baseLDS, N=20)
    np.testing.assert_almost_equal(err, 0, 2)
Beispiel #5
0
def main(argv=None):
    if argv is None: 
        argv = sys.argv

    parser = OptionParser(add_help_option=False)
    parser.add_option("-p", dest="pFile")
    parser.add_option("-i", dest="iFile") 
    parser.add_option("-t", dest="iType")
    parser.add_option("-o", dest="oFile")
    parser.add_option("-n", dest="nStates", type="int", default=+5)
    parser.add_option("-m", dest="doMovie", type="int", default=-1)
    parser.add_option("-a", dest="svdRand", action="store_true", default=False)
    parser.add_option("-e", dest="doEstim", action="store_true", default=False)
    parser.add_option("-s", dest="doSynth", action="store_true", default=False)
    parser.add_option("-h", dest="shoHelp", action="store_true", default=False)
    parser.add_option("-v", dest="verbose", action="store_true", default=False) 
    opt, args = parser.parse_args()
    
    if opt.shoHelp: 
        usage()
    
    dataMat = None
    dataSiz = None
    try:
        if opt.iType == 'vFile':
            (dataMat, dataSiz) = dsutil.loadDataFromVideoFile(opt.iFile)
        elif opt.iType == 'aFile':
            (dataMat, dataSiz) = dsutil.loadDataFromASCIIFile(opt.iFile)
        elif opt.iType == 'lFile':
            (dataMat, dataSiz) = dsutil.loadDataFromIListFile(opt.iFile)
        else:
            msg.fail("Unsupported file type : %s", opt.iType)    
            return -1
    except Exception as e:
        dsinfo.fail(e)
        return -1

    try:
        # try loading the DT model
        if not opt.pFile is None:
            with open(opt.pFile) as fid:
                dsinfo.info('trying to load model %s' % opt.pFile)
                dt = pickle.load(fid)

        # run estimation
        if opt.doEstim:
            if not opt.pFile is None:
                dsinfo.fail('re-estimation attempt detected!')
                return -1
            dt = LinearDS(opt.nStates, approx=opt.svdRand, verbose=opt.verbose)
            dt.suboptimalSysID(dataMat)

        # synthesize output
        if opt.doSynth:
           dataSyn, _ = dt.synthesize(tau=50, mode='s')

        # show a movie of the synthesis result
        if opt.doMovie > 0:
            if opt.doSynth:
                dsutil.showMovie(dataSyn, dataSiz, fps=opt.doMovie)

        # write DT model to file
        if not opt.oFile is None:
            dsinfo.info('writing model to %s' % opt.oFile)
            with open(opt.oFile, 'w') as fid:
                pickle.dump(dt, fid)
     
    # catch pyds exceptions
    except ErrorDS as e:
        dsinfo.fail(e)
        return -1
Beispiel #6
0
def main(argv=None):
    if argv is None: 
        argv = sys.argv

    parser = OptionParser(add_help_option=False)
    parser.add_option("-p", dest="pFile")
    parser.add_option("-i", dest="iFile") 
    parser.add_option("-t", dest="iType")
    parser.add_option("-o", dest="oFile")
    parser.add_option("-n", dest="nStates", type="int", default=+5)
    parser.add_option("-m", dest="doMovie", type="int", default=-1)
    parser.add_option("-a", dest="svdRand", action="store_true", default=False)
    parser.add_option("-e", dest="doEstim", action="store_true", default=False)
    parser.add_option("-s", dest="doSynth", action="store_true", default=False)
    parser.add_option("-h", dest="shoHelp", action="store_true", default=False)
    parser.add_option("-v", dest="verbose", action="store_true", default=False) 
    opt, args = parser.parse_args()
    
    if opt.shoHelp: 
        usage()
    
    dataMat = None
    dataSiz = None
    try:
        if opt.iType == 'vFile':
            (dataMat, dataSiz) = dsutil.loadDataFromVideoFile(opt.iFile)
        elif opt.iType == 'aFile':
            (dataMat, dataSiz) = dsutil.loadDataFromASCIIFile(opt.iFile)
        elif opt.iType == 'lFile':
            (dataMat, dataSiz) = dsutil.loadDataFromIListFile(opt.iFile)
        elif opt.iType == 'mFile':
            (dataMat, dataSiz) = dsutil.loadDataFromVolumeFile(opt.iFile)
        else:
            msg.fail("Unsupported file type : %s", opt.iType)    
            return -1
    except Exception as e:
        dsinfo.fail(e)
        return -1

    try:
        # try loading the DT model
        if not opt.pFile is None:
            with open(opt.pFile) as fid:
                dsinfo.info('trying to load model %s' % opt.pFile)
                dt = pickle.load(fid)

        # run estimation
        if opt.doEstim:
            if not opt.pFile is None:
                dsinfo.fail('re-estimation attempt detected!')
                return -1
            dt = LinearDS(opt.nStates, approx=opt.svdRand, verbose=opt.verbose)
            dt.suboptimalSysID(dataMat)

        # synthesize output
        if opt.doSynth:
           dataSyn, _ = dt.synthesize(tau=50, mode='s')

        # show a movie of the synthesis result
        if opt.doMovie > 0:
            if opt.doSynth:
                dsutil.showMovie(dataSyn, dataSiz, fps=opt.doMovie)

        # write DT model to file
        if not opt.oFile is None:
            dsinfo.info('writing model to %s' % opt.oFile)
            with open(opt.oFile, 'w') as fid:
                pickle.dump(dt, fid)
     
    # catch pyds exceptions
    except ErrorDS as e:
        dsinfo.fail(e)
        return -1