当前位置: 代码网 > it编程>前端脚本>Python > yolov5使用flask部署至前端实现照片\视频识别功能

yolov5使用flask部署至前端实现照片\视频识别功能

2024年09月28日 Python 我要评论
前言初学yolo flask时,需要此功能,csdn、github、b站找到许多代码,效果并不满意。近期,再度尝试,实现简单功能。实现功能:上传图片并识别,可以点击图片放大查看上传视频并识别识别后的文

前言

初学yolo flask时,需要此功能,csdn、github、b站找到许多代码,效果并不满意。

近期,再度尝试,实现简单功能。

实现功能:

上传图片并识别,可以点击图片放大查看

上传视频并识别

识别后的文件下载功能

效果图如下

文件结构如下:

project/
  static/

  空

  templates/
    index.html
    
  app.py

相关代码:

app.py

import cv2
import numpy as np
import torch
from flask import flask, request, jsonify, render_template
import base64
import os
from datetime import datetime

app = flask(__name__)

# 全局变量:模型
model = none

# 提前加载模型
def load_model():
    global model
    model = torch.hub.load('', 'custom', path='yolov5s.pt', source='local')

# 路由处理图片检测请求
@app.route('/predict_image', methods=['post'])
def predict_image():
    global model

    # 获取图像文件
    file = request.files['image']
    # 读取图像数据并转换为rgb格式
    image_data = file.read()
    nparr = np.frombuffer(image_data, np.uint8)
    image = cv2.imdecode(nparr, cv2.imread_unchanged)

    results = model(image)

    image = results.render()[0]

    # 将图像转换为 base64 编码的字符串
    _, buffer = cv2.imencode('.png', image)
    image_str = base64.b64encode(buffer).decode('utf-8')

    # 获取当前时间,并将其格式化为字符串
    current_time = datetime.now().strftime('%y%m%d%h%m%s')
    # 构建保存路径
    save_dir = 'static'
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    filename, extension = os.path.splitext(file.filename)  # 获取上传文件的文件名和扩展名
    save_filename = f'{filename}_{current_time}{extension}'
    save_path = os.path.join(save_dir, save_filename)
    cv2.imwrite(save_path, image)

    return jsonify({'image': image_str})

# 函数用于在视频帧上绘制检测结果
def detect_objects(frame, model):
    results = model(frame)
    detections = results.xyxy[0].cpu().numpy()  # 获取检测结果

    # 在帧上绘制检测结果
    for det in detections:
        # 获取边界框信息
        x1, y1, x2, y2, conf, class_id = det[:6]

        # 在帧上绘制边界框
        cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)

        # 在帧上绘制类别和置信度
        label = f'{model.names[int(class_id)]} {conf:.2f}'
        cv2.puttext(frame, label, (int(x1), int(y1) - 10), cv2.font_hershey_simplex, 0.5, (0, 255, 0), 2)
    return frame

# 路由处理视频检测请求

@app.route("/predict_video", methods=["post"])
def predict_video():
    global model

    # 从请求中获取视频文件
    video_file = request.files["video"]
    # 保存视频到临时文件
    temp_video_path = "temp_video.mp4"
    video_file.save(temp_video_path)

    # 逐帧读取视频
    video = cv2.videocapture(temp_video_path)

    # 获取视频的帧率和尺寸
    fps = video.get(cv2.cap_prop_fps)
    width = int(video.get(cv2.cap_prop_frame_width))
    height = int(video.get(cv2.cap_prop_frame_height))

    # 视频写入对象
    output_video_filename = f"output_video_{datetime.now().strftime('%y%m%d%h%m%s')}.mp4"
    output_video_path = os.path.join("static", output_video_filename)
    fourcc = cv2.videowriter_fourcc(*"avc1")
    out_video = cv2.videowriter(output_video_path, fourcc, fps, (width, height))

    # 逐帧处理视频并进行目标检测
    while true:
        ret, frame = video.read()
        if not ret:
            break

        # 进行目标检测
        detection_result = detect_objects(frame, model)

        # 将处理后的帧写入输出视频
        out_video.write(detection_result)

    # 释放视频对象
    video.release()
    out_video.release()

    return jsonify({"output_video_path": output_video_filename})

