def crack(tocrack): im = cv.CreateImage(cv.GetSize(tocrack), 8, 1) cv.Split(tocrack, None, None, im, None) cv.Threshold(im, im, 250, 255, cv.CV_THRESH_BINARY) txt = pytesser.iplimage_to_string(im) return txt[:-2]
def crack(tocrack): im = cv.CreateImage(cv.GetSize(tocrack), 8, 1) cv.Split(tocrack, None, None, im, None) cv.Threshold(im, im, 250, 255, cv.CV_THRESH_BINARY) txt = pytesser.iplimage_to_string(im) return txt[:-2]
def process_all(results): dir = "Ebay" #Consider that all images are stored in the dir 'Ebay' for file,r in zip(os.listdir(dir),results): im = cv.LoadImage(os.path.join(dir,file),cv.CV_LOAD_IMAGE_GRAYSCALE) #Load the image im = crack(im) #intent to crack it res = pytesser.iplimage_to_string(im,psm=pytesser.PSM_SINGLE_WORD) #Do characters recognition res = res[:-2] #Remove the two \n\n always put at the end of the result if res == r: #Compare the result of the value contained in our list print file+": "+res+" | "+r+ " OK" else: print file+": "+res+" | "+r+" NO"
def crack(self,dilateiter=4, erodeiter=4, threshold=200, size=(155,55)): #Take all parameters resized = resizeImage(self.image, (self.image.width*6, self.image.height*6)) dilateImage(resized, dilateiter) erodeImage(resized, erodeiter) thresholdImage(resized,threshold, cv.CV_THRESH_BINARY) resized = resizeImage(resized, size) #Call the tesseract engine ret = pytesser.iplimage_to_string(resized) ret = ret[:-2] return ret
def crack(self, dilateiter=4, erodeiter=4, threshold=200, size=(155, 55)): #Take all parameters resized = resizeImage(self.image, (self.image.width * 6, self.image.height * 6)) dilateImage(resized, dilateiter) erodeImage(resized, erodeiter) thresholdImage(resized, threshold, cv.CV_THRESH_BINARY) resized = resizeImage(resized, size) #Call the tesseract engine ret = pytesser.iplimage_to_string(resized) ret = ret[:-2] return ret
def run(self, img): def normalize(img): img = cv2.cvtColor(img, cv2.CV_32F) gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) gray = cv2.equalizeHist(gray) ret, th0 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) return th0 def resize(img, resize=1): h, w = img.shape img = cv2.resize(img, (w * resize, h * resize), interpolation=cv2.INTER_CUBIC) return img def feature(img): kernel = np.ones((1, 1), np.uint8) # smooth backgroun noise blur = cv2.GaussianBlur(img, (5, 5), 0) # threshold filter ret, th1 = cv2.threshold(blur, 235, 255, cv2.THRESH_BINARY) # colsing/opening open = cv2.morphologyEx(th1, cv2.MORPH_OPEN, kernel, iterations=5) close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel, iterations=5) mask = cv2.bitwise_and(th1, th1, mask=close) return mask def debug(img): cv2.imshow('test', img) cv2.waitKey(0) cv2.destroyAllWindows() ft = feature(resize(normalize(img))) text = pytesser.iplimage_to_string(cv.fromarray(ft), 'eng').strip() if self._debug: debug(ft) text = raw_input("debug:") return text if text else ''
def run(self, img): def normalize(img): img = cv2.cvtColor(img, cv2.CV_32F) gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) gray = cv2.equalizeHist(gray) ret,th0 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) return th0 def resize(img, resize=1): h,w = img.shape img = cv2.resize(img, (w*resize,h*resize), interpolation=cv2.INTER_CUBIC) return img def feature(img): kernel = np.ones((1,1), np.uint8) # smooth backgroun noise blur = cv2.GaussianBlur(img, (5,5), 0) # threshold filter ret,th1 = cv2.threshold(blur, 235, 255, cv2.THRESH_BINARY) # colsing/opening open = cv2.morphologyEx(th1, cv2.MORPH_OPEN, kernel, iterations=5) close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel, iterations=5) mask = cv2.bitwise_and(th1, th1, mask=close) return mask def debug(img): cv2.imshow('test', img) cv2.waitKey(0) cv2.destroyAllWindows() ft = feature(resize(normalize(img))) text = pytesser.iplimage_to_string(cv.fromarray(ft), 'eng').strip() if self._debug: debug(ft) text = raw_input("debug:") return text if text else ''
def run(self, img): def normalize(img): img = cv2.cvtColor(img, cv2.CV_32F) gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) gray = cv2.equalizeHist(gray) ret,th0 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) return th0 def resize(img, resize=1): h,w = img.shape img = cv2.resize(img, (w*resize,h*resize), interpolation=cv2.INTER_CUBIC) return img def boundary(img, bund=3): h, w = img.shape # find best match captcha area # smooth background noise blur = cv2.GaussianBlur(img, (5,5), 0) # threshold filter ret,th1 = cv2.threshold(blur, 235, 255, cv2.THRESH_BINARY) contours,hierarchy = cv2.findContours(th1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) area = lambda (x, y, w, h): (w*h, x, y, w, h) best = sorted([area(cv2.boundingRect(cnt)) for cnt in contours], reverse=True) # iter sub captcha x0 = lambda x: x-bund if x > bund else 0 y0 = lambda y: y-bund if y > bund else 0 x1 = lambda x: x+bund if w-bund > x else w y1 = lambda y: y+bund if h-bund > y else h for it in sorted(best[:5], key=lambda x: x[1]): yield ((x0(it[1]),y0(it[2])), (x1(it[1]+it[3]),y1(it[2]+it[4]))) def feature(img): kernel = np.ones((1,1), np.uint8) # smooth backgroun noise blur = cv2.GaussianBlur(img, (5,5), 0) # threshold filter ret,th1 = cv2.threshold(blur, 235, 255, cv2.THRESH_BINARY) # colsing/opening open = cv2.morphologyEx(th1, cv2.MORPH_OPEN, kernel, iterations=5) close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel, iterations=5) mask = cv2.bitwise_and(th1, th1, mask=close) return mask def debug(img, bund): cv2.rectangle(img, bund[0], bund[1], (255,255,255), 1) cv2.imshow('test', img) cv2.waitKey(0) cv2.destroyAllWindows() text = '' img = resize(normalize(img), 1) h, w = img.shape bkimg = np.zeros((h,w,3), np.uint8) bkimg = resize(normalize(bkimg), 1) for bund in boundary(img): x,y = zip(*bund) bkimg[y[0]:y[1],x[0]:x[1]] = img[y[0]:y[1],x[0]:x[1]] if self._debug: debug(feature(bkimg), bund) text = pytesser.iplimage_to_string(cv.fromarray(feature(bkimg)), 'eng').strip() return text if text else ''
def run(self, img): def normalize(img): img = cv2.cvtColor(img, cv2.CV_32F) # rgb 2 gray gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) gray = cv2.equalizeHist(gray) ret,th0 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) return th0 def resize(img, resize=1): # scalar must be int h,w = img.shape img = cv2.resize(img, (w*resize,h*resize), interpolation=cv2.INTER_CUBIC) return img def img_absdiff(imgA, imgB): xA, yA = imgA.shape xB, yB = imgB.shape # resize imgB to imgA if size not same if xB != xA or yB != yA: imgB = cv2.resize(imgB, (x,y), interpolation=cv2.INTER_CUBIC) diff = np.diff(imgA.astype('float') - imgB.astype('float')) absdiff = np.sum(diff**2) return absdiff / (xA * yA) def feature_extract(img): # smooth background noise kernel = np.ones((4,4), np.uint8) erosion = cv2.erode(img, kernel, iterations=1) blurred = cv2.GaussianBlur(erosion, (5,5), 0) # enhance img edge edged = cv2.Canny(blurred, 30, 150) dilation = cv2.dilate(edged, kernel, iterations=1) return dilation def ini_img_as_blank(h, w): bkimg = np.zeros((h,w,3), np.uint8) bkimg = resize(normalize(bkimg), 1) return bkimg def bitwise_and_noise_reduce(img): kernel = np.ones((1,1), np.uint8) # smooth backgroun noise blur = cv2.GaussianBlur(img, (5,5), 0) # threshold filter ret,th1 = cv2.threshold(blur, 235, 255, cv2.THRESH_BINARY) # colsing/opening open = cv2.morphologyEx(th1, cv2.MORPH_OPEN, kernel, iterations=5) close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel, iterations=5) mask = cv2.bitwise_and(th1, th1, mask=close) return mask def iter_find_best_char_index(img, bund=1, limit=5): # feature select order by roll win area size h, w = img.shape area = lambda (x, y, w, h): (w*h, x, y, w, h) contours, hierarchy = cv2.findContours(img.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) best = sorted([area(cv2.boundingRect(cnt)) for cnt in contours], reverse=True) # iter sub captcha x0 = lambda x: x-bund if x > bund else 0 y0 = lambda y: y-bund if y > bund else 0 x1 = lambda x: x+bund if w-bund > x else w y1 = lambda y: y+bund if h-bund > y else h for it in sorted(best[:limit], key=lambda x: x[1]): yield ((x0(it[1]),y0(it[2])), (x1(it[1]+it[3]),y1(it[2]+it[4]))) def blank_boundary_edge(img, outer=4): h, w = img.shape bh = h+outer*2 bw = w+outer*2 bkimg = ini_img_as_blank(bh,bw) bkimg[outer:h+outer,outer:w+outer] = img[0:h,0:w] return bkimg def debug(img): cv2.imshow('test', img) cv2.waitKey(0) cv2.destroyAllWindows() chars = [] nwimg = bitwise_and_noise_reduce(normalize(img)) h, w = nwimg.shape for bund in iter_find_best_char_index(nwimg, 1, 5): x,y = zip(*bund) ff = nwimg[y[0]:y[1],x[0]:x[1]] nwff = blank_boundary_edge(ff.copy(), 4) if self._debug: debug(nwff) char = pytesser.iplimage_to_string(cv.fromarray(nwff), 'eng', 10).strip() chars.append(char.upper()) return ''.join(chars)
def detect(filename, folder, file_no): # print 'in' img = cv2.imread(filename) img = cv2.medianBlur(img, 1) #smothing image # extracting white characters image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) imgray = crop(image) # imgray = image imgray = cv2.resize(imgray, (880, 100)) (h, w) = imgray.shape (img_h, img_w) = (h, w) drawing = noise(imgray, imgray.shape, 1000, 25000) cv2.imshow('win1', drawing) cv2.waitKey() cv2.imwrite('E:\Results\\res.jpg', drawing) # character recorgnition image = cv.LoadImage("E:\Results\\res.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) lang = ['eng'] result = 0 for i in lang: data = pytesser.iplimage_to_string(image, i, pytesser.PSM_SINGLE_LINE, makebox=True) # print data word = '' section = [] #all the characters and coordinates in a list line = [] for i in data: if i == ' ' or i == '\n': line.append(word) word = '' elif (i != '\n'): word += i if i == '\n': section.append(line) line = [] coord = [] chars = [] #coordinates of the characters count = 0 for i in section: for j in i: # print count if j.isdigit() and count < 5 and count > 0: coord.append(int(j)) count += 1 count = 0 chars.append(coord) coord = [] # removing all symbols dele = 0 delete = [] for i in section: if not i[0].isdigit() and not i[0].isalpha(): delete.append(dele) dele += 1 delete.reverse() for i in delete: section.pop(i) chars.pop(i) c = 0 point = [] for i in chars: cv2.rectangle(drawing, (i[0], i[1]), (i[2], i[3]), (255, 255, 255), 2) cv2.imshow('rec', drawing) cv2.waitKey(1) cv2.imwrite('E:\\report\\res.jpg', drawing) counter = 0 line_coordinates = [0] if len(chars) == 1: for i in chars: if i[0] > 400: line_coordinates.append(i[0] / 2) line_coordinates.append(i[0]) # for debugging, draws box using coordinates received from make box function for i in chars: counter += 1 if c == 0: point.append(i[2]) c = 1 elif c == 1: point.append(i[0]) line = ((point[0] + point[1]) / 2) line_coordinates.append(line) cv2.line(drawing, (line, img_h), (line, 0), (255, 255, 255), 2) c = 0 point = [] if counter == len(chars): if img_w - i[2] < 150: line_coordinates.append(img_w) else: line_coordinates.append(i[2]) line_coordinates.append(img_w) cv2.line(drawing, (i[2], img_h), (i[2], 0), (255, 255, 255), 2) # spliting image and processing iterator = 1 id = '' while iterator < len(line_coordinates): roi = imgray[0:img_h, line_coordinates[iterator - 1]:line_coordinates[iterator]] iterator += 1 corrected = noise(roi, roi.shape, 300, 20000) cv2.imwrite('E:\Results\\res.jpg', corrected) cv2.imshow('win1', corrected) cv2.waitKey(1) image = cv.LoadImage("E:\Results\\res.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) data = pytesser.iplimage_to_string(image, 'enm', 7) # print data for i in data: if i == '!': i = '1' if i.isalpha() or i.isdigit(): if i == 'O' or i == 'o': i = '0' if i == 'L' or i == 'l': i = '1' if i == 'i' or i == 'I': i = '1' id = id + i # print id if id != '': print id sol = check(id) final = sol[0] alternative = sol[1] possible = sol[2] else: return None # print sol if not final: os.chdir(folder) filename = 'error' + str(file_no) img_name = filename + '.jpg' txt_name = filename + '.txt' cv2.imwrite(img_name, img) if len(alternative) > 1: #storing alternatives in a text file f = open(txt_name, 'w') for i in alternative: f.write(i + '\n') f.close() return None else: return possible
def run(self, img): def normalize(img): img = cv2.cvtColor(img, cv2.CV_32F) gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) gray = cv2.equalizeHist(gray) ret, th0 = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) return th0 def resize(img, resize=2): h, w = img.shape img = cv2.resize(img, (w * resize, h * resize), interpolation=cv2.INTER_CUBIC) return img def filter(img): edges = cv2.Canny(img, 150, 250, apertureSize=3) lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 1, 3, 1) # remove boundary as white line for line in lines: for x1, y1, x2, y2 in line: cv2.line(img, (x1, y1), (x2, y2), (255, 255, 255), 14) # smooth background noise blur = cv2.GaussianBlur(img, (5, 5), 0) # threshold filter ret, th1 = cv2.threshold(blur, 250, 255, cv2.THRESH_BINARY) return th1 def extend(img, ext=30): h, w = img.shape wtimg = 255 - normalize(np.zeros((h + ext, w, 3), np.uint8)) wtimg[(h + ext) // 2 - h // 2:(h + ext) // 2 + h // 2, :w] = img[:h, :w] return wtimg def boundary(img, bund=-1): # find best match captcha area contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) area = lambda (x, y, w, h): (w * h, x, y, w, h) best = sorted([area(cv2.boundingRect(cnt)) for cnt in contours], reverse=True) # iter sub captcha x0 = lambda x: x - bund if x > bund else 0 y0 = lambda y: y - bund if y > bund else 0 x1 = lambda x: x + bund if w - bund > x else w y1 = lambda y: y + bund if h - bund > y else h for it in sorted(best[1:6], key=lambda x: x[1]): yield ((x0(it[1]), y0(it[2])), (x1(it[1] + it[3]), y1(it[2] + it[4]))) def feature(img): kernel = np.ones((1, 1), np.uint8) # smooth backgroun noise blur = cv2.GaussianBlur(img, (5, 5), 0) # threshold filter ret, th1 = cv2.threshold(blur, 235, 255, cv2.THRESH_BINARY) # colsing/opening open = cv2.morphologyEx(th1, cv2.MORPH_OPEN, kernel, iterations=5) close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel, iterations=5) mask = cv2.bitwise_and(th1, th1, mask=close) return mask def debug(img, bund): cv2.rectangle(img, bund[0], bund[1], (0, 0, 0), 1) cv2.imshow('test', img) cv2.waitKey(0) cv2.destroyAllWindows() text = '' img = extend(resize(normalize(img), 4)) h, w = img.shape bkimg = np.zeros((h, w, 3), np.uint8) bkimg = 255 - resize(normalize(bkimg), 1) for bund in boundary(filter(img.copy()), -7): x, y = zip(*bund) bkimg[y[0]:y[1], x[0]:x[1]] = img[y[0]:y[1], x[0]:x[1]] if self._debug: debug(bkimg, bund) text = pytesser.iplimage_to_string(cv.fromarray(bkimg), 'eng').strip() return text if text else ''
upper_line=[] #stores elements from maxValueOfRowsOfEdgesImage which denotes starting of line in image lower_line=[] #stores elements from maxValueOfRowsOfEdgesImage which denotes ending of line in image #this loop finds at which row in image, line starts and at which row line in image ends for i in range(rows+1): if ((maxValueOfRowsOfEdgesImage[i+1]==0)|(maxValueOfRowsOfEdgesImage[i]==0)): if ((maxValueOfRowsOfEdgesImage[i+1]!=0) & (maxValueOfRowsOfEdgesImage[i]==0)): #img_deskewed = cv2.line(img_deskewed,(0,i),(cols,i),(100,0,100),1) #this line draws upper line on image upper_line.append(i) elif((maxValueOfRowsOfEdgesImage[i+1]==0) & (maxValueOfRowsOfEdgesImage[i]!=0)): #img_deskewed = cv2.line(img_deskewed,(0,i),(cols,i),(100,0,100),1) #this line draws lower line on image lower_line.append(i) upper_line=np.array(upper_line) #converts list in array lower_line=np.array(lower_line) #cuts each line from image and does character recognition for each lines separately fout = open(output+".txt","w") print "Please wait while I'm processing your Image..." for i in range(len(upper_line)): h = lower_line[i]-upper_line[i] #hight of line y = upper_line[i] #Y coordinate of line img_line = img_deskewed[y:y+h,0:cols] #cut detected line from image and save in img_line if(thr == True): img_line = cv2.adaptiveThreshold(img_line,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY,15,2) txt = pt.iplimage_to_string(img_line, lang, psm) #txt contains recognized string from line fout.write(txt) fout.close()
def run(self, img): def normalize(img): img = cv2.cvtColor(img, cv2.CV_32F) gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) gray = cv2.equalizeHist(gray) ret,th0 = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) return th0 def resize(img, resize=2): h,w = img.shape img = cv2.resize(img, (w*resize,h*resize), interpolation=cv2.INTER_CUBIC) return img def filter(img): edges = cv2.Canny(img, 150, 250, apertureSize=3) lines = cv2.HoughLinesP(edges,1,np.pi/180, 1, 3, 1) # remove boundary as white line for line in lines: for x1,y1,x2,y2 in line: cv2.line(img,(x1,y1),(x2,y2),(255,255,255),14) # smooth background noise blur = cv2.GaussianBlur(img, (5,5), 0) # threshold filter ret,th1 = cv2.threshold(blur, 250, 255, cv2.THRESH_BINARY) return th1 def extend(img, ext=30): h, w = img.shape wtimg = 255 - normalize(np.zeros((h+ext,w,3), np.uint8)) wtimg[(h+ext)//2-h//2:(h+ext)//2+h//2,:w] = img[:h,:w] return wtimg def boundary(img, bund=-1): # find best match captcha area contours,hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) area = lambda (x, y, w, h): (w*h, x, y, w, h) best = sorted([area(cv2.boundingRect(cnt)) for cnt in contours], reverse=True) # iter sub captcha x0 = lambda x: x-bund if x > bund else 0 y0 = lambda y: y-bund if y > bund else 0 x1 = lambda x: x+bund if w-bund > x else w y1 = lambda y: y+bund if h-bund > y else h for it in sorted(best[1:6], key=lambda x: x[1]): yield ((x0(it[1]),y0(it[2])), (x1(it[1]+it[3]),y1(it[2]+it[4]))) def feature(img): kernel = np.ones((1,1), np.uint8) # smooth backgroun noise blur = cv2.GaussianBlur(img, (5,5), 0) # threshold filter ret,th1 = cv2.threshold(blur, 235, 255, cv2.THRESH_BINARY) # colsing/opening open = cv2.morphologyEx(th1, cv2.MORPH_OPEN, kernel, iterations=5) close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel, iterations=5) mask = cv2.bitwise_and(th1, th1, mask=close) return mask def debug(img, bund): cv2.rectangle(img, bund[0], bund[1], (0,0,0), 1) cv2.imshow('test', img) cv2.waitKey(0) cv2.destroyAllWindows() text = '' img = extend(resize(normalize(img), 4)) h, w = img.shape bkimg = np.zeros((h,w,3), np.uint8) bkimg = 255 - resize(normalize(bkimg), 1) for bund in boundary(filter(img.copy()), -7): x,y = zip(*bund) bkimg[y[0]:y[1],x[0]:x[1]] = img[y[0]:y[1],x[0]:x[1]] if self._debug: debug(bkimg, bund) text = pytesser.iplimage_to_string(cv.fromarray(bkimg), 'eng').strip() return text if text else ''
def run(self, img): def normalize(img): img = cv2.cvtColor(img, cv2.CV_32F) gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) gray = cv2.equalizeHist(gray) ret, th0 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) return th0 def resize(img, resize=1): h, w = img.shape img = cv2.resize(img, (w * resize, h * resize), interpolation=cv2.INTER_CUBIC) return img def boundary(img, bund=3): h, w = img.shape # find best match captcha area # smooth background noise blur = cv2.GaussianBlur(img, (5, 5), 0) # threshold filter ret, th1 = cv2.threshold(blur, 235, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(th1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) area = lambda (x, y, w, h): (w * h, x, y, w, h) best = sorted([area(cv2.boundingRect(cnt)) for cnt in contours], reverse=True) # iter sub captcha x0 = lambda x: x - bund if x > bund else 0 y0 = lambda y: y - bund if y > bund else 0 x1 = lambda x: x + bund if w - bund > x else w y1 = lambda y: y + bund if h - bund > y else h for it in sorted(best[:5], key=lambda x: x[1]): yield ((x0(it[1]), y0(it[2])), (x1(it[1] + it[3]), y1(it[2] + it[4]))) def feature(img): kernel = np.ones((1, 1), np.uint8) # smooth backgroun noise blur = cv2.GaussianBlur(img, (5, 5), 0) # threshold filter ret, th1 = cv2.threshold(blur, 235, 255, cv2.THRESH_BINARY) # colsing/opening open = cv2.morphologyEx(th1, cv2.MORPH_OPEN, kernel, iterations=5) close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel, iterations=5) mask = cv2.bitwise_and(th1, th1, mask=close) return mask def debug(img, bund): cv2.rectangle(img, bund[0], bund[1], (255, 255, 255), 1) cv2.imshow('test', img) cv2.waitKey(0) cv2.destroyAllWindows() text = '' img = resize(normalize(img), 1) h, w = img.shape bkimg = np.zeros((h, w, 3), np.uint8) bkimg = resize(normalize(bkimg), 1) for bund in boundary(img): x, y = zip(*bund) bkimg[y[0]:y[1], x[0]:x[1]] = img[y[0]:y[1], x[0]:x[1]] if self._debug: debug(feature(bkimg), bund) text = pytesser.iplimage_to_string(cv.fromarray(feature(bkimg)), 'eng').strip() return text if text else ''
def run(self, img): def normalize(img): img = cv2.cvtColor(img, cv2.CV_32F) # rgb 2 gray gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) gray = cv2.equalizeHist(gray) ret, th0 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) return th0 def resize(img, resize=1): # scalar must be int h, w = img.shape img = cv2.resize(img, (w * resize, h * resize), interpolation=cv2.INTER_CUBIC) return img def img_absdiff(imgA, imgB): xA, yA = imgA.shape xB, yB = imgB.shape # resize imgB to imgA if size not same if xB != xA or yB != yA: imgB = cv2.resize(imgB, (x, y), interpolation=cv2.INTER_CUBIC) diff = np.diff(imgA.astype('float') - imgB.astype('float')) absdiff = np.sum(diff**2) return absdiff / (xA * yA) def feature_extract(img): # smooth background noise kernel = np.ones((4, 4), np.uint8) erosion = cv2.erode(img, kernel, iterations=1) blurred = cv2.GaussianBlur(erosion, (5, 5), 0) # enhance img edge edged = cv2.Canny(blurred, 30, 150) dilation = cv2.dilate(edged, kernel, iterations=1) return dilation def ini_img_as_blank(h, w): bkimg = np.zeros((h, w, 3), np.uint8) bkimg = resize(normalize(bkimg), 1) return bkimg def bitwise_and_noise_reduce(img): kernel = np.ones((1, 1), np.uint8) # smooth backgroun noise blur = cv2.GaussianBlur(img, (5, 5), 0) # threshold filter ret, th1 = cv2.threshold(blur, 235, 255, cv2.THRESH_BINARY) # colsing/opening open = cv2.morphologyEx(th1, cv2.MORPH_OPEN, kernel, iterations=5) close = cv2.morphologyEx(open, cv2.MORPH_CLOSE, kernel, iterations=5) mask = cv2.bitwise_and(th1, th1, mask=close) return mask def iter_find_best_char_index(img, bund=1, limit=5): # feature select order by roll win area size h, w = img.shape area = lambda (x, y, w, h): (w * h, x, y, w, h) contours, hierarchy = cv2.findContours(img.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) best = sorted([area(cv2.boundingRect(cnt)) for cnt in contours], reverse=True) # iter sub captcha x0 = lambda x: x - bund if x > bund else 0 y0 = lambda y: y - bund if y > bund else 0 x1 = lambda x: x + bund if w - bund > x else w y1 = lambda y: y + bund if h - bund > y else h for it in sorted(best[:limit], key=lambda x: x[1]): yield ((x0(it[1]), y0(it[2])), (x1(it[1] + it[3]), y1(it[2] + it[4]))) def blank_boundary_edge(img, outer=4): h, w = img.shape bh = h + outer * 2 bw = w + outer * 2 bkimg = ini_img_as_blank(bh, bw) bkimg[outer:h + outer, outer:w + outer] = img[0:h, 0:w] return bkimg def debug(img): cv2.imshow('test', img) cv2.waitKey(0) cv2.destroyAllWindows() chars = [] nwimg = bitwise_and_noise_reduce(normalize(img)) h, w = nwimg.shape for bund in iter_find_best_char_index(nwimg, 1, 5): x, y = zip(*bund) ff = nwimg[y[0]:y[1], x[0]:x[1]] nwff = blank_boundary_edge(ff.copy(), 4) if self._debug: debug(nwff) char = pytesser.iplimage_to_string(cv.fromarray(nwff), 'eng', 10).strip() chars.append(char.upper()) return ''.join(chars)
def detect(filename, folder, file_no): # print 'in' img = cv2.imread(filename) img = cv2.medianBlur(img, 1) #smothing image # extracting white characters image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) imgray = crop(image) # imgray = image imgray = cv2.resize(imgray, (880,100)) (h,w) = imgray.shape (img_h,img_w) = (h,w) drawing = noise(imgray, imgray.shape, 1000, 25000) cv2.imshow('win1', drawing) cv2.waitKey() cv2.imwrite('E:\Results\\res.jpg', drawing) # character recorgnition image = cv.LoadImage("E:\Results\\res.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) lang = ['eng'] result = 0 for i in lang: data = pytesser.iplimage_to_string(image, i, pytesser.PSM_SINGLE_LINE,makebox=True) # print data word = '' section = [] #all the characters and coordinates in a list line = [] for i in data: if i == ' ' or i=='\n': line.append(word) word = '' elif (i!='\n'): word+=i if i == '\n': section.append(line) line = [] coord = [] chars = [] #coordinates of the characters count = 0 for i in section: for j in i: # print count if j.isdigit() and count < 5 and count > 0: coord.append(int(j)) count += 1 count = 0 chars.append(coord) coord = [] # removing all symbols dele= 0 delete=[] for i in section: if not i[0].isdigit() and not i[0].isalpha(): delete.append(dele) dele += 1 delete.reverse() for i in delete: section.pop(i) chars.pop(i) c = 0 point = [] for i in chars: cv2.rectangle(drawing,(i[0],i[1]),(i[2],i[3]),(255,255,255),2) cv2.imshow('rec', drawing) cv2.waitKey(1) cv2.imwrite('E:\\report\\res.jpg', drawing) counter = 0 line_coordinates = [0] if len(chars) == 1: for i in chars: if i[0] > 400: line_coordinates.append(i[0]/2) line_coordinates.append(i[0]) # for debugging, draws box using coordinates received from make box function for i in chars: counter += 1 if c == 0: point.append(i[2]) c = 1 elif c == 1: point.append(i[0]) line = ((point[0] + point[1]) / 2) line_coordinates.append(line) cv2.line(drawing,(line,img_h),(line,0),(255,255,255),2) c = 0 point = [] if counter == len(chars): if img_w - i[2] < 150: line_coordinates.append(img_w) else: line_coordinates.append(i[2]) line_coordinates.append(img_w) cv2.line(drawing,(i[2],img_h),(i[2],0),(255,255,255),2) # spliting image and processing iterator = 1 id = '' while iterator < len(line_coordinates): roi = imgray[0:img_h, line_coordinates[iterator-1]:line_coordinates[iterator]] iterator += 1 corrected = noise(roi, roi.shape, 300, 20000) cv2.imwrite('E:\Results\\res.jpg', corrected) cv2.imshow('win1', corrected) cv2.waitKey(1) image = cv.LoadImage("E:\Results\\res.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) data = pytesser.iplimage_to_string(image, 'enm', 7 ) # print data for i in data: if i == '!': i = '1' if i.isalpha() or i.isdigit(): if i == 'O' or i =='o': i = '0' if i == 'L' or i == 'l': i = '1' if i == 'i' or i == 'I': i = '1' id = id + i # print id if id != '': print id sol = check(id) final = sol[0] alternative = sol[1] possible = sol[2] else: return None # print sol if not final: os.chdir(folder) filename = 'error' + str(file_no) img_name = filename + '.jpg' txt_name = filename + '.txt' cv2.imwrite(img_name, img) if len(alternative) > 1: #storing alternatives in a text file f = open(txt_name, 'w') for i in alternative: f.write(i+'\n') f.close() return None else: return possible