Deploy computer vision for quality inspection, defect detection, and process monitoring on the factory floor. Covers model selection, edge deployment, camera setup, and ROI analysis.
This guide describes how to implement computer vision systems for manufacturing processes using standard tools and frameworks. It applies when deploying visual inspection, quality control, or automation workflows in production environments where images are captured from cameras and analyzed for defects, measurements, or classification.
Manufacturing computer vision systems begin with image capture from industrial cameras. Use OpenCV's VideoCapture interface to read video streams:
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
Frame rate must be consistent; mismatched FPS between camera and processing pipeline causes temporal misalignment. Use cv2.CAP_PROP_FPS to query device capabilities:
fps = cap.get(cv2.CAP_PROP_FPS)
print(f"Camera FPS: {fps}")
For industrial lighting, apply gamma correction using OpenCV's cv2.LUT with a lookup table. Ensure consistent illumination across all camera angles to avoid false positives.
Choose between off-the-shelf models or custom training based on defect types. For binary classification (defect/no-defect), use ResNet50 with transfer learning:
# Using TensorFlow/Keras command line tool
python -m tensorflow.keras.applications.ResNet50 \
--weights imagenet \
--include_top False \
--input_shape 224,224,3
Training data must be balanced. Use --class_weight parameter to handle class imbalance:
python train.py \
--data_dir /data/images \
--model_save_path /models/defect_detector.h5 \
--class_weight "0:1.0,1:3.0"
Deploy models using TensorFlow Lite for edge inference. Convert a Keras model:
# Convert to TensorFlow Lite format
tflite_convert \
--keras_model_file /models/defect_detector.h5 \
--output_file /models/defect_detector.tflite \
--target_ops=TF_OPERATORS
Use --allow_custom_ops for unsupported layers. The conversion process silently drops unsupported ops, causing runtime failures without error messages.
Connect vision systems to factory control systems using OPC UA protocol. Establish connection with:
import opcua
client = opcua.Client("opc.tcp://localhost:4840")
client.connect()
Read/write values from nodes using get_value() and set_value() methods. Nodes must be properly configured in the OPC UA server; missing or incorrectly typed nodes cause silent failures.
Implement inference pipeline with threading to avoid blocking camera input. Use Python's multiprocessing module:
from multiprocessing import Process, Queue
def inference_worker(input_queue, output_queue):
while True:
image = input_queue.get()
if image is None:
break
result = model.predict(image)
output_queue.put(result)
Log all failures during image processing:
import logging
logging.basicConfig(filename="/var/log/vision_system.log", level=logging.INFO)
try:
result = model.predict(frame)
except Exception as e:
logging.error(f"Inference failed: {e}")
raise
Handle corrupted frames gracefully. Most vision libraries do not validate image integrity before processing, so corrupted JPEGs silently produce invalid results.
Validate system performance using ground-truth images stored in /data/validation. Compare predictions with manual inspections:
# Calculate accuracy metrics
def calculate_accuracy(predictions, labels):
correct = sum(p == l for p, l in zip(predictions, labels))
return correct / len(labels)
Monitor inference latency using system calls. Measure time per frame:
# Using time command for process timing
time python inference.py --input /data/frame.jpg
Set maximum acceptable inference time (e.g., 50ms) and alert when exceeded. Use psutil to monitor CPU usage during processing.
Calibrate cameras using OpenCV's calibrateCamera function with a checkerboard pattern:
# Example calibration procedure
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
Incorrect calibration causes geometric distortion in results, especially for dimensional measurements.
Store processed images in a time-series database like InfluxDB. Use influxdb-client to write metrics:
from influxdb_client import InfluxDBClient
client = InfluxDBClient(url="http://localhost:8086", token="my-token")
write_api = client.write_api()
Write measurements with timestamps for historical analysis.
Models silently fail when input resolution does not match training data. Input images must be 224x224 pixels; mismatched sizes cause incorrect predictions without explicit errors.
Camera configuration may specify frame size but not actual capture resolution. Use cv2.CAP_PROP_FRAME_WIDTH and cv2.CAP_PROP_FRAME_HEIGHT to verify settings:
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
If these differ from expected values, the system produces images of incorrect dimensions.
Use unit tests for image processing functions:
import unittest
class TestImageProcessing(unittest.TestCase):
def test_correct_image_size(self):
img = cv2.imread("test.jpg")
self.assertEqual(img.shape[:2], (224, 224))
Validate that all pipeline components handle missing or corrupted data gracefully.
This page was rewritten on 15 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy