Exemple #1
0
def cut_rec_text(img, rect):
    cut = img_cut(img, rect)
    path = "temp/temp.png"
    cut.save(path)
    text = tesseract.image_file_to_string(path, graceful_errors=True)
    text = text.replace(" ", "").replace("\n", "").replace(",", "").replace(".", "")
    return text
def _text_from_img(img_path, path):
    """
        return the text results of pytesser OCR
    """
    # potential optimization:
    # only OCR that are x% black
    return pytesser.image_file_to_string(img_path, path=path)
def mainer(filen = "one.jpg",show = True):

	try:
		img = cv2.imread(filen)
		img = cv2.GaussianBlur(img,(3,3),0)
		gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
		graye = cv2.Canny(gray, 100, 300)
		#cv2.imshow("one2",graye)
		#cv2.imwrite("temp.jpg",graye)

		tresh = 5
		ltresh = 10
		tup = None

		lines = cv2.HoughLinesP(graye, 1, math.pi, 2, None, 25, 1);
		vlines = sorted(lines[0],key = lambda x: x[0])

		rmarray = []
		for x in range(len(vlines)):
			if x<len(vlines)-1:
				if vlines[x][0] > (vlines[x+1][0] - tresh ):
					if vlines[x][1] > vlines[x+1][1] - ltresh:
						if abs(vlines[x][3]-vlines[x][1]) > abs(vlines[x+1][3]-vlines[x+1][1]) :
							rmarray.append(vlines[x+1])
						else :
							rmarray.append(vlines[x])
		for r in rmarray:
			for n,v in enumerate(vlines):
				if numpy.array_equal(r,v):
					del vlines[n]

		for line in vlines:
			pt1 = (line[0],line[1])
			pt2 = (line[2],line[3])
			cv2.line(img, pt1, pt2, (0,0,255), 3)

		lines = cv2.HoughLinesP(graye, 1, math.pi/2, 2, None, 40, 1);
		hlines = sorted(lines[0],key = lambda x: x[1])

		rmarray = []
		for x in range(len(hlines)):
			if x<len(hlines)-1:
				if hlines[x][1] > (hlines[x+1][1] - tresh ):
					if hlines[x][0] > hlines[x+1][0] - ltresh:
						if abs(hlines[x][2]-hlines[x][0]) > abs(hlines[x+1][2]-hlines[x+1][0]) :
							rmarray.append(hlines[x+1])
						else :
							rmarray.append(hlines[x])
		for r in rmarray:
			for n,v in enumerate(hlines):
				if numpy.array_equal(r,v):
					del hlines[n]
		for line in hlines:
			pt1 = (line[0],line[1])
			pt2 = (line[2],line[3])
			cv2.line(img, pt1, pt2, (0,0,255), 3)

		hp =  parallelh(hlines,10)
		vp = parallelv(vlines,10)
		hp = sorted(hp,key = lambda x:x[2])
		vp = sorted(vp,key = lambda x:x[2])
		trsh = 10
		ltrsh = 10
		for h in vp:
			bmin = min(vlines[h[0]][0],vlines[h[1]][0])
			bmax = max(vlines[h[0]][0],vlines[h[1]][0])
			lmin = min(min(vlines[h[0]][1],vlines[h[0]][3]),min(vlines[h[1]][1],vlines[h[1]][3]))
			lmax = max(max(vlines[h[0]][1],vlines[h[0]][3]),max(vlines[h[1]][1],vlines[h[1]][3]))
			for hv in hlines:
				if hv[1] > lmin -30 and hv[1]  < lmax +30:
					tup = (min(bmax,bmin)+ltrsh,lmin-trsh,max(bmax,bmin)-ltrsh,lmax+trsh)
		if tup ==None:
			print "Lines try"

			for h in hp:
				bmin = min(min(hlines[h[0]][0],hlines[h[0]][2]),min(hlines[h[1]][0],hlines[h[1]][2]))
				bmax = max(max(hlines[h[0]][0],hlines[h[0]][2]),max(hlines[h[1]][0],hlines[h[1]][2]))
				lmin = min(hlines[h[0]][1],hlines[h[1]][1])
				lmax = max(hlines[h[0]][1],hlines[h[1]][1])
				trsh = 10
				ltrsh = 10
				for hv in vlines:
					if hv[0] > bmin -30 and hv[0]  < bmax +30:
						tup = (min(bmax,bmin) - ltrsh,lmin+trsh,max(bmax,bmin)+ltrsh,lmax - trsh)
		pt1 = (tup[0],tup[1])
		pt2 = (tup[2],tup[3])
		cv2.line(img, pt1, pt2, (0,255,0), 3)
		left = tup[0]
		top = tup[1]
		new_width = tup[2]-tup[0]
		new_height = tup[3]-tup[1]


		img0 = cv2.cv.LoadImage(filen, 1)
		img1 = cv2.cv.CloneImage(img0)
		cropped = cv2.cv.CreateImage( (new_width, new_height), 8, 3)
		src_region = cv2.cv.GetSubRect(img1, (left, top, new_width, new_height) )
		cv2.cv.Copy(src_region, cropped)
		cv2.cv.SaveImage("a.png",cropped)
		dtext = pt.image_file_to_string(os.path.join(os.path.abspath("."),"a.png"))
		dtext = dtext.strip("\n").strip().replace(" ","")
		print dtext
		if show == True:
			cv2.imshow("one",img)
			cv2.waitKey(0)
		if dtext.find("#")!=0:
			print "Error!!"
			return None
		else:
			return dtext
	except TypeError :
		return None
Exemple #4
0
    def operation(keyword, url, browser, transform, next_button_xpath, deep):
        href_dic = {}
        if keyword == 'CNKI':
            index = url.find('keyValue=')
            if index != -1:
                end_index = url.find('&S=')
                if end_index != -1:
                    keywords = url[index + 9:end_index]
                    # keywords = (keywords)
                    browser.get('http://www.cnki.net')
                    button = browser.find_element_by_id('btnSearch')
                    text = browser.find_element_by_class_name('rekeyword')
                    keywords = unquote(keywords).decode('utf-8')
                    text.send_keys(keywords)
                    button.click()
                    #wait one second
                    time.sleep(1)
                    retry = False
                    browser.get(url)
                    """
                    _has_available_url:该变量标识着循环读取的循环是否结束,该变量为false时候代表所有的可用URL都被读取完毕,循环结束

                    """

                    _has_available_url = True
                    try:
                        while (deep > 0 or deep == -1) and _has_available_url:
                            _has_available_url = False
                            # wait render page completely
                            time.sleep(random.uniform(1, 3))
                            # execute javascript to get page content

                            if not retry:
                                retry = False

                                html_str = browser.execute_script(
                                    "return document.documentElement.outerHTML"
                                )
                                # transform html string to document
                                html = etree.HTML(html_str)
                                # transform html with xslt template

                                result_tree = transform(html)

                                print(result_tree)

                            # 点击下一页的按钮
                            if deep > 1 or deep == -1:
                                # 首先看看是否需要输入验证码才能够进行访问
                                check_coede = browser.find_elements_by_id(
                                    'CheckCodeImg')

                                if len(check_coede) == 1:

                                    random_int = random.randint(100000, 999999)
                                    pic_name = '%d' % random_int
                                    pic_path = './tmp/' + pic_name + '.jpg'
                                    browser.get_screenshot_as_file(pic_path)
                                    img = Image.open(pic_path)

                                    document_str = browser.execute_script(
                                        "return document.documentElement.outerHTML"
                                    )
                                    _error_message_index = document_str.find(
                                        '验证码错误')

                                    if _error_message_index == -1:
                                        region = (32, 60, 95, 82)
                                    else:
                                        region = (32, 96, 95, 118)
                                    crop_img = img.crop(region)
                                    crop_img.save(pic_path)

                                    im = Image.open(pic_path)

                                    imgry = im.convert('L')

                                    threshold = 120
                                    table = []
                                    for i in range(256):
                                        if i < threshold:
                                            table.append(0)
                                        else:
                                            table.append(1)
                                    out = imgry.point(table, '1')
                                    out.save(pic_path)
                                    code = pytesser.image_file_to_string(
                                        pic_path)
                                    if code == '':
                                        code = 'a'
                                    check_code_input_field = browser.find_element_by_id(
                                        'CheckCode')
                                    check_code_input_field.send_keys(
                                        code.decode('utf-8'))
                                    button = browser.find_element_by_xpath(
                                        '/html/body/p[1]/input[2]')
                                    button.click()
                                    os.remove(pic_path)
                                    _has_available_url = True
                                else:
                                    # 通过地址栏将所有的地址存起来,有不同的就加进去吧
                                    # 将所有的地址栏的href连接地址通过字典来进行存储
                                    # 其中key就是href值,value就是对应的从browser中获得的元素
                                    # 每一次得到地址栏中的所有href,判断这些href是否存在
                                    # 如果存在,并且value值不是was_read则更新value,因为翻页后,同一个位置的元素有可能已经变了,而且这个页面还没有被点击
                                    # 如果不存,插入key -value
                                    # 所有的href遍历结束后,将这些开始寻找第一个value不是 was_read的元素就行点击,并将他的value更新为was_read  #
                                    # get all element with href attribute
                                    _has_next_button = False
                                    next_buttons = browser.find_elements_by_xpath(
                                        next_button_xpath + '//*[@href]')

                                    for element in next_buttons:
                                        inner_html = element.text
                                        index = inner_html.find('下一页')
                                        if index == -1:
                                            index = inner_html.find('后页')
                                            if index == -1:
                                                index = inner_html.find('ext')
                                        if index != -1:
                                            _has_next_button = True
                                            _has_available_url = True
                                            element.click()
                                            break
                                    #没有下一页按钮,安心的找下一页的URL吧
                                    if not _has_next_button:
                                        for element in next_buttons:
                                            href_value = element.get_attribute(
                                                'href')
                                            _is_existence = href_dic.has_key(
                                                href_value)
                                            # href还没有存
                                            if not _is_existence:
                                                href_dic[href_value] = element

                                            elif href_dic.get(
                                                    href_value) != 'was_read':
                                                # href has been existence and not read,update it
                                                href_dic[href_value] = element

                                        ##traversal href_dic and find the first value is not equal to was_read

                                        for key in href_dic.keys():
                                            if href_dic[key] != 'was_read':
                                                try:
                                                    _has_available_url = True

                                                    browser.get(key)
                                                    # clicked_button.click()
                                                    href_dic[key] = 'was_read'

                                                    browser.get_screenshot_as_file(
                                                        'D:/res.png')
                                                    break
                                                except:
                                                    browser.back()
                                                    retry = True
                                                    print("ERROR")
                                                    browser.refresh()
                                                    browser.get_screenshot_as_file(
                                                        'D:/res.png')
                                                    break
                                if deep != -1:
                                    deep -= 1

                        if not _has_available_url:
                            print("爬取结束")
                        else:
                            print("异常结束")
                        # 退出浏览器,节省资源
                        browser.quit()
                    except:
                        print("异常结束")
                        browser.get_screenshot_as_file('D://ex.png')
