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

本系列以吴恩达老师的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

所以提前下载了三个文件 

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

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

【机器学习部署】Machine Learning Operations(MLOps) --1(Yolov3物体识别)_第2张图片

【机器学习部署】Machine Learning Operations(MLOps) --1(Yolov3物体识别)_第3张图片 

改好后,再运行

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

【机器学习部署】Machine Learning Operations(MLOps) --1(Yolov3物体识别)_第4张图片

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

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

当confidence=0.2时,结果

【机器学习部署】Machine Learning Operations(MLOps) --1(Yolov3物体识别)_第5张图片

 

 当confidence=0.8时,结果

 

【机器学习部署】Machine Learning Operations(MLOps) --1(Yolov3物体识别)_第6张图片

没有一个成功标记出来。 

你可能感兴趣的:(tensorflow2.x,MLOps,yolov3,机器学习,机器学习部署,MLOps)