1.使用pyhton安装pytesseract
2.使用brew 安装tesseract
3.将pytesseract包的里的pytesseract.py 中的
tesseract_cmd的路径换成brew 安装的tesseract包的安装目录。
这样才能一次性成功。
完整源码
import pytesseract
from PIL import Image
import pymysql
from coverage.annotate import os
# 建立数据库连接
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='root',
db='cut_movie',
charset='utf8'
)
# 获取游标
cursor = conn.cursor()
# 执行sql语句
sql = 'select * from frame'
rows = cursor.execute(sql) # 返回结果是受影响的行数
data = cursor.fetchall()
for row in data:
id = row[0]
va = row[1] + ""
# va = va.replace("/Users/zhangjunhua/Movies/young/vedio/", "/Users/zhangjunhua/Movies/young/cut_video/")
if not os.path.exists(va):
print("文件不存在")
continue
data = Image.open(va)
res = pytesseract.image_to_string(data, lang='chi_sim')
print(res)
if not res:
res = "无"
else:
res = ','.join(res[0])
print(res)
if res != '' or res != '无':
sta = 0
else:
sta = 1
sql1 = 'UPDATE frame SET va="%s",status=%s WHERE id=%s'
val = (res,sta,id)
cursor.execute(sql1,val)
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()