@app.route('/')
def index():
    return render_template('index.html')

# 初始加载模型
load_model()

if __name__ == '__main__':
    app.run(debug=true)

index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>object detection</title>
    <style>
        body {
            font-family: arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f3f3f3;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            flex-direction: column;
        }

        #content {
            text-align: center;
            max-width: 820px;
            margin-top: 20px;
        }

        h1 {
            color: #333;
        }

        h2 {
            color: #666;
        }

        input[type="file"] {
            margin-bottom: 10px;
        }

        .media-container {
            display: flex;
            max-width: 100%;
            margin-bottom: 20px;
        }

        .media-container:first-child {
            margin-right: 20px; /* 在第一个容器的右侧添加间隔 */
        }

        .media-container img,
        .media-container video {
            max-width: 100%;
            height: auto;
        }

        .original {
            width: 400px;
            overflow: hidden;
        }

        .processed {
            flex: 2; /* 右边容器占据剩余空间 */
        }

        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-bottom: 10px;
        }

        /* 新增样式:模态框 */
        .modal {
            display: none; /* 默认隐藏 */
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.9); /* 半透明黑色背景 */
        }

        .modal-content {
            margin: auto;
            display: block;
            width: 80%;
            max-width: 800px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            text-align: center; /* 居中显示图片 */
        }

        .close {
            color: #ccc;
            font-size: 36px;
            font-weight: bold;
            cursor: pointer;
            position: absolute;
            top: 10px;
            right: 10px;
        }

        .close:hover,
        .close:focus {
            color: #fff;
            text-decoration: none;
        }
        #downloadbutton {
           padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-bottom: 10px;

        }

        /* 新增样式:响应式图片 */
        .modal-content img,
        .modal-content video {
            max-width: 100%;
            height: auto;
        }

    </style>
