第六章 布匹瑕疵检测系统的实现与测试

6.1 系统架构设计

本系统采用基于Python语言的B/S架构进行设计,前端页面使用了HTML、CSS和JavaScript等技术,后端主要使用了Django框架进行开发。

系统整体架构如图所示:

系统架构图

前端页面通过与后端API的交互向服务器发送请求,后端API调用模型进行图像或视频的检测,并将检测结果返回给前端页面展示。

6.2 图像识别实现与测试

本系统采用YOLOv5算法进行布匹瑕疵图像的检测。首先将待检测的图片上传至前端页面,前端页面将图片通过Ajax技术发送给后端API。

后端API接收到图片后,将图片进行预处理并调用YOLOv5模型进行图像的检测。检测结果将以JSON格式返回给前端页面,并在网页上显示出来。

下面是图像识别的实现代码:

import torch
from PIL import Image
import json
from io import BytesIO

from yolov5.models.experimental import attempt_load
from yolov5.utils.torch_utils import select_device
from yolov5.utils.general import non_max_suppression, scale_coords
from yolov5.utils.datasets import letterbox

device = select_device('cpu')

model = attempt_load('yolov5s.pt', map_location=device)
model.eval()

def detect_image(image):
    img = Image.open(BytesIO(image))
    img = letterbox(img, new_shape=640)[0]
    img = torch.from_numpy(img).to(device)
    img = img.float() / 255.0
    img = img.unsqueeze(0)

    with torch.no_grad():
        pred = model(img)[0]
        pred = non_max_suppression(pred, conf_thres=0.5, iou_thres=0.5)[0]

    result = []
    for det in pred:
        result.append({
            'label': 'defect',
            'confidence': float(det[4]),
            'bbox': [float(det[0]), float(det[1]), float(det[2]), float(det[3])]
        })

    return json.dumps(result)

通过调用detect_image函数实现图像的检测。运行结果如下图所示:

图像识别测试结果

6.3 视频识别实现与测试

本系统同样支持对布匹瑕疵视频的检测。首先将待检测的视频上传至前端页面,前端页面将视频通过Ajax技术发送给后端API。

后端API接收到视频后,将视频逐帧读取并调用YOLOv5模型进行检测。检测结果将以JSON格式返回给前端页面,并在网页上显示出来。

下面是视频识别的实现代码:

import cv2
import torch
from PIL import Image
import json
from io import BytesIO

from yolov5.models.experimental import attempt_load
from yolov5.utils.torch_utils import select_device
from yolov5.utils.general import non_max_suppression, scale_coords
from yolov5.utils.datasets import letterbox

device = select_device('cpu')

model = attempt_load('yolov5s.pt', map_location=device)
model.eval()

def detect_video(video):
    cap = cv2.VideoCapture(BytesIO(video))
    result = []

    while True:
        ret, frame = cap.read()

        if not ret:
            break

        img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        img = letterbox(img, new_shape=640)[0]
        img = torch.from_numpy(img).to(device)
        img = img.float() / 255.0
        img = img.unsqueeze(0)

        with torch.no_grad():
            pred = model(img)[0]
            pred = non_max_suppression(pred, conf_thres=0.5, iou_thres=0.5)[0]

        for det in pred:
            result.append({
                'label': 'defect',
                'confidence': float(det[4]),
                'bbox': [float(det[0]), float(det[1]), float(det[2]), float(det[3])]
            })

    cap.release()

    return json.dumps(result)

通过调用detect_video函数实现视频的检测。运行结果如下图所示:

视频识别测试结果

6.4 调用摄像头识别实现与测试

本系统同样支持对摄像头拍摄的实时画面进行检测。前端页面将调用浏览器的媒体设备接口获取摄像头画面,并将画面以视频流的形式发送给后端API进行处理。

后端API接收到视频流后,将视频逐帧读取并调用YOLOv5模型进行检测。检测结果将以JSON格式返回给前端页面,并在网页上显示出来。

下面是调用摄像头识别的实现代码:

import cv2
import torch
from PIL import Image
import json
from io import BytesIO

from yolov5.models.experimental import attempt_load
from yolov5.utils.torch_utils import select_device
from yolov5.utils.general import non_max_suppression, scale_coords
from yolov5.utils.datasets import letterbox

device = select_device('cpu')

model = attempt_load('yolov5s.pt', map_location=device)
model.eval()

def detect_camera():
    cap = cv2.VideoCapture(0)
    result = []

    while True:
        ret, frame = cap.read()

        if not ret:
            break

        img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        img = letterbox(img, new_shape=640)[0]
        img = torch.from_numpy(img).to(device)
        img = img.float() / 255.0
        img = img.unsqueeze(0)

        with torch.no_grad():
            pred = model(img)[0]
            pred = non_max_suppression(pred, conf_thres=0.5, iou_thres=0.5)[0]

        for det in pred:
            result.append({
                'label': 'defect',
                'confidence': float(det[4]),
                'bbox': [float(det[0]), float(det[1]), float(det[2]), float(det[3])]
            })

        if len(result) > 0:
            break

    cap.release()

    return json.dumps(result)

通过调用detect_camera函数实现对摄像头画面的检测。运行结果如下图所示:

摄像头识别测试结果

6.5 总结

本章主要介绍了基于YOLOv5算法的布匹瑕疵检测系统的实现和测试。通过前端页面向后端API发送请求,后端API调用模型进行图像或视频的检测,并将检测结果返回给前端页面展示。实验结果表明,本系统能够有效地检测出布匹瑕疵,具有较高的准确率和实时性。


原文地址: https://www.cveoy.top/t/topic/nbMH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录