Skip to content

Instantly share code, notes, and snippets.

@twmht
Created August 31, 2016 09:54
Show Gist options
  • Save twmht/bc1a4e7634f28111b1107c19b98176d5 to your computer and use it in GitHub Desktop.
Save twmht/bc1a4e7634f28111b1107c19b98176d5 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 6 10:13:27 2016
@author: gene
"""
import xml.etree.cElementTree as ET
import Image
import os, glob
#filename = "000001"
#width = 500
#height = 353
#depth = 3
#classlabel = "Gunkanmaki"
#xmin = 0
#ymin = 0
#xmax = width
#ymax = height
def CreateTrainData(imgroot, max_wsize, max_hsize, degrees_set, xmin, ymin, xmax, ymax):
# Check the output folders is ready or not
VOCroot = "VOCdevkit2007/VOC2007/"
Res_path = "VOCdevkit2007/results/VOC2007/Main"
Ann_path = VOCroot+"Annotations/"
Set_path = VOCroot+"ImageSets/Main/"
Jpg_path = VOCroot+"JPEGImages/"
Tag_path = 'VOC_XML_Labeling/data/'
if not os.path.exists(VOCroot):
os.makedirs(VOCroot)
if not os.path.exists(Res_path):
os.makedirs(Res_path)
if not os.path.exists(Ann_path):
os.makedirs(Ann_path)
if not os.path.exists(Set_path):
os.makedirs(Set_path)
if not os.path.exists(Jpg_path):
os.makedirs(Jpg_path)
if not os.path.exists(Tag_path):
os.makedirs(Tag_path)
# Search each folder
classN = 0
counter = 0
# Clear Labeling Tags
with open(Tag_path+"predefined_classes.txt","a") as file:
file.write("")
for folder_name in os.listdir(imgroot):
classN += 1
print '{} {}'.format(classN, folder_name)
# Create Labeling Tags
with open(Tag_path+"predefined_classes.txt","a") as file:
file.write(folder_name+"\n")
# Each image
for image_path in glob.glob(imgroot+folder_name+"/*.JPG"):
image_obj = ImageResizeNCrop(image_path, max_wsize, max_hsize)
# Image Rotation
for degrees in degrees_set:
counter += 1
# Create xml of the image
filename = '{:06d}'.format(counter)
tree = VOCxml(filename, image_obj, folder_name, xmin, ymin, xmax, ymax)
tree.write(Ann_path + filename + ".xml")
# Save the image list of training
with open(Set_path+"trainval.txt","a") as file:
file.write(filename+"\n")
with open(Set_path+"test.txt","a") as file:
file.write(filename+"\n")
# Rotate image and save it
image_obj.rotate(degrees).save( Jpg_path+filename+'.jpg'.format(counter), "jpeg" )
return classN
# Create the xml info of the image
def VOCxml(filename, image_obj, classlabel, xmin, ymin, xmax, ymax, font_type ):
width, height = image_obj.size
depth = 3
#xmin = width/4
#ymin = height/4
#xmax = (width/4)*3
#ymax = (width/4)*3
root = ET.Element("annotation")
ET.SubElement(root, "folder").text = "VOC2007"
ET.SubElement(root, "filename").text = filename+".jpg"
node1 = ET.SubElement(root, "source")
ET.SubElement(node1, "database").text = "The VOC2007 Database"
ET.SubElement(node1, "annotation").text = "PASCAL VOC2007"
ET.SubElement(node1, "image").text = "Acer"
ET.SubElement(node1, "flickrid").text = "no"
ET.SubElement(node1, "font_type").text = font_type
node2 = ET.SubElement(root, "owner")
ET.SubElement(node2, "flickrid").text = "Acer"
ET.SubElement(node2, "name").text = "KR7800"
node3 = ET.SubElement(root, "size")
ET.SubElement(node3, "width").text = str(height)
ET.SubElement(node3, "height").text = str(width)
ET.SubElement(node3, "depth").text = str(depth)
ET.SubElement(root, "segmented").text = "0"
node4 = ET.SubElement(root, "object")
ET.SubElement(node4, "name").text = classlabel
ET.SubElement(node4, "pose").text = "unknow"
ET.SubElement(node4, "truncated").text = "1"
ET.SubElement(node4, "difficult").text = "0"
node5 = ET.SubElement(node4, "bndbox")
ET.SubElement(node5, "xmin").text = str(xmin)
ET.SubElement(node5, "ymin").text = str(ymin)
ET.SubElement(node5, "xmax").text = str(xmax)
ET.SubElement(node5, "ymax").text = str(ymax)
return ET.ElementTree(root)
# Resize the image and crop
def ImageResizeNCrop(image_path, max_wsize, max_hsize):
image_obj = Image.open(image_path)
img_width,img_height = image_obj.size
# Calculate the resize rate by short edge
resize_rate = max_hsize / min(img_width,img_height)
img_width = int(img_width * resize_rate)
img_height = int(img_height * resize_rate)
# Crop the
cropx = int((img_width -max_wsize)/2)
cropy = int((img_height-max_hsize)/2)
image_obj.thumbnail( (img_width, img_height), Image.ANTIALIAS)
image_obj = image_obj.crop( ( cropx, cropy, int(max_wsize)+cropx, int(max_hsize)+cropy) )
return image_obj
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 5 13:47:01 2016
@author: gene
"""
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 2 11:55:25 2016
@author: gene
"""
import cv2
from PIL import Image as PIL_Image
import math
import Image
import ImageFont, ImageDraw, ImageOps
import numpy as np
import matplotlib.pyplot as plt
from skimage import data, img_as_float, io, color
from skimage.restoration import denoise_tv_chambolle, denoise_bilateral
from skimage.transform import ProjectiveTransform
from skimage.transform import warp, AffineTransform
from skimage import util
from noise_map import *
import sys, glob
from Any2VOC_function import *
def find_coeffs(pa, pb):
matrix = []
for p1, p2 in zip(pa, pb):
matrix.append([p1[0], p1[1], 1, 0, 0, 0, -p2[0]*p1[0], -p2[0]*p1[1]])
matrix.append([0, 0, 0, p1[0], p1[1], 1, -p2[1]*p1[0], -p2[1]*p1[1]])
A = np.matrix(matrix, dtype=np.float)
B = np.array(pb).reshape(8)
res = np.dot(np.linalg.inv(A.T * A) * A.T, B)
return np.array(res).reshape(8)
def rand_degree(st,en,gap):
return (np.fix(np.random.random()* (en-st) * gap )+st)
def img_transform(img):
width, height = img.size
m = -0.5
xshift = abs(m) * width
new_width = width + int(round(xshift))
img = img.transform((new_width, height), PIL_Image.AFFINE,
(1, m, -xshift if m > 0 else 0, 0, 1, 0), PIL_Image.BICUBIC)
range_n = 250
gap_n = 1
x1 = rand_degree(0,range_n,gap_n)
y1 = rand_degree(0,range_n,gap_n)
x2 = rand_degree(width-range_n,width,gap_n)
y2 = rand_degree(0,range_n,gap_n)
x3 = rand_degree(width-range_n,width,gap_n)
y3 = rand_degree(height-range_n,height,gap_n)
x4 = rand_degree(0,range_n,gap_n)
y4 = rand_degree(height-range_n,height,gap_n)
coeffs = find_coeffs(
[(x1, y1), (x2, y2), (x3, y3), (x4, y4)],
[(0, 0), (width, 0), (new_width, height), (xshift, height)])
img = img.transform((width, height), PIL_Image.PERSPECTIVE, coeffs, PIL_Image.BICUBIC)
return img
def random_space(min_num,max_num):
n_space = int(np.fix(np.random.random()*(max_num-min_num+1))+min_num)
space = ""
for qww in range(0,n_space,1) :
space = space + " "
return space
def gen_code():
E_code = ""
N_code = ""
check_sum=0
E_code_book = {"A": 10, "B": 12, "C": 13, "D": 14, "E": 15, "F": 16, "G": 17, "H": 18, "I": 19,
"J": 20, "K": 21, "L": 23, "M": 24, "N": 25, "O": 26, "P": 27, "Q": 28, "R": 29,
"S": 30, "T": 31, "U": 32, "V": 34, "W": 35, "X": 36, "Y": 37, "Z": 38}
# CATEGORY_INDENTIFIER
CI = ['EGHU', 'EGSU', 'EISU', 'EMCU', 'HMCU', 'PCIU', 'DRYU', 'EITU', 'WHLU', 'TCNU', 'IMTU', 'KKFU']
E_weight = [1, 2, 4, 8]
N_weight = [16, 32, 64, 128, 256, 512]
CI_n = int(np.fix(np.random.random()*12))
for En_n in range(0,4):
En = CI[CI_n][En_n]
check_sum = check_sum + E_code_book[En] * E_weight[En_n]
E_code = E_code + str(En)
for Nu_n in range(0,6):
Nu = int(np.fix(np.random.random()*10)+0)
check_sum = check_sum + Nu * N_weight[Nu_n]
N_code = N_code + str(Nu)
check_sum = check_sum/11.0+0.09
check_code = int(np.fix( (check_sum - np.fix(check_sum)) * 10 ))
return(E_code, N_code, str(check_code))
def gen_img(filename, font_list, bg_list):
# Shipping Cantanter Code = SCC
fontcolor = (255,255,255)
fontsize = 180
# padding rate for setting the image size of font
fimg_padding = 1.1
# check code bbox padding rate
bbox_gap = fontsize * 0.05
# Rrotation +- N degree
rotation = 20
#--------------------------------------------------------------------------
#---Generate the Shipping Cantanter Code---
C_code, N_code, check = gen_code()
#--------------------------------------------------------------------------
#---Add space between cdoes---
code = C_code + random_space(1,3) + N_code + random_space(1,3)
#--------------------------------------------------------------------------
#---Choice a font type for output---
font_no = int(np.fix(np.random.random()*10))
font_path = font_list[font_no]
font = ImageFont.truetype(font_path, fontsize)
#--------------------------------------------------------------------------
#---Get the related info of font---
code_w, code_h = font.getsize(code)
check_w, check_h = font.getsize(check)
#--------------------------------------------------------------------------
#---Setting the image size of font---
img_size = int((code_w+check_w) * fimg_padding)
#--------------------------------------------------------------------------
#---Create a RGBA image for shipping cantanter code
img = Image.new("RGBA", (img_size,img_size),fontcolor+(0,))
d = ImageDraw.Draw(img)
#--------------------------------------------------------------------------
#---Draw container code on RGBimage---
code_x = (img_size-code_w-check_w)/2
code_y = (img_size-code_h-check_h)/2
d.text( ( code_x, code_y ), code, fontcolor, font=font)
#--------------------------------------------------------------------------
#---Draw check code with bbox on RGBimage---
check_x = code_x + code_w
check_y = code_y
d.text( ( check_x, check_y ), check, fontcolor, font=font)
d = ImageDraw.Draw(img)
d = ImageDraw.Draw(img)
#--------------------------------------------------------------------------
#---Draw bbox of the check code---
alpha = img.split()[-1]
real_w, real_h = img.crop(alpha.getbbox()).size
bbox_x1 = int(check_x - bbox_gap )
bbox_y1 = int(check_y - bbox_gap + (check_h-real_h) )
bbox_x2 = int(check_x + check_w + bbox_gap)
bbox_y2 = int(check_y + check_h + bbox_gap)
d.line((bbox_x1, bbox_y1, bbox_x1, bbox_y2), fill=(255,255,255,255), width=4)
d.line((bbox_x1, bbox_y1, bbox_x2, bbox_y1), fill=(255,255,255,255), width=4)
d.line((bbox_x1, bbox_y2, bbox_x2, bbox_y2), fill=(255,255,255,255), width=4)
d.line((bbox_x2, bbox_y1, bbox_x2, bbox_y2), fill=(255,255,255,255), width=4)
#--------------------------------------------------------------------------
#---Imagre rotation---
#degree = np.fix(np.random.random()*rotation*2)+1-rotation
#degree = 0
#img = img.rotate( degree , Image.BILINEAR)
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------
# +++++++++ noise map +++++++++
img = add_noise(img, output_size)
#--------------------------------------------------------------------------
#---Transform the image---
img = img_transform(img)
#--------------------------------------------------------------------------
#---Crop min image---
alpha = img.split()[-1]
img = img.crop(alpha.getbbox())
#--------------------------------------------------------------------------
#---Add background for the SCC---
bg_no = int(np.fix(np.random.random()*65))
bg = Image.open(bg_list[bg_no]).convert("RGBA")
bg_w, bg_h = bg.size
# resize the SCC image for background
paste_size = int( min((bg_w, bg_h)) * (0.7 + (np.random.random()*2 -1) * 0.1 ) )
img.thumbnail((paste_size,paste_size), Image.ANTIALIAS)
# paste SSC to background
img_w, img_h = img.size
allow_w = bg_w - img_w - 20
allow_h = bg_h - img_h - 20
paste_x = int(np.fix(np.random.random()*allow_w)+1) + 10
paste_y = int(np.fix(np.random.random()*allow_h)+1) + 10
bg.paste(img, (paste_x, paste_y), img)
#--------------------------------------------------------------------------
#---Create .xml of image---
tree = VOCxml( filename +'.jpg', bg, 'number', paste_x, paste_y, paste_x+img_w, paste_y+img_h, font_path )
tree.write('./Annotations/'+ filename + ".xml")
#--------------------------------------------------------------------------
#---Save image (No noise version)---
#bg.save('./JPEGImages/'+ filename +'.jpg','JPEG', quality=90)
#--------------------------------------------------------------------------
#---Save image (with noise)---
bg = img_as_float(bg)
mean = np.random.uniform(0, 0.003)
var = np.random.uniform(0.00001, 0.0005)
bg = util.random_noise(bg, mode='gaussian', mean=mean, var=var)
io.imsave('./JPEGImages/'+ filename +'.jpg', bg, quality=90)
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------
def give_me_SCC_images(img_num):
font_list = glob.glob('./Fonts/*.*')
bg_list = glob.glob('./background/*.*')
if os.path.isfile('./trainval.txt'):
os.remove("./trainval.txt")
for code_no in range(1, img_num+1):
filename = '{:07d}'.format(code_no)
print filename
with open("./trainval.txt","a") as file:
file.write(filename+"\n")
with open("./test.txt","a") as file:
file.write(filename+"\n")
gen_img(filename, font_list, bg_list)
import random, numpy, math, time
import matplotlib.pyplot as plt
import cv2
from PIL import Image,ImageEnhance,ImageFilter
#-----------------Setting----------------------
im_size = 80
perm = range(256)
random.shuffle(perm)
perm += perm
dirs = [(math.cos(a * 1 * math.pi / 256),
math.sin(a * 20 * math.pi / 256))
for a in range(256)]
#----------------------------------------------
def noise(x, y, per):
def surflet(gridX, gridY):
distX, distY = abs(x-gridX), abs(y-gridY)
polyX = 1 - 6*distX**5 + 15*distX**4 - 10*distX**3
polyY = 1 - 6*distY**5 + 15*distY**4 - 10*distY**3
hashed = perm[perm[int(gridX)%per] + int(gridY)%per]
grad = (x-gridX)*dirs[hashed][0] + (y-gridY)*dirs[hashed][1]
return polyX * polyY * grad
intX, intY = int(x), int(y)
return (surflet(intX+0, intY+0) + surflet(intX+1, intY+0) +
surflet(intX+0, intY+1) + surflet(intX+1, intY+1))
def fBm(x, y, per, octs):
val = 0
for o in range(octs):
val += 0.5**o * noise(x*2**o, y*2**o, per*2**o)
return val
def gen_noise_map(output_size):
size, freq, octs, data = im_size, 1/2.005, 2, []
for y in range(size):
for x in range(size):
data.append(fBm(x*freq, y*freq, int(size*freq), octs))
im_noise = Image.new("L", (size, size))
im_noise.putdata(data, im_size, im_size)
#contrast = ImageEnhance.Contrast(im_noise)
#im_noise = contrast.enhance(3.0)
#im_noise.save("C:\\Users\\1510081\\Desktop\\Noise\\noise.png")
np_noise = numpy.asarray(im_noise)
np_noise = cv2.medianBlur(np_noise, 9)
#np_noise = 255 - np_noise
img = Image.fromarray(np_noise)
contrast = ImageEnhance.Contrast(img)
im_noise = contrast.enhance( numpy.random.rand()*30+3 )
np_noise = numpy.asarray(im_noise).astype(float)
np_noise = (abs((np_noise - np_noise.mean())*2))
#np_noise = 255 - np_noise
im_noise = Image.fromarray(np_noise).convert('L')
im_noise = im_noise.resize( (output_size, output_size), Image.BILINEAR )
contrast = ImageEnhance.Contrast(im_noise)
im_noise = contrast.enhance(1.2)
return im_noise
def add_noise(img, output_size):
T_bg = Image.new("RGBA", (img.size), (255,255,255,0,) )
noise_map = gen_noise_map(max(img.size))
return Image.composite(T_bg, img, noise_map)
#im_noise.save("C:\\Users\\1510081\\Desktop\\Noise\\noiseXD.png")
#
#tStart = time.time()
#im_noise = gen_noise_map(512)
#im_noise.save("C:\\Users\\1510081\\Desktop\\Noise\\noiseXD.png")
#tEnd = time.time()
#print tEnd - tStart
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 8 11:05:02 2016
@author: gene
"""
from give_me_the_code import *
from noise_map import *
#im = gen_noise_map(512)
give_me_SCC_images(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment