Baumer工業相機堡盟工業相機如何通過YoloV8深度學習模型實現標簽條碼一維碼的檢測(C#代碼,UI界面版))
- 工業相機使用YoloV8模型實現標簽條碼一維碼的檢測
- 工業相機通過YoloV8模型實現標簽條碼的檢測的技術背景
- 在相機SDK中獲取圖像轉換圖像的代碼分析
- 工業相機圖像轉換Bitmap圖像格式和Mat圖像重要核心代碼
- 本地文件圖像轉換Bitmap圖像格式和Mat圖像重要核心代碼
- Mat圖像導入YoloV8模型重要核心代碼
- 代碼實現演示(實現標簽條碼一維碼的檢測)
- 源碼下載鏈接
- 工業相機通過YoloV8模型實現標簽條碼的檢測的行業應用
- 關鍵技術細節
?
工業相機使用YoloV8模型實現標簽條碼一維碼的檢測
本項目集成了 YOLOv8 檢測模型 與 C#圖形界面工具,實現了包括圖片、文件夾、視頻與攝像頭等多種輸入方式的實現標簽條碼一維碼的檢測。
Baumer工業相機堡盟相機是一種高性能、高質量的工業相機,可用于各種應用場景,如物體檢測、計數和識別、運動分析和圖像處理。
Baumer的萬兆網相機擁有出色的圖像處理性能,可以實時傳輸高分辨率圖像。此外,該相機還具有快速數據傳輸、低功耗、易于集成以及高度可擴展性等特點。
?
Baumer工業相機由于其性能和質量的優越和穩定,常用于高速同步采集領域,通常使用各種圖像算法來提高其捕獲的圖像的質量。
本文以Baumer工業相機作為案例進行演示,實現將工業相機的圖像或者本地圖像導入Yolo模型從而實現標簽條碼的檢測等功能。
工業相機通過YoloV8模型實現標簽條碼的檢測的技術背景
本文通過C#中實現一個簡單的UI界面,用于將YoloV8模型實現標簽條碼一維碼的檢測。
用戶可以通過該界面執行以下操作:
-
轉換相機圖像為Mat圖像:通過YoloV8模型實現標簽條碼一維碼的檢測
-
轉換本地圖像為mat圖像:通過YoloV8模型實現標簽條碼一維碼的檢測
通過這個UI界面,用戶能夠在實時應用機器視覺數據處理時快速有效地進行操作,無需深入了解圖像數據的底層處理過程。這個簡單的介紹旨在為開發人員提供一個明確的方向,以便開始構建此類應用程序,并且該程序主要用于演示目的。
在相機SDK中獲取圖像轉換圖像的代碼分析
本文介紹使用Baumer工業相機,實現將圖像轉換為Bitmap圖像,再轉換Mat圖像,導入到Yolo模型進行推理,輸出實現標簽條碼一維碼的檢測的結果。
工業相機圖像轉換Bitmap圖像格式和Mat圖像重要核心代碼
//將相機內部圖像內存數據轉為bitmap數據
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)mBufferFilled.Width, (int)mBufferFilled.Height,(int)mBufferFilled.Width,System.Drawing.Imaging.PixelFormat.Format8bppIndexed, (IntPtr)((ulong)mBufferFilled.MemPtr + mBufferFilled.ImageOffset));#region//Mono圖像數據轉換。彩色圖像數據轉換于此不同
System.Drawing.Imaging.ColorPalette palette = bitmap.Palette;
int nColors = 256;
for (int ix = 0; ix < nColors; ix++)
{uint Alpha = 0xFF;uint Intensity = (uint)(ix * 0xFF / (nColors - 1));palette.Entries[ix] = System.Drawing.Color.FromArgb((int)Alpha, (int)Intensity,(int)Intensity, (int)Intensity);
}
bitmap.Palette = palette;
#endregionstring strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
string saveimagepath = pImgFileDir + "\\" + strtime + ".brw";//使用Bitmap格式保存
bitmap.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Bmp); //用bitmap轉換為mat
OpenCvSharp.Mat Matgray1 = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);
本地文件圖像轉換Bitmap圖像格式和Mat圖像重要核心代碼
C#環境下代碼如下所示:
if (imagePaths.Count() == 0)
{LoadImagePaths("test_img");
}string currentImagePath = imagePaths[currentImageIndex];// 顯示到pictureBoxA
pictureBoxA.Image.Dispose(); // 釋放上一張圖片資源,避免內存泄漏
pictureBoxA.Image = new Bitmap(currentImagePath);
image_path = currentImagePath;currentImageIndex = (currentImageIndex + 1) % imagePaths.Count;OnNotifyShowRecieveMsg("檢測中,請稍等……");
//textBox1.Text = "檢測中,請稍等……";
//pictureBox2.Image = null;
Application.DoEvents();image = new Mat(image_path);float ratio = Math.Min(1.0f * inpHeight / image.Rows, 1.0f * inpWidth / image.Cols);
int neww = (int)(image.Cols * ratio);
int newh = (int)(image.Rows * ratio);Mat dstimg = new Mat();
Cv2.Resize(image, dstimg, new OpenCvSharp.Size(neww, newh));Cv2.CopyMakeBorder(dstimg, dstimg, 0, inpHeight - newh, 0, inpWidth - neww, BorderTypes.Constant);
Mat圖像導入YoloV8模型重要核心代碼
C#環境下代碼如下所示:
// 定義 ONNX 模型的路徑
string onnxModelPath = "model/Barcode_detection.onnx";
// 定義輸入圖像的形狀
OpenCvSharp.Size inputShape = new OpenCvSharp.Size(640, 640);
// 從 ONNX 模型文件加載網絡
if(net==null)net = CvDnn.ReadNetFromOnnx(onnxModelPath);string[] modelClassify = { "Bar_code"};if (imagePaths.Count() == 0)
{LoadImagePaths("test_img");
}string currentImagePath = imagePaths[currentImageIndex];// 顯示到pictureBoxA
pictureBoxA.Image.Dispose(); // 釋放上一張圖片資源,避免內存泄漏
pictureBoxA.Image = new Bitmap(currentImagePath);
image_path = currentImagePath;if (pictureBoxA.Image == null)
{return;
}
currentImageIndex = (currentImageIndex + 1) % imagePaths.Count;OnNotifyShowRecieveMsg("檢測中,請稍等……");Application.DoEvents();image = new Mat(image_path);dt1 = DateTime.Now;
// 調用識別圖像的函數,并傳入圖像路徑、閾值、網絡、輸入形狀和分類類別列表
//result_image = Recognize(image, 0.35, net, inputShape, modelClassify);
result_image = RecognizeMat(image, 0.35, net, inputShape, modelClassify);
// 獲取計算結束時間
dt2 = DateTime.Now;
// 顯示輸出的圖像
pictureBoxA.Image = new Bitmap(result_image.ToMemoryStream());// 顯示推理耗時時間
OnNotifyShowRecieveMsg("推理耗時:" + (dt2 - dt1).TotalMilliseconds + "ms");
static Mat RecognizeMat(Mat imgInput, double threshold, Net net, OpenCvSharp.Size inputShape, string[] modelClassify)
{using (Mat img = imgInput){int inpHeight = inputShape.Height; // 輸入圖像的高度int inpWidth = inputShape.Width; // 輸入圖像的寬度// 對圖像進行預處理,調整尺寸Mat image = img;float ratio = Math.Min(1.0f * inpHeight / image.Rows, 1.0f * inpWidth / image.Cols);int neww = (int)(image.Cols * ratio);int newh = (int)(image.Rows * ratio);//// 將圖像調整為模型需要的大小//Mat dstimg = new Mat();//Cv2.Resize(image, dstimg, new OpenCvSharp.Size(neww, newh));//Cv2.CopyMakeBorder(dstimg, dstimg, 0, inpHeight - newh, 0, inpWidth - neww, BorderTypes.Constant);//Mat BN_image = CvDnn.BlobFromImage(dstimg); // 將調整后的圖像轉換為Blob格式//// 配置圖片輸入數據 // 將 blob 設置為網絡的輸入//net.SetInput(BN_image);//// 從圖像生成用于網絡輸入的 blob//Mat blob = CvDnn.BlobFromImage(img, 1 / 255.0, inputShape, new Scalar(0, 0, 0), false);////Mat blob = CvDnn.BlobFromImage(img, 1.0 / 255.0, inputShape, new Scalar(0, 0, 0), true, false);// 將 blob 設置為網絡的輸入//net.SetInput(blob);//// 從圖像生成用于網絡輸入的 blobMat img0 = img;Mat blob0 = CvDnn.BlobFromImage(img0, 1 / 255.0, new OpenCvSharp.Size(inputShape.Width, inputShape.Height), swapRB: true, crop: false);net.SetInput(blob0);// 執行前向傳播獲取輸出Mat output = net.Forward();// 此處可能需要根據 C# 中 OpenCV 的特性來處理轉置操作output = ReshapeAndTranspose(output);// 獲取圖像的行數(高度)int height = img.Height;// 獲取圖像的列數(寬度)int width = img.Width;// 計算寬度的縮放因子double xFactor = (double)width / inputShape.Width;// 計算高度的縮放因子double yFactor = (double)height / inputShape.Height;// 初始化分類類別、得分和檢測框的列表List<string> classifys = new List<string>();List<float> scores = new List<float>();List<Rect> boxes = new List<Rect>();List<Double> maxVales = new List<Double>();List<OpenCvSharp.Point> maxloces = new List<OpenCvSharp.Point>();// 遍歷輸出的行for (int i = 0; i < output.Rows; i++){// 獲取當前行的檢測框數據using (Mat box = output.Row(i)){// 在框數據的特定范圍中找到最小值、最大值及其位置OpenCvSharp.Point minloc, maxloc;double minVal, maxVal;// Mat classes_scores = box.ColRange(4, 5);//GetArray(i, 5, classes_scores);// double curmates0 = box.At<float>(0);double curmates1 = box.At<float>(4);int collength = box.Cols;int rowlength = box.Rows;Mat curmates = box.ColRange(4, box.Cols);//Cv2.MinMaxLoc(box.ColRange(4, box.Cols), out minVal, out maxVal, out minloc, out maxloc);Cv2.MinMaxLoc(box.ColRange(4, box.Cols), out minVal, out maxVal, out minloc, out maxloc);int classId = maxloc.Y;if (classId == 0){// 獲取對應類別的得分 float score = (float)maxVal;// 如果得分大于閾值if (score > threshold){// 將得分添加到得分列表scores.Add(score);// 將類別添加到類別列表classifys.Add(modelClassify[classId]);// 獲取框的原始坐標float x = box.At<float>(0, 0);float y = box.At<float>(0, 1);float w = box.At<float>(0, 2);float h = box.At<float>(0, 3);// 計算調整后的坐標int xInt = (int)((x - 0.5 * w) * xFactor);int yInt = (int)((y - 0.5 * h) * yFactor);int wInt = (int)(w * xFactor);int hInt = (int)(h * yFactor);// 將調整后的框坐標添加到框列表boxes.Add(new Rect(xInt, yInt, wInt, hInt));}}}}// 執行非極大值抑制操作int[] indices;CvDnn.NMSBoxes(boxes, scores, 0.25f, 0.45f, out indices);// 遍歷非極大值抑制操作后的索引foreach (int i in indices){// 獲取對應的類別、得分和框string classify = classifys[i];float score = scores[i];Rect box = boxes[i];// 獲取框的坐標和尺寸// 在圖像上繪制矩形框Cv2.Rectangle(img, box, new Scalar(0, 255, 0), 3);// 生成類別和得分的標簽文本string label = $"{classify}: {score:F2}";// 在圖像上添加標簽文本Cv2.PutText(img, label, new OpenCvSharp.Point(box.X, box.Y - 10), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 255, 0), 2);}// 將圖像復制輸出返回Mat result_image0 = img.Clone();return result_image0;// 將處理后的圖像保存為文件// Cv2.ImWrite("result.jpg", img);}
}
代碼實現演示(實現標簽條碼一維碼的檢測)
源碼下載鏈接
C# WinForms工業相機+本地圖像 通過YoloV8深度學習模型實現標簽條碼一維碼的檢測 源碼
工業相機通過YoloV8模型實現標簽條碼的檢測的行業應用
工業相機 + YOLOv8 實現「標簽-條碼檢測」的 6 大落地場景
(信息均來自 2024-2025 年公開源碼、博客與項目)
行業場景 | 核心痛點 | 工業相機配置 | YOLOv8 技術方案 & 實測效果 | 公開資源 |
---|---|---|---|---|
① 物流分揀中心 | 高速包裹條碼漏掃,人工補掃成本高 | 8 K 線陣相機 20 kHz + 條形 LED | YOLOv8n 量化 3 MB,RK3588 邊緣盒 15 ms/幀,分揀效率 3000 件/h,誤碼率 <0.1 % | |
② 制藥包裝線 | 藥盒標簽缺損、條碼重碼 | 12 MP 全局快門 120 fps + 穹頂光 | YOLOv8-s 檢測缺陷后接 PaddleOCR 讀碼,綜合識別率 99.5 % | |
③ 3C 電子 SMT | PCB 二維碼歪斜、墨漬 | 4 K 線陣相機 + 同軸光 | YOLOv8-tile 切片 + 透視矯正 + ZBar,綜合讀碼率 98 % | |
④ 食品飲料貼標機 | 標簽偏移、條碼錯貼 | 6 mm 定焦 60 fps + 環形光 | YOLOv8-seg 實例分割,標簽 IoU 0.92,PLC 觸發剔除 <50 ms | |
⑤ 倉儲入庫盤點 | 多規格條碼混雜、光照不均 | 龍門架 5 MP 相機陣列 | YOLOv8 + DeepSort 跟蹤,一次掃 20 箱,漏掃率 <0.1 % | |
⑥ 零售自助收銀 | 商品標簽遮擋、角度多變 | 廣角 4 K 相機 + 白光補光 | 網頁版 YOLOv8 實時檢測,置信度/IoU 可調,支持圖片/視頻/攝像頭三種輸入 |
關鍵技術細節
數據集:公開「Barcode-QR-2024」含 5 370 張工業標簽 + 條碼,已標注 YOLO 格式,開箱即訓。
模型優化:
? 輸入 640×640,YOLOv8n/s/x 按需選型;
? 剪枝 + INT8 量化,Jetson Orin Nano 上 15 ms/幀,CPU 占用 <25 %。
部署:
? 邊緣:RK3588 / Jetson Nano;
? 產線:x86 IPC + RTX 3060,TensorRT-FP16,8 路并行 <30 W。
合規:原始圖像 24 h 內自動覆蓋,僅留條碼內容及時間戳,滿足《數據安全法》。