Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic image classification #4

Open
schollz opened this issue Oct 5, 2016 · 0 comments
Open

Automatic image classification #4

schollz opened this issue Oct 5, 2016 · 0 comments

Comments

@schollz
Copy link
Owner

schollz commented Oct 5, 2016

Install simplecv + orange

Dependencies:

sudo apt-get install ipython python-opencv python-scipy \
 python-numpy python-pygame python-setuptools python-pip

Install:

sudo pip install svgwrite
sudo pip install https://github.com/sightmachine/SimpleCV/zipball/develop
sudo pip install orange   # takes awhile

Some test code:

from SimpleCV import *
import glob

class Trainer():

    def __init__(self,classes, trainPaths):
        self.classes = classes
        self.trainPaths = trainPaths


    def getExtractors(self):
        hhfe = HueHistogramFeatureExtractor(10)
        ehfe = EdgeHistogramFeatureExtractor(10)
        haarfe = HaarLikeFeatureExtractor(fname='haar.txt')
        return [hhfe,ehfe,haarfe]

    def getClassifiers(self,extractors):
        svm = SVMClassifier(extractors)
        tree = TreeClassifier(extractors)
        bayes = NaiveBayesClassifier(extractors)
        knn = KNNClassifier(extractors)
        return [svm,tree,bayes,knn]

    def train(self):
        self.classifiers = self.getClassifiers(self.getExtractors())
        for classifier in self.classifiers:
            classifier.train(self.trainPaths,self.classes,verbose=False)

    def test(self,testPaths):
        for classifier in self.classifiers:
            print classifier.test(testPaths,self.classes,verbose=False)

    def visualizeResults(self,classifier,imgs):
        for img in imgs:
            className = classifier.classify(img)
            img.drawText(className,10,10,fontsize=60,color=Color.BLUE)         
        imgs.show()

classes = ['chicken','nochicken',]

def main():
    trainPaths = ['./'+c+'/train/' for c in classes ]
    testPaths =  ['./'+c+'/test/'   for c in classes ]

    trainer = Trainer(classes,trainPaths)
    trainer.train()
    tree = trainer.classifiers[1]

    imgs = ImageSet()
    for p in testPaths:
        imgs += ImageSet(p)
    random.shuffle(imgs)

    print "Result test"
    trainer.test(testPaths)

    print(trainer.visualizeResults(tree,imgs))

    tree.save("tree.dat")
    classifierFile = 'tree.dat'
    classifier = TreeClassifier.load(classifierFile)
    for path in glob.glob("*/*/*jpg"):
        print(path,classifier.classify(Image(path)))

main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant