Reputation: 395
This is more of general "where do I find good resources to do something" question. I am attempting to use Python (OpenCV or otherwise) to classify images based on a training set.
My training set: this is made up of numerous images of product defects. Each image can be taken in 1 of 3 locations on the product and each image will contain 1 of 5 types of product defects. The defects have been manually classified and validated by a human.
Images to classify: These are made up of similar images, taken in the same 3 locations but the type of defect is not classified (although the defective area IS recognized by the tool taking the picture, it's just that the tool does not CORRECTLY classify them and I can't change the tool).
I have attempted to do this classification following recommendations in the book Programming Computer Vision with Python: Tools and algorithms for analyzing images. In this case I use SIFT descriptors stored in a mySQL database (training data) in a Bag of Words approach. So far I am not having too much luck (I continue to troubleshoot) and thought I'd seek advice from any OpenCV experts out there.
Any references or advice would be much appreciated.
So, coming back to the question I thought that it would be worth sharing what I've learned. I don't know that it's "the answer" but this is where I've ended up. A work in progress, you can always get better.
My solution right now has been to combine 3 different approaches. All are searchable on the internet so I won't go to great detail on the how.
First, I used the SIFT approach, generating SIFT histograms using a command line call to VLFeat. This may be an option elsewhere in Python, it's just what I used. I used k-means clustering to do the visual bag of words vocabulary thing and built a database tying the centroid's to word histograms associated with the training images. I improved results a bit by adding a Root SIFT step. Then I created a separate database using Dense SIFT (but no Root SIFT adjustments). Lastly, I created a database of color histograms based on the RGB components of the training images. I don't use all 256 bins of RGB but went with summing individual R,G and B values over 8 bins and then flattening the values to a 24 bin histogram.
This same process is followed with the unknown images, and then histogram vectors are compared using Euclidean distance. I tried Chi Squared comparison as well but, in my case Euclidean provided better results. I take the top 3 results from each process, with image classification based on 5 of 9 vote. If a majority isn't reached then the analysis is indeterminate.
For my closed population of images I am at 3.1% misclassification rate, 3.1% indeterminate.
Upvotes: 3
Views: 8974
Reputation: 1321
You can try building a model by uploading your training data (Defective, Not Defective) to demo.nanonets.ai (free to use)
1) Upload your training data here:
2) Then query the API using the following (Python Code):
import requests
import json
import urllib
model_name = "Enter-Your-Model-Name-Here"
url = "http://anzalonelawcolorado.com/wp-content/uploads/2013/10/product.jpg"
files = {'uploadfile': urllib.urlopen(url).read()}
url = "http://demo.nanonets.ai/classify/?appId="+model_name
r = requests.post(url, files=files)
print json.loads(r.content)
3) the response looks like:
{
"message": "Model trained",
"result": [
{
"label": "Defective",
"probability": 0.97
},
{
"label": "Not Defective",
"probability": 0.03
}
]
}
Upvotes: 1
Reputation: 1723
Implementing a CNN in Theano will probably give you better results than anything in OpenCV. If you search on Google scholar there are a huge number of papers on image classification using CNNs - most of these approaches should not be difficult to implement using Theano.
Upvotes: 0
Reputation: 5649
I have worked on the problem of image classification using Bag of features (BoF)and SVM. I did it using C++ and OpenCV but I am sure that you can get similar methods for python too.
Concept:
Create BoF Dictionary:
Training:
Testing:
You can refer this article
Upvotes: 7