【机器学习部署】Machine Learning Operations(MLOps) --1(Yolov3物体识别)

3 篇文章 0 订阅
2 篇文章 0 订阅

本系列以吴恩达老师的MLops的课程为基础,并且结合了网上找到的其他材料和自己实践中遇到的坑。

Step1 环境配置

假设已经安装好了anaconda(具体如何安装就不写了,网上资料很多)。

具体细节流程可以看我以前写的 tensorflow2.1 安装 

需要的包及版本为:

cvlib==0.2.6
opencv-python-headless==4.5.3.56
Pillow==8.4.0.
tensorflow==2.7.0
uvicorn==0.16.0
python-multipart==0.0.5
fastapi==0.70.1
nest-asyncio==1.5.4
jupyterlab==3.2.5

可以把上述包的需求写入一个txt 文件 命名requirements.txt

再用 pip install -r requirements.txt 注意路径里面一定要包含这个requirements.txt文件

安装配置好了之后开始课程1中的使用 yolov3-tiny 识别物体。 

Step2 利用yolov3-tiny 识别物体

代码如下:

import os

dir_name = "images_with_boxes"
if not os.path.exists(dir_name):
    os.mkdir(dir_name)

import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox


def detect_and_draw_box(filename, model="yolov3-tiny", confidence=0.5):
    """Detects common objects on an image and creates a new image with bounding boxes.

    Args:
        filename (str): Filename of the image.
        model (str): Either "yolov3" or "yolov3-tiny". Defaults to "yolov3-tiny".
        confidence (float, optional): Desired confidence level. Defaults to 0.5.
    """

    # Images are stored under the images/ directory
    img_filepath = f'images/{filename}'

    # Read the image into a numpy array
    img = cv2.imread(img_filepath)

    # Perform the object detection
    bbox, label, conf = cv.detect_common_objects(img, confidence=confidence, model=model)

    # Print current image's filename
    print(f"========================\nImage processed: {filename}\n'course1/week1-ungraded-lab/yolov3.txt'")

    # Print detected objects with confidence level
    for l, c in zip(label, conf):
        print(f"Detected object: {l} with confidence level of {c}\n")

    # Create a new image that includes the bounding boxes
    output_image = draw_bbox(img, bbox, label, conf)

    # Save the image in the directory images_with_boxes
    cv2.imwrite(f'images_with_boxes/{filename}', output_image)

    # Display the image with bounding boxes
    display(Image(f'images_with_boxes/{filename}'))

注意:cv.detect_common_objects 这里面有可能需要改一下代码,由于网络的问题可能会出现下载失败的情况

Downloading yolov3-tiny.cfg from https://github.com/pjreddie/darknet/raw/master/cfg/yolov3-tiny.cfg
Could not establish connection. Download failed

所以提前下载了三个文件 

再把 cv.detect_common_objects (按住control 点击这个函数就可以进入源码)源码里面的几个需要访问文件位置(如下图)改成上面这三个文件的位置,再运行即可。 

 

改好后,再运行

在images_with_boxes文件夹里面可以发现实现了识别。

 detect_and_draw_box("fruits.jpg", confidence=0.2) 

可以用这个函数控制置信度得到结果 :

当confidence=0.2时,结果

 

 当confidence=0.8时,结果

 

没有一个成功标记出来。 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值