1 Star 1 Fork 1

guojy/pytorch-captcha

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
datasets.py 1.84 KB
一键复制 编辑 原始数据 按行查看 历史
ice-tong 提交于 2019-03-23 15:01 . Add files via upload
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 12 21:49:21 2019
@author: icetong
"""
import os
from PIL import Image
import torch
from torch.utils.data import Dataset
source = [str(i) for i in range(0, 10)]
source += [chr(i) for i in range(97, 97+26)]
alphabet = ''.join(source)
def img_loader(img_path):
img = Image.open(img_path)
return img.convert('RGB')
def make_dataset(data_path, alphabet, num_class, num_char):
img_names = os.listdir(data_path)
samples = []
for img_name in img_names:
img_path = os.path.join(data_path, img_name)
target_str = img_name.split('.')[0]
assert len(target_str) == num_char
target = []
for char in target_str:
vec = [0] * num_class
vec[alphabet.find(char)] = 1
target += vec
samples.append((img_path, target))
return samples
class CaptchaData(Dataset):
def __init__(self, data_path, num_class=36, num_char=4,
transform=None, target_transform=None, alphabet=alphabet):
super(Dataset, self).__init__()
self.data_path = data_path
self.num_class = num_class
self.num_char = num_char
self.transform = transform
self.target_transform = target_transform
self.alphabet = alphabet
self.samples = make_dataset(self.data_path, self.alphabet,
self.num_class, self.num_char)
def __len__(self):
return len(self.samples)
def __getitem__(self, index):
img_path, target = self.samples[index]
img = img_loader(img_path)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, torch.Tensor(target)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/covwo/pytorch-captcha.git
[email protected]:covwo/pytorch-captcha.git
covwo
pytorch-captcha
pytorch-captcha
master

搜索帮助