因为我制作了一个名为卡片扫描的项目,用于使用pytesseract和OPENCV从卡片中提取准确的数据,所以我已经完成了这一点,我想将我的代码转换为python模块,我也是空的,所以可以任何人解释我如何处理这个并为我建议一些链接。
这是我的python代码:
import re
import requests as req
# import Flask as Flask
import imutils
# from flask import Flask,render_template,request,json,jsonify
import cv2
import os
import pytesseract
from PIL import Image
from flask import Flask, request, render_template, json
import uuid
app = Flask(__name__)
BINARY_THREHOLD = 180
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
# Start
# make a UUID based on the host ID and current time
@app.route('/',methods=['post','get'])
def main():
return render_template("start.html")
@app.route('/start',methods=['POST','GET'])
def start():
print("stay")
if request.files['attachement']:
target = os.path.join(APP_ROOT, 'static/files')
print(target)
# os.makedirs(target)
file = request.files['attachement']
print(file)
filename = file.filename
destination = "/".join([target, filename])
print(destination)
file.save(destination)
base = os.path.basename(destination)
print(base)
oriimage = cv2.imread(destination)
newimage = cv2.resize(oriimage,(984,582))
# cv2.imshow("iiihufasfva",newimage)
# dob=cv2.imread(newimage)
# cv2.imshow("dob",newimage)
dob=cv2.imread(destination)
cv2.imwrite("C:/Users/shruthipriyanka/PycharmProjects/Scanning/newone.png",newimage)
imgray1 = cv2.cvtColor(newimage,cv2.COLOR_BGR2GRAY)
ret2,threshone = cv2.threshold(imgray1,139,255,cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/shruthipriyanka/PycharmProjects/Scanning/threshone.png",threshone)
single=Image.open("C:/Users/shruthipriyanka/PycharmProjects/Scanning/threshone.png")
singlejoin=pytesseract.image_to_string(single,lang='eng',config='--tessdata-dir "C://Tesseract-OCR//tessdata"')
print(singlejoin)
fetchdate=re.findall("\d{2}/+\d{2}/+\d{4}",singlejoin)
print(fetchdate)
lofetch=[]
for dd in fetchdate:
print(dd)
duty=re.split("/",dd)
print(duty)
lofetch.append(dd)
print(lofetch)
print(lofetch[0])
# name
singlenamepart = newimage[110:180, 190:650]
cv2.imshow("singlenamepart", singlenamepart)
singlenamepartgray = cv2.cvtColor(singlenamepart, cv2.COLOR_BGR2GRAY)
ret2, threshsingle = cv2.threshold(singlenamepartgray, 189, 255, cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/shruthipriyanka/PycharmProjects/Scanning/singlenamepart.png", singlenamepart)
singlenamepartopen = Image.open("C:/Users/shruthipriyanka/PycharmProjects/Scanning/singlenamepart.png")
singlenameparttrain = pytesseract.image_to_string(singlenamepartopen, lang='eng',
config='--tessdata-dir "C://Tesseract-OCR//tessdata"')
print("singlenameparttrain:", singlenameparttrain)
# namesecond
namesecond = newimage[150:210, 190:650]
cv2.imshow("singlenamepart", namesecond)
namesecondpartgray = cv2.cvtColor(namesecond, cv2.COLOR_BGR2GRAY)
ret2, threshsingle = cv2.threshold(namesecondpartgray, 189, 255, cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/shruthipriyanka/PycharmProjects/Scanning/namelysecond.png", namesecond)
nome = Image.open("C:/Users/shruthipriyanka/PycharmProjects/Scanning/namelysecond.png")
nometrain = pytesseract.image_to_string(nome, lang='eng',
config='--tessdata-dir "C://Tesseract-OCR//tessdata"')
print("nome:", nometrain)
# address
adress = newimage[260:350, 350:950]
cv2.imshow("adress", adress)
adresspartry = cv2.cvtColor(namesecond, cv2.COLOR_BGR2GRAY)
ret2, threshsingle = cv2.threshold(adresspartry, 189, 255, cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/shruthipriyanka/PycharmProjects/Scanning/ado.png", adress)
addadd = Image.open("C:/Users/shruthipriyanka/PycharmProjects/Scanning/ado.png")
adding = pytesseract.image_to_string(addadd, lang='eng',
config='--tessdata-dir "C://Tesseract-OCR//tessdata"')
print("adding:", adding)
# dateofbirth
datyformat = dob[165:225, 190:650]
cv2.imshow("datyformat", datyformat)
nametwogray = cv2.cvtColor(datyformat, cv2.COLOR_BGR2GRAY)
ret2, threshnametwo = cv2.threshold(nametwogray, 135, 255, cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/shruthipriyanka/PycharmProjects/Scanning/datyformat.png", datyformat)
datyformatopen = Image.open("C:/Users/shruthipriyanka/PycharmProjects/Scanning/datyformat.png")
datyformattrainone = pytesseract.image_to_string(datyformatopen, lang='eng',
config='--tessdata-dir "C://Tesseract-OCR//tessdata"')
print("nameone:", datyformattrainone)
dateformatone = re.findall('\d{2}/+\d{2}/+\d{4}', datyformattrainone)
print(dateformatone)
datepart = dob[195:300, 190:950]
cv2.imshow("datepart", datepart)
datepartgray = cv2.cvtColor(datepart, cv2.COLOR_BGR2GRAY)
ret2, datepartnametwo = cv2.threshold(datepartgray, 135, 255, cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/shruthipriyanka/PycharmProjects/Scanning/datepart.png", datepartnametwo)
datepartopen = Image.open("C:/Users/shruthipriyanka/PycharmProjects/Scanning/datepart.png")
dateparttrain = pytesseract.image_to_string(datepartopen, lang='eng',
config='--tessdata-dir "C://Tesseract-OCR//tessdata"')
print("datepart:", dateparttrain)
dateformat = re.findall('\d{2}/+\d{2}/+\d{4}', dateparttrain)
print(dateformat)
dateformatstring=re.findall('\d+',dateparttrain)
print(dateformatstring)
if dateformatone:
aa = dateformatone
print(aa)
elif dateformat:
aa = dateformat
print(aa)
# singlecharacter
singlepart = newimage[200:290, 350:950]
cv2.imshow("singlepart",singlepart)
singlepartgray = cv2.cvtColor(singlepart, cv2.COLOR_BGR2GRAY)
ret2, threshsingle = cv2.threshold(singlepartgray, 189, 255, cv2.THRESH_BINARY)
cv2.imwrite("C:/Users/shruthipriyanka/PycharmProjects/Scanning/singlepart.png",singlepart)
singlepartopen=Image.open("C:/Users/shruthipriyanka/PycharmProjects/Scanning/singlepart.png")
singleparttrain=pytesseract.image_to_string(singlepartopen,lang='eng',config='--tessdata-dir "C://Tesseract-OCR//tessdata"')
print("singlecharar:",singleparttrain)
def upperOnly(s):
onlyCaps = ""
for char in s:
if char.isupper() == True:
onlyCaps += char
return onlyCaps
upper=upperOnly(singleparttrain)
print(upper)
singlele=[]
for pos in singleparttrain:
singlele.append(pos)
print(pos)
print(singlele)
carddid = "CardID" + '_' + uuid.uuid4().hex[:4].upper()
print("Pass_ID:", carddid)
gen=""
for ap in singlele:
if ap=="F"or"f":
gen="Female"
print(gen)
pass
elif ap=="M"or"m":
gen="Male"
print(gen)
pass
elif ap!="F"and"M":
gen="Female"
# elif ap=="F" and "M":
# gen="female"
# print(gen)
datas = {"paasid": carddid, "name": nometrain,
"surname": singlenameparttrain, "addtwo": adding, "dob": lofetch[0], "gender": gen}
if __name__ == '__main__':
app.run()