</head>
<body>

    <!-- 新增模态框 -->
    <div id="mymodal" class="modal" onclick="closemodal()">
        <div class="modal-content" id="modalcontent" onclick="stoppropagation(event)">
            <!-- 放大后的图片或视频将在这里显示 -->
            <span class="close" onclick="closemodal()">&times;</span>
        </div>
    </div>

    <div id="content">
        <h1>照片/视频检测</h1>

        <!-- 上传图片 -->
        <h2>上传图片</h2>
        <input type="file" id="imagefile" accept="image/*" onchange="displayselectedimage()">
        <button onclick="uploadimage()">上传</button>
        <button id="downloadimagebutton"  onclick="downloadprocessedimage()">下载</button>
        <br>
        <div class="media-container">
            <div class="original media-container" onclick="enlargeimage()">
                <img id="uploadedimage" src="#" alt="uploaded image" style="display:none;">
                <button id="zoominbutton" style="display:none;">zoom in</button>
            </div>
            <div class="processed media-container" onclick="enlargeimage2()">
                <img id="processedimage" src="#" alt="processed image" style="display:none;">

            </div>
        </div>
        <br>

        <!-- 上传视频 -->
        <h2>上传视频</h2>
        <input type="file" id="videofile" accept="video/mp4,video/x-m4v,video/*" onchange="displayselectedvideo()">
        <button onclick="uploadvideo()">上传</button>
        <button id="downloadbutton" onclick="downloadprocessedvideo()">下载</button>
        <br>
        <div class="media-container">
            <div class="original media-container" >
                <video id="uploadedvideo" src="#" controls style="display:none;"></video>
            </div>
            <div class="processed media-container">
                <video id="processedvideo" controls style="display:none;"></video>

            </div>
        </div>
        <br>

    </div>

    <script>
         // 显示选择的权重文件

        // 显示选择的图片并添加点击放大功能
        function displayselectedimage() {
            var fileinput = document.getelementbyid('imagefile');
            var file = fileinput.files[0];
            var imageelement = document.getelementbyid('uploadedimage');
            imageelement.src = url.createobjecturl(file);
            imageelement.style.display = 'inline';
            document.getelementbyid('zoominbutton').style.display = 'inline';
        }

        // 显示模态框并放大图片
        function enlargeimage() {
            var modal = document.getelementbyid('mymodal');
            var modalimg = document.getelementbyid('modalcontent');
            var img = document.getelementbyid('uploadedimage');
            modal.style.display = 'block';
            modalimg.innerhtml = '<img src="' + img.src + '">';
        }
        // 显示模态框并放大图片
        function enlargeimage2() {
            var modal = document.getelementbyid('mymodal');
            var modalimg = document.getelementbyid('modalcontent');
            var img = document.getelementbyid('processedimage');
            modal.style.display = 'block';
            modalimg.innerhtml = '<img src="' + img.src + '">';
        }


        // 显示选择的视频并添加点击放大功能
        function displayselectedvideo() {
            var fileinput = document.getelementbyid('videofile');
            var file = fileinput.files[0];
            var videoelement = document.getelementbyid('uploadedvideo');
            videoelement.src = url.createobjecturl(file);
            videoelement.style.display = 'block';
        }


        // 上传图片并向后端发送请求
        function uploadimage() {
            var fileinput = document.getelementbyid('imagefile');
            var file = fileinput.files[0];
            var formdata = new formdata();
            formdata.append('image', file);

            fetch('/predict_image', {
                method: 'post',
                body: formdata
            })
            .then(response => response.json())
            .then(data => {
                var imageelement = document.getelementbyid('processedimage');
                imageelement.src = 'data:image/png;base64,' + data.image;
                imageelement.style.display = 'inline';
                document.getelementbyid('downloadimagebutton').style.display = 'inline';
            })
            .catch(error => console.error('error:', error));
        }

        // 下载处理后的图片
        function downloadprocessedimage() {
            var imageelement = document.getelementbyid('processedimage');
            var url = imageelement.src;
            var a = document.createelement('a');
            a.href = url;
            a.download = 'processed_image.png';
            document.body.appendchild(a);
            a.click();
            document.body.removechild(a);
        }

        // 上传视频并向后端发送请求
        function uploadvideo() {
            var fileinput = document.getelementbyid('videofile');
            var file = fileinput.files[0];
            var formdata = new formdata();
            formdata.append('video', file);

            fetch('/predict_video', {
                method: 'post',
                body: formdata
            })
            .then(response => response.json())
            .then(data => {
                var videoelement = document.getelementbyid('processedvideo');
                // 修改路径为正确的 flask url_for 生成的路径
                videoelement.src = '{{ url_for("static", filename="") }}' + data.output_video_path;
                videoelement.style.display = 'block';
                var downloadbutton = document.getelementbyid('downloadbutton');
                downloadbutton.style.display = 'block';
            })
            .catch(error => console.error('error:', error));
        }

        // 下载处理后的视频
        function downloadprocessedvideo() {
            var videoelement = document.getelementbyid('processedvideo');
            var url = videoelement.src;
            var a = document.createelement('a');
            a.href = url;
            a.download = 'processed_video.mp4';
            document.body.appendchild(a);
            a.click();
            document.body.removechild(a);
        }

        // 关闭模态框
        function closemodal() {
            var modal = document.getelementbyid('mymodal');
            modal.style.display = 'none';
        }
    </script>
</body>
</html>

使用说明:

index.html放入templates文件夹中

运行app.py

注:此处加载模型路径更改为自己的

model = torch.hub.load('', 'custom', path='yolov5s.pt', source='local')

如果模型读取不到,显示

filenotfounderror: [errno 2] no such file or directory: 'hubconf.py'

去yolov5官网,下载yolov5-master到项目文件夹

并将yolov5s.pt文件复制到yolov5-master文件夹中,修改model路径

model = torch.hub.load('yolov5-master', 'custom', path='yolov5s.pt', source='local')

总结

到此这篇关于yolov5使用flask部署至前端实现照片\视频识别功能的文章就介绍到这了,更多相关yolov5 flask部署前端照片视频识别内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com