Esempio n. 1
0
def crack(file):
    img = Image.open(file.stream)
    image = np.array(img)
    image = img2vec(img2gray(image))
    y_ = sess.run([prediction], feed_dict={x: [image]})
    result = "".join([str(s) for s in y_[0][0]])
    return result
Esempio n. 2
0
def scan(file):
    img = Image.open(file.stream)
    image = np.array(img)
    image = utils.img2gray(image)
    image = utils.clearImgGray(image)    
    utils.save(image, os.path.join(curr_dir,"test","src.png"))
    split_images = utils.splitImg(image)
    
    ocr_texts = []

    for i, split_image in enumerate(split_images):
        # image = utils.img2bwinv(split_image)
        image = utils.clearImgGray(split_image)
        # image = utils.clearBackgroundColor(image, 255)    
        image = 255. - image
        image = utils.dropZeroEdges(image)  
        image = utils.resize(image, ocr.image_height)
        utils.save(image,os.path.join(curr_dir,"test","%s.png"%i))
        image = image / 255.
        maxImageWidth = image.shape[1]
        maxImageWidth = maxImageWidth + (ocr.POOL_SIZE - maxImageWidth % ocr.POOL_SIZE)
        image_vec = utils.img2vec(image,ocr.image_height,maxImageWidth)
        ocr_inputs = np.zeros([1, maxImageWidth, ocr.image_height])
        ocr_inputs[0,:] = np.transpose(image_vec.reshape((ocr.image_height,maxImageWidth)))         
        ocr_seq_len = np.ones(ocr_inputs.shape[0]) * (maxImageWidth * ocr.image_height) // (ocr.POOL_SIZE * ocr.POOL_SIZE)
        feed = {inputs: ocr_inputs, seq_len: ocr_seq_len,  input_keep_prob: 1.0}
        start = time.time()
        decoded_list = session.run(decoded[0], feed)
        seconds = round(time.time() - start,2)
        print("filished ocr %s , paid %s seconds" % (i,seconds))
        detected_list = ocr.decode_sparse_tensor(decoded_list)            
        for detect_number in detected_list:
            ocr_texts.append(ocr.list_to_chars(detect_number))

    return ocr_texts
Esempio n. 3
0
def get_batch(batch_size=128):
    batch_x = np.zeros([batch_size, image_size])
    batch_y = np.zeros([batch_size, captcha_size])
    for i in range(batch_size):
        text, image = captcha(
            char_set=char_set, captcha_size=captcha_size, width=image_w, height=image_h)
        batch_x[i, :] = img2vec(img2gray(image))
        batch_y[i, :] = list(text)  # 注意 这里的 lable 不能 one hot
    return batch_x, batch_y
Esempio n. 4
0
def scan(file):
    img = Image.open(file.stream)
    image = np.array(img)
    image = utils.img2gray(image)
    utils.save(image * 255, os.path.join(curr_dir, "test", "p0.png"))
    # image = utils.clearImgGray(image)
    # utils.save(image * 255, os.path.join(curr_dir,"test","p1.png"))
    split_images = utils.splitImg(image)

    ocr_texts = []

    for i, split_image in enumerate(split_images):
        inv_image = utils.img2bwinv(split_image)
        inv_image = utils.clearImg(inv_image)
        image = 255. - split_image
        image = utils.dropZeroEdges(inv_image, image)
        image = utils.resize(image, ocr.image_height)
        image = image / 255.
        ocr_inputs = np.zeros([1, ocr.image_size, ocr.image_size])
        ocr_inputs[0, :] = utils.square_img(
            image, np.zeros([ocr.image_size, ocr.image_size]))

        ocr_seq_len = np.ones(1) * (ocr.image_size * ocr.image_size) // (
            ocr.POOL_SIZE * ocr.POOL_SIZE)

        start = time.time()
        p_net_g = session.run(net_g, {inputs: ocr_inputs})
        p_net_g = np.squeeze(p_net_g, axis=3)

        debug_net_g = np.copy(p_net_g)
        for j in range(1):
            _t_img = utils.unsquare_img(p_net_g[j], ocr.image_height)
            _t_img_bin = np.copy(_t_img)
            _t_img_bin[_t_img_bin <= 0.2] = 0
            _t_img = utils.dropZeroEdges(_t_img_bin, _t_img, min_rate=0.1)
            _t_img = utils.resize(_t_img, ocr.image_height)
            if _t_img.shape[0] * _t_img.shape[
                    1] <= ocr.image_size * ocr.image_size:
                p_net_g[j] = utils.square_img(
                    _t_img, np.zeros([ocr.image_size, ocr.image_size]),
                    ocr.image_height)

        _img = np.vstack((ocr_inputs[0], debug_net_g[0], p_net_g[0]))
        utils.save(_img * 255, os.path.join(curr_dir, "test", "%s.png" % i))

        decoded_list = session.run(res_decoded[0], {
            inputs: p_net_g,
            seq_len: ocr_seq_len
        })
        seconds = round(time.time() - start, 2)
        print("filished ocr %s , paid %s seconds" % (i, seconds))
        detected_list = utils.decode_sparse_tensor(decoded_list)
        for detect_number in detected_list:
            ocr_texts.append(ocr.list_to_chars(detect_number))

    return ocr_texts
