How to Use Computer Vision for Quality Control on a Small Manufacturing Line

How to Use Computer Vision for Quality Control on a Small Manufacturing Line Machine Learning

On a line that turns out a few hundred parts an hour, quality control often comes down to one person with sharp eyes and a sore back. That works until the third hour of a shift, or the batch of material that reflects light oddly, or the flaw that only shows up from one angle. A camera and a small model will not replace that person. They can, however, reliably catch the one defect that keeps reaching your customer.

What follows is the version of this project that fits a small shop: an off-the-shelf camera, a light, a trigger sensor, a modest industrial PC, and a willingness to spend a couple of weeks collecting images before writing any clever code.

Decide What a Defect Is Before You Buy Anything

Start with the reject rule, not the hardware. Write down each failure mode you care about — crack, dent, scratch, missing hole, wrong insert, discoloration — and the size at which it becomes unacceptable. A 0.5 mm scratch on a hidden bracket face is not the same problem as a 0.5 mm scratch on a visible panel. Being explicit now prevents the classic trap of building a system that flags everything a human would ignore.

Then decide where the inspection happens. End-of-line inspection is the simplest to install and the easiest to justify, because you only need one camera and one part in a fixed position. In-line inspection catches errors earlier and saves scrap, but it constrains cycle time and usually needs a faster trigger and a sturdier mount. For a first project, choose end-of-line and one defect type.

Finally, define the action. Does a bad part get blown into a reject bin, marked with a dab of ink, or does the line stop? The answer determines what hardware you need and how much a false reject actually costs you.

Light the Part Properly

Lighting decides more outcomes than model architecture ever will. A diffuse dome light flattens reflections on machined or shiny surfaces and makes dents readable. Backlighting turns the part into a silhouette, which is ideal for checking hole presence, edge shape and burrs. A low-angle bar light rakes across a surface and makes scratches and tool marks visible that would otherwise vanish.

A few rules that save weeks:

  • Fix the camera distance and lock it. A bracket bolted to the frame beats a tripod, and an enclosure beats both.
  • Rule of thumb: the smallest feature you must detect should span at least ten pixels. Work backwards from that to pick resolution.
  • Use a global-shutter camera if the part is moving. Rolling shutter produces skewed edges on fast motion.
  • Lock exposure, gain and focus manually. Auto settings drift and quietly change your data distribution.
  • Block ambient light. A window that changes brightness at 4 p.m. will break a system that worked all morning.

A phone camera on a clamp is fine for a two-day feasibility test. It is not fine for a line running eight hours a day.

Collect Real Images, Label Them Consistently

Plan on a few hundred images minimum, captured across shifts, material batches and lighting conditions you actually see. Include good parts, bad parts, and the ambiguous ones — oil smudges, dust, chips, harmless reflections. Those hard negatives are what teach the system the difference between a defect and a nuisance.

Label with bounding boxes if you need to locate the flaw, or with a simple pass/fail tag if you only need a decision. CVAT, LabelImg and similar open tools all do the job. Write a one-page labeling guide and have two people label the same twenty images independently; the disagreements will reveal rules you have not articulated yet.

You can rehearse the whole pipeline on public data before your own images exist. The MVTec AD benchmark contains industrial objects with various defect types and is built for anomaly detection work. NEU-DET covers surface defects on hot-rolled steel. The DAGM dataset offers textured surfaces with weak supervision, and several Kaggle collections show casting and machining defects. Check each license before commercial use, and remember these parts are not your parts — use them to learn the tooling, not to ship a final model.

Choose a Model That Fits Your Data

There are two sensible starting points, and your data decides between them.

If you can collect hundreds of examples of each defect, train a supervised detector — a YOLO-family model for bounding boxes, or a small convolutional classifier on cropped patches if the part is always in the same place. Start from pretrained weights and fine-tune; you rarely need to train from scratch.

If defects are rare, which is the usual case, use anomaly detection. Train only on good parts and let the model score deviation. PatchCore and PaDiM are practical choices, and a simple baseline — average all good images, then threshold the pixel difference — is worth trying first because it takes an afternoon and sometimes works.

Whatever you pick, split your data by time or production batch rather than randomly. Random splits let near-duplicate images from the same hour land in both training and test sets, which flatters your results and hides real failures.

Connect the Camera to the Line

Inference can run on a small industrial PC. Export the model to ONNX and run it with a lightweight runtime; a modest CPU handles a small detector in tens of milliseconds, and a GPU is only necessary if you inspect many parts per second or use a heavy segmentation model.

  1. A photoelectric sensor detects the part arriving in the inspection zone.
  2. The sensor triggers the camera and a strobe or continuous light source.
  3. The PC runs the model and returns pass or fail.
  4. A digital output sends the result to the PLC, which actuates a reject cylinder or stops the line.
  5. Every image and decision is written to disk with a timestamp.

Budget your latency. If a part arrives every 400 ms and inference takes 90 ms, you have room. If it takes 500 ms, the part is already gone and no amount of model tuning will fix it — move the camera upstream or slow the belt.

Run the system in shadow mode for the first week or two. It logs decisions; a human still decides. This tells you how the model behaves on real production without risking a bin full of good parts.

Measure It Like a Production Tool

A single accuracy number tells you almost nothing. Track precision and recall per defect type, and weigh them by cost. A missed crack that ships to a customer is usually far more expensive than a good part in the reject bin, but only you know the real ratio for your product.

Review escapes — bad parts that passed — every week and look at the images. Most failures come from conditions you did not think to capture: a new material supplier, a worn tool, a changed coolant pattern. When the process changes, retrain and keep the old model version so you can compare.

Start Small, Then Expand

Pick the single most costly defect, one station, one camera, one light. Spend two weeks on images and lighting before you spend two days on the model. Get the system to the point where it clearly beats a tired human on that one defect at that one station, and document everything: the labeling rules, the images, the model version, the failure cases.

Only then add a second defect type or a second station. The first system is the expensive one, mostly in learning. The next ones reuse the mounting, the lighting habits, the labeling guide and the deployment script, and they come together far faster than you would expect.

Photo: Keegan Checks / Pexels