在Jupyter Notebook中实现图像检测服务的调用

1、环境说明

CentOS7(部署在VMware Workstation Pro中的虚拟机)
需要安装有docker


2、前提条件:镜像准备

部署了图形检测服务的镜像:codait/max-object-detector
Github地址:https://github.com/IBM/MAX-Object-Detector

该模型识别出COCO数据集中80个不同的高级对象类的图像中存在的对象。

    
部署了Jupyter服务的镜像:onnx/onnx-ecosystem
Github地址: https://github.com/onnx/onnx-docker/tree/master/onnx-ecosystem

由于CentOS中未安装jupyter,因此直接使用一个包含jupyter的镜像,在使用该镜像前使用了其它镜像,但是可能存在包安装不成功的问题。

3、启动镜像

docker run -it -p 5000:5000 max-object-detector

该容器提供的图像检测服务的地址为http://宿主机IP:5000/model/predict

docker run -p 8888:8888 onnx/onnx-ecosystem

4、在Notebook中调用图像检测服务

Github项目https://github.com/IBM/MAX-Object-Detector中提供的demo.ipynb中给出了示例代码,在使用时,需要注意如下两点:
1> 需要在Jupyter中上传要检查的图像,和要运行的notebook放在一个目录下即可
2> 修改demo.ipynb中的代码

  • 修改call_model中的本地服务的地址
def call_model(input_img, local_port=None):
    """
    Takes in input image file path, posts the image to the model and returns face bboxes and emotion predictions
    If local port is not specified, uses the long running instance.
    If local port is specificed, uses the local instance.
    """
    if local_port:
        url = 'http://192.168.64.242:'+ str(local_port)+'/model/predict'
    else:
        url = 'http://max-object-detector.max.us-south.containers.appdomain.cloud/model/predict'

    files = {'image': ('image.jpg', open(input_img, 'rb'), 'images/jpeg') }
    r = requests.post(url, files=files).json()
    
    return r
  • 修改调用上述函数的代码:
model_response = call_model(img_path, 5000)

其它代码不需要修改。

详细截图如下所示:

发布了227 篇原创文章 · 获赞 94 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/wiborgite/article/details/95649750