HWDB1.1数据集.png转换成.tfrecord

创建data_prepare文件夹,内含data_convert.py和src/tfrecord.py。data文件夹分test和train,分别存放.png图片。运行代码将.png转换为.tfrecord文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、首先建立一个data_prepare文件夹,data_prepare文件夹中包含以下三个文件
文件夹中包含以上三个文件文件夹中包含在这里插入图片描述
data_convert.py代码

# coding:utf-8
from __future__ import absolute_import
import argparse
import os
import logging
from src.tfrecord import main

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--tensorflow-data-dir', default='data/')
    parser.add_argument('--train-shards', default=2, type=int)
    parser.add_argument('--test-shards', default=2, type=int)
    parser.add_argument('--num-threads', default=2, type=int)
    parser.add_argument('--dataset-name', default='satellite', type=str)
    return parser.parse_args()

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    args = parse_args()
    args.tensorflow_dir = args.tensorflow_data_dir
    args.train_directory = os.path.join(args.tensorflow_dir, 'train')
    args.test_directory = os.path.join(args.tensorflow_dir, 'test')
    args.output_directory = args.tensorflow_dir
    args.labels_file = os.path.join(args.tensorflow_dir, 'label.txt')
    if os.path.exists(args.labels_file) is False:
        logging.warning('Can\'t find label.txt. Now create it.')
        all_entries = os.listdir(args.train_directory)
        dirnames = []
        for entry in all_entries:
            if os.path.isdir(os.path.join(args.train_directory, entry)):
                dirnames.append(entry)
        with open(args.labels_file, 'w') as f:
            for dirname in dirnames:
                f.write(dirname + '\n')
    main(args)

2、其中data文件夹中包含test 和 train两个文件夹,里面分别含有测试集和训练集.png格式的图片
在这里插入图片描述
3、其中src文件夹中包含tfrecord.py文件
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190410154815514.png
tfrecord.py代码

# coding:utf-8
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Converts image data to TFRecords file format with Example protos.
The image data set is expected to reside in JPEG files located in the
following directory structure.
  data_dir/label_0/image0.jpeg
  data_dir/label_0/image1.jpg
  ...
  data_dir/label_1/weird-image.jpeg
  data_dir/label_1/my-image.jpeg
  ...
where the sub-directory is the unique label associated with these images.
This TensorFlow script converts the training and evaluation data into
a sharded data set consisting of TFRecord files
  train_directory/train-00000-of-01024
  train_directory/train-00001-of-01024
  ...
  train_directory/train-00127-of-01024
and
  test_directory/test-00000-of-00128
  test_directory/test-00001-of-00128
  ...
  test_directory/test-00127-of-00128
where we have selected 1024 and 128 shards for each data set. Each record
within the TFRecord file is a serialized Example proto. The Example proto
contains the following fields:
  image/encoded: string containing JPEG encoded image in RGB colorspace
  image/height: integer, image height in pixels
  image/width: integer, image w
HWDB1.1数据集是一个手写汉字数据库,通常包含了大量的手写汉字样本以及相关的标注信息。如果你想要将HWDB1.1数据集中的.png格式的图像文件转换成JSON格式的文件,这通常意味着你想要将图像的元数据和可能的标注信息存储到JSON结构中。 要实现这个转换,你可以遵循以下步骤: 1. 解析.png图像文件:这通常需要你有一个读取图像数据的工具或者库,例如Python中的Pillow库。 2. 提取或生成标注信息:如果你有的话,需要从数据集中提取出每张图像的标注信息,比如图像中的汉字是什么,位置信息,分类信息等。 3. 编写JSON格式数据:根据你的需求,将图像的元数据和标注信息按照JSON的结构格式编写成文本。 下面是一个简化的例子,展示了如何将一张图像的信息转换成JSON格式: ```python import json from PIL import Image # 假设你已经有了图像文件和它的标注信息 image_path = 'path_to_your_image.png' annotation = { 'image_path': image_path, 'label': '某个汉字', # 假设你已经知道图像中的汉字是什么 'position': [100, 150, 20, 30], # 假设你有边界框位置信息 'other_info': '其他可能的标注信息' } # 读取图像信息 image = Image.open(image_path) image_info = { 'width': image.width, 'height': image.height } # 合并图像信息和标注信息 combined_data = { 'image_info': image_info, 'annotation': annotation } # 将信息转换为JSON字符串 json_str = json.dumps(combined_data, indent=4) # 保存JSON字符串到文件 with open('output.json', 'w') as json_file: json_file.write(json_str) ``` 在上面的代码中,我们创建了一个Python脚本来处理这个转换。实际情况下,你需要根据你的具体数据集结构和标注信息来调整代码。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值