python算法部署(通信篇)

1.docker+flask方式

# YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
"""
Run a Flask REST API exposing one or more YOLOv5s models
"""


import argparse
import io
import json


import torch
from flask import Flask, jsonify, request,Response
from PIL import Image
import numpy as np
import cv2
import requests 
from collections import OrderedDict
import os

app = Flask(__name__)
models = {}

DETECTION_URL = '/v1/object-detection/<model>'

@app.route(DETECTION_URL, methods=['POST'])
# @app.route(DETECTION_URL, methods=['GET'])

def predict(model):

    if request.method != 'POST':
    # if request.method != 'GET':
        
        return jsonify({'error':'Invalid request method'})
    print(request.data)
    
    if request.data:
        try:
            # img = np.frombuffer(request.data,dtype=np.uint8)#转成np矩阵
            img = cv2.imdecode(np.frombuffer(request.data,dtype=np.uint8),cv2.IMREAD_COLOR) 
            if img is None:
                raise Exception("Invalid image data")
        except Exception as e:
            # print(f"An error occurred: {str(e)}")
            
            return jsonify({'error':'Invalid image data'})
        if model in models:
            # results = models[model](img, size=640)  # reduce size=320 for faster inference
            results = models[model](img)  # reduce size=320 for faster inference
            # return results.pandas().xyxy[0].to_json(orient='records')
            detection_results = []
            for detection in results.pandas().xyxy[0].to_dict(orient='records'):
                detection_result = {
                    "name": detection["name"],
                    "score": float(detection["confidence"]),
                    "bbox": [
                        float(detection["xmin"]),
                        float(detection["ymin"]),
                        detection["xmax"] - detection["xmin"],
                        detection["ymax"] - detection["ymin"]
                    ]
                }
                detection_results.append(detection_result)
            response_data = OrderedDict()
            response_data["imgUrl"] = img
            response_data["imgSize"] = [img.shape[1], img.shape[0]]
            response_data["code"] = 0
            response_data["msg"] = ""   
            response_data["objects"] = detection_results
            
            return Response(json.dumps(response_data), mimetype='application/json')


    if request.files.get('image'):
        # Method 1
        # with request.files["image"] as f:
        #     im = Image.open(io.BytesIO(f.read()))

        # Method 2
        im_file = request.files['image']
        im_filename = os.path.join(os.path.dirname(__file__),im_file.filename)
        im_bytes = im_file.read()
        try:
            im = Image.open(io.BytesIO(im_bytes))

        except Exception as e:
            response_data = OrderedDict()
            response_data["imgUrl"] = ""
            response_data["imgSize"] = [0,0]
            response_data["code"] = 1
            response_data["msg"] = ""  
            response_data["objects"] = []
            response_data["error"] = 'Invalid image file'

            return Response(json.dumps(response_data), mimetype='application/json')
        
        # if model in models:
        #     results = models[model](im, size=640)  # reduce size=320 for faster inference
        #     # return results.pandas().xyxy[0].to_json(orient='records')
        #     detection_results = []
        #     for detection in results.pandas().xyxy[0].to_dict(orient='records'):
        #         detection_result = {
        #             "name": detection["name"],
        #             "score": float(detection["confidence"]),
        #             "bbox": [
        #                 float(detection["xmin"]),
        #                 float(detection["ymin"]),
        #                 detection["xmax"] - detection["xmin"],
        #                 detection["ymax"] - detection["ymin"]
        #             ]
        #         }
        #         detection_results.append(detection_result)

        #     response_data = OrderedDict()
        #     response_data["imagePath"] = im_filename
        #     response_data["imageSize"] = [im.width, im.height]
        #     response_data["code"] = 0
        #     response_data["msg"] = ""   
        #     response_data["objects"] = detection_results
            
        #     return Response(json.dumps(response_data), mimetype='application/json')


    if request.form.get('image_url'):
        image_url = request.form.get('image_url')
        try:
            response = requests.get(image_url)
            im = Image.open(io.BytesIO(response.content))
        except Exception as e:
            return jsonify({'error':'Failed to download or open image from URL'})
        if model in models:
            results = models[model](im, size=640)  # reduce size=320 for faster inference
            # return results.pandas().xyxy[0].to_json(orient='records')
            detection_results = []
            for detection in results.pandas().xyxy[0].to_dict(orient='records'):
                detection_result = OrderedDict()
                detection_result = {
                    "name": detection["name"],
                    "score": float(detection["confidence"]),
                    "bbox": [
                        float(detection["xmin"]),
                        float(detection["ymin"]),
                        detection["xmax"] - detection["xmin"],
                        detection["ymax"] - detection["ymin"]
                    ]
                }
                detection_results.append(detection_result)

            response_data = OrderedDict()
            response_data["imgUrl"] = image_url
            response_data["imgSize"] = [im.width, im.height]
            response_data["code"] = 0  
            response_data["msg"] = "" 
            response_data["objects"] = detection_results
            return Response(json.dumps(response_data), mimetype='application/json')
        
    response_data = OrderedDict()
    response_data["imgUrl"] = ""
    response_data["imgSize"] = [0,0]
    response_data["code"] = 1
    response_data["msg"] = ""  
    response_data["objects"] = []
    response_data["error"] = 'No image file or URL or model found'

    return Response(json.dumps(response_data), mimetype='application/json')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Flask API exposing YOLOv5 model')
    parser.add_argument('--port', default=8008, type=int, help='port number')
    parser.add_argument('--model', nargs='+', default=['yolov5s'], help='model(s) to run, i.e. --model yolov5n yolov5s')
    opt = parser.parse_args()

    for m in opt.model:
        # models[m] = torch.hub.load('ultralytics/yolov5', m, force_reload=True, skip_validation=True)
        # models[m] = torch.hub.load('./', m, source="local")WWWWWW
        # models[m] = torch.hub.load('./', 'custom', path='/data1/hzb/yolov5/animals-80.pt', source="local")
        models[m] = torch.hub.load('./', 'custom', path='/data1/hzb/yolov5/zs_animals.pt', source="local")
    app.run(host='0.0.0.0', port=opt.port)  # debug=True causes Restarting with stat