Exemple #5
0
def rec_text(path):
    text = tesseract.image_file_to_string(path, graceful_errors=True)
    text = text.replace(" ", "").replace("\n", "").replace(",", "")
    return text
from consts import *

filepath = 'D:\Projects\PokerBot\\temp\\debug_screen.png'
start = time.clock()
screen = Image.open(filepath)
img = img_cut(screen, (ENEMY_BANKS[2]))

# img = img.convert("L").point(lambda x: 0 if x < 120 else 255)
# arr = np.array(img)
# arr.save("D:\Projects\PokerBot\\img\\bid_digits\\1.npy")

# img = img.convert('L')
# img = ops.grayscale(img)
# img = ops.invert(img)
# img = img.point(lambda x: 0 if x < 128 else 255, '1')

# text = pytesser.image_file_to_string(filepath, graceful_errors=True)
img.save("D:\Projects\PokerBot\\temp\\temp.png")
# text = pytesser.image_to_string(img)
text = pytesser.image_file_to_string("D:\Projects\PokerBot\\temp\\temp.png")

# text = pytesser.image_to_string(img)

fin = time.clock()
print("Time = " + str(fin - start))
print text
# text2 = int(text.replace("\n", "").replace(",", ""))
# print(text2)

plt.imshow(img)
plt.show()
Exemple #7
0
            x[i, j] = (255, 255, 255)
        elif x[i, j] == (153, 153, 153):
            x[i, j] = (255, 255, 255)
        else:
            x[i, j] = (0, 0, 0)

img1 = img1.convert('1')
filn = "Score.txt"
if not os.path.exists('output' + cn):
    os.makedirs('output' + cn)
img1.save(os.path.join('output' + cn, inp))
if not os.path.exists('text_output' + cn):
    os.makedirs('text_output' + cn)
dat = open(os.path.join('text_output' + cn, inp[:inp.rfind('.')] + '.txt'),
           "w")
