0

0

【PaddleHub模型贡献】一行代码实现水表的数字表盘分割

P粉084495128

P粉084495128

发布时间:2025-07-21 10:06:44

|

1014人浏览过

|

来源于php中文网

原创

本文介绍将水表数字表盘分割模型贡献到PaddleHub的方法。先安装必要库,复现模型:准备数据集,配置GPU,定义图像预处理流程和数据集,用DeepLabv3p训练模型并导出。接着转换模型为PaddleHub模型,补充代码实现旋转剪裁等功能,最后测试安装与调用,实现水表数字表盘分割。

☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

【paddlehub模型贡献】一行代码实现水表的数字表盘分割 - php中文网

【PaddleHub模型贡献】一行代码实现水表的数字表盘分割

一、安装必要的库

In [3]
!pip install paddlex -i https://mirror.baidu.com/pypi/simple
!pip install --upgrade paddlepaddle-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install --upgrade paddlehub==2.0.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
   

二、模型训练

项目作者使用PaddleX做的语义分割,因为作者没有直接公开训练好的模型,所以这里我们先按照作者的思路复现模型。

1.准备表盘数据集

In [ ]
!unzip -oq /home/aistudio/data/data73852/water.zip
   

2. 模型训练

2.1 配置GPU

In [ ]
# 设置使用0号GPU卡(如无GPU,执行此代码后仍然会使用CPU训练模型)import matplotlib
matplotlib.use('Agg') 
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'import paddlex as pdx
   

2.2 定义图像预处理流程transforms

定义数据处理流程,其中训练和测试需分别定义,训练过程包括了部分测试过程中不需要的数据增强操作,如在本示例中,训练过程使用了RandomHorizontalFlip和RandomPaddingCrop两种数据增强方式,更多图像预处理流程transforms的使用可参见paddlex.seg.transforms。

In [ ]
from paddlex.seg import transforms
train_transforms = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.Resize(target_size=512),
    transforms.RandomPaddingCrop(crop_size=500),
    transforms.Normalize()
])
eval_transforms = transforms.Compose([
    transforms.Resize(512),
    transforms.Normalize()
])
   

2.3 定义数据集Dataset

实例分割使用SegDataset格式的数据集,因此采用pdx.datasets.SegDataset来加载数据集,该接口的介绍可参见文档pdx.datasets.SegDataset。

In [ ]
train_dataset = pdx.datasets.SegDataset(
    data_dir='water',
    file_list='water/train.txt',
    label_list='water/class_names.txt',
    transforms=train_transforms,
    shuffle=True)
eval_dataset = pdx.datasets.SegDataset(
    data_dir='water',
    file_list='water/val.txt',
    label_list='water/class_names.txt',
    transforms=eval_transforms)
       
2021-03-11 14:54:48 [INFO]	150 samples in file water/train.txt
2021-03-11 14:54:48 [INFO]	11 samples in file water/val.txt
       

2.4 模型开始训练

使用本数据集在P40上训练,如有GPU,模型的训练过程预估为13分钟左右;如无GPU,则预估为5小时左右。更多训练模型的参数可参见文档paddlex.seg.DeepLabv3p。模型训练过程每间隔save_interval_epochs轮会保存一次模型在save_dir目录下,同时在保存的过程中也会在验证数据集上计算相关指标,具体相关日志参见文档。

In [ ]
num_classes = len(train_dataset.labels)
model = pdx.seg.DeepLabv3p(num_classes=num_classes)
model.train(
    num_epochs=40,
    train_dataset=train_dataset,
    train_batch_size=4,
    eval_dataset=eval_dataset,
    learning_rate=0.01,
    save_interval_epochs=1,    # pretrain_weights='output/deeplab4/best_model',
    save_dir='output/water')
   

最后一轮的输出如下所示:

