def ocrend(img_src): #image_name = "img.jpg" #im = Image.open(image_name) user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' heads = {'User-Agent':user_agent} req = urllib2.Request(img_src,headers=heads) fails = 0 while True: try: if fails >= 10: break response = urllib2.urlopen(req,timeout=30) html = response.read() except: fails += 1 print "Handing brand,the network may be not Ok,please wait...",fails else: break file1 = StringIO(html) im = Image.open(file1) im = im.filter(ImageFilter.MedianFilter()) enhancer = ImageEnhance.Contrast(im) im = enhancer.enhance(2) im = im.convert('1') #im.save("1.tif") print pytesseract.image_to_string(im)
def getNumbers(img, name, threshhold): # normal threshHold #ret1, thresh = cv2.threshold(img, threshhold, 255, cv2.THRESH_BINARY) # adaptive threshHold thresh = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, THRESH_HOLD_BOX,8) cv2.imwrite('threshHold/' + name + '.tiff', thresh) _, contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) imT = thresh.copy() outImg = np.zeros(img.shape, np.uint8) outImg += 255 for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) if isNumber(x, y, w, h, img.shape): cv2.rectangle(imT,(x,y),(x+w,y+h),(0,255,0),5) outImg[y:y + h, x:x + w] = thresh[y:y + h, x:x + w] cv2.drawContours(outImg, [cnt], 0, (125, 125, 125), 5 ) cv2.imwrite('out/' + name + '.tiff', outImg) cv2.imwrite( 'contour/' + name + '.tiff', imT ) height, width = outImg.shape outImg = cv2.resize( outImg, ( 400, 400 * height / width ) ) image = Image.fromarray( outImg ) print pytesseract.image_to_string( image, config = "-psm 6 config" )
def ocr_img(image): # 切割题目和选项位置,左上角坐标和右下角坐标,自行测试分辨率 question_im = image.crop((50, 350, 1000, 560)) # 坚果 pro1 choices_im = image.crop((75, 535, 990, 1150)) # question = img.crop((75, 315, 1167, 789)) # iPhone 7P # 转化为灰度图 question_im = question_im.convert('L') choices_im = choices_im.convert('L') # 把图片变成二值图像。 question_im=binarizing(question_im,190) choices_im = binarizing(choices_im, 190) # img2=depoint(img1) #img1.show() # tesseract 路径 pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract' # 语言包目录和参数 tessdata_dir_config = '--tessdata-dir "C:\\Program Files (x86)\\Tesseract-OCR\\tessdata" --psm 6' # lang 指定中文简体 question = pytesseract.image_to_string(question_im, lang='chi_sim', config=tessdata_dir_config) question = question.replace("\n", "")[2:] choice = pytesseract.image_to_string(choices_im, lang='chi_sim', config=tessdata_dir_config) choices = choice.strip().split("\n") choices = [ x for x in choices if x != '' ] return question, choices
def _get_captcha(self, type): s = requests.Session() if type.upper() == 'R': try: r = s.get('http://www.sunat.gob.pe/cl-ti-itmrconsruc/captcha?accion=image') except s.exceptions.RequestException as e: return (False,e) img=Image.open(StringIO.StringIO(r.content)) captcha_val=pytesseract.image_to_string(img) captcha_val=captcha_val.strip().upper() return (s, captcha_val) elif type.upper() == 'D': try: r = s.get('https://cel.reniec.gob.pe/valreg/codigo.do') except s.exceptions.RequestException as e: return (False,e) img=Image.open(StringIO.StringIO(r.content)) img = img.convert("RGBA") pixdata = img.load() for y in xrange(img.size[1]): for x in xrange(img.size[0]): red, green, blue, alpha=pixdata[x, y] if blue<100: pixdata[x, y] = (255, 255, 255, 255) temp_captcha_val=pytesseract.image_to_string(img) temp_captcha_val=temp_captcha_val.strip().upper() captcha_val='' for i in range(len(temp_captcha_val)): if temp_captcha_val[i].isalpha() or temp_captcha_val[i].isdigit(): captcha_val=captcha_val+temp_captcha_val[i] return (s, captcha_val.upper())
def image2text(filepath, lang='rus'): import os from PIL import Image import pytesseract if os.path.exists(filepath): print pytesseract.image_to_string(Image.open(filepath), lang=lang) return pytesseract.image_to_string(Image.open(filepath), lang=lang)
def process(self, dir): img = cv2.imread(dir, 0) daima = self.jsonLoad(dir+'.desc.json', 'fapiaodaima', 'bounding_box') daima_x = int(float(daima['top_left'][0])) daima_y = int(float(daima['top_left'][1])) daima_w = int(float(daima['low_right'][0])) - daima_x daima_h = int(float(daima['low_right'][1])) - daima_y daima_text = self.jsonLoad(dir+'.desc.json', 'fapiaodaima', 'text').encode('utf-8') self.digits_num += len(daima_text) daima_img = cropImage(img, int(daima_x), int(daima_y), int(daima_h), int(daima_w)) cv2.imwrite('./tmp.png', daima_img) daima_predicted = pytesseract.image_to_string(Image.open('./tmp.png')) self.cal_digits_right(dir, daima_text, daima_predicted) os.remove('./tmp.png') haoma = self.jsonLoad(dir+'.desc.json', 'fapiaohaoma', 'bounding_box') haoma_x = int(float(haoma['top_left'][0])) haoma_y = int(float(haoma['top_left'][1])) haoma_w = int(float(haoma['low_right'][0])) - haoma_x haoma_h = int(float(haoma['low_right'][1])) - haoma_y haoma_text = self.jsonLoad(dir+'.desc.json', 'fapiaohaoma', 'text').encode('utf-8') self.digits_num += len(haoma_text) haoma_img = cropImage(img, int(haoma_x), int(haoma_y), int(haoma_h), int(haoma_w)) cv2.imwrite('./tmp.png', haoma_img) haoma_predicted = pytesseract.image_to_string(Image.open('./tmp.png')) self.cal_digits_right(dir, haoma_text, haoma_predicted) os.remove('./tmp.png')
def get_text(self): """Does OCR on this image.""" image_writer = ImageWriter("temp") try: temp_image = image_writer.export_image(self._image_obj) except PDFNotImplementedError: # No filter method available for this stream # https://github.com/euske/pdfminer/issues/99 return u"" try: text = image_to_string(Image.open("temp/" + temp_image), lang="fin") except IOError: # PdfMiner did not return an image # Let's try to create one ourselves # TODO: Create proper color_mode values from ColorSpace # Most of the times "L" will create something good enough # for OCR, though temp_image = Image.frombuffer("L", self._image_obj.srcsize, self._stream.get_data(), "raw", "L", 0, 1) text = image_to_string(temp_image, lang="fin") unlink("temp/" + temp_image) return text
def pic2str(x, lang=None): if 'http' == x[:4]: res = urllib2.urlopen(x) t = cStringIO.StringIO(res.read()) res.close() elif 'base64' in x: res = x.split('base64,')[1] t = cStringIO.StringIO(base64.decodestring(res)) else: return None image = Image.open(t) t = numpy.asarray(image) # 转换灰度图 gray = cv2.cvtColor(t, cv2.COLOR_BGR2GRAY) # 二值化 temp = int(t.max() / 2) thd, image_b = cv2.threshold(gray, temp, 255, cv2.THRESH_BINARY) c, r = image_b.shape image_b = cv2.resize(image_b, (r * 2, c * 2)) flag, image_a = cv2.imencode('.jpeg', image_b) if flag: image_ok = Image.open(cStringIO.StringIO(image_a.tostring())) if not lang: return pytesseract.image_to_string(image_ok) else: return pytesseract.image_to_string(image_ok, lang=lang) else: return None
def omocr2(img_name,path1,co):#ex:20150508104553-1 #print "B" timefin = '' content = "" patnum = path1[path1.rfind(u'\\'):len(path1)] #rfind find from right patnum1 = path1.replace(patnum, u'&') patnum2 = patnum1[patnum1.rfind(u'\\')+1:patnum1.rfind(u'&')-11] ispath = os.path.exists(u'path_OCR\\path_ocr') #print ispath print u"第"+str(co)+u"筆 " for x in range(y): str1 = piece1[x] str2 = patnum2.encode('utf-8') if str1 == str2 : content = piece1[x+1] print content.decode('UTF-8') strsp = path1+"\\"+img_name im = Image.open(strsp+u'.jpg').convert('L') isExists = os.path.exists(strsp) if isExists : im.crop((200, 2680, 650, 2770)).save(strsp+u'\\day.png') im.crop((200, 2770, 550, 2860)).save(strsp+u'\\time.png') im.crop((3240, 2770, 3560, 2850)).save(strsp+u'\\spdown.png') im.crop((3080, 2680, 3400, 2770)).save(strsp+u'\\spup.png') im.crop((15, 2130, 900, 2680)).save(strsp+u'\\li.png') day = pytesseract.image_to_string(Image.open(strsp+u'\\day.png')).replace('/','').replace(" ", "").replace("O", "0") time = pytesseract.image_to_string(Image.open(strsp+u'\\time.png')).replace(':','').replace(" ", "").replace("O", "0") spdown ="0"+pytesseract.image_to_string(Image.open(strsp+u'\\spdown.png')).replace(" ", "").replace("O", "0") spup = pytesseract.image_to_string(Image.open(strsp+u'\\spup.png')).replace(" ", "").replace("O", "0") day=filter(str.isdigit, day) time=filter(str.isdigit, time) spdown=filter(str.isdigit, spdown) spup=filter(str.isdigit, spup) day2 = day[0:8] time3 = img_name[8:12]#時間抓檔案名稱 if day[0] == "2": day2 = int(day2)-19110000 time2 = time[0:4] spup2 = spup[0:3] spdown2 = spdown[0:3] if time3 == time2 : timefin = time2 else: timefin = time3 f = file(strsp+'\\'+img_name+'.txt', 'w+') f.write(img_name+u';'+str(day2)+u';'+str(timefin)+u';'+str(spup2)+u';'+str(spdown2)+u';') # write text to file if ispath : f.write(content)#寫入抓到的照相地點 else: f.write(' ; ') f.close() print u'檔 名:'+img_name print u'日 期:'+str(day2) print u'時 間:'+timefin print u'速 限:'+spup2 print u'車 速:'+spdown2 print "OK2"
def setperson(u0, p0): driver = webdriver.PhantomJS(executable_path='D:\phantomjs-2.1.1-windows\phantomjs.exe') # driver = webdriver.Firefox() url = 'http://ssfw.tjut.edu.cn/ssfw/login/ajaxlogin.do' driver.get(url) driver.maximize_window() driver.save_screenshot('static\images\i2.jpg') image = Image.open('static\images\i2.jpg') box = (703, 149, 766, 170) # box = (700, 130, 766, 150) image = image.crop(box) image = image.convert('L') image = image.convert('RGB') image.save('static\images\i4.jpg') print pytesseract.image_to_string(image).replace('\t', '') y0 = pytesseract.image_to_string(image).replace('\t', '') driver.find_element_by_id('j_username').send_keys(u0) driver.find_element_by_id('j_password').send_keys(p0) driver.find_element_by_id('validateCode').send_keys(y0) driver.find_element_by_id('loginBtn').click() time.sleep(2) js = 'window.location.href="http://ssfw.tjut.edu.cn/ssfw/xjgl/jbxx.do"' driver.execute_script(js) print driver.current_url try: # print driver.find_element_by_id('form1').find_element_by_id('yxdm').getText() print driver.find_element_by_id('xh').get_attribute('value') # 学号 student_id = driver.find_element_by_id('xh').get_attribute('value') print driver.find_element_by_id('xm').get_attribute('value') # 姓名 student_name = driver.find_element_by_id('xm').get_attribute('value') print driver.find_element_by_id('xbdm').get_attribute('value') # 性别 sex = driver.find_element_by_id('xbdm').get_attribute('value') print driver.find_element_by_id('njdm').get_attribute('value') # 年级 grade = driver.find_element_by_id('njdm').get_attribute('value') print driver.find_element_by_id('yxdm').get_attribute('value') # 院系college college = driver.find_element_by_id('yxdm').get_attribute('value') print driver.find_element_by_id('zydm').get_attribute('value') # 专业major major = driver.find_element_by_id('zydm').get_attribute('value') print driver.find_element_by_id('bjh').get_attribute('value') # 所在班级 inclass = driver.find_element_by_id('bjh').get_attribute('value') print driver.find_element_by_id('xzdm').get_attribute('value') # 学制 length_of_schooling = driver.find_element_by_id('xzdm').get_attribute('value') driver.close() try: print "................." u = User(student_id=student_id, student_name=student_name, sex=sex, grade=grade, college=college, major=major, inclass=inclass, length_of_schooling=length_of_schooling) db.session.add(u) db.session.commit() except Exception, e: print e pass except Exception, e: driver.close() print e setperson(u0, p0)
def omocr(img_name,path1,co):#ex:20150512_124104_906_1794_ # print "A" content = "" patnum = path1[path1.rfind(u'\\'):len(path1)] #rfind find from right patnum1 = path1.replace(patnum, u'&') patnum2 = patnum1[patnum1.rfind(u'\\')+1:patnum1.rfind(u'&')-11] ispath = os.path.exists(u'path_OCR\\path_ocr') # print ispath print u"第"+str(co)+u"筆 " for x in range(y): str1 = piece1[x] str2 = patnum2.encode('utf-8') if str1 == str2 : content = piece1[x+1] print content.decode('UTF-8') strsp = path1+"\\"+img_name im = Image.open(strsp+u'.jpg').convert('L') isExists = os.path.exists(strsp) if isExists : im.crop((185, 30, 480, 85)).save(strsp+u'\\day.png') im.crop((185, 90, 330, 150)).save(strsp+u'\\time.png') im.crop((959, 90, 1180, 150)).save(strsp+u'\\spdown.png') im.crop((959, 25, 1180, 85)).save(strsp+u'\\spup.png') im.crop((1, 1400, 610, 1710)).save(strsp+u'\\li.png') day = pytesseract.image_to_string(Image.open(strsp+u'\\day.png')).replace('/','').replace(" ", "").replace("O", "0") time = pytesseract.image_to_string(Image.open(strsp+u'\\time.png')).replace(':','').replace(" ", "").replace("O", "0") spdown = pytesseract.image_to_string(Image.open(strsp+u'\\spdown.png')).replace(" ", "").replace("O", "0") spup = pytesseract.image_to_string(Image.open(strsp+u'\\spup.png')).replace(" ", "").replace("O", "0") day=filter(str.isdigit, day) time=filter(str.isdigit, time) spdown=filter(str.isdigit, spdown) spup=filter(str.isdigit, spup) day2 = day[0:8] #day2 = img_name[0:6]+day[len(day)-2:len(day)]#日期年月抓檔案名稱日抓OCR辨識 if day[0] == "2": day2 = int(day2)-19110000 time2 = time[0:4] spdown2 = spdown[0:3] spup2 = spup[0:3] f = file(strsp+'\\'+img_name+'.txt', 'w+') f.write(img_name+u';'+str(day2)+u';'+str(time2)+u';'+str(spup2)+u';'+str(spdown2)+u';') # write text to file if ispath : f.write(content)#寫入抓到的照相地點 else: f.write(' ; ') f.close() print u'檔 名:'+img_name print u'日 期:'+str(day2) print u'時 間:'+time2 print u'速 限:'+spup2 print u'車 速:'+spdown2 print "OK"
def main(argv): import getopt url = "" try: opts, args = getopt.getopt(argv, "", ["url="]) except getopt.GetoptError: print("Run with --url=http://mysite.com/myImage.png") sys.exit(2) for opt, arg in opts: if opt == "--url": url = arg import PIL from PIL import Image from PIL import ImageOps import pytesseract import urllib.request urllib.request.urlretrieve(url, "local.png") img = Image.open("local.png") img = ImageOps.grayscale(img) img = img.resize((img.width * 2, img.height * 2)) text = pytesseract.image_to_string( img, None, False, "-c tessedit_char_whitelist=0123456789-ABCDEFGHIJKLMNOPQRSTUVWXYZ") text += " " text += pytesseract.image_to_string( img, None, False, "-c tessedit_char_whitelist=0123456789-abcdefghijklmnopqrstuvqxyz") chunks = text.split() validChunks = list() for c in chunks: if c.count("-") == 2: validChunks.append(c) if c.count("-") == 4: validChunks.append(c) print("Possible keys:") for c in validChunks: print(c)
def main(): cap = cv2.VideoCapture(0) i = 0 while True: ret, frame = cap.read() bw_img = cv2.cvtColor(frame, cv.CV_BGR2GRAY) if i == 15: im = Image.fromarray(np.uint8(cm.gist_earth(bw_img)*255)) print pytesseract.image_to_string(im) i = 0 i += 1 cv2.imshow("camera", bw_img) c = cv2.waitKey(1)
def parse(img): if img.startswith('http'): req = urllib.request.urlopen(img) data = req.read() im = BytesIO() im.write(data) image = Image.open(im) vcode = pytesseract.image_to_string(image) return vcode else: im = Image.open(img) vcode = pytesseract.image_to_string(im) return vcode
def performTessOCR(self,imagePath): """ Performs OCR with Tesseract. Please train Tesseract Elsewhere if necessary. JTessBox is a nice program implemented with VietOCR. SVM is used for OCR (e.g. covariance matrices with letter images appended as pixels) or transforms. *Required Parameters* :param imagePath: string ipath,fp,cstr of Image, or PIL Image """ if type(imagePath) is str or type(imagePath) is cStringIO or type(imagePath) is file: return image_to_string(Image.open(imagePath),True) else: return image_to_string(imagePath, True)
def get_ocr_strings(evidence, helper): # This is the actual OCR call try: cached = os.path.isfile(evidence['file_cache_path']) if cached: return pytesseract.image_to_string(Image.open(evidence['file_cache_path'])) else: strings = pytesseract.image_to_string(Image.open( helper.pathspec_helper._open_file_object(evidence['pathspec']))) helper.pathspec_helper._close_file_object(evidence['pathspec']) return strings except: logging.warn('Failed to perform OCR on file "' + evidence['file_cache_path'] + '"') abort(400, 'It appears that the pathspec is for a file that the Tesseract cannot perform OCR on')
def do_ocr(self): import subprocess temp_filename = os_path.join("temp", "ocr.png") try: arglist = ["gs", "-dSAFE", "-dQUIET", "-o" + temp_filename, "-sDEVICE=pnggray", "-r600", "-dFirstPage=" + str(self.page_number), "-dLastPage=" + str(self.page_number), self.pdf_path] msg = subprocess.check_output(args=arglist, stderr=subprocess.STDOUT) print msg except OSError as e: logging.error("Failed to run GhostScript." + "I/O error({0}): {1}".format(e.errno, e.strerror)) # Do OCR import time time.sleep(1) # make sure the server has time to write the files Image.MAX_IMAGE_PIXELS = None # Allow very large files text = image_to_string( Image.open(temp_filename), lang="fin") unlink(temp_filename) return text
def crackVCode(self, vcodeUrl, tempDir, fileName, session): result=False try: if vcodeUrl: if not os.path.exists(tempDir): os.makedirs(tempDir) pngFileName = os.path.join(tempDir, fileName+'.png') outfile=open(pngFileName, 'wb') resp = session.get(vcodeUrl) outfile.write(resp.content) outfile.close() png = Image.open(pngFileName) png.load() bg=Image.new("RGB", png.size, (255, 255, 255)) bg.paste(png, mask=png.split()[3]) jpgFileName = os.path.join(tempDir, fileName+'.jpg') bg.save(jpgFileName, 'JPEG', quality=80) image = Image.open(jpgFileName) result = pytesseract.image_to_string(image) else: print('ERROR: fileUrl is NULL!') except Exception,e: import traceback traceback.print_exc() print('Exception when crack the vcode', e) result=None
def parse(self): """runs each mask(crop) across the image file to improve OCR functionality""" image = Image.open(self.image_path) for form_field, bounding_box in self.bounding_box_dict.items(): # the crops are scaled up and the contrast maxed out in order to enhance character # features and increase OCR success x1, y1, x2, y2 = bounding_box xx = (x2-x1) << 2 yy = (y2-y1) << 2 the_crop = image.crop(bounding_box) the_crop = the_crop.resize((xx,yy),PIL.Image.LANCZOS) area = (xx * yy) gray = the_crop.convert('L') bw = np.asarray(gray).copy() bw[bw < 200] = 0 bw[bw >= 200] = 255 the_crop = misc.toimage(bw) # use this to check out a particular mask #if "box_c_address_city_town_zip_postal_code" is form_field: # the_crop.show() if "checkbox" in form_field: # a box is considered checked if 10% or more of it's area is black checked = np.sum(bw) >= (0.1 * area) self.component_contents_dict[form_field] = checked else: self.component_contents_dict[form_field] = self.clean_text(pytesseract.image_to_string(the_crop)) print([self.component_contents_dict['box_c_address_city_town_zip_postal_code']])
def get_screen_text(self): """ Return the text from a text console. Uses OCR to scrape the text from the console image taken at the time of the call. """ image_str = self.get_screen() # Write the image string to a file as pytesseract requires # a file, and doesn't take a string. tmp_file = tempfile.NamedTemporaryFile(suffix='.jpeg') tmp_file.write(image_str) tmp_file.flush() tmp_file_name = tmp_file.name # Open Image file, resize it to high resolution, sharpen it for clearer text # and then run image_to_string operation which returns unicode that needs to # be converted to utf-8 which gives us text [typr(text) == 'str'] # higher resolution allows tesseract to recognize text correctly text = (image_to_string(((Image.open(tmp_file_name)).resize((7680, 4320), Image.ANTIALIAS)).filter(ImageFilter.SHARPEN), lang='eng', config='--user-words eng.user-words')).encode('utf-8') tmp_file.close() logger.info('screen text:{}'.format(text)) return text
def ocr_captcha(image): """ Recognize the characters in image using pytesseract :param image: A PIL.Image object :return: text str """ # Generate a new image (50x20) as a container to let the original # captcha paste on it in order to be recognized by tesseract-ocr out = Image.new('RGB', (50, 20), (255,)*3) out.paste(image, (5, 5)) # Convert the image to grey-scale out = out.convert('L') # Binarization threshold = 140 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) out = out.point(table, '1') text = pytesseract.image_to_string(out) if text.isnumeric() is False: raise CaptchaIsNotNumberException(text) return text
def to_str(self): """ 处理图片,然后OCR """ name = 'test.png' img = Image.open(name) img = self.__resize(img) # 灰度 img2 = img.convert('L') # img2.save('l-' + name) # 去杂色 table = [] for i in range(256): if i == 135 - 1: table.append(0) # 1, 白色, 0, 黑色 else: table.append(1) # 黑色 out = img2.point(table, '1') out.save('dst-' + name) # 转成字符串 text = image_to_string(out) print text
def to_str2(self): """ 处理图片,然后OCR """ name = 'appcaptcha.png' img = Image.open(name) # img = self.__resize(img) # 灰度 img2 = img.convert('L') img2.save('bak2.png') # 去杂色 table = [] for i in range(256): # 1, 白色, 0, 黑色 if i < 240: table.append(0) else: table.append(1) # 黑色 img3 = img2.point(table, '1') img3.save('bak3.png') img3 = self.__resize(img3) # 转成字符串 text = image_to_string(img3) print text
def get_captcha_by_OCR(self,data): # OCR 识别 img = Image.open(BytesIO(data)) img = img.Convert('L') captcha = pytesseract.image_to_string(img) img.close() return captcha
def print_text(img): try: text = pytesseract.image_to_string(img, lang="eng") if text: print(text) except Exception: pass
def capture(): c = Clickonacci() c.run() x = '' x += pytesseract.image_to_string(Image.open('C:/Python27/output.png')) lang = '' if('include' in x and 'std::' not in x and 'namespace' not in x): lang = 'C' elif('include' in x): lang = 'CPP' elif('php' in x): lang = 'PHP' elif('func main' in x): lang = 'GO' elif('def' in x): lang = 'RUBY' elif('write' in x or 'writeln' in x): lang = 'PERL' elif('using System' in x): lang = 'CSHARP' else: lang = 'JAVA' def fix(x): if(lang == 'C'): x = x.replace('%If','%lf') elif(lang == 'JAVA'): while(x.count('|') != 0): a = x.index('|') if(x[a+1] == 'n' and x[a+2] == 't'): x = x.replace(x[a],'I') else: x = x.replace(x[a],'l') x = x[2:len(x)] elif(lang == 'CSHARP'): x = x.replace('Tolnt','ToInt') elif(lang == 'GO' or lang == 'RUBY'): x = x.replace('|','l') fix(x) x = x.decode('utf-8') data = { 'client_secret': CLIENT_SECRET, 'async': 0, 'source': x, 'lang': lang, 'time_limit': 5, 'memory_limit': 262144, } r = requests.post(RUN_URL, data=data) out = r.json() next_out = out['run_status'] output = '' for element in next_out: if(element != 'output_html'): output += (str(element)+' : '+str(next_out[element])) output += '\n' output = output.decode('utf-8') return render_template('index.html',lang=lang,input1 = x, output = output)
def image_to_string(img): image=Image.open(img) image.save('./tmp.jpg') image=Image.open('./tmp.jpg') vcode=pytesseract.image_to_string(image) print vcode return vcode
def make_data_and_cookies(r): vcode = '' while len(vcode) is not 4: soup = bs4.BeautifulSoup(r.get(BASE_URL).text, "lxml") img_url = BASE_URL + soup.find('img', id='loginform-verifycode-image').get('src') vcv = soup.find('input', type='hidden').get('value') img = Image.open(BytesIO(ses.get(img_url).content)) if configurations.USE_TESSERACT: # 使用了自定义的语言数据 vcode = pytesseract.image_to_string(img, lang='ar', config="--psm 7 digits") print(vcode) else: img = img.convert('1') img = img.resize((int(img.width * 0.5), int(0.4 * img.height))) pt = img.load() for y in range(0, img.height - 4): for x in range(img.width): print('*' if pt[x, y] == 255 else ' ', end='') print() vcode = input('验证码:') return { "LoginForm[username]": USERNAME, "LoginForm[password]": PASSOWORD, "LoginForm[verifyCode]": vcode, "_csrf": vcv, "login-button": "" }
def ExtractText(self, _CV2Image, _Image_Contours, _Threshold_Delta2): if not _Image_Contours: return '', None _PlateNumber = [] _Rect = [] for _, _nContour in enumerate ( _Image_Contours ): if not self.ValidateRotationAndRatio( cv2.minAreaRect( _nContour ) ): continue _Left, _Top, _Width, _Height = cv2.boundingRect( _nContour ) _Plate_Image = _CV2Image [ _Top : _Top + _Height, _Left : _Left + _Width ] if not self.IsWhitePlate( _Plate_Image ): continue _Clean_Plate, _Clean_Rect = self.CleanPlate( _Plate_Image, _Threshold_Delta2, cv2.RETR_EXTERNAL) if not _Clean_Rect: continue _Clean_Plate_Image = Image.fromarray(_Clean_Plate) _txt = ARK_Utils.CleanText(tess.image_to_string( _Clean_Plate_Image, lang= 'eng' )) if not _txt.isalpha() and '' != _txt: _PlateNumber.append(_txt) _Rect.append(_Clean_Rect) return _PlateNumber, _Rect
def perform_ocr(self, image: Image.Image, colors: List[Color]) -> str: """Perform OCR on an image""" # image.save("before.png") image = self.prepare_image(image, colors) # image.save("after.png") self.conn.send("Performing OCR...") return image_to_string(image)
M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) ocr4 = Image.fromarray(ocr_img) # Remove Noises kernel = np.ones((1, 1), np.uint8) # Dilating the Image ocr_img = cv2.dilate(ocr_img, kernel, iterations=1) ocr5 = Image.fromarray(ocr_img) # Eroding the Image ocr_img = cv2.erode(ocr_img, kernel, iterations=1) ocr6 = Image.fromarray(ocr_img) # Obtain the OCR Text ocr_res = pytesseract.image_to_string(Image.fromarray(ocr_img)).lower() # Print The OCR Result cv2.putText(frame, ocr_res, (210, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # Operations for OCR Translate if cv2.waitKey(1) & 0xFF == ord('t'): # If 't' is Pressed Translate the OCR Result to Translation Language ocr_trans = gt.translate(ocr_res, dest='tr', src='en').text # Print the Translated Text cv2.putText(frame, 'Translated Text : ' + ocr_trans, (160, 135), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # Operations for Credit Card OCR if cv2.waitKey(1) & 0xFF == ord('r'):
def img_pipeline(): # pytesseract.pytesseract.tesseract_cmd = r".\Tesseract-OCR\tesseract.exe" # Reading in the images to process # Test images # img_one = cv2.imread("images/alamy.jpg") # Nope lol # img_two = cv2.imread("images/amoxicillin.jpeg") # Good # img_three = cv2.imread("images/calvin.jpeg") # Good but there needs to be some unnecessary text removed # img_four = cv2.imread("images/chris.jpg") # Not Good (directive) # img_five = cv2.imread("images/elvis.jpeg") # Good (duration) # img_six = cv2.imread("images/chris2.jpg") # Good Further processing required # img_seven = cv2.imread("images/jane.jpeg") # Good # img_eight = cv2.imread("images/miley.png") # Good # img_nine = cv2.imread("images/opioid-bottle.jpg") # Good # img_ten = cv2.imread("images/warfarin.jpg") # Good # tr_exe_dir = os.path.join(os.path.abspath( # os.path.dirname((__file__))), "Tesseract-OCR/tesseract.exe") # pytesseract.pytesseract.tesseract_cmd = tr_exe_dir pytesseract.pytesseract.tesseract_cmd = '/app/.apt/usr/bin/tesseract' img_list = [] # find the file with a .png extension for file in glob.glob('imageToSave.png'): img_list.append(file) if len(img_list) == 0: print("Can't find the file") exit() # Building the Image Processing Pipeline. # 1 # Resize image_three to make it easier for OCR to analyze # This size depends on the size of the picture's text # If the image is larger than a certain size reduce its size else increase its size # Change this so the user can select the image resized = resize(cv2.imread(img_list[0])) # 2 # Convert the image to grayscale to remove any filtering greyscale = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) # # 3 # # Find some edges # edges = cv2.Canny(greyscale, 30, 200) # cv2.waitKey(0) # # Find the contours # _, contours = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # cv2.imshow("Canny edges after contouring", edges) # cv2.waitKey(0) # This is useful for reducing the noise from a background that is not homogeneous threshold = cv2.adaptiveThreshold(greyscale, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 85, 20) img_to_return = threshold # Use NLTK To complete the Directives and their Durations # Convert Directives and Logic into events on an ics file. # cv2.imshow("IMG2", resized) # cv2.imshow("IMG1", threshold) # Keep the image open cv2.waitKey(0) # Use NLTK To complete the Directives and their Durations # Convert Directives and Logic into events on an ics file. preprocessed_string = pytesseract.image_to_string(img_to_return) # I had to write the text to a file to remove the formatting that pytessaract has on the string write_to_file("tessaract_text.txt", preprocessed_string) string_from_file = get_text("tessaract_text").replace("\n", " ") return string_from_file
import pytesseract as tess tess.pytesseract.tesseract_cmd = r'C:/Users/MME/AppData/Local/Tesseract-OCR/tesseract.exe' from PIL import Image img = Image.open('barcode.jpg') text = tess.image_to_string(img) print(text)
if len(approx) == 4: screenCnt = approx break # show the contour (outline) of the piece of paper cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2) cv2.imshow("Outline", image) cv2.waitKey(0) cv2.destroyAllWindows() output_image = cv2.resize(image, (650, 650)) cv2.imshow("output", output_image) cv2.imwrite('out/' + 'output-Image.PNG', output_image) cv2.waitKey(0) output = tess.image_to_string( PIL.Image.open('out/' + 'output-Image.PNG').convert("RGB"), lang='eng') email = re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", output) phone = re.findall(r'[\+\(]?[1-9][0-9 .\-\(\)]{8,}[0-9]', output) email = email[0] phone = phone[0] # Call http post request to send data to backend payload = {'phone': phone, 'email': email} headers = { 'content-type': 'application/json', } params = (('priority', 'normal'), )
import os import cv2 import pytesseract os.chdir("data") plates = os.listdir() print("---------------------------------") for plate in plates: plate_number = cv2.imread(plate) text = pytesseract.image_to_string(plate_number) cv2.ShowImage("plate_number",plate_number) cv2.waitKey(200) print("real plate: "+plate[:-4]) print("plate read:"+text) print("---------------------------------") os.chdir("..")
def detect(cpp): global result, taxi frame = cv2.imread(cpp) blob = dnn.blobFromImage(frame, inScaleFactor, (inWidth, inHeight), meanVal) net.setInput(blob) t0 = time.time() detections = net.forward() cols = frame.shape[1] rows = frame.shape[0] if cols / float(rows) > WHRatio: cropSize = (int(rows * WHRatio), rows) else: cropSize = (cols, int(cols / WHRatio)) y1 = (rows - cropSize[1]) / 2 y2 = y1 + cropSize[1] x1 = (cols - cropSize[0]) / 2 x2 = x1 + cropSize[0] cols = frame.shape[1] rows = frame.shape[0] i = 1 for i in range(detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > 0.3: class_id = int(detections[0, 0, i, 1]) xLeftBottom = int(detections[0, 0, i, 3] * cols) yLeftBottom = int(detections[0, 0, i, 4] * rows) xRightTop = int(detections[0, 0, i, 5] * cols) yRightTop = int(detections[0, 0, i, 6] * rows) #expand box xLeftBottom = xLeftBottom - buffer yLeftBottom = yLeftBottom - buffer xRightTop = xRightTop + buffer yRightTop = yRightTop + buffer if xLeftBottom < 0: xLeftBottom = 0 if yLeftBottom < 0: yLeftBottom = 0 crop_img = frame[yLeftBottom:yRightTop,xLeftBottom:xRightTop] taxi, wimg = bincount_app(crop_img) cv2.imshow(cpp, wimg) cv2.waitKey(0) cv2.destroyAllWindows() if taxi: text = image_to_string(wimg, config=config) else: text = image_to_string(cv2.bitwise_not(wimg), config=config) result = process_text(text) if len(result) <= 3: result = 'No License Plate Found' return result, taxi
def code(request): im = Image.open('out.png') vcode = pytesseract.image_to_string(im) print(vcode)
def get_img_text(img): """Get cleaned text from image.""" img_text = pytesseract.image_to_string(img) return _clean_string(img_text)
# -*- coding: utf-8 -*- """ Created on Fri Sep 21 12:31:59 2018 @author: DELL """ import pytesseract from PIL import Image m = Image.open("d1.jpg") text = pytesseract.image_to_string(m, lang = 'eng') text=pytesseract.image_to_string(m, config='-psm 6') text import matplotlib.image as mpimg img = mpimg.imread('d1.jpg') img = rgb2gray(imread('image.jpg')); import numpy as np import matplotlib.pyplot as plt charlie = plt.imread('d1.jpg') # colormaps plt.cm.datad # cmaps = set(plt.cm.datad.keys()) cmaps = {'afmhot', 'autumn', 'bone', 'binary', 'bwr', 'brg', 'CMRmap', 'cool', 'copper', 'cubehelix', 'Greens'} X = [ (4,3,1, (1, 0, 0)), (4,3,2, (0.5, 0.5, 0)), (4,3,3, (0, 1, 0)), (4,3,4, (0, 0.5, 0.5)), (4,3,(5,8), (0, 0, 1)), (4,3,6, (1, 1, 0)), (4,3,7, (0.5, 1, 0) ), (4,3,9, (0, 0.5, 0.5)),
url2 = "http://158.69.76.135/captcha.php" user_id = "679" dict_data = { "id": user_id, "holdthedoor": user_id, "key": value, "captcha": "" } s = requests.Session() headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", "Referer": url } c = 0 while True: r = s.get(url, headers=headers) soup = BeautifulSoup(r.content, "lxml") value = soup.find("input", {"name": "key"})['value'] dict_data["key"] = value r1 = s.get(url2, headers=headers) img = Image.open(BytesIO(r1.content)) dict_data["captcha"] = pytesseract.image_to_string(img) print(pytesseract.image_to_string(img)) res = s.post(url, headers=headers, data=dict_data) if res.text[:3] == "See": continue c += 1 if c == 1024: break
sys.exit(1) # Read image path from command line imPath = sys.argv[1] # Uncomment the line below to provide path to tesseract manually # pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # Define config parameters. # '-l eng' for using the English language # '--oem 1' sets the OCR Engine Mode to LSTM only. # # There are four OCR Engine Mode (oem) available # 0 Legacy engine only. # 1 Neural nets LSTM engine only. # 2 Legacy + LSTM engines. # 3 Default, based on what is available. # # '--psm 3' sets the Page Segmentation Mode (psm) to auto. # Other important psm modes will be discussed in a future post. config = ('-l eng --oem 1 --psm 3') # Read image from disk im = cv2.imread(imPath, cv2.IMREAD_COLOR) # Run tesseract OCR on image text = pytesseract.image_to_string(im, config=config) # Print recognized text print(text)
def OCR(image): print('Starting OCR Process') im = image image_x = im.size[0] image_y = im.size[1] new_image_x = myround(im.size[0], 594) new_image_y = myround(im.size[1], 1037) slices = new_image_x // 594 * (new_image_y // 1037) leftover_x = image_x - new_image_x leftover_y = image_y - new_image_y im = im.crop( (0, 0, image_x - leftover_x, image_y - leftover_y)).save('card.png') tiles = image_slicer.slice('card.png', slices, False) excel_info = [] for tile in tiles: card_ID = randint(100000, 999999) name = ('card {0}.png').format(card_ID) print('Processing:', name) tile.save(name) string = pytesseract.image_to_string(Image.open(name)) numbers = (',').join(extract_phone_numbers(string)) emails = (',').join(extract_email_addresses(string)) names = (',').join(extract_names(string)) sites = (',').join(extract_web_addresses(string)) info = [card_ID, names, sites, emails, numbers, string] excel_info.append(info) print('Finished reading and seperating cards. Saving information...') excel = win32.gencache.EnsureDispatch('Excel.Application') workbook = xlsxwriter.Workbook('cardInfo.xlsx') worksheet = workbook.add_worksheet() bold = workbook.add_format({'bold': True}) excel_info = tuple(excel_info) row = 1 col = 0 print('Creating Excel file') worksheet.write('A1', 'Card ID', bold) worksheet.write('B1', 'Client Name', bold) worksheet.write('C1', 'Websites', bold) worksheet.write('D1', 'Emails', bold) worksheet.write('E1', 'Phone Numbers', bold) worksheet.write('F1', 'Unorganized Info', bold) print('Saving Information onto Excel file') for card_number, name, site, email, number, text in excel_info: worksheet.write(row, col, card_number) worksheet.write(row, col + 1, name) worksheet.write(row, col + 2, site) worksheet.write(row, col + 3, email) worksheet.write(row, col + 4, number) worksheet.write(row, col + 5, text) row += 1 workbook.close() print('Formatting Excel File') dir_path = os.path.dirname(os.path.realpath(__file__)) dir_path = dir_path.replace('\\', '/') dir_path = dir_path + '/cardInfo.xlsx' wb = excel.Workbooks.Open(dir_path) ws = wb.Worksheets('Sheet1') ws.Columns.AutoFit() wb.Save() excel.Application.Quit() print( 'Finished OCR process. Please be aware that cards may not scan properly if the card is not simply designed. Double check information on the excel file!' ) print( 'Or you can simply reference a card by using the generated pictures. The ID on the Excel file uses the same ID as the image file.' )
# Defino la URL-principal y la ruta al driver de chrome main_url = 'http://158.69.76.135/level3.php' # URL principal chromedriver = './chromedriver' # Abrimos una ventana con la URL-principal dcap = dict(DesiredCapabilities.PHANTOMJS) dcap["phantomjs.page.settings.userAgent"] = ( "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36" ) for i in range(1024): browser = webdriver.PhantomJS( executable_path= '/Users/holberton/hodor/level_3/phantomjs-2.1.1-macosx/bin/phantomjs', desired_capabilities=dcap) browser.get(main_url) browser.save_screenshot('screen.png') captcha = Image.open('screen.png') left = 0 top = 695 right = 65 bottom = 726 captcha1 = captcha.crop((left, top, right, bottom)) captcha1.save("captcha.png") text = pytesseract.image_to_string(Image.open('captcha.png')) formulario = browser.find_element_by_name('id') captchafield = browser.find_element_by_name('captcha') submit = browser.find_element_by_name('holdthedoor') formulario.send_keys('1160') captchafield.send_keys(text) submit.click() browser.close()
# -*- encoding:utf-8 -*- # author: liuheng """利用自动化测试工具,对字体截图,再利用OCR文字识别""" import asyncio from pyppeteer import launch import pytesseract from PIL import Image async def crawl(): url = "http://www.porters.vip/confusion/movie.html" browser = await launch(headless=False) page = await browser.newPage() await page.goto(url) font = await page.screenshot({ 'path': './font.png', 'clip': { 'x': 820, 'y': 400, 'width': 75, 'height': 40 } }) await browser.close() if __name__ == '__main__': # asyncio.get_event_loop().run_until_complete(crawl()) pytesseract.pytesseract.tesseract_cmd = r'S:\\Tesseract\\tesseract.exe' content = pytesseract.image_to_string(Image.open('./font.png')) print(content)
import cv2 import pdf2image import pytesseract import numpy as np from PIL import Image pages = pdf2image.convert_from_path(pdf_path='./Statement20190630.pdf', dpi=500) for i, page in enumerate(pages): page = np.array(page) gray = cv2.cvtColor(page, cv2.COLOR_BGR2GRAY) gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] filename = './temp.png' cv2.imwrite(filename, gray) text = pytesseract.image_to_string(Image.open(filename)) print(text) # page.save(f'out_{i}.jpg', 'JPEG') print('done')
def reconhecer_caracteres(imagem, layout): return image_to_string( imagem, config= f"""--psm {layout} -c tessedit_char_whitelist={ascii_letters + digits}""")
gray = cv2.cvtColor(imgWarpColored, cv2.COLOR_BGR2GRAY) # blur = cv2.GaussianBlur(gray, (3, 3), 0) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] kernel = cv2.getStructuringElement( cv2.MORPH_RECT, (gausionsDilate, gausionsDilate)) opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1) # closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) invert = 255 - opening # cv2.imshow('Bien So', invert) config = r'--oem 1 --psm 7 outputbase' data = pytesseract.image_to_string(invert, lang='eng', config=config) check(data, listOfResult[i], True) if len(numberPlate) == 0: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) thresh = cv2.Canny(gray, 20, 200) contours, h = cv2.findContours(thresh, 1, 2) largest_rectangle = [0, 0] for cnt in contours: approx = cv2.approxPolyDP(cnt, 0.05 * cv2.arcLength(cnt, True), True) if len(approx) == 4: area = cv2.contourArea(cnt) if area > largest_rectangle[0] and area >= img.shape[1] * img.shape[0] * 0.01 and area <= img.shape[ 1] * \ img.shape[0] * 0.9:
#==================================================================================== #--> IMPORTAMOS LA LIBRERIA PARA OCR EN PYTHON 3 PYTESERACT try: from PIL import Image except ImportError: import Image import pytesseract #--> UBICACION PAQUETE TESSERACT EN LOCAL pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract' #--->SE GUARDA LOS DATOS DE IMAGEN PROCESADA POR PYTESSERACT d = pytesseract.image_to_string(Image.open("jhon4.png")) print(d) #imprime el string de datos obtenidos a traves de pytesseract
"""linha de código colocada como comentário pois não foi necessário inverter a coloração para negativa""" #img = cv2.bitwise_not(img) #aumentando o tamanho da imagem img = cv2.resize(img, None, fx = 2, fy = 2) #rodando o comando de dilatação da imagem para facilitar a leitura do pytesseract kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5)) dilated = cv2.dilate(img, kernel, iterations= 1) #removendo a coloração cinza do fundo da imagem ret, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) #otimizando a imagem para facilitar a leitura pelo pytesseract ret2, img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) #colocando efeito para melhorar a qualidade da imagem (desembaçamento), facilitando leitura do pytesseract blur = cv2.GaussianBlur(img, (5, 5), 0) ret3, img = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) #criando um arquivo no diretório após a aplicação de todas as camadas de aperfeiçoamento da imagem, mostrando em tela até que algo seja pressionado e o programa extraia o texto. cv2.imwrite("otimizada.jpg", img) cv2.imshow("img", img) cv2.waitKey(0) cv2.destroyAllWindows() #tesseract puxa a imagem trabalhada, com algumas configurações definidas para que facilite o entendimento do texto, deixando explicito quais caracteres poderão ser utilizados, removendo os espaços entre caracteres e colocando tudo em minúsculo. texto = pytesseract.image_to_string(img, config= '--psm 8 --dpi 200 --oem 0 -c tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ').replace(" ", "").lower() #apresentação do texto após OpenCV e Pytesseract print(texto)
def recognize_plate(img, coords): # separate coordinates from box xmin, ymin, xmax, ymax = coords # get the subimage that makes up the bounded region and take an additional 5 pixels on each side box = img[int(ymin)-5:int(ymax)+5, int(xmin)-5:int(xmax)+5] # grayscale region within bounding box gray = cv2.cvtColor(box, cv2.COLOR_RGB2GRAY) # resize image to three times as large as original for better readability gray = cv2.resize(gray, None, fx = 3, fy = 3, interpolation = cv2.INTER_CUBIC) # perform gaussian blur to smoothen image blur = cv2.GaussianBlur(gray, (5,5), 0) #cv2.imshow("Gray", gray) #cv2.waitKey(0) # threshold the image using Otsus method to preprocess for tesseract ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV) #cv2.imshow("Otsu Threshold", thresh) #cv2.waitKey(0) # create rectangular kernel for dilation rect_kern = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) # apply dilation to make regions more clear dilation = cv2.dilate(thresh, rect_kern, iterations = 1) #cv2.imshow("Dilation", dilation) #cv2.waitKey(0) # find contours of regions of interest within license plate try: contours, hierarchy = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) except: ret_img, contours, hierarchy = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # sort contours left-to-right sorted_contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0]) # create copy of gray image im2 = gray.copy() # create blank string to hold license plate number plate_num = "" # loop through contours and find individual letters and numbers in license plate for cnt in sorted_contours: x,y,w,h = cv2.boundingRect(cnt) height, width = im2.shape # if height of box is not tall enough relative to total height then skip if height / float(h) > 6: continue ratio = h / float(w) # if height to width ratio is less than 1.5 skip if ratio < 1.5: continue # if width is not wide enough relative to total width then skip if width / float(w) > 15: continue area = h * w # if area is less than 100 pixels skip if area < 100: continue # draw the rectangle rect = cv2.rectangle(im2, (x,y), (x+w, y+h), (0,255,0),2) # grab character region of image roi = thresh[y-5:y+h+5, x-5:x+w+5] # perfrom bitwise not to flip image to black text on white background roi = cv2.bitwise_not(roi) # perform another blur on character region roi = cv2.medianBlur(roi, 5) try: pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract' text = pytesseract.image_to_string(roi, config='-c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ --psm 8 --oem 3') # clean tesseract text by removing any unwanted blank spaces clean_text = re.sub('[\W_]+', '', text) plate_num += clean_text except: text = None if plate_num != None: print("License Plate #: ", plate_num) #cv2.imshow("Character's Segmented", im2) #cv2.waitKey(0) return plate_num
# Convert the image to gray scale gray = cv2.cvtColor(table, cv2.COLOR_BGR2GRAY) cv2.imwrite('img-gray.png', gray) # Performing OTSU threshold ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV) cv2.imwrite('thresh.png', thresh1) # Specify structure shape and kernel size. # Kernel size increases or decreases the area # of the rectangle to be detected. # A smaller value like (10, 10) will detect # each word instead of a sentence. text = pytesseract.image_to_string(thresh1) separated = text.split() #print(separated) offers = 0 not_word = False for word in separated: for letter in word: if letter.isdigit(): not_word = True if not_word == False: offers += 1 #print(offers) if separated[offers * 2 + 1] == 'k': final_price = separated[offers * 2] + 'k' else: final_price = separated[offers * 2]
if detected == 1: cv2.drawContours(img, [screenCnt], -1, (0, 255, 0), 3) # Masking the part other than the number plate mask = np.zeros(gray.shape, np.uint8) new_image = cv2.drawContours(mask, [screenCnt], 0, 255, -1, ) new_image = cv2.bitwise_and(img, img, mask=mask) # Now crop (x, y) = np.where(mask == 255) (topx, topy) = (np.min(x), np.min(y)) (bottomx, bottomy) = (np.max(x), np.max(y)) Cropped = gray[topx:bottomx + 1, topy:bottomy + 1] # Print number text = pytesseract.image_to_string(Cropped, config='--psm 11') print("programming_fever's License Plate Recognition\n") print("Detected license plate Number is:", text) img = cv2.resize(img, (500, 300)) Cropped = cv2.resize(Cropped, (350, 90)) #cv2.imshow('car', img) #cv2.imshow('Cropped', Cropped) # Display image cv2.imshow('Input image', img) cv2.imshow('License plate', Cropped) cv2.waitKey(0) cv2.destroyAllWindows() cv2.waitKey(0)
def extractText(dimensions): return pytesseract.image_to_string(imgEntire.crop(dimensions), lang="swe").replace("\n", ",")
import pytesseract import sys import argparse import cv2 pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe' try: import Image except ImportError: from PIL import Image from subprocess import check_output path = "C:\\Users\\jhemsley\\Pictures\\cap.jpg" # def resolve(path): # print("Resampling the Image") # check_output(['magick', path, '-resample', '600', path]) # return pytesseract.image_to_string(Image.open(path)) # # print(resolve(path)) img = cv2.imread("C:\\Users\\jhemsley\\Pictures\\cap.jpg") text = pytesseract.image_to_string(img) print(text)
dataframe_final=[] for i in range(len(boxes_list)): for j in range(len(boxes_list[i])): s='' if(len(boxes_list[i][j])==0): dataframe_final.append(' ') else: for k in range(len(boxes_list[i][j])): y,x,w,h = boxes_list[i][j][k][0],boxes_list[i][j][k][1], boxes_list[i][j][k][2],boxes_list[i][j][k][3] roi = bitnot[x:x+h, y:y+w] kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 1)) border = cv2.copyMakeBorder(roi,2,2,2,2, cv2.BORDER_CONSTANT,value=[255,255]) resizing = cv2.resize(border, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) dilation = cv2.dilate(resizing, kernel,iterations=1) erosion = cv2.erode(dilation, kernel,iterations=2) out = pytesseract.image_to_string(erosion) if(len(out)==0): out = pytesseract.image_to_string(erosion) s = s +" "+ out dataframe_final.append(s) print(dataframe_final) arr = np.array(dataframe_final) import pandas as pd dataframe = pd.DataFrame(arr.reshape(len(rows), total_cells)) data = dataframe.style.set_properties(align="left") #print(data) #print(dataframe) d=[] for i in range(0,len(rows)): for j in range(0,total_cells): print(dataframe[i][j],end=" ")
def predict_by_tesseract(plate_image): pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract.exe' text = pytesseract.image_to_string(plate_image, config='--psm 13') #processed_text = ''.join([ch for ch in text if ch.isalnum()]) #return processed_text return text
import os from PIL import Image from pytesseract import image_to_string print( image_to_string(Image.open(os.path.abspath('./files-imports/test.jpg')), lang='por'))
left = location['x'] top = location['y'] right = location['x'] + size['width'] bottom = location['y'] + size['height'] #print left,top,right,bottom #从文件读取截图,截取验证码位置再次保存 """ 注意:这块有个坑,获取的数值都是float,需要强制转化成int,否则会报错 """ img = Image.open(screenImg).crop( (int(left), int(top), int(right), int(bottom))) #img.show() #time.sleep(10) img = img.convert('L') #转换模式:L | RGB img = ImageEnhance.Contrast(img) #增强对比度 img = img.enhance(2.0) #增加饱和度 img.save(screenImg) #再次读取识别验证码 img = Image.open(screenImg) code = pytesseract.image_to_string(img) browser.find_element_by_id("TANGRAM__PSP_3__verifyCode").send_keys( code.strip()) #输出识别码 print(code.strip()) #提交数据 browser.find_element_by_id("TANGRAM__PSP_3__submit").click() time.sleep(10) browser.quit()
import numpy as np import imutils import cv2 from matplotlib import pyplot as plt import pytesseract imagePath = "../../resources/img/national-id-card3.jpg" ori_img = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE) ori_img = imutils.resize(ori_img, height=1024) img = ori_img[350:900, 420:1300] img = cv2.GaussianBlur(img, (5, 5), 0) otsu_threshold_img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] kernel = np.ones((2, 2), np.uint8) otsu_threshold_img = cv2.dilate(otsu_threshold_img, kernel, iterations=2) otsu_threshold_img = cv2.erode(otsu_threshold_img, kernel, iterations=2) plt.imshow(otsu_threshold_img, cmap='gray', interpolation='bicubic') plt.show() print( pytesseract.image_to_string(otsu_threshold_img, lang='ben+eng', config="--tessdata-dir ../tessdata --psm 3"))
response = urllib2.urlopen( urllib2.Request(login_url, data=None, headers=post_header)) CheckUrl(response) login_url = response.geturl() post_header['Referer'] = login_url # 处理验证码 validate_code = '' validate_code_url = 'https://passport.ustc.edu.cn/validatecode.jsp?type=login' response = urllib2.urlopen( urllib2.Request(validate_code_url, data=None, headers=post_header)) CheckUrl(response) pytesseract.pytesseract.tesseract_cmd = 'tesseract' validate_code_img = Image.open(cStringIO.StringIO(response.read())) raw_code = pytesseract.image_to_string(validate_code_img) validate_code = ''.join(e for e in raw_code if e.isalnum()) if(len(validate_code) != 4): print("[X] 验证码识别出错:" + validate_code + ",请查看当前目录下_tmp.png,手动打码") validate_code_img.save('_tmp.png') validate_code = raw_input('---> 验证码:') validate_code_img.save('_tmp_'+validate_code+'.png') else: print("[-] 验证码识别完成,结果:"+raw_code+' -> '+validate_code) validate_code_img.save(validate_code+'.png') validate_code_img.close() #构造Post数据,2019.03上线含验证码版本 login_data = {'model' : 'uplogin.jsp', 'service': 'http://yqzx.ustc.edu.cn/login_cas', 'username' : raw_input('UserID : '),