import cv2
import os
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
from PIL import Image
import pytesseract as tess
# 验证码识别
image_url = "D:\\python\\psc.jpg" # 要识别的图片
img = cv2.imread(image_url)
img = cv2.blur(img, (3, 3))
# cv2.imshow('yzm',img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# cv2.imshow('gray',gray)
ret, binary = cv2.threshold(
gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_TRIANGLE)
kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (6, 6))
kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))
open1 = cv2.erode(binary, kernel=kernel1)
open2 = cv2.morphologyEx(open1, cv2.MORPH_OPEN, kernel=kernel2)
# cv2.imshow('detect',open2)
cv2.bitwise_not(open2, open2)
textimg = Image.fromarray(open2)
text = tess.image_to_string(gray)
print("验证码为:%s" % text)
cv2.waitKey()
报错信息
所在位置 行:1 字符: 93
+ ... l/Programs/Python/Python39/python.exe c:/Users/123/Desktop/print().py
+ ~
“(”后面应为表达式。
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedExpression
重新编译后
Traceback (most recent call last):
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 255, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
text = tess.image_to_string(gray)
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 423, in image_to_string
return {
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 426, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 288, in run_and_get_output
run_tesseract(**kwargs)
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 260, in run_tesseract
raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.
PS C:\Users\123\Desktop\C> & C:/Users/123/AppData/Local/Programs/Python/Python39/python.exe "d:/python/import cv2.py"
Traceback (most recent call last):
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 255, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\python\import cv2.py", line 24, in <module>
text = tess.image_to_string(gray)
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 423, in image_to_string
return {
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 426, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 288, in run_and_get_output
run_tesseract(**kwargs)
File "C:\Users\123\AppData\Local\Programs\Python\Python39\lib\site-packages\pytesseract\pytesseract.py", line 260, in run_tesseract
raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.