2021-03-11 15:02:56 [INFO]	[TRAIN] Epoch=40/40, Step=1/37, loss=0.010831, lr=0.000362, time_each_step=0.18s, eta=0:0:10
2021-03-11 15:02:56 [INFO]	[TRAIN] Epoch=40/40, Step=3/37, loss=0.010944, lr=0.000344, time_each_step=0.2s, eta=0:0:10
2021-03-11 15:02:57 [INFO]	[TRAIN] Epoch=40/40, Step=5/37, loss=0.009099, lr=0.000326, time_each_step=0.22s, eta=0:0:10
2021-03-11 15:02:57 [INFO]	[TRAIN] Epoch=40/40, Step=7/37, loss=0.011186, lr=0.000308, time_each_step=0.24s, eta=0:0:10
2021-03-11 15:02:57 [INFO]	[TRAIN] Epoch=40/40, Step=9/37, loss=0.008269, lr=0.00029, time_each_step=0.25s, eta=0:0:10
2021-03-11 15:02:58 [INFO]	[TRAIN] Epoch=40/40, Step=11/37, loss=0.011792, lr=0.000272, time_each_step=0.25s, eta=0:0:10
2021-03-11 15:02:58 [INFO]	[TRAIN] Epoch=40/40, Step=13/37, loss=0.010976, lr=0.000254, time_each_step=0.26s, eta=0:0:9
2021-03-11 15:02:58 [INFO]	[TRAIN] Epoch=40/40, Step=15/37, loss=0.01399, lr=0.000236, time_each_step=0.26s, eta=0:0:9
2021-03-11 15:02:58 [INFO]	[TRAIN] Epoch=40/40, Step=17/37, loss=0.009998, lr=0.000217, time_each_step=0.26s, eta=0:0:8
2021-03-11 15:02:58 [INFO]	[TRAIN] Epoch=40/40, Step=19/37, loss=0.012266, lr=0.000198, time_each_step=0.26s, eta=0:0:8
2021-03-11 15:02:58 [INFO]	[TRAIN] Epoch=40/40, Step=21/37, loss=0.011713, lr=0.00018, time_each_step=0.13s, eta=0:0:5
2021-03-11 15:02:58 [INFO]	[TRAIN] Epoch=40/40, Step=23/37, loss=0.010291, lr=0.00016, time_each_step=0.11s, eta=0:0:5
2021-03-11 15:02:58 [INFO]	[TRAIN] Epoch=40/40, Step=25/37, loss=0.010211, lr=0.000141, time_each_step=0.09s, eta=0:0:4
2021-03-11 15:02:59 [INFO]	[TRAIN] Epoch=40/40, Step=27/37, loss=0.02097, lr=0.000121, time_each_step=0.08s, eta=0:0:4
2021-03-11 15:02:59 [INFO]	[TRAIN] Epoch=40/40, Step=29/37, loss=0.008198, lr=0.000101, time_each_step=0.07s, eta=0:0:3
2021-03-11 15:02:59 [INFO]	[TRAIN] Epoch=40/40, Step=31/37, loss=0.010346, lr=8.1e-05, time_each_step=0.06s, eta=0:0:3
2021-03-11 15:02:59 [INFO]	[TRAIN] Epoch=40/40, Step=33/37, loss=0.009331, lr=6e-05, time_each_step=0.06s, eta=0:0:3
2021-03-11 15:02:59 [INFO]	[TRAIN] Epoch=40/40, Step=35/37, loss=0.01259, lr=3.8e-05, time_each_step=0.06s, eta=0:0:3
2021-03-11 15:02:59 [INFO]	[TRAIN] Epoch=40/40, Step=37/37, loss=0.013072, lr=1.4e-05, time_each_step=0.06s, eta=0:0:3
2021-03-11 15:02:59 [INFO]	[TRAIN] Epoch 40 finished, loss=0.011522, lr=0.000195 .
2021-03-11 15:02:59 [INFO]	Start to evaluating(total_samples=11, total_steps=3)...
100%|██████████| 3/3 [00:02<00:00,  1.00it/s]
2021-03-11 15:03:02 [INFO]	[EVAL] Finished, Epoch=40, miou=0.814756, category_iou=[0.99168644 0.63782582], oacc=0.991806, category_acc=[0.99431391 0.84710874], kappa=0.774722, category_F1-score=[0.99582587 0.77886893] .
2021-03-11 15:03:03 [INFO]	Model saved in output/water/epoch_40.
2021-03-11 15:03:03 [INFO]	Current evaluated best model in eval_dataset is epoch_35, miou=0.8284633456567256
   

