Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import time
- from collections import Counter
- from Tkinter import *
- from PIL import ImageTk, Image
- import os, sys, glob, ntpath
- from os.path import relpath
- def create_database():
- number_arr_ex = open('numArEx.txt', 'a')
- numbers_we_have = range(0, 10)
- versions_we_have = range(1, 10)
- for eachNum in numbers_we_have:
- for eachVer in versions_we_have:
- img_file_path = 'images/numbers/'+str(eachNum)+'.'+str(eachVer)+'.jpg'
- ei = Image.open(img_file_path)
- eiar = np.array(ei)
- eiar1 = str(eiar.tolist())
- line_to_write = str(eachNum)+'::'+eiar1+'\n'
- number_arr_ex.write(line_to_write)
- def tresh_from_img_np_array(img_arr):
- balance_arr = []
- new_arr = img_arr
- for row in new_arr:
- for pixel in row:
- avg_num = pixel[0]/3+(pixel[1]/3)+(pixel[2]/3)
- balance_arr.append(avg_num)
- balance = reduce(lambda x, y: x+y, balance_arr)/len(balance_arr)
- for row in new_arr:
- for pixel in row:
- temp = pixel[0] / 3 + (pixel[1] / 3) + (pixel[2] / 3)
- if temp > balance:
- pixel[0] = 255
- pixel[1] = 255
- pixel[2] = 255
- else:
- pixel[0] = 0
- pixel[1] = 0
- pixel[2] = 0
- return new_arr
- def tresh_from_img(img):
- imgarr = np.asarray(img)
- imgarr.flags.writeable = True
- balance_arr = []
- new_arr = imgarr
- for row in new_arr:
- for pixel in row:
- avg_num = pixel[0]/3+(pixel[1]/3)+(pixel[2]/3)
- balance_arr.append(avg_num)
- balance = reduce(lambda x, y: x+y, balance_arr)/len(balance_arr)
- for row in new_arr:
- for pixel in row:
- temp = pixel[0] / 3 + (pixel[1] / 3) + (pixel[2] / 3)
- if temp > balance:
- pixel[0] = 255
- pixel[1] = 255
- pixel[2] = 255
- else:
- pixel[0] = 0
- pixel[1] = 0
- pixel[2] = 0
- image = Image.fromarray(new_arr)
- return image
- def tresh_from_img_path(path):
- img = Image.open(path)
- to_arr = np.asarray(img)
- to_arr.flags.writeable = True
- balance_arr = []
- new_arr = to_arr
- for row in new_arr:
- for pixel in row:
- avg_num = pixel[0]/3+(pixel[1]/3)+(pixel[2]/3)
- balance_arr.append(avg_num)
- balance = reduce(lambda x, y: x + y, balance_arr)/len(balance_arr)
- for row in new_arr:
- for pixel in row:
- temp = pixel[0] / 3 + (pixel[1] / 3) + (pixel[2] / 3)
- if temp > balance:
- pixel[0] = 255
- pixel[1] = 255
- pixel[2] = 255
- else:
- pixel[0] = 0
- pixel[1] = 0
- pixel[2] = 0
- image = Image.fromarray(new_arr)
- return image
- def what_num(my_file):
- matched_black = []
- max_matched_one_pic = []
- miss_match = []
- load_ex = open('numArEx.txt','r').read()
- load_ex = load_ex.split('\n')
- iar = my_file
- iar1 = iar.tolist()
- in_qst = str(iar1)
- in_qst = in_qst.split('],')
- i = 0
- for eachEx in load_ex:
- if len(eachEx) > 3:
- split_ex = eachEx.split('::')
- current_num = split_ex[0]
- current_ar = split_ex[1]
- each_pix_ex = current_ar.split('],')
- x = 0
- matched_one_pic = []
- while x < len(each_pix_ex):
- if each_pix_ex[x] == in_qst[x]:
- matched_one_pic.append(int(current_num))
- matched_black.append(int(current_num))
- if len(matched_one_pic) > len(max_matched_one_pic):
- max_matched_one_pic = matched_one_pic
- else:
- miss_match.append(int(current_num))
- x += 1
- i += 1
- x = Counter(matched_black)
- print matched_black
- print x
- for number, count in x.most_common(1):
- print "Your number is "+str(number)
- print "Most accurate number was "+str(max_matched_one_pic[0])
- ### grafikai ###
- # graphX = []
- # graphY = []
- # for each in x:
- # graphX.append(each)
- # graphY.append(x[each])
- # fig = plt.figure()
- # ax1 = plt.subplot2grid((4,4),(0,0),rowspan = 1, colspan=4)
- # ax2 = plt.subplot2grid((4,4),(1,0),rowspan = 3, colspan=4)
- # ax1.imshow(iar)
- # ax2.bar(graphX,graphY,align='center')
- #plt.ylim(400)
- # xloc = plt.MaxNLocator(12)
- #ax2.xaxis.set_major_locator(xloc)
- # plt.show()
- #
- # fig = plt.figure()
- # ax1 = plt.subplot2grid((4,3),(0,0),rowspan=12,colspan=9)
- # ax1.imshow(iar)
- # plt.show()
- # for i in range(0,10):
- # for j in range(1,10):
- # print i,j
- # pic = Image.open("images/numbers/"+str(i)+"."+str(j)+".png")
- # -------------------------------------------------------
- # path = "/Users/erlandas_t/PycharmProjects/untitled5/images/numbers"
- # dirs = os.listdir(path)
- # print len(dirs)
- # for fil in dirs:
- # print "images/numbers/"+fil
- # pic = Image.open("images/numbers/"+fil)
- # iar = np.asarray(pic)
- # iar.flags.writeable = True
- # print "aa"
- # what_num(tresh(iar))
- # -------------------------------------
- def crop_white_edges(img):
- print
- # -------------------------------------
- def convert_to_jpg(img, path):
- head, tail = ntpath.split(path)
- img.save("images/jpg/" + tail[:-4] + ".jpg", format='JPEG', subsampling=0, quality=100)
- def convert_dir_cont_to_jpg(dir_path):
- dirs = os.listdir(dir_path)
- print len(dirs)
- for fil in dirs:
- img = tresh_from_img_path(dir_path+fil)
- convert_to_jpg(img, dir_path+fil)
- def main():
- path = "/Users/erlandas_t/PycharmProjects/untitled5/images/numbers/"
- convert_dir_cont_to_jpg(path)
- create_database()
- # root = Tk()
- # img = ImageTk.PhotoImage(Image.open("images/test.png"))
- # panel = Label(root, image=img)
- # panel.pack(side="bottom", fill="both", expand="yes")
- # root.mainloop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement