一、上代码

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. C#编程(十八)----------C#中的结构

    C#中的结构 假设有一个类: class Dimensions { public double Length; public double Width; } 定义了一个类,它只存储某一项的长度和宽度. ...

  2. c++中 extern

    用例子给你示范 // 1.cpp int x = 10; // 2.cpp 注意没有包含1.cpp #include <iostream> using namespace std; ext ...

  3. IP地址和子网划分学习笔记之《预备知识:进制计数》

    一.序:IP地址和子网划分学习笔记开篇 只要记住你的名字,不管你在世界的哪个地方,我一定会去见你.——新海诚 电影<你的名字> 在我们的日常生活中,每个人的名字对应一个唯一的身(敏)份(感 ...

  4. 架构:Hexagonal Architecture Guidelines for Rails(转载)

    原文地址:http://theaudaciouscodeexperiment.com/blog/2014/03/17/hexagonal-architecture-guidelines-for-rai ...

  5. cocos2d-x 3.0 正式版 项目创建

    官方示比例如以下: Example: $ cd cocos2d-x $ ./setup.py $ source FILE_TO_SAVE_SYSTEM_VARIABLE $ cocos new Eng ...

  6. cloudera项目源代码

    以下项目都需要安装git,Linux的git还是比较容易安装的,windows的git安装参考项目区域:软件版本控制-在Windows中使用Git视频介绍 git相关软件安装参考win7安装 git软 ...

  7. IOS Devices Version

    游戏项目中有一个专门用于收集IOS崩溃的接口和查询页,运营/测试的同事有时候会通过查询页大概看一下每日崩溃的情况,经常会问iPhone6,1是什么,iPhone7,1又是什么设备?   我从网上仔细搜 ...

  8. easyUi 的DataGrid的绑定

    html代码: @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Index_Layout.cshtml&quo ...

  9. 选中TreeView的某节点,并加背景颜色

    一:按钮事件,遍历所有节点 private void button2_Click(object sender, EventArgs e) { foreach (TreeNode n in TreeVi ...

  10. JAVA 是否会发生内存泄露(转)

    原文链接: JAVA 是否会发生内存泄露 几次面试,面试官都问到了这个问题,于是搜集了答案.总结出虽然java自身有垃圾回收机制,但是很多情况下还是发生内存泄露的. java导致内存泄露的原因很明确: ...