Ask Your Question
0

Python training custom object detector using the HOG + Linear SVM

asked 2017-03-13 11:01:42 -0500

zelade gravatar image

Hello.

Im using Python and OpenCV on my raspberry pi 3 for some kind of object recognition. I want to do this by applying the HOG + Linear SVM framework for object detection. My problem is, that i need a dataset for training my detector.I would like to orientate on these five steps(from Pyimagesearch):

1. Extract HOG features from your positive training set.
2. Compute HOG feature vectors from your negative training set.
3. Train your Linear SVM.
4. Apply hard-negative mining.
5. Re-train your Linear SVM using the positive samples, negative samples, and hard-negative samples.
Has someone already made this and could help me? Is there some kind of documentation available? I would appreciate a step by step tutorial, but i already searched and found none.

I hope someone can help me. Kind regards.

edit retag flag offensive close merge delete

Comments

berak gravatar imageberak ( 2017-03-13 12:06:26 -0500 )edit

Thank you. Did you use this too?

zelade gravatar imagezelade ( 2017-03-14 06:46:22 -0500 )edit

why are you asking ?

berak gravatar imageberak ( 2017-03-14 07:24:31 -0500 )edit

I wanted to know if it worked reliably.

zelade gravatar imagezelade ( 2017-03-14 08:50:21 -0500 )edit

it all depends on your data, and your object (which is ?)

also, yes, adding steps 4 and 5 would improve it.

berak gravatar imageberak ( 2017-03-14 08:53:25 -0500 )edit

please clarify: do you want to train a HOGDetector (and use detectMultiScale() later) or an SVM classifier (with HOG features) ?

berak gravatar imageberak ( 2017-03-15 01:47:37 -0500 )edit

I´m not sure what is the best for implementation in videostreams. I made the example for the pedestrian detector: hog = cv2.HOGDescriptor(); hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() ). I think i want to do it this way with my objects (specific traffic signs). Back to your question i think i want to use detectMultiScale()).

zelade gravatar imagezelade ( 2017-03-15 03:12:31 -0500 )edit

I'm trying to code it myself in Python, if im successful i post the code here. Do you know, what is the Input Format for "hog.setSVMDetector(...)"? To compare and test, how can i run the Train_HOG.cpp on my raspberry pi or virtualbox?

zelade gravatar imagezelade ( 2017-03-16 03:16:56 -0500 )edit

"Do you know, what is the Input Format for "hog.setSVMDetector(...)"? "

it's the SVM's single supportvector, with -rho appended, see here

"how can i run the Train_HOG.cpp" -- maybe cmake -DBUILD_EXAMPLES=ON (but that will build all examples ..., if you don't want that, try to build trainHOG as a standalone prog

berak gravatar imageberak ( 2017-03-16 03:35:13 -0500 )edit

Okay i installed trainHog as standalone and ran it like you said it in this post (http://answers.opencv.org/question/96925/how-to-use-train_hogcpp/). I got the followin error:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /home/pyimagesearch/opencv/modules/core/src/matrix.cpp, line 495 terminate called after throwing an instance of 'cv::Exception' what(): /home/pyimagesearch/opencv/modules/core/src/matrix.cpp:495: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat

Aborted (core dumped)

zelade gravatar imagezelade ( 2017-03-16 10:39:49 -0500 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2017-03-17 06:15:19 -0500

zelade gravatar image

I still can't run the "train_HOG.cpp" file. What images does that need? Does it only work with a specific size? I tried png images 64x128.

I also tried another programm and could generate a trained file (*.yaml), is this the right file extension? The file looks as follows:

%YAML:1.0
cvHOGClassifier: !!opencv-object-detector-hog
   winSize: [ 64, 128 ]
   blockSize: [ 16, 16 ]
   blockStride: [ 8, 8 ]
   cellSize: [ 8, 8 ]
   nbins: 9
   derivAperture: 1
   winSigma: 4.
   histogramNormType: 0
   L2HysThreshold: 2.0000000000000001e-01
   gammaCorrection: 1
   nlevels: 64
   signedGradient: 0
   SVMDetector: [ 2.09888332e-02, 1.40957851e-02, 9.92207229e-03,....
edit flag offensive delete link more

Comments

indeed, your train images have to be same size as the winSize. so either resize your images to the default[64x128], or:

HOGDescriptor hog;
hog.winSize = size;
berak gravatar imageberak ( 2017-03-17 06:20:49 -0500 )edit

I tried it with 64x128. Apart from this i got better results with this file: https://github.com/DaHoC/trainHOG/blob/master/main.cpp. Above you see the YAML classifier file i created with it. Now i want to use it with SVMDetector(). I imported the YAML file and extractet the Detector data, but i receive a type error.

with open('cvHOGClassifiert.yaml') as infile:
   for i in range(skip_lanes):
             _ = infile.readline()
data = yaml.load(infile)
hog.setSVMDetector(data['SVMDetector'])

TypeError: _svmdetector is not a numpy array, neither a scalar

zelade gravatar imagezelade ( 2017-03-17 09:12:35 -0500 )edit

it's probably: hog.setSVMDetector( np.asarray( data['SVMDetector'], np.float32) )

then: i'm not sure, your example is using libsvm, not cv::ml::SVM ? no idea about compatibility here.

berak gravatar imageberak ( 2017-03-17 10:10:03 -0500 )edit

Thank you very much, that works. I just learned, that the parameters for the HOGDescriptor are very important. They must match perfectly, otherwise an error occurs. Thank you for your patience, i appreciate that.

zelade gravatar imagezelade ( 2017-03-17 11:08:23 -0500 )edit

I'm getting an error on: for i in range(skip_lanes): NameError: name 'skip_lanes' is not defined Do I have to import something?

mmc gravatar imagemmc ( 2017-05-19 15:58:30 -0500 )edit

^^ mmc, totally unrelated to anything in this topic.please 1. ask a new question. 2. add context

berak gravatar imageberak ( 2017-05-19 17:20:25 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-13 11:01:42 -0500

Seen: 5,894 times

Last updated: Mar 17 '17