filler = open(os.path.join(".", filn), "a")

from pytesser import pytesser
text = pytesser.image_file_to_string(os.path.join('output' + cn, inp))
print text.strip('\n')
dat.write("Guessed " + text.strip('\n') + '\n')
dat.write("Actual " + txt)
if txt == text.strip('\n'):
    dat.write("Success!")
    filler.write("1" + "\n")
else:
    dat.write("Failed!")
    filler.write("0" + "\n")
dat.close()
filler.close()
# Apply perspective transform to obtain a top-down
# view of the original image
print "Perspective transform can be applied - Press any key to start transform"
cv2.waitKey(0)
print "Please wait while perspective transform is being applied..."
warped = pers_transform(orig, screenCnt.reshape(4, 2) * ratio)

# Grayscale the warped image, then apply adaptive thresholding on it to binarize
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
warped = threshold_adaptive(warped, 250, offset = 10)
warped = warped.astype("uint8") * 255

# Can smooth the warped image if that seems to give better result
warped = cv2.GaussianBlur(warped,(1,1),0)

# Display the resultant image after perspective transform
print "Perspective transform applied successfully. Press any key to start text extraction"
cv2.imshow("DocScan V1.0 - Press any key to continue", resize_img(warped, height = 500))
cv2.waitKey(0)

# Save the resulatant image and call tesseract with the saved image file name 
cv2.imwrite("result/out.tiff",warped)
print "Starting text extraction with Tesseract..."
text = image_file_to_string("result/out.tiff")
f = open("result/out.txt","w")
f.write(text)
print "Text extraction was successful. Text saved to out.txt in result folder"
print "*** Thanks for using DocScan V1.0! See you again, soon! ***"
cv2.destroyAllWindows()
Exemple #9
0
def mainer(filen="one.jpg", show=True):

    try:
        img = cv2.imread(filen)
        img = cv2.GaussianBlur(img, (3, 3), 0)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        graye = cv2.Canny(gray, 100, 300)
        #cv2.imshow("one2",graye)
        #cv2.imwrite("temp.jpg",graye)

        tresh = 5
        ltresh = 10
        tup = None

        lines = cv2.HoughLinesP(graye, 1, math.pi, 2, None, 25, 1)
        vlines = sorted(lines[0], key=lambda x: x[0])

        rmarray = []
        for x in range(len(vlines)):
            if x < len(vlines) - 1:
                if vlines[x][0] > (vlines[x + 1][0] - tresh):
                    if vlines[x][1] > vlines[x + 1][1] - ltresh:
                        if abs(vlines[x][3] -
                               vlines[x][1]) > abs(vlines[x + 1][3] -
                                                   vlines[x + 1][1]):
                            rmarray.append(vlines[x + 1])
                        else:
                            rmarray.append(vlines[x])
        for r in rmarray:
            for n, v in enumerate(vlines):
                if numpy.array_equal(r, v):
                    del vlines[n]

        for line in vlines:
            pt1 = (line[0], line[1])
            pt2 = (line[2], line[3])
            cv2.line(img, pt1, pt2, (0, 0, 255), 3)

        lines = cv2.HoughLinesP(graye, 1, math.pi / 2, 2, None, 40, 1)
        hlines = sorted(lines[0], key=lambda x: x[1])

        rmarray = []
        for x in range(len(hlines)):
            if x < len(hlines) - 1:
                if hlines[x][1] > (hlines[x + 1][1] - tresh):
                    if hlines[x][0] > hlines[x + 1][0] - ltresh:
                        if abs(hlines[x][2] -
                               hlines[x][0]) > abs(hlines[x + 1][2] -
                                                   hlines[x + 1][0]):
                            rmarray.append(hlines[x + 1])
                        else:
                            rmarray.append(hlines[x])
        for r in rmarray:
            for n, v in enumerate(hlines):
                if numpy.array_equal(r, v):
                    del hlines[n]
        for line in hlines:
            pt1 = (line[0], line[1])
            pt2 = (line[2], line[3])
            cv2.line(img, pt1, pt2, (0, 0, 255), 3)

        hp = parallelh(hlines, 10)
        vp = parallelv(vlines, 10)
        hp = sorted(hp, key=lambda x: x[2])
        vp = sorted(vp, key=lambda x: x[2])
        trsh = 10
        ltrsh = 10
        for h in vp:
            bmin = min(vlines[h[0]][0], vlines[h[1]][0])
            bmax = max(vlines[h[0]][0], vlines[h[1]][0])
            lmin = min(min(vlines[h[0]][1], vlines[h[0]][3]),
                       min(vlines[h[1]][1], vlines[h[1]][3]))
            lmax = max(max(vlines[h[0]][1], vlines[h[0]][3]),
                       max(vlines[h[1]][1], vlines[h[1]][3]))
            for hv in hlines:
                if hv[1] > lmin - 30 and hv[1] < lmax + 30:
                    tup = (min(bmax, bmin) + ltrsh, lmin - trsh,
                           max(bmax, bmin) - ltrsh, lmax + trsh)
        if tup == None:
            print "Lines try"

            for h in hp:
                bmin = min(min(hlines[h[0]][0], hlines[h[0]][2]),
                           min(hlines[h[1]][0], hlines[h[1]][2]))
                bmax = max(max(hlines[h[0]][0], hlines[h[0]][2]),
                           max(hlines[h[1]][0], hlines[h[1]][2]))
                lmin = min(hlines[h[0]][1], hlines[h[1]][1])
                lmax = max(hlines[h[0]][1], hlines[h[1]][1])
                trsh = 10
                ltrsh = 10
                for hv in vlines:
                    if hv[0] > bmin - 30 and hv[0] < bmax + 30:
                        tup = (min(bmax, bmin) - ltrsh, lmin + trsh,
                               max(bmax, bmin) + ltrsh, lmax - trsh)
        pt1 = (tup[0], tup[1])
        pt2 = (tup[2], tup[3])
        cv2.line(img, pt1, pt2, (0, 255, 0), 3)
        left = tup[0]
        top = tup[1]
        new_width = tup[2] - tup[0]
        new_height = tup[3] - tup[1]

        img0 = cv2.cv.LoadImage(filen, 1)
        img1 = cv2.cv.CloneImage(img0)
        cropped = cv2.cv.CreateImage((new_width, new_height), 8, 3)
        src_region = cv2.cv.GetSubRect(img1,
                                       (left, top, new_width, new_height))
        cv2.cv.Copy(src_region, cropped)
        cv2.cv.SaveImage("a.png", cropped)
        dtext = pt.image_file_to_string(
            os.path.join(os.path.abspath("."), "a.png"))
        dtext = dtext.strip("\n").strip().replace(" ", "")
        print dtext
        if show == True:
            cv2.imshow("one", img)
            cv2.waitKey(0)
        if dtext.find("#") != 0:
            print "Error!!"
            return None
        else:
            return dtext
    except TypeError:
        return None
