<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: PRASANNA G</title>
    <description>The latest articles on DEV Community by PRASANNA G (@prasanna_g_8cdb20a7852c7c).</description>
    <link>https://dev.to/prasanna_g_8cdb20a7852c7c</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3596108%2F941612ed-04c6-4698-846e-7c49f9e65f84.png</url>
      <title>DEV Community: PRASANNA G</title>
      <link>https://dev.to/prasanna_g_8cdb20a7852c7c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prasanna_g_8cdb20a7852c7c"/>
    <language>en</language>
    <item>
      <title>How I Built a 95% Accurate Defect Detection System with an ESP32-CAM and Python</title>
      <dc:creator>PRASANNA G</dc:creator>
      <pubDate>Wed, 05 Nov 2025 15:36:51 +0000</pubDate>
      <link>https://dev.to/prasanna_g_8cdb20a7852c7c/how-i-built-a-95-accurate-defect-detection-system-with-an-esp32-cam-and-python-503e</link>
      <guid>https://dev.to/prasanna_g_8cdb20a7852c7c/how-i-built-a-95-accurate-defect-detection-system-with-an-esp32-cam-and-python-503e</guid>
      <description>&lt;p&gt;I wanted to build a system that was:&lt;/p&gt;

&lt;p&gt;Low-cost: Using the super-cheap ESP32-CAM module.&lt;/p&gt;

&lt;p&gt;Scalable: Easy to adapt to new products.&lt;/p&gt;

&lt;p&gt;Real-time: Fast enough for a production line.&lt;/p&gt;

&lt;p&gt;Most people would jump straight to a complex deep learning model like YOLO or a big CNN. But that comes with its own set of problems:&lt;/p&gt;

&lt;p&gt;Data Hunger: You need thousands of labeled defect images to train the model.&lt;/p&gt;

&lt;p&gt;"Black Box" Problem: When it fails, it's hard to know why.&lt;/p&gt;

&lt;p&gt;Heavy Hardware: These models often need a GPU to run in real-time, which kills the "low-cost" goal.&lt;/p&gt;

&lt;p&gt;So, I tried a different approach. Instead of teaching a complex AI what a "defect" looks like, I decided to just teach my system what a perfect product looks like.&lt;/p&gt;

&lt;p&gt;My secret weapon? A classical computer vision technique called the Structural Similarity Index (SSIM).&lt;/p&gt;

&lt;p&gt;💡 The Tech Stack&lt;br&gt;
The architecture is a simple client-server model:&lt;/p&gt;

&lt;p&gt;Client: An ESP32-CAM module. Its only job is to capture images of the product (in my case, a washing machine outer component&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbu362f7w9yeush97k00t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbu362f7w9yeush97k00t.png" alt=" " width="800" height="470"&gt;&lt;/a&gt;Manual quality control is a pain. It's slow, expensive, and prone to human error. While high-end machine vision systems exist, they are often too expensive for small shops or hobbyist projects.&lt;/p&gt;

&lt;p&gt;Server: A Python Flask server. This is the "brain." It runs on a PC and does all the heavy lifting.&lt;/p&gt;

&lt;p&gt;The "Brain": The OpenCV library. It receives images, cleans them up, and runs the SSIM comparison.&lt;/p&gt;

&lt;p&gt;Here’s the basic flow:&lt;/p&gt;

&lt;p&gt;]&lt;/p&gt;

&lt;p&gt;⚙️ How It Works: The SSIM Workflow&lt;br&gt;
The core idea is simple: I store a "golden" (defect-free) reference image on the server. Then, for every new image that comes from the ESP32-CAM, the server compares it to that golden image.&lt;/p&gt;

