Python+H5py实现将SVHN样本库转换为FasterRcnn训练样本
一、上代码
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训练样本的更多相关文章
- Python小工具:利用ffmpy3库3秒钟将视频转换为音频
作者 | pk 哥 来源公众号 | Python知识圈(ID:PythonCircle) 最近,有读者微信上私聊我,想让我写一篇视频批量转换成音频的文章,我答应了,周末宅家里把这个小工具做出来了. 这 ...
- 学习Python要知道哪些重要的库和工具
本文转自:https://github.com/jobbole/awesome-python-cn 环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具. ...
- (转)Python爬虫利器一之Requests库的用法
官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...
- Windows系统下 Python(Anaconda)的 Dlib库 的安装
0.引言 介绍 Windows 10 64位系统下,利用 Anaconda 开发环境,在python中安装 Dlib库 : windows下dlib的安装十分不友好,所以在这里分享下安装过程: win ...
- Python - 常用更新命令以及常见库安装
库的安装方式一般有两种: 一. pip直接安装(或使用豆瓣源) pip install scrapy pip install -i https://pypi.douban.com/simple/ sc ...
- Python爬虫之Beautiful Soup解析库的使用(五)
Python爬虫之Beautiful Soup解析库的使用 Beautiful Soup-介绍 Python第三方库,用于从HTML或XML中提取数据官方:http://www.crummv.com/ ...
- Python不使用int()函数把字符串转换为数字
Python不使用int()函数把字符串转换为数字 2018年05月21日 14:18:45 边缘ob边缘ob 阅读数:1035 https://blog.csdn.net/qq_33192555/a ...
- Python C/C++ 拓展使用接口库(build-in) ctypes 使用手册
Python C/C++ 拓展使用接口库(build-in) ctypes 使用手册 ctypes 是一个Python 标准库中的一个库.为了实现调用 DLL,或者共享库等C数据类型而设计.它可以把这 ...
- 您好,python的请求es的http库是urllib3, 一个请求到贵司的es节点,想了解下,中间有哪些网关啊?冒昧推测,贵司的部分公共网关与python-urllib3的对接存在异常?
您好,python的请求es的http库是urllib3, 一个请求到贵司的es节点,想了解下,中间有哪些网关啊?冒昧推测,贵司的部分公共网关与python-urllib3的对接存在异常? 负载均衡( ...
随机推荐
- 使用jQuery实现图片懒加载原理
原文:https://www.liaoxuefeng.com/article/00151045553343934ba3bb4ed684623b1bf00488231d88d000 在网页中,常常需要用 ...
- ZServer4D开源项目
ZServer4D开源项目 ZServer4D 是一套从商业项目剥离而出的云服务器中间件,可以承载百万级的分布式负载服务,并且支持IoT及内网穿透. 作者将它开源了 https://github.co ...
- lemon OA 我长时间经历的第一个开源项目
对于原作者来说, 他长时间运营了一个项目,lemon OA .目前,八百多star.在运营这个项目的过程中,我想说,他成了activiti 目前国内比较牛逼的几个人.还有 spring securit ...
- tomcat8.0.15+spring4.1.2的集群下共享WebSocketSession?
环境:nginx+Tomcat服务器 A B C 问题:如果用户 1 访问由服务器 A socket服务 ,用户2 由服务器 C socket服务 ,此时如果用户 1, 2 想通过 sock ...
- ifram 取父窗体的URL地址
var url=''; try { url = window.top.document.referrer ; } catch(M) { if (window.parent) ...
- mysql update select
根据文件名 更新外键ID UPDATE tb_obj INNER JOIN tb_img ON tb_img.filename=tb_obj.filename SET tb_objinfo.img ...
- The "Out of socket memory" error
The "Out of socket memory" error I recently did some work on some of our frontend machines ...
- js转义和反转义html htmlencode htmldecode
文章目录 JS实现HTML标签转义及反转义 用Javascript进行HTML转义 1.HTML转义 2.反转义 3.一个有意思的认识 4.完整版本的代码 其他 [转义字符]HTML 字符实体< ...
- IIS(互联网信息服务)
ylbtech-Miscellaneos:IIS(互联网信息服务) IIS是Internet Information Services的缩写,意为互联网信息服务,是由微软公司提供的基于运行Micros ...
- [leetcode]N-Queens @ Python
原题地址:https://oj.leetcode.com/problems/n-queens/ 题意:经典的N皇后问题. 解题思路:这类型问题统称为递归回溯问题,也可以叫做对决策树的深度优先搜索(df ...