|
from ultralytics import YOLO |
|
import easyocr |
|
import cv2 |
|
from flask import Flask, render_template, request, redirect, url_for,jsonify |
|
import os |
|
import time |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
UPLOAD_FOLDER = 'static/uploads' |
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER |
|
|
|
|
|
|
|
|
|
@app.route('/', methods=['GET', 'POST']) |
|
def upload_file(): |
|
if request.method == 'POST': |
|
|
|
uploaded_file = request.files['image'] |
|
|
|
if uploaded_file.filename != '': |
|
|
|
filename = uploaded_file.filename |
|
filepath = os.path.join(app.config['UPLOAD_FOLDER'], "input.pdf") |
|
uploaded_file.save(filepath) |
|
|
|
return redirect('/result') |
|
return render_template('index.html') |
|
|
|
@app.route('/result/') |
|
def upload_success(): |
|
|
|
|
|
|
|
try: |
|
model = YOLO('./yolov8-custom.pt') |
|
confidence = 0.6 |
|
results = model.predict(source = f"./static/uploads/input.jpg", save = True, show = False, conf = confidence) |
|
print(results) |
|
|
|
img_main = cv2.imread("./static/uploads/input.jpg") |
|
r = results[0] |
|
box = r.boxes[0] |
|
[left, top, right, bottom] = box.xyxy[0] |
|
left = int(left) |
|
top = int(top) |
|
right = int(right) |
|
bottom = int(bottom) |
|
cropped_img = img_main[top+1:bottom-1, left+1:right-1] |
|
output_path = "./static/uploads/processed.jpg" |
|
cv2.imwrite(output_path, cropped_img) |
|
print(f"Largest image saved at {output_path}") |
|
time.sleep(2) |
|
print("detection and cropping is successfull. ") |
|
return redirect('/final_result/') |
|
except Exception as e : |
|
print(e) |
|
return render_template('error.html') |
|
|
|
@app.route('/final_result/') |
|
def final_result(): |
|
try: |
|
print("now easy ocr part.") |
|
reader = easyocr.Reader(['bn'], gpu = False) |
|
result = reader.readtext("./static/uploads/processed.jpg", detail = 0, paragraph = True) |
|
print(result) |
|
return render_template('index.html', message=result[0]) |
|
except Exception as e : |
|
return render_template('error.html') |
|
@app.route("/download") |
|
def download(): |
|
import requests |
|
|
|
|
|
url = "https://raw.githubusercontent.com/user/repository/branch/file.txt" |
|
|
|
|
|
response = requests.get(url) |
|
|
|
|
|
if response.status_code == 200: |
|
|
|
filename = url.split("/")[-1] |
|
|
|
|
|
with open(filename, "wb") as f: |
|
f.write(response.content) |
|
|
|
print(f"Downloaded {filename} successfully!") |
|
else: |
|
print(f"Failed to download file. Status code: {response.status_code}") |
|
|
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |
|
|