3.模型导出

模型训练时会自动保存模型参数,我们需要把训练模型导出成可预测模型。

In [ ]
!paddlex --export_inference --model_dir=output/water/best_model --save_dir=./inference_model
       
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/setuptools/depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
W0311 15:49:28.613981   782 device_context.cc:362] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.0, Runtime API Version: 10.1
W0311 15:49:28.618839   782 device_context.cc:372] device: 0, cuDNN Version: 7.6.
2021-03-11 15:49:32 [INFO]	Model[DeepLabv3p] loaded.
2021-03-11 15:49:32 [INFO]	Model for inference deploy saved in ./inference_model.
       

三、封装Module

下面正式开始模型转换!

1.模型转换

PaddleX模型可以快速转换成PaddleHub模型,只需要用下面这一句命令即可:

In [ ]
!hub convert --model_dir inference_model \
              --module_name WatermeterSegmentation \
              --module_version 1.0.0 \
              --output_dir outputs
   

转换成功后的模型保存在outputs文件夹下,我们解压一下:

扣子编程
扣子编程

扣子推出的AI编程开发工具

下载
In [ ]
!gzip -dfq /home/aistudio/outputs/WatermeterSegmentation.tar.gz
!tar -xf /home/aistudio/outputs/WatermeterSegmentation.tar
   

2.补充代码

刚刚转换的模型其实已经是PaddleHub的Module了,但是原项目中,作者做了一些图片的裁剪等操作,把数字提取出来了,因此,我们需要把这部分代码补充进去。

完整的module.py文件内容如下:

