Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ Code for generating synthetic text images as described in ["Synthetic Data for T
The library is written in Python. The main dependencies are:

```
pygame, opencv (cv2), PIL (Image), numpy, matplotlib, h5py, scipy
pygame, opencv (cv3), PIL (Image), numpy, matplotlib, h5py, scipy
```

please use
```
pip install -r requirements.txt
```
to install all dependencies

### Generating samples

```
Expand Down
42 changes: 21 additions & 21 deletions colorize3_poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, col_file):
# convert color-means from RGB to LAB for better nearest neighbour
# computations:
self.colorsLAB = np.r_[self.colorsRGB[:,0:3], self.colorsRGB[:,6:9]].astype('uint8')
self.colorsLAB = np.squeeze(cv.cvtColor(self.colorsLAB[None,:,:],cv.cv.CV_RGB2Lab))
self.colorsLAB = np.squeeze(cv.cvtColor(self.colorsLAB[None,:,:],cv.COLOR_RGB2Lab))


def sample_normal(self, col_mean, col_std):
Expand All @@ -70,7 +70,7 @@ def sample_from_data(self, bg_mat):
each of these is a 3-vector.
"""
bg_orig = bg_mat.copy()
bg_mat = cv.cvtColor(bg_mat, cv.cv.CV_RGB2Lab)
bg_mat = cv.cvtColor(bg_mat, cv.COLOR_RGB2Lab)
bg_mat = np.reshape(bg_mat, (np.prod(bg_mat.shape[:2]),3))
bg_mean = np.mean(bg_mat,axis=0)

Expand All @@ -92,10 +92,10 @@ def sample_from_data(self, bg_mat):
return (col1, col2)

def mean_color(self, arr):
col = cv.cvtColor(arr, cv.cv.CV_RGB2HSV)
col = cv.cvtColor(arr, cv.COLOR_RGB2HSV)
col = np.reshape(col, (np.prod(col.shape[:2]),3))
col = np.mean(col,axis=0).astype('uint8')
return np.squeeze(cv.cvtColor(col[None,None,:],cv.cv.CV_HSV2RGB))
return np.squeeze(cv.cvtColor(col[None,None,:],cv.COLOR_HSV2RGB))

def invert(self, rgb):
rgb = 127 + rgb
Expand All @@ -105,34 +105,34 @@ def complement(self, rgb_color):
"""
return a color which is complementary to the RGB_COLOR.
"""
col_hsv = np.squeeze(cv.cvtColor(rgb_color[None,None,:], cv.cv.CV_RGB2HSV))
col_hsv = np.squeeze(cv.cvtColor(rgb_color[None,None,:], cv.COLOR_RGB2HSV))
col_hsv[0] = col_hsv[0] + 128 #uint8 mods to 255
col_comp = np.squeeze(cv.cvtColor(col_hsv[None,None,:],cv.cv.CV_HSV2RGB))
col_comp = np.squeeze(cv.cvtColor(col_hsv[None,None,:],cv.COLOR_HSV2RGB))
return col_comp

def triangle_color(self, col1, col2):
"""
Returns a color which is "opposite" to both col1 and col2.
"""
col1, col2 = np.array(col1), np.array(col2)
col1 = np.squeeze(cv.cvtColor(col1[None,None,:], cv.cv.CV_RGB2HSV))
col2 = np.squeeze(cv.cvtColor(col2[None,None,:], cv.cv.CV_RGB2HSV))
col1 = np.squeeze(cv.cvtColor(col1[None,None,:], cv.COLOR_RGB2HSV))
col2 = np.squeeze(cv.cvtColor(col2[None,None,:], cv.COLOR_RGB2HSV))
h1, h2 = col1[0], col2[0]
if h2 < h1 : h1,h2 = h2,h1 #swap
dh = h2-h1
if dh < 127: dh = 255-dh
col1[0] = h1 + dh/2
return np.squeeze(cv.cvtColor(col1[None,None,:],cv.cv.CV_HSV2RGB))
return np.squeeze(cv.cvtColor(col1[None,None,:],cv.COLOR_HSV2RGB))

def change_value(self, col_rgb, v_std=50):
col = np.squeeze(cv.cvtColor(col_rgb[None,None,:], cv.cv.CV_RGB2HSV))
col = np.squeeze(cv.cvtColor(col_rgb[None,None,:], cv.COLOR_RGB2HSV))
x = col[2]
vs = np.linspace(0,1)
ps = np.abs(vs - x/255.0)
ps /= np.sum(ps)
v_rand = np.clip(np.random.choice(vs,p=ps) + 0.1*np.random.randn(),0,1)
col[2] = 255*v_rand
return np.squeeze(cv.cvtColor(col[None,None,:],cv.cv.CV_HSV2RGB))
return np.squeeze(cv.cvtColor(col[None,None,:],cv.COLOR_HSV2RGB))


class Colorize(object):
Expand Down Expand Up @@ -253,7 +253,7 @@ def color_border(self, col_text, col_bg):
"""
choice = np.random.choice(3)

col_text = cv.cvtColor(col_text, cv.cv.CV_RGB2HSV)
col_text = cv.cvtColor(col_text, cv.COLOR_RGB2HSV)
col_text = np.reshape(col_text, (np.prod(col_text.shape[:2]),3))
col_text = np.mean(col_text,axis=0).astype('uint8')

Expand All @@ -268,24 +268,24 @@ def get_sample(x):
if choice==0:
# increase/decrease saturation:
col_text[0] = get_sample(col_text[0]) # saturation
col_text = np.squeeze(cv.cvtColor(col_text[None,None,:],cv.cv.CV_HSV2RGB))
col_text = np.squeeze(cv.cvtColor(col_text[None,None,:],cv.COLOR_HSV2RGB))
elif choice==1:
# get the complementary color to text:
col_text = np.squeeze(cv.cvtColor(col_text[None,None,:],cv.cv.CV_HSV2RGB))
col_text = np.squeeze(cv.cvtColor(col_text[None,None,:],cv.COLOR_HSV2RGB))
col_text = self.font_color.complement(col_text)
else:
# choose a mid-way color:
col_bg = cv.cvtColor(col_bg, cv.cv.CV_RGB2HSV)
col_bg = cv.cvtColor(col_bg, cv.COLOR_RGB2HSV)
col_bg = np.reshape(col_bg, (np.prod(col_bg.shape[:2]),3))
col_bg = np.mean(col_bg,axis=0).astype('uint8')
col_bg = np.squeeze(cv.cvtColor(col_bg[None,None,:],cv.cv.CV_HSV2RGB))
col_text = np.squeeze(cv.cvtColor(col_text[None,None,:],cv.cv.CV_HSV2RGB))
col_bg = np.squeeze(cv.cvtColor(col_bg[None,None,:],cv.COLOR_HSV2RGB))
col_text = np.squeeze(cv.cvtColor(col_text[None,None,:],cv.COLOR_HSV2RGB))
col_text = self.font_color.triangle_color(col_text,col_bg)

# now change the VALUE channel:
col_text = np.squeeze(cv.cvtColor(col_text[None,None,:],cv.cv.CV_RGB2HSV))
col_text = np.squeeze(cv.cvtColor(col_text[None,None,:],cv.COLOR_RGB2HSV))
col_text[2] = get_sample(col_text[2]) # value
return np.squeeze(cv.cvtColor(col_text[None,None,:],cv.cv.CV_HSV2RGB))
return np.squeeze(cv.cvtColor(col_text[None,None,:],cv.COLOR_HSV2RGB))

def color_text(self, text_arr, h, bg_arr):
"""
Expand Down Expand Up @@ -393,8 +393,8 @@ def check_perceptible(self, txt_mask, bg, txt_bg):
"""
bgo,txto = bg.copy(), txt_bg.copy()
txt_mask = txt_mask.astype('bool')
bg = cv.cvtColor(bg.copy(), cv.cv.CV_RGB2Lab)
txt_bg = cv.cvtColor(txt_bg.copy(), cv.cv.CV_RGB2Lab)
bg = cv.cvtColor(bg.copy(), cv.COLOR_RGB2Lab)
txt_bg = cv.cvtColor(txt_bg.copy(), cv.COLOR_RGB2Lab)
bg_px = bg[txt_mask,:]
txt_px = txt_bg[txt_mask,:]
bg_px[:,0] *= 100.0/255.0 #rescale - L channel
Expand Down
13 changes: 13 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pygame==1.9.3
opencv-python==3.4.2.17
Pillow==5.0.0
numpy==1.13.3
matplotlib==2.1.1
h5py==2.7.1
scipy==1.0.0
cycler==0.10.0
pyparsing==2.2.0
python-dateutil==2.6.1
pytz==2017.3
six==1.11.0
wget==3.2
16 changes: 9 additions & 7 deletions synthgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from PIL import Image
import numpy as np
#import mayavi.mlab as mym
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import os.path as osp
import scipy.ndimage as sim
Expand Down Expand Up @@ -78,7 +80,7 @@ def filter(seg,area,label):

coords = np.c_[xs,ys].astype('float32')
rect = cv2.minAreaRect(coords)
box = np.array(cv2.cv.BoxPoints(rect))
box = np.array(cv2.boxPoints(rect))
h,w,rot = TextRegions.get_hw(box,return_rot=True)

f = (h > TextRegions.minHeight
Expand Down Expand Up @@ -215,9 +217,9 @@ def get_text_placement_mask(xyz,mask,plane,pad=2,viz=False):
REGION : DICT output of TextRegions.get_regions
PAD : number of pixels to pad the placement-mask by
"""
contour,hier = cv2.findContours(mask.copy().astype('uint8'),
mode=cv2.cv.CV_RETR_CCOMP,
method=cv2.cv.CV_CHAIN_APPROX_SIMPLE)
image,contour,hier = cv2.findContours(mask.copy().astype('uint8'),
mode=cv2.RETR_CCOMP,
method=cv2.CHAIN_APPROX_SIMPLE)
contour = [np.squeeze(c).astype('float') for c in contour]
#plane = np.array([plane[1],plane[0],plane[2],plane[3]])
H,W = mask.shape[:2]
Expand All @@ -236,7 +238,7 @@ def get_text_placement_mask(xyz,mask,plane,pad=2,viz=False):

# unrotate in 2D plane:
rect = cv2.minAreaRect(pts_fp[0].copy().astype('float32'))
box = np.array(cv2.cv.BoxPoints(rect))
box = np.array(cv2.boxPoints(rect))
R2d = su.unrotate2d(box.copy())
box = np.vstack([box,box[0,:]]) #close the box for visualization

Expand All @@ -260,7 +262,7 @@ def get_text_placement_mask(xyz,mask,plane,pad=2,viz=False):

pts_fp_i32 = [(pts_fp[i]+minxy[None,:]).astype('int32') for i in xrange(len(pts_fp))]
cv2.drawContours(place_mask,pts_fp_i32,-1,0,
thickness=cv2.cv.CV_FILLED,
thickness=cv2.FILLED,
lineType=8,hierarchy=hier)

if not TextRegions.filter_rectified((~place_mask).astype('float')/255):
Expand Down Expand Up @@ -559,7 +561,7 @@ def char2wordBB(self, charBB, text):
# change shape from 2x4xn_i -> (4*n_i)x2
cc = np.squeeze(np.concatenate(np.dsplit(cc,cc.shape[-1]),axis=1)).T.astype('float32')
rect = cv2.minAreaRect(cc.copy())
box = np.array(cv2.cv.BoxPoints(rect))
box = np.array(cv2.boxPoints(rect))

# find the permutation of box-coordinates which
# are "aligned" appropriately with the character-bb.
Expand Down
2 changes: 2 additions & 0 deletions visualize_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import os
import os.path as osp
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import h5py
from common import *
Expand Down