Vì vậy, tôi có một dự án cá nhân mà tôi biết là khá kém hiệu quả nhưng vẫn hiệu quả. Tôi đang viết mã python thực thi phiên bản không phải pip của tesseract (apt được cài đặt trong linux). Mã của tôi hoạt động trên linux, nhưng tôi gặp lỗi này trên windows:
FileNotFoundError: [WinError 2] Hệ thống không thể tìm thấy tệp
đã chỉ định: 'DRIVE_LETTER:\PROJECT_FOLDER\FOLDER/FILE.txt'
Tôi đang sử dụng Atom IDE, khá mới đối với python, vì vậy nếu có ai có thể chỉ ra những lỗi ngu ngốc của tôi, tôi sẽ đánh giá cao điều đó, cảm ơn! Lỗi xảy ra trên quy trình con.run dòng vì tệp error.txt nói rằng nó không thể tìm thấy đường dẫn cụ thể.
Đây là mã của tôi:
từ bình nhập Flask,url_for,redirect,render_template,request,send_file
từ werkzeug.utils nhập secure_filename
quy trình con nhập khẩu
ứng dụng=Flask(__name__)
app.config['UPLOAD_DIRECTORY']="uploads/"
app.config['FILE_NAME']=""
app.config['OUTPUT_DIRECTORY']="textresult/"
app.config['EXTENSION']=".txt"
@app.route("/",methods=["POST","GET"])
def to_upload():
err_msg=""
nếu request.method=="POST":
nếu request.files['fileupload']:
f=request.files['fileupload']
tên tệp=secure_filename(f.filename)
app.config['FILE_NAME']=tên tệp
f.save(app.config['UPLOAD_DIRECTORY']+tên tệp)
chuyển hướng trở lại (url_for(" process_upload ", tên tệp = tên tệp))
khác:
err_msg="Không có tập tin nào được chọn!"
trả về render_template("index.html",error=err_msg)
@app.route("/upload/<filename>",methods=["POST","GET"])
def process_upload(tên tệp):
f1=open("log/out.txt","w")
f2=open("log/error.txt","w")
out=sub process.run([f"tesseract uploads/{filename}"+f" textresult/{filename}"],shell=True,stdout=f1,stderr=f2)
chuyển hướng trở lại (url_for ("output_file"))
@app.route("/result/",methods=["GET"])
def đầu ra_file():
trả về render_template("output.html")
@app.route("/download/")
def download_file():
file=app.config['OUTPUT_DIRECTORY']+app.config['FILE_NAME']+app.config['EXTENSION']
trả về send_file(file,as_attachment=True)
nếu __name__=="__main__":
app.run(host="0.0.0.0",port="2000",debug=True)
CHỈNH SỬA:
Cuối cùng đã có nó để làm việc! Đã xóa / trong app.config['UPLOAD_DIRECTORY'] và app.config['OUTPUT_DIRECTORY'] vì hiện tại tôi đang sử dụng os.path.join và đây là những dòng sau cho cả Linux và Windows mà tôi đã làm cho chúng hoạt động tại:
Linux:
to_convert=os.path.join(app.config['UPLOAD_DIRECTORY'],tên tệp)
convert2txt=os.path.join(app.config['OUTPUT_DIRECTORY'],tên tệp)
out=sub process.run(["tesseract %s %s"%(to_convert,convert2txt)],shell=True,stdout=f1,stderr=f2)
Các cửa sổ:
to_convert=os.path.join(app.config['UPLOAD_DIRECTORY'],tên tệp)
convert2txt=os.path.join(app.config['OUTPUT_DIRECTORY'],tên tệp)
out=sub process.run(["tesseract",to_convert,convert2txt],shell=True,stdout=f1,stderr=f2)
Cảm ơn tất cả các đầu vào của bạn!