from __future__ import absolute_importfrom __future__ import divisionimport osimport cv2import argparseimport base64import paddlex as pdxfrom math import *import time, math, reimport numpy as npimport paddlehub as hubfrom paddlehub.module.module import moduleinfo, runnable, servingdef base64_to_cv2(b64str):
    data = base64.b64decode(b64str.encode('utf8'))
    data = np.fromstring(data, np.uint8)
    data = cv2.imdecode(data, cv2.IMREAD_COLOR)    return datadef cv2_to_base64(image):
    # return base64.b64encode(image)
    data = cv2.imencode('.jpg', image)[1]    return base64.b64encode(data.tostring()).decode('utf8')def read_images(paths):
    images = []    for path in paths:
        images.append(cv2.imread(path))    return images'''旋转图像并剪裁'''def rotate(
        img,  # 图片
        pt1, pt2, pt3, pt4,
        imgOutSrc):
    # print(pt1,pt2,pt3,pt4)
    withRect = math.sqrt((pt4[0] - pt1[0]) ** 2 + (pt4[1] - pt1[1]) ** 2)  # 矩形框的宽度
    heightRect = math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) **2)    # print("矩形的宽度",withRect, "矩形的高度", heightRect)
    angle = acos((pt4[0] - pt1[0]) / withRect) * (180 / math.pi)  # 矩形框旋转角度
    # print("矩形框旋转角度", angle)

    if withRect > heightRect:        if pt4[1]>pt1[1]:            # print("顺时针旋转")
            pass
        else:            # print("逆时针旋转")
            angle=-angle    else:        # print("逆时针旋转")
        angle=90 - angle

    height = img.shape[0]  # 原始图像高度
    width = img.shape[1]   # 原始图像宽度
    rotateMat = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1)  # 按angle角度旋转图像
    heightNew = int(width * fabs(sin(radians(angle))) + height * fabs(cos(radians(angle))))
    widthNew = int(height * fabs(sin(radians(angle))) + width * fabs(cos(radians(angle))))

    rotateMat[0, 2] += (widthNew - width) / 2
    rotateMat[1, 2] += (heightNew - height) / 2
    imgRotation = cv2.warpAffine(img, rotateMat, (widthNew, heightNew), borderValue=(255, 255, 255))    # cv2.imwrite("imgRotation.jpg", imgRotation) 

    # 旋转后图像的四点坐标
    [[pt1[0]], [pt1[1]]] = np.dot(rotateMat, np.array([[pt1[0]], [pt1[1]], [1]]))
    [[pt3[0]], [pt3[1]]] = np.dot(rotateMat, np.array([[pt3[0]], [pt3[1]], [1]]))
    [[pt2[0]], [pt2[1]]] = np.dot(rotateMat, np.array([[pt2[0]], [pt2[1]], [1]]))
    [[pt4[0]], [pt4[1]]] = np.dot(rotateMat, np.array([[pt4[0]], [pt4[1]], [1]]))    # 处理反转的情况
    if pt2[1]>pt4[1]:
        pt2[1],pt4[1]=pt4[1],pt2[1]    if pt1[0]>pt3[0]:
        pt1[0],pt3[0]=pt3[0],pt1[0]

    imgOut = imgRotation[int(pt2[1]):int(pt4[1]), int(pt1[0]):int(pt3[0])]
    cv2.imwrite(imgOutSrc, imgOut) # 裁减得到的旋转矩形框@moduleinfo(
    name='WatermeterSegmentation',    type='CV/semantic_segmentatio',
    author='郑博培、彭兆帅',
    author_email='2733821739@qq.com',
    summary='Digital dial segmentation of water meter',
    version='1.0.0')class MODULE(hub.Module):
    def _initialize(self, **kwargs):
        self.default_pretrained_model_path = os.path.join(
            self.directory, 'assets')
        self.model = pdx.deploy.Predictor(self.default_pretrained_model_path,
                                          **kwargs)    def predict(self,
                images=None,
                paths=None,
                data=None,
                batch_size=1,
                use_gpu=False,
                **kwargs):

        all_data = images if images is not None else read_images(paths)
        total_num = len(all_data)
        loop_num = int(np.ceil(total_num / batch_size))

        res = []        for iter_id in range(loop_num):
            batch_data = list()
            handle_id = iter_id * batch_size            for image_id in range(batch_size):                try:
                    batch_data.append(all_data[handle_id + image_id])                except IndexError:                    break
            out = self.model.batch_predict(batch_data, **kwargs)
            res.extend(out)        return res    def cutPic(self, picUrl):
        # seg = hub.Module(name='WatermeterSegmentation')
        image_name = picUrl
        im = cv2.imread(image_name)
        result = self.predict(images=[im])        # 将多边形polygon转矩形
        contours, hier = cv2.findContours(result[0]['label_map'], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 
        print(type(contours[0]))
        n = 0
        m = 0
        for index,contour in enumerate(contours):            if len(contour) > n:
                n = len(contour)
                m = index

        image = cv2.imread(image_name)        # 获取最小的矩形
        rect = cv2.minAreaRect(contours[m])
        box = np.int0(cv2.boxPoints(rect))        # 获取到矩形的四个点
        tmp = cv2.drawContours(image, [box], 0, (0, 0, 255), 3)
        imgOutSrc = 'result.jpg'
        rotate(image, box[0], box[1], box[2], box[3], imgOutSrc)
        res = []
        res.append(imgOutSrc)        return res    @serving
    def serving_method(self, images, **kwargs):
        """
        Run as a service.
        """
        images_decode = [base64_to_cv2(image) for image in images]
        results = self.predict(images_decode, **kwargs)
        res = []        for result in results:            if isinstance(result, dict):                # result_new = dict()
                for key, value in result.items():                    if isinstance(value, np.ndarray):
                        result[key] = cv2_to_base64(value)                    elif isinstance(value, np.generic):
                        result[key] = np.asscalar(value)            elif isinstance(result, list):                for index in range(len(result)):                    for key, value in result[index].items():                        if isinstance(value, np.ndarray):
                            result[index][key] = cv2_to_base64(value)                        elif isinstance(value, np.generic):
                            result[index][key] = np.asscalar(value)            else:                raise RuntimeError('The result cannot be used in serving.')
            res.append(result)        return res    @runnable
    def run_cmd(self, argvs):
        """
        Run as a command.
        """
        self.parser = argparse.ArgumentParser(
            description="Run the {} module.".format(self.name),
            prog='hub run {}'.format(self.name),
            usage='%(prog)s',
            add_help=True)
        self.arg_input_group = self.parser.add_argument_group(
            title="Input options", description="Input data. Required")
        self.arg_config_group = self.parser.add_argument_group(
            title="Config options",
            description=            "Run configuration for controlling module behavior, not required.")
        self.add_module_config_arg()
        self.add_module_input_arg()
        args = self.parser.parse_args(argvs)
        results = self.predict(
            paths=[args.input_path],
            use_gpu=args.use_gpu)        return results    def add_module_config_arg(self):
        """
        Add the command config options.
        """
        self.arg_config_group.add_argument(            '--use_gpu',            type=bool,
            default=False,            help="whether use GPU or not")    def add_module_input_arg(self):
        """
        Add the command input options.
        """
        self.arg_input_group.add_argument(            '--input_path', type=str, help="path to image.")if __name__ == '__main__':
    module = MODULE(directory='./new_model')
    images = [cv2.imread('./cat.jpg'), cv2.imread('./cat.jpg'), cv2.imread('./cat.jpg')]
    res = module.predict(images=images)
   

3.模型测试

首先安装我们刚刚写好的Module:

In [ ]
!hub install WatermeterSegmentation
       
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/setuptools/depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized[2021-03-11 16:42:50,225] [    INFO] - Successfully uninstalled WatermeterSegmentation[2021-03-11 16:42:50,441] [    INFO] - Successfully installed WatermeterSegmentation-1.0.0
       

模型调用:

In [4]
import cv2import paddlehub as hub

seg = hub.Module(name='WatermeterSegmentation')
res = seg.cutPic(picUrl="water/images/val/20200521105032.png")
       
[2021-03-11 17:13:36,113] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object
       
       

预测结果如下。

输入图片:

【PaddleHub模型贡献】一行代码实现水表的数字表盘分割 - php中文网        

最终将截取的图片显示效果如下:

【PaddleHub模型贡献】一行代码实现水表的数字表盘分割 - php中文网        

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
硬盘接口类型介绍
硬盘接口类型介绍

硬盘接口类型有IDE、SATA、SCSI、Fibre Channel、USB、eSATA、mSATA、PCIe等等。详细介绍:1、IDE接口是一种并行接口,主要用于连接硬盘和光驱等设备,它主要有两种类型:ATA和ATAPI,IDE接口已经逐渐被SATA接口;2、SATA接口是一种串行接口,相较于IDE接口,它具有更高的传输速度、更低的功耗和更小的体积;3、SCSI接口等等。

1126

2023.10.19

PHP接口编写教程
PHP接口编写教程

本专题整合了PHP接口编写教程,阅读专题下面的文章了解更多详细内容。

192

2025.10.17

php8.4实现接口限流的教程
php8.4实现接口限流的教程

PHP8.4本身不内置限流功能,需借助Redis(令牌桶)或Swoole(漏桶)实现;文件锁因I/O瓶颈、无跨机共享、秒级精度等缺陷不适用高并发场景。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

1606

2025.12.29

java接口相关教程
java接口相关教程

本专题整合了java接口相关内容,阅读专题下面的文章了解更多详细内容。

20

2026.01.19

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

143

2026.01.28

包子漫画在线官方入口大全
包子漫画在线官方入口大全

本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。

28

2026.01.28

ao3中文版官网地址大全
ao3中文版官网地址大全

AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。

64

2026.01.28

php怎么写接口教程
php怎么写接口教程

本合集涵盖PHP接口开发基础、RESTful API设计、数据交互与安全处理等实用教程,助你快速掌握PHP接口编写技巧。阅读专题下面的文章了解更多详细内容。

2

2026.01.28

php中文乱码如何解决
php中文乱码如何解决

本文整理了php中文乱码如何解决及解决方法,阅读节专题下面的文章了解更多详细内容。

4

2026.01.28

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 22.3万人学习

Django 教程
Django 教程

共28课时 | 3.6万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号