0

So I've been trying to run the command

opencv_traincascade -data HandsData -vec hands.vec -bg HandsNeg.txt -numPos 3641 -numNeg 2578 -numStages 20 -w 27 -h 48 -mode ALL -minHitRate 0.999 -maxFalseAlarmRate 0.5 -precalcValBufSize 1024 -precalcIdxBufSize 1024

and I get the error

===== TRAINING 0-stage =====
<BEGIN
POS count : consumed   3641 : 3641
OpenCV Error: Assertion failed (_img.cols == winSize.width) in get, file /builddir/build/BUILD/OpenCV-2.0.0/apps/traincascade/imagestorage.cpp, line 86
terminate called after throwing an instance of 'cv::Exception'
Aborted

I've seen suggestions around to change the positive image number, where I use 1043 instead and still get the same error. Then I also see suggestions of editing the source code. The problem is that I built OpenCV with yum and would like to not have to rebuild from source.

sudo find / -name imagestorage.cpp

turns up nothing.

I'm at a complete loss of what to do.

Additional info: Steps I took to get to this point

I created everything from some videos using ffmpeg. These were from a phone and VLC lists the info as

Resolution: 1920x1090
Display resolution: 1920x1080

The ffmpeg command was (replacing input/output with respective videos and locations)

steven ~/computer_vision $ ffmpeg -i videos/Not\ hands\ stuff.mp4 -y -r 40 -s 27x48 -f image2 NotHandsorFists/Negs-%4d.png

Files in Hands.txt are of the form

steven ~/computer_vision $ cat Hands.txt | head -n 1
Hands/LeftHand-0001.png 1 0 0 27 48

I compiled the vec file with

steven ~/computer_vision $ opencv_createsamples -info Hands.txt -num 3641 -w 27 -h 48 -vec hands.vec

The negative file is in the form

steven ~/computer_vision $ cat HandsNeg.txt | head -n 1
Fists/LeftFist-0001.png

and the working directory is

steven ~/computer_vision $ ls
Fists  fists.txt  Hands  HandsData  HandsNeg.txt  Hands.txt  hands.vec  NotHandsorFists  NotHandsorFists.txt  videos

Edit: I've tried changing png to jpg and bmp to get rid of the channels. No help.

