一、上代码

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. Consul替代Eureka

    原文:https://www.cnblogs.com/ityouknow/p/9340591.html 在上个月我们知道 Eureka 2.X 遇到困难停止开发了,但其实对国内的用户影响甚小,一方面国 ...

  2. Java 字符串包含

    函数boolean containsAny(String str, String searchChars) 判断str字符串中是否包含searchChars字符串 String khh_str = & ...

  3. Java Jackson - Json Polymorphism

    from://http://www.studytrails.com/java/json/java-jackson-Serialization-polymorphism.jsp Jackson prov ...

  4. 用SDWebImage加载FLAnimatedImage

    用SDWebImage加载FLAnimatedImage 效果 源码 https://github.com/YouXianMing/Animations // // GifPictureControl ...

  5. Mysql一个非常有用的内置函数今天碰到要把MySQL数据库中的varchar转换成date类型进

    Mysql一个非常有用的内置函数 今天碰到要把MySQL数据库中的varchar转换成date类型进行时间的比较和查询.在网上找了找,发现MySQL也跟其他数据库一样有自己内置的转换函数:str_to ...

  6. Library drmframework_jni not found

    http://piotrbuda.eu/2012/06/trying-to-solve-error-491-in-play-store-on-android-emulator.html http:// ...

  7. [转]PHP中替换换行符

    FROM :http://www.cnblogs.com/siqi/archive/2012/10/12/2720713.html //php 有三种方法来解决 //1.使用str_replace 来 ...

  8. [转]thinkphp在iis下的rewrite伪静态的配置方法

    From : http://code-tech.diandian.com/post/2012-11-12/40042151797 首先你要安装IIS下的rewrite组建,下载地址:Rewrite.z ...

  9. Leap Motion 上手体验

    很早之前就关注了Leap Motion这个颠覆性的体感操作设备,如今7月22日上市至今已经一个月左右,淘宝的价格也已经降到650元,虽说相对国外还是偏贵,但是已经忍不住尝尝鲜了. Leap Motio ...

  10. 初识EntityFramework6【转】

    http://www.cnblogs.com/wujingtao/p/5401132.html 什么是EF? EF是一种ORM(Object-relational mapping)框架,它能把我们在编 ...