''' Easy! mini tutorial Build a model for object detection matz 6/18/2013 ''' import cvac import easy import zipfile # # create a RunSet from corporate logo images # print("==== Training runset: ====") #trainset = easy.createRunSet( "trainImg" ) trainset = cvac.RunSet() caset = easy.createRunSet("trainImg/ca") krset = easy.createRunSet("trainImg/kr") usset = easy.createRunSet("trainImg/us") easy.addToRunSet(trainset, caset, cvac.Purpose(cvac.PurposeType.MULTICLASS, 0)) easy.addToRunSet(trainset, krset, cvac.Purpose(cvac.PurposeType.MULTICLASS, 1)) easy.addToRunSet(trainset, usset, cvac.Purpose(cvac.PurposeType.MULTICLASS, 2)) easy.printRunSetInfo(trainset, printLabels=True) # # Connect to the trainer for a Bag of Words algorithm, then # train with the given runset # print("starting training, this might take a few minutes...") trainer = easy.getTrainer("BOW_Trainer") trainedModel = easy.train(trainer, trainset)
# pick a subset: all license plates license_plates = categories['license plate'] print("There are {0} license plate labels.".format( len(license_plates) )) # another subset: all labels starting with "car" cars = [] for key in categories.keys(): if key.startswith("car"): cars.append( categories[key] ) print("There are {0} car-related labels.".format( len(cars) )) # Note that Labels are cached in the CorpusServer, but the corpus currently # needs to re-mirror if the CorpusServer is restarted because Labels are # not stored to disk. Images are stored to disk. quit() # done for now # Train a detector on license plates trainer = easy.getTrainer( "BOW_Trainer:default -p 10103 ") trainset = easy.createRunSet( license_plates, "pos" ) easy.printRunSetInfo( trainset ) licenseplateModel = easy.train( trainer, trainset ) # test the license plate detector on the known locations of cars; # this will only try to detect within the boundaries of cars. testset = easy.createRunSet( cars, "unpurposed" ) detector = easy.getDetector( "BOW_Detector:default -p 10104" ) results = easy.detect( detector, licenseplateModel, testset ) printResults( results )
c.detectorData = "detectors/dpmStarbucksLogo.zip" c.foundMap = { 'Positive': easy.getPurpose('pos'), 'Negative': easy.getPurpose('neg') } contenders.append(c) # OpenCVCascade, with special settings for anticipated poor performance if (easy.getTrainer("OpenCVCascadeTrainer") == None): print("Cascade service(s) are insufficiently configured, skipping.") else: c = ev.Contender("cascade") c.trainerString = "OpenCVCascadeTrainer" c.detectorString = "OpenCVCascadeDetector" # c.foundMap = {'any':easy.getPurpose('pos')} c.foundMap = {'positive': posPurpose, 'negative': negPurpose} detector = easy.getDetector(c.detectorString) detectorProps = easy.getDetectorProperties(detector) c.detectorProps = detectorProps c.detectorProps.props["maxRectangles"] = "200" c.detectorProps.minNeighbors = 0 # This prevents hang up in evaluator when training has too few samples contenders.append(c) runset = easy.createRunSet("trainImg/kr", "pos") easy.addToRunSet(runset, "trainImg/ca", "neg") easy.printRunSetInfo(runset, printArtifacts=False, printLabels=True) perfdata = ev.joust(contenders, runset, folds=3) ev.printEvaluationResults(perfdata[0])
c3.detectorString = "DPM_Detector:default -p 10116" c3.detectorData = "detectors/dpmStarbucksLogo.zip" c3.foundMap = {'Positive':easy.getPurpose('pos'), 'Negative':easy.getPurpose('neg')} # OpenCVCascade c2 = Contender("cascade") c2.trainerString = "OpenCVCascadeTrainer:default -p 10107" c2.detectorString = "OpenCVCascadeDetector:default -p 10102" c2.foundMap = {'positive':posPurpose, 'negative':negPurpose} detector = easy.getDetector(c2.detectorString) detectorProps = easy.getDetectorProperties(detector) c2.detectorProps = detectorProps; c2.detectorProps.props["maxRectangles"] = "200" c2.detectorProps.minNeighbors = 0; # This prevents hang up in evaluator when training has too few samples c4 = Contender("cascade") c4.trainerString = "OpenCVCascadeTrainer:default -p 10107" c4.detectorString = "OpenCVCascadeDetector:default -p 10102" c4.foundMap = {'any':easy.getPurpose('pos')} runset = easy.createRunSet( "trainImg/kr/Kr001.jpg", "pos" ) easy.addToRunSet( runset, "trainImg/kr/Kr002.jpg", "pos" ) easy.addToRunSet( runset, "trainImg/kr/Kr003.jpg", "pos" ) easy.addToRunSet( runset, "trainImg/ca/ca0003.jpg", "neg" ) easy.addToRunSet( runset, "trainImg/ca/ca0004.jpg", "neg" ) easy.addToRunSet( runset, "trainImg/ca/ca0005.jpg", "neg" ) easy.printRunSetInfo( runset, printArtifacts=False, printLabels=True ) perfdata = joust( [c1, c2], runset, folds=3 ) printEvaluationResults(perfdata)
Invoke a remote service, send files, receive files, receive messages matz 6/17/2013 ''' import easy # # Example 1: Test on a remote machine. # specify the host name of the service # host = "-h vision.nps.edu" # # create a simple RunSet with just one unlabeled image # rs1 = easy.createRunSet("testImg/italia.jpg") # # Make sure all files in the RunSet are available on the remote site; # it is the client's responsibility to upload them if not. # The putResult contains information about which files were actually transferred. # print("------- Remote detection, local result display: -------") fileserver = easy.getFileServer("PythonFileService:default -p 10111 " + host) putResult = easy.putAllFiles(fileserver, rs1) modelfile = "detectors/haarcascade_frontalface_alt.xml" if not fileserver.exists(easy.getCvacPath(modelfile)): easy.putFile(fileserver, easy.getCvacPath(modelfile)) # # detect remotely: note the host specification
# Create a list of labelable files under a directory. lablist2 = easy.getLabelableList( "trainImg" ) categories2 = easy.getCategories(lablist2) print("\n=== Corpus 2: ==="); print('Obtained {0} labeled artifact{1} from trainImg directory:'.format( len(lablist2), ("s","")[len(lablist2)==1])); easy.printCategoryInfo( categories2 ) # Note how both corpora contain flag images, but they have different # labels. To use them for evaluation, let's assign the same purpose # to syntactically different but semantically identical labels. # Because we don't specify it, this guesses the specific Purpose that # is assigned to the labels. # Also obtain this mapping from Purpose to label name, called "classmap." rs1 = easy.createRunSet( categories1['CA_flag']+categories2['ca'], "0" ) easy.addToRunSet( rs1, categories1['KO_flag']+categories2['kr'], "1" ) easy.addToRunSet( rs1, categories1['US_flag']+categories2['us'], "2" ) print("\n=== The Corpora combined into one RunSet: ==="); easy.printRunSetInfo( rs1 ) # A runset can be used for training or for testing print("------- Bag of Words results for corporate logos: -------") detector = easy.getDetector( "BOW_Detector" ) modelfile = "detectors/bowUSKOCA.zip" results1 = easy.detect( detector, modelfile, rs1 ) print("Note that both original and found labels are printed:") easy.printResults( results1 ) # Print again, this time replacing the found labels with a double # mapping from foundLabel -> guessed Purpose -> classmap label;
''' Easy! mini tutorial Kalman tracker via Matlab Bridge matz 10/2014 ''' import easy # # create a test set as in the Matlab sample at # http://www.mathworks.com/help/vision/ug/object-tracking.html # You need to have the Matlab vision toolbox installed. The # singleball.avi movie is here: # YourMatlabProgramPath/toolbox/vision/visiondemos/singleball.avi # You should move it to the Easy! data directory for the demo # runset = easy.createRunSet("singleball.avi") # Optionally, specify a folder that contains a sequence of the video's # frames as image files # runset = easy.createRunSet( "singleball.avi_frames" ) # # evaluate your tracking algorithm with a common match scoring method # tracker = easy.getDetector("KalmanTracker") results = easy.detect(tracker, None, runset) easy.printResults(results) easy.printLabeledTrack(results)
''' Easy! mini tutorial Repeatedly train and evaluate for efficient label use; bootstrap. matz 6/21/2013 ''' import os import easy # # Create a training set from one sample each of 9 corporate logos # trainset1 = easy.createRunSet("corporate_logos") # train, round 1 trainer = easy.getTrainer("BOW_Trainer") model1 = easy.train(trainer, trainset1) # evaluate the model on a separate test set, images and videos # in DataDir/testImg testset1 = easy.createRunSet("testImg", "UNPURPOSED") easy.printRunSetInfo(testset1) detector = easy.getDetector("BOW_Detector") result1 = easy.detect(detector, model1, testset1) easy.printResults(result1) # sort the images from the testdata1 folder into subfolders of # "testresults1" corresponding to the found labels; # if multiple labels were found per original, consider only # the label with the highest confidence easy.sortIntoFolders(result1, outfolder="testresults1", multi="highest")
''' Easy! mini tutorial Build a model for object detection matz 6/18/2013 ''' import easy import zipfile # # create a RunSet from corporate logo images # print("==== Training runset: ====") trainset = easy.createRunSet( "trainImg" ); easy.printRunSetInfo( trainset, printLabels=True ) # # Connect to the trainer for a Bag of Words algorithm, then # train with the given runset # trainer = easy.getTrainer( "BOW_Trainer") trainedModel = easy.train( trainer, trainset ) # # Display information about the file in which the model is stored; # this is generally no concern to algorithm users and only of # interest to algorithm developers since it is algorithm-specific # zipfname = easy.getFSPath( trainedModel ) print("{0}".format( zipfname )) zipf = zipfile.ZipFile( zipfname )
# print dataset information about this corpus # cs = easy.getCorpusServer("CorpusServer:default -p 10011") #corpus = easy.openCorpus( cs, "corpus/CvacCorpusTest.properties" ) #corpus = easy.openCorpus( cs, "corporate_logos" ); corpus = easy.openCorpus( cs, "trainImg" ); categories, lablist = easy.getDataSet( corpus, corpusServer=cs ) print('Obtained {0} labeled artifact{1} from corpus "{2}":'.format( len(lablist), ("s","")[len(lablist)==1], corpus.name )); easy.printCategoryInfo( categories ) # # add all samples from corpus to a RunSet, # also obtain a mapping from class ID to label name # res = easy.createRunSet( categories ) runset = res['runset'] classmap = res['classmap'] # # Make sure all files in the RunSet are available on the remote site; # it is the client's responsibility to upload them if not. # host = "-h localhost" #host = "-h vision.nps.edu" fileserver = easy.getFileServer( "FileService:default -p 10110 " + host ) putResult = easy.putAllFiles( fileserver, runset ) # # Connect to a trainer service, train on the RunSet #
# pick a subset: all license plates license_plates = categories['license plate'] print("There are {0} license plate labels.".format(len(license_plates))) # another subset: all labels starting with "car" cars = [] for key in categories.keys(): if key.startswith("car"): cars.append(categories[key]) print("There are {0} car-related labels.".format(len(cars))) # Note that Labels are cached in the CorpusServer, but the corpus currently # needs to re-mirror if the CorpusServer is restarted because Labels are # not stored to disk. Images are stored to disk. quit() # done for now # Train a detector on license plates trainer = easy.getTrainer("BOW_Trainer:default -p 10103 ") trainset = easy.createRunSet(license_plates, "pos") easy.printRunSetInfo(trainset) licenseplateModel = easy.train(trainer, trainset) # test the license plate detector on the known locations of cars; # this will only try to detect within the boundaries of cars. testset = easy.createRunSet(cars, "unpurposed") detector = easy.getDetector("BOW_Detector:default -p 10104") results = easy.detect(detector, licenseplateModel, testset) printResults(results)
generated by matz 7/16/2013 updated by k.lee May/2014 ''' import easy import evaluate import cvac import time #for computing computation time import os #for chdir stime = time.clock() ''' Making training data ''' #trainImg_roc_simple #trainImg_roc trainsetPos = easy.createRunSet('corporate_logos') trainsetNeg = easy.createRunSet('trainImg') runset = cvac.RunSet() easy.addToRunSet(runset, trainsetPos, 'pos') easy.addToRunSet(runset, trainsetNeg, 'neg') easy.printRunSetInfo( runset, printLabels=True, ) strTrainer = "BOW_Trainer" strDetector = "BOW_Detector" list_nWord = [5, 10, 15, 20]
# Create a list of labelable files under a directory. lablist2 = easy.getLabelableList("trainImg") categories2 = easy.getCategories(lablist2) print("\n=== Corpus 2: ===") print('Obtained {0} labeled artifact{1} from trainImg directory:'.format( len(lablist2), ("s", "")[len(lablist2) == 1])) easy.printCategoryInfo(categories2) # Note how both corpora contain flag images, but they have different # labels. To use them for evaluation, let's assign the same purpose # to syntactically different but semantically identical labels. # Because we don't specify it, this guesses the specific Purpose that # is assigned to the labels. # Also obtain this mapping from Purpose to label name, called "classmap." rs1 = easy.createRunSet(categories1['CA_flag'] + categories2['ca'], "0") easy.addToRunSet(rs1, categories1['KO_flag'] + categories2['kr'], "1") easy.addToRunSet(rs1, categories1['US_flag'] + categories2['us'], "2") print("\n=== The Corpora combined into one RunSet: ===") easy.printRunSetInfo(rs1) # A runset can be used for training or for testing print("------- Bag of Words results for corporate logos: -------") detector = easy.getDetector("BOW_Detector") modelfile = "detectors/bowUSKOCA.zip" results1 = easy.detect(detector, modelfile, rs1) print("Note that both original and found labels are printed:") easy.printResults(results1) # Print again, this time replacing the found labels with a double # mapping from foundLabel -> guessed Purpose -> classmap label;
''' Easy! mini tutorial Build a model for object detection matz 6/18/2013 ''' import cvac import easy import zipfile # # create a RunSet from corporate logo images # print("==== Training runset: ====") #trainset = easy.createRunSet( "trainImg" ) trainset = cvac.RunSet() caset = easy.createRunSet("trainImg/ca") krset = easy.createRunSet("trainImg/kr") usset = easy.createRunSet("trainImg/us") easy.addToRunSet(trainset, caset, cvac.Purpose(cvac.PurposeType.MULTICLASS,0)) easy.addToRunSet(trainset, krset, cvac.Purpose(cvac.PurposeType.MULTICLASS,1)) easy.addToRunSet(trainset, usset, cvac.Purpose(cvac.PurposeType.MULTICLASS,2)) easy.printRunSetInfo( trainset, printLabels=True ) # # Connect to the trainer for a Bag of Words algorithm, then # train with the given runset # print("starting training, this might take a few minutes...") trainer = easy.getTrainer( "BOW_Trainer") trainedModel = easy.train( trainer, trainset )
Invoke a remote service, send files, receive files, receive messages matz 6/17/2013 ''' import easy # # Example 1: Test on a remote machine. # specify the host name of the service # host = "-h vision.nps.edu" # # create a simple RunSet with just one unlabeled image # rs1 = easy.createRunSet( "testImg/italia.jpg" ) # # Make sure all files in the RunSet are available on the remote site; # it is the client's responsibility to upload them if not. # The putResult contains information about which files were actually transferred. # fileserver = easy.getFileServer( "FileService:default -p 10110 " + host ) putResult = easy.putAllFiles( fileserver, rs1 ) modelfile = "detectors/haarcascade_frontalface_alt.xml" if not fileserver.exists( easy.getCvacPath(modelfile) ): easy.putFile( fileserver, easy.getCvacPath(modelfile) ) # # detect remotely: note the host specification #
''' Easy! mini tutorial Kalman tracker via Matlab Bridge matz 10/2014 ''' import easy # # create a test set as in the Matlab sample at # http://www.mathworks.com/help/vision/ug/object-tracking.html # You need to have the Matlab vision toolbox installed. The # singleball.avi movie is here: # YourMatlabProgramPath/toolbox/vision/visiondemos/singleball.avi # You should move it to the Easy! data directory for the demo # runset = easy.createRunSet( "singleball.avi" ) # Optionally, specify a folder that contains a sequence of the video's # frames as image files # runset = easy.createRunSet( "singleball.avi_frames" ) # # evaluate your tracking algorithm with a common match scoring method # tracker = easy.getDetector( "KalmanTracker" ) results = easy.detect( tracker, None, runset ) easy.printResults( results ) easy.printLabeledTrack( results )
else: c = ev.Contender("DPM") c.detectorString = "DPM_Detector" c.detectorData = "detectors/dpmStarbucksLogo.zip" c.foundMap = {'Positive':easy.getPurpose('pos'), 'Negative':easy.getPurpose('neg')} contenders.append( c ); # OpenCVCascade, with special settings for anticipated poor performance if (easy.getTrainer("OpenCVCascadeTrainer")==None): print("Cascade service(s) are insufficiently configured, skipping.") else: c = ev.Contender("cascade") c.trainerString = "OpenCVCascadeTrainer" c.detectorString = "OpenCVCascadeDetector" # c.foundMap = {'any':easy.getPurpose('pos')} c.foundMap = {'positive':posPurpose, 'negative':negPurpose} detector = easy.getDetector(c.detectorString) detectorProps = easy.getDetectorProperties(detector) c.detectorProps = detectorProps; c.detectorProps.props["maxRectangles"] = "200" c.detectorProps.minNeighbors = 0; # This prevents hang up in evaluator when training has too few samples contenders.append( c ); runset = easy.createRunSet( "trainImg/kr", "pos" ) easy.addToRunSet( runset, "trainImg/ca", "neg" ) easy.printRunSetInfo( runset, printArtifacts=False, printLabels=True ) perfdata = ev.joust( contenders, runset, folds=3 ) ev.printEvaluationResults(perfdata[0])
correct = easy.isProperRunSet(trainset, deleteInvalid=True) if not correct: print("failed Integrity test!!!!") exit() #trainset = easy.createRunSet( categories, purpose=posPurpose ); #easy.printRunSetInfo( trainset, printLabels=True ) easy.printRunSetInfo( trainset ) # # Connect to the trainer for a Bag of Words algorithm, then # train with the given runset # #trainer = easy.getTrainer( "BOW_Trainer") trainer = easy.getTrainer( "BOW_Trainer:default -p 10103") trainedModel = easy.train( trainer, trainset ) zipfname = easy.getFSPath( trainedModel ) print("Training model stored in file {0}".format( zipfname )) # # test the trained model on a separate set of images # print("==== Test runset: ====") testset = easy.createRunSet( "testImg" ) easy.printRunSetInfo( testset, printLabels=True ) detector = easy.getDetector( "BOW_Detector" ) results = easy.detect( detector, trainedModel, testset ) print("==== Results: ====") easy.printResults( results )
''' import easy import evaluate import cvac import time #for computing computation time import os #for chdir stime = time.clock() ''' Making training data ''' #trainImg_roc_simple #trainImg_roc trainsetPos = easy.createRunSet( 'corporate_logos' ) trainsetNeg = easy.createRunSet( 'trainImg' ) runset = cvac.RunSet() easy.addToRunSet(runset, trainsetPos, 'pos') easy.addToRunSet(runset, trainsetNeg, 'neg') easy.printRunSetInfo( runset, printLabels=True, ) strTrainer = "BOW_Trainer" strDetector = "BOW_Detector" list_nWord = [5,10,15,20] doWithNegativeSample = True
''' Easy! mini tutorial Repeatedly train and evaluate for efficient label use; bootstrap. matz 6/21/2013 ''' import os import easy # # Create a training set from one sample each of 9 corporate logos # trainset1 = easy.createRunSet( "corporate_logos" ) # train, round 1 trainer = easy.getTrainer( "BOW_Trainer") model1 = easy.train( trainer, trainset1 ) # evaluate the model on a separate test set, images and videos # in DataDir/testdata1 testset1 = easy.createRunSet( "testImg", "UNPURPOSED" ) easy.printRunSetInfo( testset1 ) detector = easy.getDetector( "BOW_Detector" ) result1 = easy.detect( detector, model1, testset1 ) easy.printResults(result1) # sort the images from the testdata1 folder into subfolders of # "testresults1" corresponding to the found labels; # if multiple labels were found per original, consider only # the label with the highest confidence easy.sortIntoFolders( result1, outfolder="testresults1", multi="highest")
''' Easy! mini tutorial Create a RunSet in several ways, use it for detection and evaluation matz 6/18/2013 ''' import easy import cvac # a simple RunSet with just one unlabeled image; # remember that paths are relative to CVAC.DataDir rs1 = easy.createRunSet( "testImg/italia.jpg" ) print("=== RunSet 1: ==="); easy.printRunSetInfo( rs1, printLabels=True ) # to give samples a purpose, state the purpose: rs2 = easy.createRunSet( "testImg/italia.jpg", "POSITIVE" ) print("\n=== RunSet 2: ==="); easy.printRunSetInfo( rs2, printLabels=True ) # add more samples to a runset; anything starting with "pos" # will be added into the POSITIVE sequence of labeled items easy.addToRunSet( rs2, "testImg/TestKrFlag.jpg", "POS" ) easy.addToRunSet( rs2, "testImg/TestCaFlag.jpg", "neg" ) easy.addToRunSet( rs2, "testImg/TestUsFlag.jpg", "0" ) print("\n=== RunSet 2, after appending: ==="); easy.printRunSetInfo( rs2, printLabels=True ) # create a runset from a folder with sub-folders rs3 = easy.createRunSet( "trainImg" ) print("\n=== RunSet 3: ===");
easy.addToRunSet(trainset, categories[objname], posPurpose) #trainset = easy.createRunSet( categories, purpose=posPurpose ); correct = easy.isProperRunSet(trainset, deleteInvalid=True) if not correct: print("failed Integrity test!!!!") exit() #trainset = easy.createRunSet( categories, purpose=posPurpose ); #easy.printRunSetInfo( trainset, printLabels=True ) easy.printRunSetInfo(trainset) # # Connect to the trainer for a Bag of Words algorithm, then # train with the given runset # #trainer = easy.getTrainer( "BOW_Trainer") trainer = easy.getTrainer("BOW_Trainer:default -p 10103") trainedModel = easy.train(trainer, trainset) zipfname = easy.getFSPath(trainedModel) print("Training model stored in file {0}".format(zipfname)) # # test the trained model on a separate set of images # print("==== Test runset: ====") testset = easy.createRunSet("testImg") easy.printRunSetInfo(testset, printLabels=True) detector = easy.getDetector("BOW_Detector") results = easy.detect(detector, trainedModel, testset) print("==== Results: ====") easy.printResults(results)