Steven Walton
  • 505
  • 6
  • 20
  • sample images must have the same size as your detector windows – Micka Sep 06 '15 at 20:20
  • @Micka, I updated my post to include some additional info about my setup. As far as I can tell my sample images are the same size as my detector windows. Though I'm new to opencv and may be missing something. – Steven Walton Sep 06 '15 at 20:33
  • 1
    can you upload LeftHand-0001.png ? – Micka Sep 07 '15 at 04:47
  • https://i.imgur.com/R7wOdff.png – Steven Walton Sep 07 '15 at 04:59
  • @Micka, do you have any suggestions? I'm completely lost here. – Steven Walton Sep 11 '15 at 23:49
  • the error says that the image width isnt the same as the window width. I cant tell more. you have 3641 images in total, or is that the position that failed? – Micka Sep 12 '15 at 08:14
  • I'm not sure if that one was, but this is the position that fails, for sure https://i.imgur.com/3DaWc9Y.png and my image viewer says it is the correct size. – Steven Walton Sep 12 '15 at 21:39
  • maybe 3641 were succesful but 3642 fails? Could you try training with a subset of your images? – Micka Sep 12 '15 at 22:23
  • Well I unintentionally did when I was trying that lower number thing. It is failing at the first image. Also, I checked the shape of my image `>>> np.asarray(image).shape (48, 27, 3)` – Steven Walton Sep 12 '15 at 22:26
  • probably you can't upload your whole training data? – Micka Sep 12 '15 at 22:28
  • Yeah, it is a lot of files. I can upload a few sections though, would that be helpful? – Steven Walton Sep 12 '15 at 22:31
  • Here's two files from the negative sets. I only used two videos and I did the ffmpeg command above on both of them. One video is short and shows fists, the other is longer and shows a bunch of random stuff https://imgur.com/a/CPisg These are the first images from those sets – Steven Walton Sep 12 '15 at 22:41
  • Have you solve this problem? Would you try `opencv_traincascade -data HandsData -vec hands.vec -bg HandsNeg.txt -numPos 1000 -numNeg 1000 -numStages 20 -w 27 -h 48 -mode ALL -minHitRate 0.999 -maxFalseAlarmRate 0.5`? – AnnieFromTaiwan Sep 17 '15 at 17:04
  • I have not solved the problem. Why would reducing the photos to 1k fix it? I dropped them to about 2k and it didn't work still. I thought OpenCV could handle up to 4k images each. And shouldn't I be preallocating memory (I tried without). – Steven Walton Sep 17 '15 at 18:58
  • 1
    I've used much more than 4k images with no problem. Anyway assertion "CV_Assert( _img.cols == winSize.width );" is inside function "bool CvCascadeImageReader::NegReader::get( Mat& _img )" which works with negative images. There's a problem with your negatives and the row "POS count : consumed 3641 : 3641" means "I've finished working will all positives successfully". – Giuseppe Dini Sep 18 '15 at 14:31
  • So what could be wrong with them? Like images are too close? Or is it somehow sized wrong? Am I correct in that the negative file just needs the path to the images? If you're able to help me solve it I'll gladly rebounty and give you those points. This problem has been plaguing me for weeks. – Steven Walton Sep 19 '15 at 07:25
  • 1
    Even though it’s difficult to understand where the problem lies, I think I could help you, as I know well the source code (I’ve modified it several times). Looking at it, it seems that it won’t crash if one negative image is smaller than the windows size. Furthermore the documentations says: “each image should be (but not necessarily) larger then a training window size”. – Giuseppe Dini Sep 19 '15 at 09:22
  • 1
    Here http://answers.opencv.org/question/10725/is-there-a-bug-in-the-traincascade-of-opencv-243/ they say that it's a bug with older releases of opencv. Have you checked this? – Giuseppe Dini Sep 19 '15 at 09:25
  • 1
    If you are using new releases of the framework, try to modify file imagestorage.cpp, add the following line "cout << imgFilenames[last] << endln;" just before "src = imread( imgFilenames[last++], 0 );" It’s a way to find out what is the line where the program crashes. – Giuseppe Dini Sep 19 '15 at 09:26
  • So I compiled it from the rpm manager. Should I just uninstall and redownload the source, edit, and build? And are you saying my negative images should be bigger? Or that my windows (the -w -h?) should be smaller than the actual file? And I have opencv 2.0.0 – Steven Walton Sep 19 '15 at 18:16
  • 1
    In the first link I provided someone stated that he solved the issue after using a new version of OpenCv because maybe there is a bug. Why don't you try to download a new release of OpenCv before trying to bebug? – Giuseppe Dini Sep 20 '15 at 06:37
  • 1
    In case the error doesn't disappear with a newer version, you should use the libraries as they are and only recompile traincascade code (modifying it to debug). I created a project with traincascade inside my c++ editor and easily modify it wherever I need it. – Giuseppe Dini Sep 20 '15 at 06:42
  • 1
    As to the image size, I said that looking at the source code and reading the documentation, if some negative image accidentally is smaller than the windows size, it shouldn't raise an error. So debugging is not easy. – Giuseppe Dini Sep 20 '15 at 06:45
  • Well I'm trying it on another machine now and it seems to be running. The problem is that I would like to be running it on my clustered machine which is a Rocks distro (centOS based). Since both machines are using the same data, but one is using OpenCV 2.0.0 and the other 2.4.11, do you think that is just the difference? I'd love to get the cascade running on the cluster. I'll try installing 2.4.11 on the cluster and not 3.0 (getting errors) – Steven Walton Sep 21 '15 at 00:12
  • Also, centOS is python2.6, would that be causing an issue here? It didn't seem to be causing an issue anywhere else. – Steven Walton Sep 21 '15 at 00:13
  • Also, it is really messing up the time passed. I have after stage 2 `Training until now has taken 0 days 7 hours 10 minutes 53 second` and my last comment was an hour ago. – Steven Walton Sep 21 '15 at 01:56
  • 1
    So it seems that there was a bug in older releases. Yes, haartraining is very slow (the entire process could go on for more than a week). You can choose LBP instead of HAAR. It is much quicker. One more note: set -numPos less than 3641. When the process will discard the first positive it will stop because there will be no positive left. – Giuseppe Dini Sep 21 '15 at 06:38
  • Well it got going, but fails at stage 6 `===== TRAINING 6-stage ===== – Steven Walton Sep 22 '15 at 00:25
  • 1
    The algorithm needs 3641 positives (or 3600 now) at each stage. It can discard some positive as it goes. If so it will replace positives from the vec file. If it contains more than numPos it is possible, otherwise it will stop as you experimented. – Giuseppe Dini Sep 22 '15 at 10:55
  • 1
    Anyway I advice you to use less than 3600, maybe 3500 or 3550 positives. It will lessen the risk of running out of samples after a week that the algorithm is running. – Giuseppe Dini Sep 22 '15 at 11:33
  • I've tried bringing them down. Even as far down as 2000. There still seems to be that 5 difference. I feel like that is a clue but I'm unsure what it means. – Steven Walton Sep 23 '15 at 04:50
  • 1
    Don't exagerate! :) Too few samples worsen performance. You have to use as much positive samples as possible without the algorithm runs out of samples when it runs. 5 are the samples (out of 3600 or 2000) that the algorithm fails to classify as positive despite they are. – Giuseppe Dini Sep 23 '15 at 07:23
  • So I've been playing around with numbers and getting nowhere. Is there an algorithm or scheme I should follow? And could it be something else? And really, thank you for all the help you've been. I really appreciate it. – Steven Walton Sep 24 '15 at 06:11
  • 1
    What do you mean "getting nowhere". Is there a problem? There is not a scheme you can follow, but only experiments (that unfortunately are very costy, considering the amount of time it takes to train) and experience. The only rule, maybe, is: "the more samples you have, and the more they are representative of their population the better is the result". Anyway take 3550 positives and at least as much negatives, with the same parameters you already used, where is the problem? – Giuseppe Dini Sep 24 '15 at 11:23
  • The problem is that I can't get past stage 5. And only 5 stages creates a really bad cascade. – Steven Walton Sep 25 '15 at 02:48
  • 1
    Probably there is a problem with your HandsNeg.txt file. Try writing absolute paths of negative images inside it. And if you don't want to waste days before discovering if this resolves your problem, try to train with LBP features before. – Giuseppe Dini Sep 25 '15 at 06:56
  • 1
    Anyway, how many negative image do you have inside that file? Hope not only one. – Giuseppe Dini Sep 25 '15 at 06:57
  • I have about 4k in there. And I've been running multithreaded so it actually got through those 5 stages in a few hours. Seemed pretty fast to me. The times it was giving me were also inaccurate. I thought this might have to do with the multithreading though. Using an Intel i7-3630QM CPU @ 2.40GHz – Steven Walton Sep 26 '15 at 00:09
  • 1
    Multithreading is not an issue. There is a problem with your negatives, check your text file, all paths have to be ok, and check that all images are regular. You have to find out what is unregular I cannot do it for you (this guy solved a similar situation http://tinyurl.com/o7p4nnv it seems similar). It is not fast, as running time will increase exponentially with stages. – Giuseppe Dini Sep 26 '15 at 08:44
  • Well I'm on linux and using vim. I don't see any excess characters floating around. I tried absolute path names on the negative text and still no luck. I'm now trying to restart with a lower hit rate. 0.995 – Steven Walton Sep 26 '15 at 16:46
  • 1
    What is the size (wxh) of your 4k negative images? – Giuseppe Dini Sep 26 '15 at 17:05
  • 27x48. Original images were from a smartphone which I split with ffmpeg. Code I used is in the OP. In fact, entire process is in the OP with how the files are structured too. – Steven Walton Sep 26 '15 at 20:41
  • The reduced hit rate failed on stage 7. I forgot to comment back about that. – Steven Walton Sep 27 '15 at 03:19
  • 1
    Your negative images shouldn't be the same sime as positives. They have to be larger images where you are sure that there are not instances of what you consider positives. For example if for you a positive is a face, then you can take a photo of a landscape with no people inside it and link it in your background file as it is, without cropping. The algorithm will scan all the images like that and find negative windows (27x48) by itself. – Giuseppe Dini Sep 28 '15 at 08:25
  • Oh, thank you. I will try that out this week. You've been a lot of help – Steven Walton Sep 29 '15 at 01:05
  • Still kept breaking at stage 7. I never figured it out. – Steven Walton Oct 17 '15 at 16:33

0 Answers0