Esempio n. 5
0
def getImage(text, font_name, font_length, font_size, noise=False, fontmode=None, fonthint=None):
    params= {}
    params['text'] = text
    params['fontname'] = font_name
    params['fontsize'] = font_size
    # params['fontmode'] = random.choice([0,1,2,4,8])
    if fontmode == None:
        params['fontmode'] = random.choice([0,1,2,4])
    else:
        params['fontmode'] = fontmode
    if fonthint == None:
        params['fonthint'] = random.choice([0,1,2,3,4,5])
    else:
        params['fonthint'] = fonthint
    
    r = http('http://192.168.2.113:8888/',params)
    _img = Image.open(io.BytesIO(r))
    img=Image.new("RGB",_img.size,(255,255,255))
    img.paste(_img,(0,0),_img)
    img = utils.trim(img)
    
    if noise:
        w,h = img.size
        _h = random.randint(9, image_height)
        _w = round(w * _h / h)
        img = img.resize((_w,_h), Image.ANTIALIAS)
        img = np.asarray(img)
        img = 1 - utils.img2gray(img)/255.   
        img = utils.dropZeroEdges(img)

        filter = np.random.random(img.shape) - 0.9
        filter = np.maximum(filter, 0) 
        img = img + filter * 5
        imin, imax = img.min(), img.max()
        img = (img - imin)/(imax - imin)
    else:
        img = np.asarray(img)
        img = utils.img2gray(img) 
        img = utils.img2bwinv(img)
        img = img / 255.
        img = utils.dropZeroEdges(img)
    return img
Esempio n. 6
0
def get_batch(batch_size=128):
    batch_x = np.zeros([batch_size, image_size])
    batch_y = np.zeros([batch_size, char_size])
    for i in range(batch_size):
        text, image = captcha(char_set=char_set,
                              captcha_size=1,
                              width=image_w,
                              height=image_h)
        batch_x[i, :] = img2vec(img2gray(image))
        batch_y[i, :] = text2vec(char_set, text)
    return batch_x, batch_y
Esempio n. 7
0
def get_batch(batch_size=128):
    batch_files = random.sample(files, batch_size)

    batch_x = np.zeros([batch_size, image_size])
    batch_y = np.zeros([batch_size, captcha_size * char_size])
    for i in range(batch_size):
        image = loadimg(os.path.join(fonts_dir, batch_files[i]))
        text = batch_files[i][:4]
        batch_x[i, :] = img2vec(img2gray(image))
        batch_y[i, :] = text2vec(char_set, text)
    return batch_x, batch_y
def getImage(CHARS, font_name, image_height, font_length, font_size,
             word_dict):
    text = ''
    n = random.random()
    if n < 0.1:
        for i in range(font_length):
            text += random.choice(
                "123456789012345678901234567890-./$,:()+-*=><")
    elif n < 0.5 and n >= 0.1:
        for i in range(font_length):
            text += random.choice(CHARS)
    else:
        while len(text) < font_length:
            word = random.choice(word_dict)
            _word = ""
            for c in word:
                if c in CHARS:
                    _word += c
            text = text + " " + _word.strip()
    text = text.strip()

    params = {}
    params['text'] = text
    params['fontname'] = font_name
    params['fontsize'] = font_size
    # params['fontmode'] = random.choice([0,1,2,4,8])
    params['fontmode'] = random.choice([0, 1, 2, 4])
    params['fonthint'] = random.choice([0, 1, 2, 3, 4, 5])

    r = http('http://192.168.2.113:8888/', params)
    _img = Image.open(io.BytesIO(r))
    img = Image.new("RGB", _img.size, (255, 255, 255))
    img.paste(_img, (0, 0), _img)
    img = utils.trim(img)
    w, h = img.size
    _h = random.randint(9, 64)
    _w = round(w * _h / h)
    img = img.resize((_w, _h), Image.ANTIALIAS)
    img = np.asarray(img)
    img = utils.clearBackgroundColor(img)
    img = 1 - utils.img2gray(img) / 255.
    img = utils.dropZeroEdges(img)

    filter = np.random.random(img.shape) - 0.9
    filter = np.maximum(filter, 0)
    img = img + filter * 5
    imin, imax = img.min(), img.max()
    img = (img - imin) / (imax - imin)

    img = utils.resize(img, image_height)
    return text, img
