yolov5 实现 detect.py 处理单张图片+Flask服务

本文介绍了如何使用Yolov5的detect.py脚本处理单张图片,并通过Flask创建服务对外提供图像预测功能。作者简化了detect.py的代码,使其能接收cv2.imread()的结果,并提供了评估接口。
摘要由CSDN通过智能技术生成

使用yoloV5 python接口(detect.py)处理图片并创建Flask服务。

具体的说有三种场景:

1 指定一张图片的位置/或使用cv2.imread() 的结果,进行model预测+画框+另存为新图片;

2 启动一个Flask 服务 对外提供1的功能

这里不得不说一下 yoloV5 提供了丰富的API接口,但是我用不太习惯,所以删减了一下detect.py的代码实现了功能1,同时保证功能1的输出和V3/V4接口有一致的形式。

实现

  • 功能1的实现代码如下:

这里不得不说一下,V5自带的接口不支持直接输入 cv2.imread() 的结果 ,于是我抽取了作者的处理逻辑,下文的predict()的入参支持 cv2.imread() 的输出  predict_path()支持输入文件路径

同时最后几行调用了 这里 实现的 evaluator 进行评估

import numpy as np

import cv2
import torch
from numpy import random
import evaluator

from models.experimental import attempt_load
from utils.general import check_img_size, non_max_suppression, scale_coords, \
    set_logging
from utils.torch_utils import select_device, time_synchronized


def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
    # Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
    shape = img.shape[:2]  # current shape [height, width]
    if isinstance(new_shape, int):
        new_shape = (new_shape, new_shape)

    # Scale ratio (new / old)
    r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
    if not scaleup:  # only scale down, do not scale up (for better test mAP)
        r = min(r, 1.0)

    # Compute padding
    ratio = r, r  # width, height ratios
    new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
    dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh padding
    if auto:  # minimum rectangle
        dw, dh = np.mod(dw, 32), np.mod(dh, 32)  # wh padding
    elif scaleFill:  # stretch
        dw, dh = 0.0, 0.0
        new_unpad = (new_shape[1], new_shape[0])
        ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]  # width, height ratios

    dw /= 2  # divide padding into 2 sides
    dh /= 2

    if shape[::-1] != new_unpad:  # resize
        img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
    top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
    left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
    img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border
    return img, ratio, (dw, dh)


weights = r'/home/jiantang/桌面/enn/workcode/v5/yolov5/runs/train/exp5/weights/best.pt'
opt_device = ''  # device = 'cpu' or '0' or '0,1,2,3'
imgsz = 640
opt_conf_thres = 0.25
opt_iou_thres = 0.5

# Initialize
set_logging()
device = select_device(opt_device)
half = device.type != 'cpu'  # half precision only supported on CUDA

# Lo
  • 17
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
抱歉,作为AI语言模型,我无法提供代码,但是我可以告诉您如何获取和使用YOLOv5detect.py代码。 YOLOv5detect.py代码可以在官方GitHub仓库中找到:https://github.com/ultralytics/yolov5/blob/master/detect.py 您可以使用以下步骤下载和使用此代码: 1. 安装依赖项:YOLOv5依赖于PyTorch,numpy和opencv-python。您可以使用以下命令安装它们: ``` pip install torch torchvision numpy opencv-python ``` 2. 克隆YOLOv5仓库: ``` git clone https://github.com/ultralytics/yolov5.git ``` 3. 进入yolov5目录: ``` cd yolov5 ``` 4. 下载预训练权重:YOLOv5官方提供了一些预训练权重,您可以从这里下载:https://github.com/ultralytics/yolov5/releases 5. 运行detect.py脚本:您可以使用以下命令运行detect.py脚本: ``` python detect.py --weights path/to/weights.pt --img 640 --conf 0.4 --source path/to/image/or/video ``` 其中,--weights指定预训练权重的路径,--img指定输入图像的大小,--conf指定置信度阈值,--source指定输入图像或视频的路径。 例如,以下命令将在输入图像中检测物体: ``` python detect.py --weights yolov5s.pt --img 640 --conf 0.4 --source /path/to/image.jpg ``` 以下命令将在输入视频中检测物体: ``` python detect.py --weights yolov5s.pt --img 640 --conf 0.4 --source /path/to/video.mp4 ``` 您还可以使用其他参数来定制检测模型的行为,例如--view-img来显示检测结果。您可以使用以下命令查看所有可用参数: ``` python detect.py --help ```
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark Kent 2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值