def isProperDetector(self, configString, detectorData=None, detectorProperties=None): '''Try to create or use the specified detector. Create a test runset and call the detector with it. Check for proper handling of intentional problems. ''' detector = easy.getDetector(configString) if not detector: print("cannot find or connect to detector") return False # make a runset including erroneous files rs = cvac.RunSet() file_normal = easy.getLabelable("testImg/italia.jpg") file_notexist = easy.getLabelable("testImg/doesnotexist.jpg") file_notconvertible = easy.getLabelable("testImg/notconvertible.xxx") easy.addToRunSet(rs, file_normal, cvac.Purpose(cvac.PurposeType.UNPURPOSED, -1)) easy.addToRunSet(rs, file_notexist, cvac.Purpose(cvac.PurposeType.UNPURPOSED, -1)) easy.addToRunSet(rs, file_notconvertible, cvac.Purpose(cvac.PurposeType.UNPURPOSED, -1)) results = easy.detect(detector, detectorData, rs, detectorProperties) # check for number of results: must be the same as runset length nRunset = 0 for pur in rs.purposedLists: nRunset += len(pur.labeledArtifacts) if len(results) != nRunset: print("incorrect result set size") return False # check that the doesnotexist file caused hasLabel==False found = False for lbl in results: # for an erroneous file if lbl.original.sub == img_notexist.sub or lbl.original.sub == img_notconvertible.sub: for foundlbl in lbl.foundLabels: if foundlbl.lab.hasLabel: print( "Incorrectly assigned label for an erroneous file." ) return False else: #for a normal file for foundlbl in lbl.foundLabels: # confidence 0..1 if not 0.0 <= foundlbl.confidence and foundlbl.confidence <= 1.0: print("Label confidence out of bounds ({0}).".format( foundlbl.confidence)) return False # check that either this was not assigned a label, or the # assigned label is of proper syntax if foundlbl.lab.hasLabel: if not isinstance(foundlbl.lab.name, str): print("Label name must be of string type.") return False return True
def isProperDetector( self, configString, detectorData=None, detectorProperties=None ): '''Try to create or use the specified detector. Create a test runset and call the detector with it. Check for proper handling of intentional problems. ''' detector = easy.getDetector( configString ) if not detector: print("cannot find or connect to detector") return False # make a runset including erroneous files rs = cvac.RunSet() file_normal = easy.getLabelable( "testImg/italia.jpg" ) file_notexist = easy.getLabelable( "testImg/doesnotexist.jpg" ) file_notconvertible = easy.getLabelable( "testImg/notconvertible.xxx") easy.addToRunSet( rs, file_normal, cvac.Purpose( cvac.PurposeType.UNPURPOSED, -1 )) easy.addToRunSet( rs, file_notexist, cvac.Purpose( cvac.PurposeType.UNPURPOSED, -1 )) easy.addToRunSet( rs, file_notconvertible, cvac.Purpose( cvac.PurposeType.UNPURPOSED, -1 ) ) results = easy.detect( detector, detectorData, rs, detectorProperties ) # check for number of results: must be the same as runset length nRunset = 0 for pur in rs.purposedLists: nRunset += len(pur.labeledArtifacts) if len(results)!=nRunset: print("incorrect result set size") return False # check that the doesnotexist file caused hasLabel==False found=False for lbl in results: # for an erroneous file if lbl.original.sub==img_notexist.sub or lbl.original.sub==img_notconvertible.sub: for foundlbl in lbl.foundLabels: if foundlbl.lab.hasLabel: print("Incorrectly assigned label for an erroneous file.") return False else: #for a normal file for foundlbl in lbl.foundLabels: # confidence 0..1 if not 0.0<=foundlbl.confidence and foundlbl.confidence<=1.0: print("Label confidence out of bounds ({0}).".format(foundlbl.confidence)) return False # check that either this was not assigned a label, or the # assigned label is of proper syntax if foundlbl.lab.hasLabel: if not isinstance(foundlbl.lab.name, str): print("Label name must be of string type.") return False return True
def evaluate( contender, runset, printVerbose=False ): if type(runset) is dict and not runset['runset'] is None\ and isinstance(runset['runset'], cvac.RunSet): runset = runset['runset'] if not runset or not isinstance(runset, cvac.RunSet) or not runset.purposedLists: raise RuntimeError("no proper runset") evalset = runset print( "---- evaluation:" ) easy.printRunSetInfo( evalset, printArtifacts=printVerbose ) detector = contender.getDetector() detections = easy.detect( detector, contender.detectorData, evalset, detectorProperties=contender.detectorProps ) ct = getConfusionTable( detections, origSet=evalset, foundMap=contender.foundMap ) # create result structure r = EvaluationResult( 0, ct[0], nores=ct[1], detail=None, name=contender.name ) return r
def test_cvacdatadir(self): print("testing cvac data dir") if sys.platform == 'win32': datadir = os.getenv("CVAC_DATADIR", None) datadir = datadir.replace("/", "\\") easy.CVAC_DataDir = datadir print("Testing using back slashes") print("Using CVAC_DATADIR as " + datadir) testset = [] easy.misc.searchDir(testset, datadir + '\\testImg', recursive=True, video=False, image=True) runset = cvac.RunSet() easy.addToRunSet(runset, testset, 'pos') modelfile = 'detectors/bowUSKOCA.zip' detector = easy.getDetector("BOW_Detector") props = easy.getDetectorProperties(detector) props.verbosity = 3 results = easy.detect(detector, modelfile, runset, detectorProperties=props) easy.printResults(results) else: print("Skipping back slash test on this platform") #run it again with all forward slashes datadir = os.getenv("CVAC_DATADIR", None) datadir = datadir.replace("\\", "/") print("Testing using all forward slashes") print("Using CVAC_DATADIR as " + datadir) easy.CVAC_DataDir = datadir testset = [] easy.misc.searchDir(testset, datadir + '/testImg', recursive=True, video=False, image=True) runset = cvac.RunSet() easy.addToRunSet(runset, testset, 'pos') modelfile = 'detectors/bowUSKOCA.zip' detector = easy.getDetector("BOW_Detector") props = easy.getDetectorProperties(detector) props.verbosity = 3 results = easy.detect(detector, modelfile, runset, detectorProperties=props) easy.printResults(results) #run it for forward slashes and relative path origDir = datadir curDir = os.getcwd() curDir = curDir.replace("\\", "/") datadir = datadir.replace("\\", "/") print("using relative paths for " + curDir + " in data dir " + datadir) if datadir.startswith(curDir): datadir = datadir[len(curDir) + 1:] easy.CVAC_DataDir = datadir print("Using CVAC_DataDir as " + datadir) testset = [] easy.misc.searchDir(testset, origDir + '/testImg', recursive=True, video=False, image=True) runset = cvac.RunSet() easy.addToRunSet(runset, testset, 'pos') modelfile = origDir + '/detectors/bowUSKOCA.zip' detector = easy.getDetector("BOW_Detector") props = easy.getDetectorProperties(detector) props.verbosity = 3 results = easy.detect(detector, modelfile, runset, detectorProperties=props) easy.printResults(results) else: RuntimeError("Bad datadir")
def doDetect(): easy.detect( detector, modelfile, runset, callbackRecv = callbackRecv, detectorProperties = props)
''' import easy # obtain a reference to a Bag of Words (BOW) detector detector = easy.getDetector( "BOW_Detector" ) # a model for distinguishing Canadian, Korean, and US flags, # trained previously with a BOW-specific trainer and stored in a file modelfile = "detectors/bowUSKOCA.zip" # a test image; the location is relative to the "CVAC.DataDir" imgfile = "testImg/TestKrFlag.jpg" # apply the detector type, using the model and testing the imgfile results = easy.detect( detector, modelfile, imgfile ) # you can print the results with Easy!'s pretty-printer; # we will explain the meaning of the "unlabeled" and the number # of the found label later. # or you can always print variables directly (uncomment the next line): # print("{0}".format( results )) print("------- Bag of Words results for flags: -------") easy.printResults( results ) # let's try a different model, pre-trained not for # flags but for various corporate logos print("------- Bag of Words results for corporate logos: -------") modelfile = "detectors/bowCorporateLogoModel.zip" imgfile = "corporate_logos/shell/shell2.png" results = easy.detect( detector, modelfile, imgfile )
rocZip = easy.makeROCdata(rocData_optimal) ''' Extract only optimal ROC points ''' detector = easy.getDetector( strDetector ) detectorProps = easy.getDetectorProperties(detector) #specify either a desired falseAlarmRate or recall, but not both: priority = "recall" if priority=="falseAlarmRate": detectorProps.falseAlarmRate = 0.01 #x-axis print("falseAlarmRate = {0}".format(detectorProps.falseAlarmRate)) else: detectorProps.recall = 0.90 #y-axis print("recall = {0}".format(detectorProps.recall)) results = easy.detect( detector, rocZip, runset, detectorProperties = detectorProps) easy.printResults( results ) else: ############################################################### # Without background data ''' Execute jousting for generating ROC points ''' contenders = [] for nWord in list_nWord: c1 = evaluate.Contender("bowROC_oneclass_"+str(nWord)) c1.trainerString = strTrainer c1.detectorString = strDetector trainer = easy.getTrainer(c1.trainerString) trainerProps = easy.getTrainerProperties(trainer) trainerProps.props["RejectClassStrategy"] = "ignore"
rocZip = easy.makeROCdata(rocData_optimal) ''' Extract only optimal ROC points ''' detector = easy.getDetector(strDetector) detectorProps = easy.getDetectorProperties(detector) #specify either a desired falseAlarmRate or recall, but not both: priority = "recall" if priority == "falseAlarmRate": detectorProps.falseAlarmRate = 0.01 #x-axis print("falseAlarmRate = {0}".format(detectorProps.falseAlarmRate)) else: detectorProps.recall = 0.90 #y-axis print("recall = {0}".format(detectorProps.recall)) results = easy.detect(detector, rocZip, runset, detectorProperties=detectorProps) easy.printResults(results) else: ############################################################### # Without background data ''' Execute jousting for generating ROC points ''' contenders = [] for nWord in list_nWord: c1 = evaluate.Contender("bowROC_oneclass_" + str(nWord)) c1.trainerString = strTrainer c1.detectorString = strDetector trainer = easy.getTrainer(c1.trainerString) trainerProps = easy.getTrainerProperties(trainer)
def crossValidate( contender, runset, folds=10, printVerbose=False ): '''Returns summary statistics tp, fp, tn, fn, recall, trueNegRate, and a detailed matrix of results with one row for each fold, and one column each for true positive, false positive, true negative, and false negative counts''' # sanity checks: # only positive and negative purposes, # count number of entries for each purpose runset_pos = asList( runset, purpose="pos" ) runset_neg = asList( runset, purpose="neg" ) num_items = ( len(runset_pos), len(runset_neg) ) # check that there are no other purposes all_items = len( asList( runset ) ) if sum(num_items)!=all_items: raise RuntimeError("crossValidate can only handle Positive and Negative purposes") if min(num_items)<2: raise RuntimeError("need more than 1 labeled item per purpose to cross validate") # make sure there are enough items for xval to make sense if folds>min(num_items): print("warning: cannot do "+folds+"-fold validation with only "+str(num_items)+" labeled items") folds = min(num_items) # calculate the size of the training and evaluation sets. # if the number of labeled items in the runset divided # by the number of folds isn't an even # division, use more items for the evaluation chunksize = (int(math.floor( num_items[0]/folds )), int(math.floor( num_items[1]/folds ))) trainsize = (chunksize[0] * (folds-1), chunksize[1] * (folds-1)) evalsize = (num_items[0]-trainsize[0], num_items[1]-trainsize[1]) print( "Will perform a {0}-fold cross-validation with {1} training samples and " "{2} evaluation samples".format( folds, trainsize, evalsize ) ) # randomize the order of the elements in the runset, once and for all folds rndidx = ( range( num_items[0] ), range( num_items[1] ) ) random.shuffle( rndidx[0] ) # shuffles items in place random.shuffle( rndidx[1] ) # shuffles items in place #confusionTables = numpy.empty( [folds, 5], dtype=int ) confusionTables = [] for fold in range( folds ): # split the runset trainset, evalset = splitRunSet( runset_pos, runset_neg, fold, chunksize, evalsize, rndidx ) print( "-------- fold number {0} --------".format(fold) ) # training print( "---- training:" ) easy.printRunSetInfo( trainset, printArtifacts=printVerbose ) trainer = contender.getTrainer() model = easy.train( trainer, trainset, trainerProperties=contender.trainerProps) # detection print( "---- evaluation:" ) easy.printRunSetInfo( evalset, printArtifacts=printVerbose ) detector = contender.getDetector() detections = easy.detect( detector, model, evalset, detectorProperties=contender.detectorProps ) confusionTables.append( \ getConfusionTable( detections, origSet=evalset, foundMap=contender.foundMap )) # calculate statistics of our tuple TestResult,nores sumTestResult = TestResult() sumNoRes = 0; for entry in confusionTables: sumTestResult.tp += entry[0].tp sumTestResult.tn += entry[0].tn sumTestResult.fp += entry[0].fp sumTestResult.fn += entry[0].fn sumNoRes += entry[1] r = EvaluationResult(folds, sumTestResult, sumNoRes, detail=None, name=contender.name) return r
# 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 # print("------- Remote detection, local result display: -------") detector = easy.getDetector( "OpenCVCascadeDetector:default -p 10102 "+host ) results = easy.detect( detector, modelfile, rs1 ) easy.printResults( results ) # # Example 2: # Train on a remote machine, obtain the model file, and test locally. # Assume the files are on the remote machine, or transfer with putAllFiles. # trainer = easy.getTrainer( "bowTrain:default -p 10103 "+ host) # remote trainset = easy.createRunSet( "trainImg" ); trainedModel = easy.train( trainer, trainset ) easy.getFile( fileserver, trainedModel.file ) # downloads the model from remote print("{0}".format(trainedModel)) detector = easy.getDetector( "bowTest:default -p 10104" ) # local service testset = easy.createRunSet("testImg","UNPURPOSED" ) results = easy.detect( detector, trainedModel, testset )
def test_cvacdatadir(self): print("testing cvac data dir") if sys.platform == 'win32': datadir = os.getenv("CVAC_DATADIR", None) datadir = datadir.replace("/", "\\") easy.CVAC_DataDir = datadir print("Testing using back slashes") print("Using CVAC_DATADIR as " + datadir) testset = [] easy.misc.searchDir(testset, datadir + '\\testImg', recursive=True, video=False, image=True) runset = cvac.RunSet() easy.addToRunSet(runset, testset, 'pos') modelfile = 'detectors/bowUSKOCA.zip' detector = easy.getDetector("BOW_Detector") props = easy.getDetectorProperties(detector) props.verbosity = 3 results = easy.detect(detector, modelfile, runset, detectorProperties=props) easy.printResults(results) else: print("Skipping back slash test on this platform") #run it again with all forward slashes datadir = os.getenv("CVAC_DATADIR", None) datadir = datadir.replace("\\", "/") print("Testing using all forward slashes") print("Using CVAC_DATADIR as " + datadir) easy.CVAC_DataDir = datadir testset = [] easy.misc.searchDir(testset, datadir + '/testImg', recursive=True, video=False, image=True) runset = cvac.RunSet() easy.addToRunSet(runset, testset, 'pos') modelfile = 'detectors/bowUSKOCA.zip' detector = easy.getDetector("BOW_Detector") props = easy.getDetectorProperties(detector) props.verbosity = 3 results = easy.detect(detector, modelfile, runset, detectorProperties=props) easy.printResults(results) #run it for forward slashes and relative path origDir = datadir curDir = os.getcwd() curDir = curDir.replace("\\", "/") datadir = datadir.replace("\\", "/") print("using relative paths for " + curDir + " in data dir " + datadir) if datadir.startswith(curDir): datadir = datadir[len(curDir)+1:] easy.CVAC_DataDir = datadir print("Using CVAC_DataDir as " + datadir) testset = [] easy.misc.searchDir(testset, origDir + '/testImg', recursive=True, video=False, image=True) runset = cvac.RunSet() easy.addToRunSet(runset, testset, 'pos') modelfile = origDir + '/detectors/bowUSKOCA.zip' detector = easy.getDetector("BOW_Detector") props = easy.getDetectorProperties(detector) props.verbosity = 3 results = easy.detect(detector, modelfile, runset, detectorProperties=props) easy.printResults(results) else: RuntimeError("Bad datadir")
# 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 )
import sys, os thisPath = os.path.dirname(os.path.abspath(__file__)) srcPath = os.path.abspath(thisPath+"/../../lib/python") sys.path.append(srcPath) import paths import cvac import easy # # First, a teaser for detection: # detector = easy.getDetector( "bowTest:default -p 10104" ) results = easy.detect( detector, "detectors/bowUSKOCA.zip", "testImg/TestCaFlag.jpg" ) easy.printResults( results ) # # Second, a quick way to train a detector. The resulting model # can be used in place of the detector above. # # TODO: currently breaks because Caltech101 doesn't get extracted as expected #categories, lablist = easy.getDataSet( "corpus/CvacCorpusTest" ) # categories, lablist = easy.getDataSet( "corpus/Caltech101.properties", createMirror=False ) #easy.printCategoryInfo( categories ) #runset = easy.createRunSet( categories["car_side"] ) #trainer = easy.getTrainer( "bowTrain:default -p 10103" ) #carSideModel = easy.train( trainer, runset ) #
# # 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") # Now manually sort through the created testresults1 and move # _incorrectly_ classified samples into correctly labeled subfolders # of a new folder "corporate_logos_round2". Found labels on locations # that are not one of the 9 logos have to be sorted into a 10th class # (subfolder), generally called the "reject" class. # # We simulate this manual process here. Note that the new folder needs to be
# Ex. for image files in a directory tFileSuffixes = ["jpg","bmp"] directory_images = cvac.PurposedDirectory(cvac.Purpose(cvac.PurposeType.MULTICLASS,1), cvac.DirectoryPath("testImg"), tFileSuffixes, 1) runset.purposedLists.append(directory_images) #=============================================================================== # # If you have video files, you may comment out this part for testing the video files # # Ex. for video files in a directory # tFileSuffixes = ["mpg","mpeg","avi"] # directory_videos = cvac.PurposedDirectory(cvac.Purpose(cvac.PurposeType.MULTICLASS,1), # cvac.DirectoryPath("trainVideo"), # tFileSuffixes, # 1) # runset.purposedLists.append(directory_videos) #=============================================================================== host = "-h localhost" detector = easy.getDetector( "RSItrTest_Detector:default -p 10109 " + host ) modelfile = '' #empty detectorData only for testing results = easy.detect( detector, modelfile, runset ) #results = easy.detect( detector, trainedModel, runset ) #easy.printResults( results, foundMap=classmap ) easy.printResults( results) quit()
# 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)
0, 0) singleImages.labeledArtifacts.append(cvac.Labelable(0, tlab, tsub)) # Ex. for image files in a directory tFileSuffixes = ["jpg", "bmp"] directory_images = cvac.PurposedDirectory( cvac.Purpose(cvac.PurposeType.MULTICLASS, 1), cvac.DirectoryPath("testImg"), tFileSuffixes, 1) runset.purposedLists.append(directory_images) #=============================================================================== # # If you have video files, you may comment out this part for testing the video files # # Ex. for video files in a directory # tFileSuffixes = ["mpg","mpeg","avi"] # directory_videos = cvac.PurposedDirectory(cvac.Purpose(cvac.PurposeType.MULTICLASS,1), # cvac.DirectoryPath("trainVideo"), # tFileSuffixes, # 1) # runset.purposedLists.append(directory_videos) #=============================================================================== host = "-h localhost" detector = easy.getDetector("RSItrTest_Detector:default -p 10109 " + host) modelfile = '' #empty detectorData only for testing results = easy.detect(detector, modelfile, runset) #results = easy.detect( detector, trainedModel, runset ) #easy.printResults( results, foundMap=classmap ) easy.printResults(results) quit()
# 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 # detector = easy.getDetector("OpenCVCascadeDetector:default -p 10102 " + host) results = easy.detect(detector, modelfile, rs1) easy.printResults(results) # # Example 2: # Train on a remote machine, obtain the model file, and test locally. # Assume the files are on the remote machine, or transfer with putAllFiles. # If no local services are installed, this will be skipped. # print("------- Remote training, local detection: -------") try: detector = easy.getDetector( "BOW_Detector:default -p 10104") # local service trainer = easy.getTrainer("BOW_Trainer:default -p 10103 " + host) # remote trainset = easy.createRunSet("trainImg") trainedModel = easy.train(trainer, trainset)
''' 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 )
# # 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) # # 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("Training model stored in file {0}".format(zipfname)) # print("file contents:\n{0}".format(zipf.namelist())) # # 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)
# The tracker needs a labeled location that defines the location # within the first frame of the video of the item we want to track. # In this we define a bounding box around the toy soldiers initial location. vlab = easy.getLabelable( vfile, labelText="soldier" ) labloc = cvac.BBox(255, 195,70,120) lab = cvac.LabeledLocation() lab.loc = labloc lab.confidence = 0 lab.sub = vlab.sub easy.addToRunSet(runset, lab, easy.getPurpose('pos')) # Setting the verbosity level gives us some feedback while the tracking is done. props = easy.getDetectorProperties(detector) props.verbosity = 7 # A model file is not required for this detector modelfile = None results = easy.detect( detector, modelfile, runset, detectorProperties = props) # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ''' Now we run it again this time setting up a callback to display the tracking frames on the images. ''' class MyDetectorCallbackReceiverI(easy.DetectorCallbackReceiverI): def __init__(self): import easy easy.DetectorCallbackReceiverI.__init__(self) def foundNewResults(self, r2, current=None): easy.DetectorCallbackReceiverI.foundNewResults(self, r2, current) # Draw the result video frame and the bounding box
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 )
# # 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") # Now manually sort through the created testresults1 and move # _incorrectly_ classified samples into correctly labeled subfolders # of a new folder "corporate_logos_round2". Found labels on locations # that are not one of the 9 logos have to be sorted into a 10th class # (subfolder), generally called the "reject" class. # # We simulate this manual process here. Note that the new folder needs to be
# 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; # Note that this fails if multiple original labels mapped to the same # Purpose. wait() print("------- Same results, but found labels replaced with guessed original labels: -------") easy.printResults( results1, foundMap=rs1['classmap'], inverseMap=True ) # Print again, this time replacing all labels with their assigned # purposes, bot original and found labels. Note the "identical label" # matches. wait()
''' 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)
# 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; # Note that this fails if multiple original labels mapped to the same # Purpose. wait() print( "------- Same results, but found labels replaced with guessed original labels: -------" ) easy.printResults(results1, foundMap=rs1['classmap'], inverseMap=True) # Print again, this time replacing all labels with their assigned # purposes, bot original and found labels. Note the "identical label"