Esempio n. 9
0
def get_next_batch(batch_size=128):
    images = []
    to_images = []
    max_width_image = 0
    font_min_length = random.randint(10, 20)
    for i in range(batch_size):
        font_name = random.choice(FontNames)
        font_length = random.randint(font_min_length - 5, font_min_length + 5)
        font_size = random.randint(9, 64)
        text, image = utils.getImage(CHARS, font_name, image_height,
                                     font_length, font_size, eng_world_list)
        images.append(image)
        if image.shape[1] > max_width_image:
            max_width_image = image.shape[1]
        to_image = utils.renderNormalFontByPIL(ConsolasFont, 64, text)
        to_image = utils.trim(to_image)

        w, h = to_image.size
        _w = round(w * image_height / h)
        _h = image_height
        if _w > max_width_image:
            _w = max_width_image
            _h = round(h * max_width_image / w)

        to_image = to_image.resize((_w, _h), Image.ANTIALIAS)
        to_image = np.asarray(to_image)
        #to_image=utils.resize(to_image, height=image_height)
        to_image = utils.img2gray(to_image)
        to_image = to_image / 255
        to_images.append(to_image)

    inputs = np.zeros([batch_size, max_width_image, image_height])
    for i in range(len(images)):
        image_vec = utils.img2vec(images[i],
                                  height=image_height,
                                  width=max_width_image,
                                  flatten=False)
        inputs[i, :] = np.transpose(image_vec)

    labels = np.zeros([batch_size, max_width_image, image_height])
    for i in range(len(to_images)):
        image_vec = utils.img2vec(to_images[i],
                                  height=image_height,
                                  width=max_width_image,
                                  flatten=False)
        labels[i, :] = np.transpose(image_vec)
    return inputs, labels
Esempio n. 10
0
checkpoint_prefix = os.path.join(log_dir, "model.ckpt")

# 找到最新的运算模型文件
metaFile= sorted(
    [
        (x, os.path.getctime(os.path.join(log_dir,x)))                  
        for x in os.listdir(log_dir) if x.endswith('.meta')  
    ],
    key=lambda i: i[1])[-1][0]

sess = tf.Session()

saver = tf.train.import_meta_graph(os.path.join(log_dir,metaFile))
ckpt = tf.train.get_checkpoint_state(log_dir)
if ckpt and ckpt.model_checkpoint_path:
    saver.restore(sess, ckpt.model_checkpoint_path)
else:
    print("error: can't load checkpoint data")
    exit()

text, image = captcha(char_set="0123456789", captcha_size=4, width=200, height=80)

image = img2vec(img2gray(image)) 

x = tf.get_default_graph().get_tensor_by_name('x:0')
prediction = tf.get_default_graph().get_tensor_by_name('prediction:0')

y_ = sess.run([prediction], feed_dict={x: [image]})
result = "".join([str(x) for x in y_[0][0]])
print("input: %s , get: %s"%(text,result))
Esempio n. 11
0
# img_path = 'img/test.jpg'

d = 25          # diameter (d=2*R) of neighborhood (domain: > 0, restriction: odd integer)
h_c = 0.05      # threshold for measure of heterogeneity (domain: [0, R])
h_s = 0.75      # threshold for measure of symmetry (domain: [0, 1])
h_ab = 0.05     # threshold for contrast (domain: [0, 1])
h_c_hat = 0.1   # threshold for the measure of the corner (domain: [0, 1])
h_e_hat = 0.95  # threshold for the measure of the edge (domain: [0, 1])

sp_noise_amount = 0     # amount of salt-paper noise (domain: [0, 1])
gaus_noise_snr = 15      # signal-to-noise ratio for gaussian noise (domain: >= 0)
sigma = 3               # sigma for gaussian filter (domain: >= 1)
# ----------

# Get image
img_orig = img2gray(plt.imread(img_path))
img = img_orig.copy()

# Add noise
if sp_noise_amount:
    img = add_sp_noise(img, amount=sp_noise_amount)
if gaus_noise_snr:
    img = add_gaussian_noise(img, snr=gaus_noise_snr)
# if sigma:
#     img = apply_gaussian_filter(img, sigma=sigma)

# Show image
plt.figure()
plt.subplot(1, 2, 1)
plt.title('Original image')
plt.imshow(img_orig, cmap='gray', vmin=0, vmax=1)