Skip to content

Instantly share code, notes, and snippets.

@mdshopon
Created July 31, 2018 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdshopon/99ab51cb5762a4d67b34e430a9d35895 to your computer and use it in GitHub Desktop.
Save mdshopon/99ab51cb5762a4d67b34e430a9d35895 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import os
import itertools
import codecs
import re
import datetime
import cairocffi as cairo
import editdistance
import numpy as np
from scipy import ndimage
import random
import pylab
from keras import backend as K
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers import Input, Dense, Activation
from keras.layers import Reshape, Lambda
from keras.layers.merge import add, concatenate
from keras.models import Model
from keras.layers.recurrent import GRU
from keras.optimizers import SGD
from keras.utils.data_utils import get_file
from keras.preprocessing import image
import keras.callbacks
import cv2
import matplotlib.pyplot as plt
# paints the string in a random location the bounding box
# also uses a random font, a slight random rotation,
# and a random amount of speckle noise
def noisy(noise_typ,image):
if noise_typ == "gauss":
row,col,ch= image.shape
mean = 0
var = 0.1
sigma = var**0.5
gauss = np.random.normal(mean,sigma,(row,col,ch))
gauss = gauss.reshape(row,col,ch)
noisy = image + gauss
return noisy
elif noise_typ == "s&p":
row,col,ch = image.shape
s_vs_p = 0.5
amount = 0.004
out = np.copy(image)
# Salt mode
num_salt = np.ceil(amount * image.size * s_vs_p)
coords = [np.random.randint(0, i - 1, int(num_salt))
for i in image.shape]
out[coords] = 1
# Pepper mode
num_pepper = np.ceil(amount* image.size * (1. - s_vs_p))
coords = [np.random.randint(0, i - 1, int(num_pepper))
for i in image.shape]
out[coords] = 0
return out
elif noise_typ == "poisson":
vals = len(np.unique(image))
vals = 2 ** np.ceil(np.log2(vals))
noisy = np.random.poisson(image * vals) / float(vals)
return noisy
elif noise_typ =="speckle":
row,col,ch = image.shape
gauss = np.random.randn(row,col,ch)
gauss = gauss.reshape(row,col,ch)
noisy = image + image * gauss
return noisy
def imsave(fname, arr, vmin=None, vmax=None, cmap='gray', format=None, origin=None):
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
fig = Figure(figsize=arr.shape[::-1], dpi=1, frameon=False)
canvas = FigureCanvas(fig)
fig.figimage(arr, cmap=cmap, vmin=vmin, vmax=vmax, origin=origin)
fig.savefig(fname, dpi=1, format=format)
def speckle(img):
severity = np.random.uniform(0, 0.4)
print(severity)
blur = ndimage.gaussian_filter(np.random.randn(*img.shape) * severity, 1)
img_speck = (img + blur)
img_speck[img_speck > 1] = 1
img_speck[img_speck <= 0] = 0
return img_speck
def sp_noise(image,prob):
output = np.zeros(image.shape,np.uint8)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 255
else:
output[i][j] = image[i][j]
return output
def paint_text(text):
newtext = ""
import random
banglachars = "অআইঈউঊঋএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহড়ঢ়য়ঃৎং"
# text = "নড়চড়"
chars = []
for i in range(0, len(banglachars), 3):
chars.append(banglachars[i:i + 3])
for i in range(0, len(text), 3):
ch = text[i:i + 3]
itsoke = 1
for j in chars:
if j == ch:
itsoke = 0
# text="অ"
w = 280
h = 68
fontsize = 65
x, y = 8, 54
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
import random
FlagBlack = random.randint(0, 4)
# FlagBlack = 1
with cairo.Context(surface) as context:
context.set_source_rgb(1,1,1)
context.paint()
# this font list works in CentOS 7
multi_fonts = True
# if multi_fonts:
# fonts = ['Bangla', 'AponaLohit', 'Nikosh', 'Siyamrupali', 'kalpurush', 'AdorshoLipi', 'Likhan', 'Lohit Bengali',
# 'SutonnyBanglaOMJ', 'Sagar', 'Rupali', 'Mukti','Solaimanlipi']
fonts = ['SutonnyBanglaOMJ', 'Lohit Bengali', 'AponaLohit', 'Nikosh', 'Siyamrupali', 'SutonnyBanglaOMJ',
'kalpurush', 'AdorshoLipi', 'Bensen', 'SutonnyBanglaOMJ']
# fonts = ['Bangla', 'AponaLohit', 'Nikosh', 'Siyamrupali', 'kalpurush', 'AdorshoLipi', 'Likhan', 'Lohit Bengali',
# 'SutonnyBanglaOMJ', 'Sagar', 'Rupali', 'Mukti']
f = np.random.choice(fonts)
boldornotbold = 1
if (f == "Solaimanlipi"):
fontsize = random.randint(62, 65)
x, y = 8, 54
elif (f == "Lohit Bengali"):
fontsize = random.randint(54, 58)
x, y = 6, 54
elif (f == "AponaLohit"):
fontsize = random.randint(52, 56)
x, y = 4, 54
elif (f == "Nikosh"):
fontsize = random.randint(72, 76)
x, y = 8, 58
elif (f == "Siyamrupali"):
fontsize = random.randint(48, 52)
x, y = 12, 54
elif (f == "kalpurush"):
fontsize = random.randint(62, 66)
x, y = 12, 56
elif (f == "AdorshoLipi"):
fontsize = random.randint(52, 56)
x, y = 9, 56
elif (f == "Bensen"):
fontsize = random.randint(70, 74)
x, y = 10, 56
elif (f == "SutonnyBanglaOMJ"):
fontsize = random.randint(68, 72)
x, y = 10, 56
# context.select_font_face(np.random.choice(fonts), cairo.FONT_SLANT_NORMAL,
# np.random.choice([cairo.FONT_WEIGHT_BOLD, cairo.FONT_WEIGHT_NORMAL]))
context.select_font_face(f, cairo.FONT_SLANT_NORMAL,
boldornotbold)
# else:
# context.select_font_face('Mukti' , cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
import random
context.set_font_size(fontsize)
box = context.text_extents(text)
border_w_h = (4, 4)
max_shift_x = w - box[2]
max_shift_y = h - box[3] - border_w_h[1]
top_left_x = np.random.randint(0, int(max_shift_x))
top_left_y = np.random.randint(0, int(max_shift_y))
context.move_to(x, y)
# context.move_to(top_left_x - int(box[0]), top_left_y - int(box[1]))
context.set_source_rgb(0,0,0)
# print(text)
context.show_text(text)
buf = surface.get_data()
a = np.frombuffer(buf, np.uint8)
a.shape = (h, w, 4)
# a = sp_noise(a,0.01)
# plt.imshow(a,cmap='gray')
# plt.show()
a = a[:, :, 0] # grab single channel
# import cv2
vis2=a
# vis2 = cv2.cvtColor(a, cv2.COLOR_GRAY2BGR)
ran = random.randint(1, 4)
# print(ran)
vis2 = cv2.resize(vis2, (280 / ran, 68 / ran))
vis2 = cv2.resize(vis2, (280, 68))
# a = imsave('dataset/file_'+str(random.randint(0,1999))+'.png',a)
a=vis2
a = cv2.adaptiveThreshold(a, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
cv2.THRESH_BINARY, 11, 2)
a = np.expand_dims(a, 0)
a = image.random_rotation(a, 3 * (w - top_left_x) / w + 1)
a = speckle(a)
a = np.reshape(a, (68, 280))
import matplotlib.pyplot as plt
plt.imshow(a,cmap='gray')
plt.show()
# a = a.astype(np.float32) / 255
a = np.expand_dims(a, 0)
# b=a
# print(b.shape)
# b=np.reshape(b,(68,280))
# print(b.shape)
# return a
for i in range(5):
paint_text("০১-২৩৪")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment