Exemplo n.º 1
0
def predict(model,data,output):
    """ Access the sample information to predict the pose. """

    # Get the list of training samples
    samples=os.listdir(data);

    # Access to each sample
    for file in samples:
        # Create the object to access the sample
        smp=ActionSample(os.path.join(data,file));

        # Create a random set of actions for this sample
        numFrame=0;
        pred=[];
        while numFrame<smp.getNumFrames():
            # Generate an initial displacement
            start=numFrame+random.randint(1,100);

            # Generate the action duration
            end=min(start+random.randint(10,100),smp.getNumFrames());

            # Generate the action ID
            actionID=random.randint(1,11);

            # Check if the number of frames are correct
            if start<end-1 and end<smp.getNumFrames():
                # Store the prediction
                pred.append([actionID,start,end])

            # Move ahead
            numFrame=end+1;

        # Store the prediction
        smp.exportPredictions(pred,output);

        # Remove the sample object
        del smp;
Exemplo n.º 2
0
def predict(model,data,output):
    """ Access the sample information to predict the pose. """

    actionID = 0 #initialize
    # Get the list of training samples
    samples=os.listdir(data);
    print samples

    # Access to each sample
    for file in samples:
        # Create the object to access the sample
        smp=ActionSample(os.path.join(data,file));
        print file

        # Create a random set of actions for this sample
        numFrame=0;
        pred=[];
        seqn = os.path.splitext(file)[0];
        while numFrame<smp.getNumFrames():
            # Generate an initial displacement
            #start=numFrame+random.randint(1,100);
            start = numFrame

            # Generate the action duration
            #end=min(start+random.randint(10,100),smp.getNumFrames());
            end = min(numFrame+30,smp.getNumFrames())


            actionFileName = "test_%s_frame%d-%d.avi" % (seqn,start,end)
            actionFileFullName = "%s%s%s%s%s" % (trainingVideos,os.path.sep,seqn, os.path.sep, actionFileName)

            w=int(smp.rgb.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
            h=int(smp.rgb.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
            fps=int(smp.rgb.get(cv2.cv.CV_CAP_PROP_FPS))
        
            if os.path.exists(actionFileFullName) and os.path.getsize(actionFileFullName) > 0:
                print actionFileFullName, ' exists'
            else:
                out = cv2.VideoWriter(actionFileFullName,cv2.cv.CV_FOURCC('X','V','I','D'),fps,(w,h))
                for n in range(start,end):
                    image=smp.getRGB(n+1);
                    #print type(image)
                    #print n
                    #img=cv2.cv.fromarray(image)
                    #cv2.imshow('my',image)
                    #cv2.waitKey(10)
                    #print type(img)
                    out.write(image)
                out.release()

            featureFileName = "densetr_%s_frame%d-%d.txt" % (seqn,start,end)
            featureFileFullName = "%s%s%s%s%s" % (denseFeatures,os.path.sep,seqn, os.path.sep, featureFileName)

            #if not os.path.exists(featureFileFullName):
            #    continue

            if os.path.exists(featureFileFullName) and os.path.getsize(featureFileFullName) > 0:
                print featureFileFullName, ' exists'
            else:
                fout = open(featureFileFullName,"w")
                print featureFileFullName
                cmd = []
                cmd.append(denseTrajectoryExe)
                seqn = os.path.splitext(file)[0];
                cmd.append(actionFileFullName)
                print cmd
                proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
                #proc[actionID].stdout.flush()
                #proc[actionID].stderr.flush()
                stdout, stderr = proc.communicate()
                fout.write(stdout)
                fout.close()

            hst = numpy.zeros(bowTraj.vocszHOG)
            if not os.path.exists(featureFileFullName):
                print featureFileFullName, ' not found'
                continue
            htot = 0
            fin = open(featureFileFullName,"r")
            for line in fin:
                D.read(line)
                #descHOG.append(D.HOG)
                #X = bowTraj.calcFeatures(scipy.vstack(tuple(D.HOG)))
                #actionID = model.predict(X)
                idx = bowTraj.bowHOG.kmeans.predict(D.HOG)
                hst[idx] = hst[idx] + 1
                htot = htot + 1
            if htot > 0:
                hst = (1.0 / float(htot)) * hst


            fin.close()
            print 'Retrieved model:'
            print model
            actionID = model.predict(hst)[0]
            print hst
            print 'Predicted: ', actionID
          

            # Generate the action ID
            #actionID=random.randint(1,11);


            # Check if the number of frames are correct
            if start<end-1 and end<smp.getNumFrames():
                # Store the prediction
                pred.append([actionID,start,end])

            # Move ahead
            #numFrame=end+1;
            numFrame=start+15;



        # Store the prediction
        smp.exportPredictions(pred,output);

        # Remove the sample object
        del smp;