You ran opencv_createsamples with an -info file, the run stopped on a parse error, and good.vec is missing or short. The images are almost never the problem; the description file is. Each line must hand the tool a path, an object count and a box, the paths must use forward slashes, and -num must match the number of lines you wrote. This page walks the failure in the order you meet it: the info file, the paths, -num, -w and -h, the vec check, and the capture rig that produces positives worth annotating. The running example is a two-image test set built for 50 x 50 samples.
Why does opencv_createsamples report a parse error?
Because the tool reads each -info line as a path, then an object count, then four integers per object, and lines written as bare coordinates give it neither: it reads your first number as the count and runs out of integers before the line satisfies that count.
The utility scans the description file line by line with a plain text reader. The format is given in the OpenCV 2.4 cascade training guide: a path, the number of objects on the image, then one rectangle per object. Nothing about the BMP files can cause this message, because the tool has not opened an image yet when it fails. Two habits in a first attempt break it: leaving the count off every line, and leaving -num at its default with only a handful of lines to read. Fix those two and the vec builds.
What does each line of the -info file need?
One path, one object count, then x, y, width and height for every box on that line, in source-image pixels measured from the top-left corner, so the two example lines become good/0.bmp 1 0 0 99 51 and good/1.bmp 1 9 7 55 103.
| Line as written | What the parser gets | Line that parses |
|---|---|---|
good\0.bmp 0 0 99 51 |
path good\0.bmp, count 0, three numbers left dangling | good/0.bmp 1 0 0 99 51 |
good\1.bmp 9 7 55 103 |
path good\1.bmp, count 9, so 9 x 4 = 36 integers wanted and 3 supplied | good/1.bmp 1 9 7 55 103 |
Keep one object per line. The count tells the parser how many four-integer boxes follow; multi-box lines are legal, but the classic toolchain stores one sample per line, so a single tight box per line is the arrangement that trains cleanly.
- Open good.dat in a plain-text editor and save it as ANSI or UTF-8 without a byte-order mark.
- Rewrite every line as
path count x y width height, slashes forward, one object per line. - Rerun the command with -num set to the number of lines, 2 for this set.
Why must the paths use forward slashes?
Because the description file is plain text that the training tools read token by token and resolve against the directory you launch from, and a forward-slash relative path survives both steps on Windows, while backslash paths fail in enough builds that forward slashes are the only form worth writing.
The second half of that answer matters as much as the first: the image paths in the dat file resolve from your working directory, not from the folder holding the dat file. If good.dat sits next to a folder of BMPs, launch the utility from that folder, or write the paths the way they look from wherever you do launch it. The same discipline runs through the pipeline. The path errors across operating systems that break scripts break training tools too, and a finished classifier can still fail at detection time over a missing cascade file path. Avoid spaces in folder names along the way; renaming beats quoting.
What value should -num have?
Exactly the number of description lines, 2 for the two-image test set, because -num defaults to 1000 and the utility treats running out of lines before the requested count as a failure rather than as a short file.
The arithmetic for the test set: the tool wants 1000 samples, the file offers 2 lines, and 998 descriptions are missing when it reaches end of file, so it exits with the parse error instead of writing a short vec. A report filed against the tool (OpenCV issue 19596) shows the same pattern at scale: the run only succeeds once -num drops below the number of descriptions available. Set -num equal to the line count, and update it every time you add lines.
How do you pick -w and -h?
In the aspect ratio of your annotated object, and identical in traincascade: the sample boxes here are 99 x 51 (1.94:1) and 55 x 103 (0.53:1), so a square 50 x 50 target stretches both by close to a factor of two in one direction.
For the wide box, 99 / 51 = 1.94, and with h = 26 the formula gives w = round(1.94 × 26) = 50, so 50 x 26 fits that part. The second box is portrait, 55 / 103 = 0.53, and one -w x -h pair cannot serve both orientations without distortion. Decide at capture time: rotate tall parts to landscape and train one cascade, or keep both orientations and train two. Give the boxes pixels to spare as well: the source box should be at least twice the vec size, so -w 50 wants boxes around 100 px wide. The 99 px box is 1.98x and survives; the 55 px box is 1.10x and gets upscaled, which softens the sample. The tool default of 24 x 24 suits near-square objects only.
How do you verify the vec file before training?
Rerun opencv_createsamples with -vec good.vec -show and page through the stored samples, then run a one-stage traincascade smoke test with -numPos at or below your line count; a clean single-stage pass proves the vec parses and holds the samples you think it does.
- Run the tool with
-vec good.vec -show: each stored sample opens in turn, and every one should show the object centered and undistorted at the aspect you chose. - Check the file size: 2 samples at 50 x 50 pixels is 2 × 2500 = 5000 bytes of sample data plus a short header, so a good.vec near 5 KB is right; half that means one sample went missing.
- Smoke-test training with
opencv_traincascade -data smoke -vec good.vec -bg neg.dat -numPos 2 -numNeg 4 -numStages 1 -w 50 -h 50, using your own -w and -h. One finished stage is a parsing proof, not an accuracy result.
Before rerunning after any edit, run through this list:
- every line carries a count that matches the boxes after it
- paths use forward slashes, no spaces, resolved from the launch directory
- -num equals the line count
- -w and -h match the traincascade values
- the dat file is plain text, saved without a byte-order mark
Why does it still fail after the file is fixed?
Three leftovers, in order of likelihood: the images are not where the command runs, the dat file is not plain text, or the tool itself is not on PATH; the first two reproduce on every machine, the third is machine-specific.
The file-not-found case, filed as OpenCV issue 14322, looks like a parse error but is a resolution problem: the dat file paths are relative to the working directory, so run the utility from the image folder once and watch the message change. The encoding case is the PowerShell redirect above. The tool case ends with a not-recognized message: add the OpenCV build bin folder, build\x64\vc12\bin in a 2.4-style install, to PATH, or call the exe by its full path. Build and path problems of that kind are the subject of fixing the OpenCV CMake config path, and the systems integration guides collect the rest. The training applications themselves belong to the 2.4 and 3.4 toolchains, so keep a 3.4 install for cascade work.
How do you capture positives that train well?
Lock everything except the object: fixed exposure and gain, fixed focus, fixed geometry on a stand, one pose per capture, and a box drawn within a few pixels of the object edge, so the cascade learns the object rather than your lighting drift.
Budget first. A workable cascade starts around 500 positive lines; 1,000 to 3,000 is comfortable, with negatives at two to three times the positives drawn from the real scene. For traincascade, set -numPos to about 90 percent of your positives: 1,000 positives gives 1,000 × 0.9 = 900, because later stages consume samples. Vary distance, angle and background across the set; never exposure.
Then fix the rig. Turn off auto exposure and auto gain so frames stay comparable, the way auto exposure and auto gain settings describe. Lock focus once and leave it, as locking the focus before calibration shows. Correct uneven illumination in software the way flat-field correction by image division does, and take the specular highlight off shiny parts with a crossed polarizer on lens and light. Draw the boxes against the actual pixels: burning a drawn ROI into the image is a quick way to see where they land.
For hardware, start with area scan cameras. The MV-CU020-90GC is a 2.5 MP GigE camera with a global shutter and a C-mount.
- Sensor format
- 1/1.7"
- Lens mount
- C-mount
- Interface
- GigE
- Resolution
- Up to 3 MP
Mounting decides repeatability, and the camera stands carry the columns and stages for it. The LS-MNT-0040 is a slide-table stand with a 700 mm height and a 400 x 450 mm base.
- Type
- Camera stands
For even light across the part, look through lighting accessories. The LS-LAC-0010 is a 170 x 30 mm bar light diffuser plate that turns a hard bar light into a softer, more uniform patch.
- Size
- 100-200 mm
When the rig itself is the next purchase, start at the camera step of the configurator:
The configurator matches a camera, lens, light, light controller and cables to each other, step by step, and lists only parts that fit what you already picked.
Questions
Can one -info line list several objects?
Yes. The count is the number of boxes, and four integers follow for each: part/12.bmp 2 10 8 60 40 90 20 55 45 describes two objects. The classic toolchain still writes one vec sample per line, so most trainers keep a single object per line and crop extra objects into their own captures instead.
Must -w and -h match between createsamples and traincascade?
Yes, to the pixel. The vec file stores each sample at the width and height you set, and traincascade expects the same integers on its own -w and -h; a mismatch stops stage 0 with a sample-read error before any training runs. Pick the pair from your box aspect ratio once and reuse it in both commands.
Does the vec file store whole images or only the objects?
Only the object. Each sample is the cropped box, converted to grayscale and resized to -w x -h, stored as one byte per pixel behind a short header, so 50 x 50 means 2500 bytes per sample. That is why a loose box hurts: the background inside it becomes the training sample.
Why does the same error appear on a different computer?
Because the mistake travels with the description file: identical lines parse identically on any machine and any build of the same tool generation, so copying the project copies the error. A machine change only helps when the real cause is local, such as the tool bin folder missing from PATH or images stored under a different drive letter.
Do the training applications ship with current OpenCV?
No. opencv_createsamples and opencv_traincascade belong to the 2.4 and 3.4 toolchains; the 4.x application set leaves them out, so keep a 3.4 build installed for classic cascade training or move detection to a DNN model. Cascade xml files you have already trained keep loading in current releases for detection.


