Factsheet
/ 6 June 2026; 4 months ago
/ 6 June 2026; 4 months ago
Image Classification in OpenCV (Python) Based on Training Set - Stack Overflow
I installed opencv with "pip install opencv-python" everything worked, but I can only import it in the command prompt!
python - How do I install opencv using pip? - Stack Overflow
Building OpenCV for an alternative python install - Python - OpenCV
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:
- Take one image from your training samples.
- Extract SIFT keypoints
- Extract SIFT descriptors
- Use k-means clustering to cluster the descriptors
- Create BoF dictionary (Refer the link which I have mentioned below)
Training:
- Load your BoF dictionary
- Initialize your BoF instance with the above BoF dictionary
- Take one image from your training samples.
- Extract SIFT keypoints
- Find BoF descriptors for the extracted keypoints.
- Use this BoF descriptor for the SVM learning
- Do the above steps for all the training images
- You will get a SVM classifier files in .xml form...save it
Testing:
- Load your SVM classifier
- Initialize your SVM classifier with the .xml file which your created above
- Capture image
- Find BoF descriptor for the captured image using your dictionary
- Use those BoF descriptor to classify it using SVM
You can refer this article
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:
demo.nanonets.ai
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
}
]
}
You've probably heard this question a thousand times. I installed opencv with pip install opencv-python. Everything worked, I can import it in the command line and use the methods. Now, if I try to import in in VSCode the "no module named cv2" error is returned.
Install opencv-python (which is the official pre-built OpenCV package for Python) by issuing the following command:
pip install opencv-python
run the following command by creating a virtual enviroment using python 3 and run
pip3 install opencv-python
to check it has installed correctly run
python3 -c "import cv2"
I found this.
OpenCV on wheels.
Unofficial OpenCV packages for Python.
Installation was painless for Ubuntu 16.04:
pip3 install opencv-python
Check the installation:
python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'
python3-opencv can be installed from the Universe repository in Ubuntu 17.10 and later. Open the terminal and type:
sudo apt install python3-opencv
Upgrade your OS to 18.04 if you are using Ubuntu 14.04 or Ubuntu 16.04. If Ubuntu 16.04 is installed in WSL sudo do-release-upgrade will upgrade it to 18.04.