在Android上运行基于TensorFlow Lite训练的二维码检测模型

本文介绍如何使用TensorFlow Lite训练二维码检测模型并在Android应用中运行。通过对比,发现虽然机器学习可以定位二维码,但在实时扫码场景中,传统图形学算法速度更快。在Android上,结合barcode SDK可以实现高效二维码扫描。
摘要由CSDN通过智能技术生成

二维码定位可以使用传统的图形学或者机器学习。那么哪种在手机上运行的效率更高?我们可以动手实验下。

使用TensorFlow Lite训练二维码检测模型

训练图集来自于boofcv.org。为了方便,我整理之后放到了GitHub仓库里。

下载二维码图集:

git clone https://github.com/yushulx/barcode-qrcode-images.git

创建一个切分数据集的Python脚本partition_dataset.py

""" usage: partition_dataset.py [-h] [-i IMAGEDIR] [-o OUTPUTDIR] [-r RATIO] [-x]
Partition dataset of images into training and testing sets
optional arguments:
  -h, --help            show this help message and exit
  -i IMAGEDIR, --imageDir IMAGEDIR
                        Path to the folder where the image dataset is stored. If not specified, the CWD will be used.
  -o OUTPUTDIR, --outputDir OUTPUTDIR
                        Path to the output folder where the train and test dirs should be created. Defaults to the same directory as IMAGEDIR.
  -r RATIO, --ratio RATIO
                        The ratio of the number of test images over the total number of images. The default is 0.1.
  -x, --xml             Set this flag if you want the xml annotation files to be processed and copied over.
"""
import os
import re
from shutil import copyfile
import argparse
import math
import random
import cv2

def png2jpg(source, filename):
    if filename[-4:] == '.png':
        filepath = os.path.join(source, filename)
        img = cv2.imread(filepath)
        filepath = filepath[:-4] + '.jpg'
        filename = filename[:-4] + '.jpg'
        cv2.imwrite(filepath, img)
        print(filepath)

    return filename

def iterate_dir(source, dest, ratio, copy_xml):
    source = source.replace('\\', '/')
    dest = dest.replace('\\', '/')
    train_dir = os.path.join(dest, 'train')
    test_dir = os.path.join(dest, 'test')

    if not os.path.exists(train_dir):
        os.makedirs(train_dir)
    if not os.path.exists(test_dir):
        os.makedirs(test_dir)

    images = [f for f in os.listdir(source)
              if re.search(r'([a-zA-Z0-9\s_\\.\-\(\):])+(?i)(.jpg|.jpeg|.png)$', f)]

    num_images = len(images)
    num_test_images = math.ceil(ratio*num_images)

    for i in range(num_test_images):
        idx = random.randint(0, len(images)-1)
        filename = images[idx]
        filename = png2jpg(source, filename)
        copyfile(os.path.join(source, filename),
                 os.path.join(test_dir, filename))
        if copy_xml:
            xml_filename = os.path.splitext(filename)[0]+'.xml'
            copyfile(os.path.join(source, xml_filename),
                     os.path.join(test_dir,xml_filename))
        images.remove(images[idx])

    for filename in images:
        filename = png2jpg(source, filename)
        copyfile(os.path.join(source, filename),
                 os.path.join(train_dir, filename))
        if copy_xml:
            xml_filename = os.path.splitext(fil
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值