首页
/ Yolov5-Flask-VUE:基于Flask和VUE的YOLOv5目标检测模型部署

Yolov5-Flask-VUE:基于Flask和VUE的YOLOv5目标检测模型部署

2026-01-23 06:48:50作者:伍希望

项目简介

本项目提供了一个基于Flask开发后端、VUE开发前端的框架,用于在WEB端部署YOLOv5目标检测模型。通过本项目,您可以轻松地将YOLOv5模型集成到您的WEB应用中,实现目标检测功能。

项目特点

  1. 前后端分离架构:采用Flask作为后端框架,VUE作为前端框架,实现了前后端分离,便于开发和维护。
  2. YOLOv5模型支持:支持YOLOv5模型的部署和预测,可以轻松集成到您的WEB应用中。
  3. 自定义模型训练:您可以根据自己的需求训练YOLOv5模型,并将其部署到本项目中。

项目结构

  • 后端:使用Flask框架,提供YOLOv5模型的预测接口。
  • 前端:使用VUE框架,提供用户界面,用户可以通过界面上传图片或视频,进行目标检测。

使用说明

1. 环境配置

  • 安装Python环境,建议使用Python 3.7及以上版本。
  • 安装Flask和VUE的相关依赖。

2. 模型训练

  • 如果您需要训练自己的YOLOv5模型,可以参考相关教程进行训练。
  • 本项目默认使用官方训练好的yolov5m.pt模型进行演示。

3. 模型预测

  • 启动Flask后端服务,提供YOLOv5模型的预测接口。
  • 在前端界面中上传图片或视频,调用后端接口进行目标检测。

4. 最终效果

  • 您可以在前端界面中查看目标检测的结果,包括检测到的目标类别、位置等信息。

代码示例

以下是YOLOv5模型预测接口的部分代码示例:

import torch
import numpy as np
from models.experimental import attempt_load
from utils.general import non_max_suppression, scale_coords, letterbox
from utils.torch_utils import select_device
import cv2
from random import randint

class Detector(object):
    def __init__(self):
        # 初始化模型和设备
        self.device = select_device('')
        self.model = attempt_load('yolov5m.pt', map_location=self.device)
        self.model.eval()

    def detect(self, img):
        # 图像预处理
        img = letterbox(img, new_shape=640)[0]
        img = img[:, :, ::-1].transpose(2, 0, 1)  # BGR to RGB
        img = np.ascontiguousarray(img)
        img = torch.from_numpy(img).to(self.device)
        img = img.float() / 255.0  # 归一化
        if img.ndimension() == 3:
            img = img.unsqueeze(0)

        # 模型预测
        with torch.no_grad():
            pred = self.model(img, augment=False)[0]
            pred = non_max_suppression(pred, conf_thres=0.4, iou_thres=0.5)

        # 处理预测结果
        for det in pred:
            if det is not None and len(det):
                det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img.shape).round()
                for *xyxy, conf, cls in det:
                    label = f'{self.model.names[int(cls)]} {conf:.2f}'
                    plot_one_box(xyxy, img, label=label, color=colors[int(cls)], line_thickness=3)

        return img

贡献

欢迎大家为本项目贡献代码或提出改进建议。如果您有任何问题或建议,请在GitHub上提交Issue。

许可证

本项目采用MIT许可证,详情请参阅LICENSE文件。

登录后查看全文
热门项目推荐
相关项目推荐