Exemple #10
0
file_data = Image.open(local_png)

file_data = file_data.convert('RGB')

width, height = file_data.size

data = file_data.load()

for i in range(width):
    for j in range(height):
        if not data[i, j] == (0, 0, 0):
            data[i, j] = (255, 255, 255)

file_data.save('2.png')

verify_code = str(str2int(image_file_to_string('2.png')))

print verify_code

data_post = urllib.urlencode({
    'referer'       :'/home.php?',
    'username'      :'he21th',
    'password'      :'welcome2bitunion',
    'verify'        :verify_code,
    'verifyimgid'   :verifyid,
    'styleid'       :'',
    'cookietime'    :'0',
    'loginsubmit'   :'登录'
})

req = urllib2.Request(
Exemple #11
0
		elif x[i,j] ==(153,153,153):
			x[i,j] = (255,255,255)
		else:
			x[i,j] = (0,0,0)


img1 = img1.convert('1')
filn = "Score.txt"
if not os.path.exists('output'+cn):
	os.makedirs('output'+cn)
img1.save(os.path.join('output'+cn,inp))
if not os.path.exists('text_output'+cn):
	os.makedirs('text_output'+cn)
dat = open(os.path.join('text_output'+cn,inp[:inp.rfind('.')]+'.txt'),"w")
filler = open(os.path.join(".",filn),"a")


from pytesser import pytesser
text = pytesser.image_file_to_string(os.path.join('output'+cn,inp))
print text.strip('\n')
dat.write("Guessed "+text.strip('\n')+'\n')
dat.write("Actual "+txt)
if txt==text.strip('\n'):
	dat.write("Success!")
	filler.write("1"+"\n")
else:
	dat.write("Failed!")
	filler.write("0"+"\n")
dat.close()
filler.close()