&lt;p&gt;Here's the step-by-step process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Capture and Transmit&lt;br&gt;
The ESP32-CAM is set to capture 640x480 resolution frames. It sends each frame to a dedicated endpoint on my Flask server using a simple HTTP POST request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pre-processing (The Most Important Step!)&lt;br&gt;
A raw image from a factory floor is messy. Lighting changes, and the camera or part might be slightly misaligned. Before I can do any comparison, I have to "normalize" the image.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Geometric Alignment: First, the server aligns the incoming image with the golden template to correct for any minor shifts or rotations.&lt;/p&gt;

&lt;p&gt;Histogram Equalization: This normalizes the image's brightness and contrast, making the system robust against flickering or changing ambient light.&lt;/p&gt;

&lt;p&gt;Gaussian Blur: A minor blur is applied to remove high-frequency sensor noise that could otherwise be flagged as a defect.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Analysis with SSIM
SSIM is a metric that measures the similarity between two images based on how a human would perceive them (it looks at structure, luminance, and contrast). It returns a score between -1 and 1, where 1 means the images are identical.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Instead of one global comparison, I use a sliding window to create an SSIM "heat map". Any region with a significant structural difference (like a scratch or crack) will produce a very low SSIM score, instantly highlighting the defect.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Decision
The system checks the SSIM scores in key Regions of Interest (ROIs), like the hinge, seal, and glass. If the score in any ROI drops below a calibrated threshold (e.g., &amp;lt; 0.85), the server outputs a "Defective" decision.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;📊 The Results: It Actually Works!&lt;br&gt;
I tested this system on a public dataset of 1,000 images (a 50/50 split of good and defective parts).&lt;/p&gt;

&lt;p&gt;Accuracy: 95% overall accuracy.&lt;/p&gt;

&lt;p&gt;Speed: 140 ms average end-to-end latency per frame (from capture to decision) on a standard quad-core CPU. That's fast enough for real-time inspection.&lt;/p&gt;

&lt;p&gt;Robustness: The SSIM method was more robust to lighting changes than a traditional ML (HOG+SVM) approach.&lt;/p&gt;

&lt;p&gt;Here’s a look at the system in action:&lt;/p&gt;

&lt;p&gt;] ] ]&lt;/p&gt;

&lt;p&gt;When I compared my 140ms SSIM approach to a more "traditional" machine learning pipeline (using HOG features + an SVM classifier), my system was 33% faster (140ms vs. 210ms) and achieved the same accuracy (95% vs 94%) .&lt;/p&gt;

&lt;p&gt;✅ Why This Approach is Awesome&lt;br&gt;
No Model Training: This is the biggest win. I don't need a massive, labeled dataset. I just need a small set of "golden" reference images.&lt;/p&gt;

&lt;p&gt;It's a "White Box": Unlike a neural network, this system is 100% explainable. If a defect is flagged, I can output the SSIM heat map and see exactly which pixels caused the failure.&lt;/p&gt;

&lt;p&gt;Super Flexible: Need to inspect a new product model? Just drop a new "golden" image into the server's template library. No retraining, no new code.&lt;/p&gt;

&lt;p&gt;🚀 What's Next?&lt;br&gt;
This SSIM-based system proved to be a simple, robust, and practical alternative to complex deep learning models for this use case.&lt;/p&gt;

&lt;p&gt;The next steps are to make it even more robust:&lt;/p&gt;

&lt;p&gt;Dynamic Template Management: Automatically update the "golden" reference image to account for gradual wear and tear.&lt;/p&gt;

&lt;p&gt;Hybrid Approach: Use a lightweight neural network to find potential defects and then use SSIM to confirm them.&lt;/p&gt;

&lt;p&gt;Smart Factory Integration: Integrate with IoT protocols like MQTT to send alerts directly to a dashboard or stop the production line.&lt;/p&gt;

&lt;p&gt;Thanks for reading! Let me know what you think in the comments.&lt;/p&gt;

</description>
      <category>python</category>
      <category>iot</category>
      <category>computervision</category>
      <category>embedded</category>
    </item>
  </channel>
</rss>
