目标检测中遇到的问题和 docker导出日志

一 docker容器导出日志

导出日志在Linux服务器的本地目录下,可以直接下载

docker logs 容器名称 > log.txt

二 Flask使用main执行

1 改dockerfile 文件内容

#CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
CMD [ "python", "./app.py" ]

2 改 app.py 中的内容

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, World!"

if __name__ == '__main__':
    app.run(host='0.0.0.0')

三 针对加载模型时间过长

将模型在主程序 main 中加载,进行flask交互时,将全局变量直接导入使用模块中,比如提前加载YOLOv5模型。

if __name__ == "__main__":
    os.makedirs("./config/", exist_ok=True)
    config = Config('config/config.json')

    print("加载YOLO模型:")
    # 从本地目录加载自定义的YOLOv5模型
    yolo_model = torch.hub.load('yolov5', 'custom', path='yolov5/best.pt', source='local')
    # 设置置信度阈值
    yolo_model.conf = config.floating_threshold
    app.run(host='0.0.0.0')

四 提取图片中的识别区,将无关部分去除

def adjust_img_size(img, width_ratio=1, height_ratio=0.8, padding_color=(255, 255, 255)):
    """
    获取图片中间长宽各1/2的中间区域,外部全部填充为指定颜色。

    Parameters:
        img (numpy.ndarray or PIL.Image.Image): 输入的图片,可以是numpy数组或PIL图像对象。
        padding_color (tuple): 填充的颜色,格式为 (R, G, B)。
        width_ratio:ratio
        height_ratio:ratio

    Returns:
        numpy.ndarray: 调整后的图片数组。
    """
    # 将输入图片转换为numpy数组
    if isinstance(img, Image.Image):
        img = np.array(img)

    # 获取图片尺寸
    height, width, channels = img.shape

    # 创建填充区域
    padding = np.full((height, width, channels), padding_color, dtype=np.uint8)

    # 计算截取的高度和宽度
    crop_height = int(height * height_ratio)
    crop_width = int(width * width_ratio)

    height_1 = int((height - crop_height)*0.5)
    width_1 = int((width - crop_width) * 0.5)

    # 截取图像
    cropped_image = img[height_1:crop_height + height_1, width_1:crop_width + width_1]

    # 将原始图片放入填充区域的中间
    padding[height_1:crop_height + height_1, width_1:crop_width + width_1] = cropped_image

    return padding

返回图片中固定比例的点

def get_point(img, height_ratio, width_ratio):
    """返回图片中的点目标点,用于在图上做标注"""
    # 获取图片尺寸
    height, width, channels = img.shape
    # print('查看形状:', img.shape)

    # 计算截取的高度和宽度
    crop_height = int(height * height_ratio)
    crop_width = int(width * width_ratio)

    height_1 = int((height - crop_height))
    width_1 = int((width - crop_width) * 0.5)

    width_2 = width - width_1
    height_2 = height - int((height - crop_height) * 0.5)
    # print('查看返回值:', width_1, height_1, width_2, height_2)

    return width_1, height_1, width_2, height_2

猜你喜欢

转载自blog.csdn.net/March_A/article/details/132214971