测试命令:

curl -X POST -F"image_url=https://img0.baidu.com/it/u=3602775810,1537495254&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" http://192.168.100.42:8008/v1/object-detection/zs_animals

2.lcm方式

详细教程可以参考官网:(写的很清楚),各种语言

http://lcm-proj.github.io/lcm/

lcm只能是组播地址
默认使用的通信方式:

LCM (std::string lcm_url="")
1
参数的含义是一个url,一般使用默认,这个url写了ip地址和端口号,我们查看一下lcm默认的地址:

"udpm://239.255.76.67:7667?ttl=1"

如果要使用多机通信
参考下面链接

https://blog.csdn.net/weixin_45467056/article/details/123569027

在 Windows 上还需要关闭防火墙,成功 ping 通后,在 Ubuntu 环境中首先需要运行ifconfig | grep -B 1 | grep “flags”| cut -d ‘:’ -f1,查看该 IP 对应的网络设备,其中 需要用实际获取到的 IP 地址进行替换。

假设我们对应的网络设备是 lo,下面用 代替,使用时需要进行替换。运行下面两条命令来显式使能 UDP 多播和添加路由表。
在这里插入图片描述
Ubuntu 配置结束,可以正常进行消息的收发。

2.tornado方式

附录:
分布式文件系统

from hdfs import InsecureClient

# 创建HDFS客户端,连接到集群的namenode
client = InsecureClient("http://192.168.100.11:9870", user="root")

# 从本地文件系统上传文件到HDFS
client.download("/tmp/000008.bin", "C:\\Users\\admin\\Desktop\\000008.bin")1条回复92510:42from hdfs import InsecureClient
--------------------------------------------------------
# 创建HDFS客户端,连接到集群的namenode
client = InsecureClient("http://192.168.100.11:9870", user="root")

# 从本地文件系统上传文件到HDFS
client.upload("/tmp/test.txt", "C:\\Users\\admin\\Desktop\\test.txt")

使用的时候会报如下错误:
在这里插入图片描述
解决方案:你配一下hosts;都配置,这是个集群
10:03192.168.100.11 nn1
192.168.100.12 nn2
192.168.100.13 dn1
192.168.100.14 dn2
192.168.100.15 dn3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值