一、上代码

import os
import h5py svhnPath = 'D:\\Project\\AIProject\\SVHNClassifier\\data' def loadSvhn(path, subdir):
print('process folder : %s' % subdir)
filenames = []
dir = os.path.join(svhnPath, subdir)
for filename in os.listdir(dir):
filenameParts = os.path.splitext(filename)
if filenameParts[1] != '.png':
continue
filenames.append(filenameParts)
svhnMat = h5py.File(name=os.path.join(dir, 'digitStruct.mat'), mode='r')
datasets = []
filecounts = len(filenames)
for idx, file in enumerate(filenames):
boxes = {}
filenameNum = file[0]
item = svhnMat['digitStruct']['bbox'][int(filenameNum) - 1].item()
for key in ['label', 'left', 'top', 'width', 'height']:
attr = svhnMat[item][key]
values = [svhnMat[attr.value[i].item()].value[0][0]
for i in range(len(attr))] if len(attr) > 1 else [attr.value[0][0]]
boxes[key] = values
datasets.append({'dir': dir, 'file': file, 'boxes': boxes})
if idx % 10 == 0: print('-- loading %d / %d' % (idx, filecounts))
return datasets if __name__ == '__main__':
for sub_dir in ['extra','train']:
data_sets = loadSvhn(svhnPath, sub_dir)
# data_sets = [{'dir': './', 'file': ('01', '.png'),
# 'boxes': {'label': ['0'], 'left': [12], 'top': [10], 'width': [20], 'height': [30]}}]
print('processing locations to txt file ...')
for ds in data_sets:
txt_file = os.path.join(ds['dir'], ds['file'][0] + '.txt')
boxes = ds['boxes']
labels = boxes['label']
lines = []
with open(txt_file, mode='w', encoding='utf-8') as fs:
for i in range(len(labels)):
label = boxes['label'][i]
left = boxes['left'][i]
top = boxes['top'][i]
width = boxes['width'][i]
height = boxes['height'][i]
lines.append('%s,%s,%s,%s,%s' % (int(label), left, top, width, height))
fs.write('\n'.join(lines))
print('done.')

二、效果

Python+H5py实现将SVHN样本库转换为FasterRcnn训练样本的更多相关文章

  1. Python小工具:利用ffmpy3库3秒钟将视频转换为音频

    作者 | pk 哥 来源公众号 | Python知识圈(ID:PythonCircle) 最近,有读者微信上私聊我,想让我写一篇视频批量转换成音频的文章,我答应了,周末宅家里把这个小工具做出来了. 这 ...

  2. 学习Python要知道哪些重要的库和工具

    本文转自:https://github.com/jobbole/awesome-python-cn 环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具. ...

  3. (转)Python爬虫利器一之Requests库的用法

    官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...

  4. Windows系统下 Python(Anaconda)的 Dlib库 的安装

    0.引言 介绍 Windows 10 64位系统下,利用 Anaconda 开发环境,在python中安装 Dlib库 : windows下dlib的安装十分不友好,所以在这里分享下安装过程: win ...

  5. Python - 常用更新命令以及常见库安装

    库的安装方式一般有两种: 一. pip直接安装(或使用豆瓣源) pip install scrapy pip install -i https://pypi.douban.com/simple/ sc ...

  6. Python爬虫之Beautiful Soup解析库的使用(五)

    Python爬虫之Beautiful Soup解析库的使用 Beautiful Soup-介绍 Python第三方库,用于从HTML或XML中提取数据官方:http://www.crummv.com/ ...

  7. Python不使用int()函数把字符串转换为数字

    Python不使用int()函数把字符串转换为数字 2018年05月21日 14:18:45 边缘ob边缘ob 阅读数:1035 https://blog.csdn.net/qq_33192555/a ...

  8. Python C/C++ 拓展使用接口库(build-in) ctypes 使用手册

    Python C/C++ 拓展使用接口库(build-in) ctypes 使用手册 ctypes 是一个Python 标准库中的一个库.为了实现调用 DLL,或者共享库等C数据类型而设计.它可以把这 ...

  9. 您好,python的请求es的http库是urllib3, 一个请求到贵司的es节点,想了解下,中间有哪些网关啊?冒昧推测,贵司的部分公共网关与python-urllib3的对接存在异常?

    您好,python的请求es的http库是urllib3, 一个请求到贵司的es节点,想了解下,中间有哪些网关啊?冒昧推测,贵司的部分公共网关与python-urllib3的对接存在异常? 负载均衡( ...

随机推荐

  1. iOS 32位、 64位系统兼容性设置-Xcode创建支持IOS4.3以上版本的应用的方法

    方法一: 如果是Xcode 5的话步骤为 点击项目名称->Build Settings->搜索 Architectures 这个里面的原始的值是Standard architectures ...

  2. Swift - 通过叠加UILabel来实现混合的进度条

    Swift - 通过叠加UILabel来实现混合的进度条 效果 源码 https://github.com/YouXianMing/Swift-Animations // // MixedColorP ...

  3. 每天一个linux命令-ls命令

    查看统计当前目录下文件的个数,包括子目录里的. ls -lR| grep "^-" | wc -l[喝小酒的网摘]http://blog.hehehehehe.cn/a/12311 ...

  4. MAC 上的 Live Writer : ecto

    ecto 在这里: http://illuminex.com/ecto/ 它是一款 MAC 上的 live writer,对我来说,没有 writer 之类的工具,我就宁可不写博客。 测试插入图片 测 ...

  5. sql server获取标识,获取最后ID IDENT_CURRENT、IDENTITY、SCOPE_IDENTITY区别

    概念解释 IDENT_CURRENT returns the last identity value generated for a specific table in any session and ...

  6. 关于NLP和深度学习,准备好好看看这个github,还有这篇介绍

    这个github感觉很不错,把一些比较新的实现都尝试了: https://github.com/brightmart/text_classification fastText TextCNN Text ...

  7. 在Cygwin里,如何进入到C盘?

    答: cd /cygdrive/c 来源: How to navigate to a directory in C:\ with Cygwin? https://stackoverflow.com/q ...

  8. "Your computer could not be joined to the domain. You have exceeded the maximum number of computer accounts you are allowed to create in this domain. Contact your system administrator to have this limit reset or increased."

    用一个普通的域帐号玩私有云的时候,遇到了如下的报错. "Your computer could not be joined to the domain. You have exceeded ...

  9. Recover Binary Search Tree leetcode java

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  10. Sudoku Solver leetcode java

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...