coder-black-mamba
commited on
Commit
•
dd5050e
1
Parent(s):
4ea84da
initial commit
Browse files- Dockerfile +11 -0
- app.py +97 -0
- static/uploads/input.jpg +0 -0
- static/uploads/processed.jpg +0 -0
- templates/error.html +19 -0
- templates/index.html +40 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["gunicorn", "-b","0.0.0.0:7860","app:app",]
|
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from ultralytics import YOLO
|
2 |
+
import easyocr
|
3 |
+
import cv2
|
4 |
+
from flask import Flask, render_template, request, redirect, url_for,jsonify
|
5 |
+
import os
|
6 |
+
import time
|
7 |
+
|
8 |
+
app = Flask(__name__)
|
9 |
+
|
10 |
+
# Set upload folder
|
11 |
+
UPLOAD_FOLDER = 'static/uploads'
|
12 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
@app.route('/', methods=['GET', 'POST'])
|
18 |
+
def upload_file():
|
19 |
+
if request.method == 'POST':
|
20 |
+
# Get the uploaded file
|
21 |
+
uploaded_file = request.files['image']
|
22 |
+
# Check if a file is selected
|
23 |
+
if uploaded_file.filename != '':
|
24 |
+
# Save the file
|
25 |
+
filename = uploaded_file.filename
|
26 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], "input.pdf")
|
27 |
+
uploaded_file.save(filepath)
|
28 |
+
# Redirect to success page (you can modify this for further processing)
|
29 |
+
return redirect('/result')
|
30 |
+
return render_template('index.html')
|
31 |
+
|
32 |
+
@app.route('/result/')
|
33 |
+
def upload_success():
|
34 |
+
# file name is the file name that some one uploaded
|
35 |
+
# so the image path will be ./static/uploads/<filename>
|
36 |
+
# call your model with keras tf of sklearn
|
37 |
+
try:
|
38 |
+
model = YOLO('./yolov8-custom.pt')
|
39 |
+
confidence = 0.6 # 0.0-1.0 # render the message that you wanted to show in message variable
|
40 |
+
results = model.predict(source = f"./static/uploads/input.jpg", save = True, show = False, conf = confidence)
|
41 |
+
print(results)
|
42 |
+
|
43 |
+
img_main = cv2.imread("./static/uploads/input.jpg")
|
44 |
+
r = results[0]
|
45 |
+
box = r.boxes[0]
|
46 |
+
[left, top, right, bottom] = box.xyxy[0]
|
47 |
+
left = int(left)
|
48 |
+
top = int(top)
|
49 |
+
right = int(right)
|
50 |
+
bottom = int(bottom)
|
51 |
+
cropped_img = img_main[top+1:bottom-1, left+1:right-1]
|
52 |
+
output_path = "./static/uploads/processed.jpg"
|
53 |
+
cv2.imwrite(output_path, cropped_img)
|
54 |
+
print(f"Largest image saved at {output_path}")
|
55 |
+
time.sleep(2)
|
56 |
+
print("detection and cropping is successfull. ")
|
57 |
+
return redirect('/final_result/')
|
58 |
+
except Exception as e :
|
59 |
+
print(e)
|
60 |
+
return render_template('error.html')
|
61 |
+
|
62 |
+
@app.route('/final_result/')
|
63 |
+
def final_result():
|
64 |
+
try:
|
65 |
+
print("now easy ocr part.")
|
66 |
+
reader = easyocr.Reader(['bn'], gpu = False)
|
67 |
+
result = reader.readtext("./static/uploads/processed.jpg", detail = 0, paragraph = True)
|
68 |
+
print(result)
|
69 |
+
return render_template('index.html', message=result[0])
|
70 |
+
except Exception as e :
|
71 |
+
return render_template('error.html')
|
72 |
+
@app.route("/download")
|
73 |
+
def download():
|
74 |
+
import requests
|
75 |
+
|
76 |
+
# Replace with the URL of the file on GitHub (use raw content URL)
|
77 |
+
url = "https://raw.githubusercontent.com/user/repository/branch/file.txt"
|
78 |
+
|
79 |
+
# Send a GET request to download the file
|
80 |
+
response = requests.get(url)
|
81 |
+
|
82 |
+
# Check for successful response
|
83 |
+
if response.status_code == 200:
|
84 |
+
# Get the filename from the URL (optional)
|
85 |
+
filename = url.split("/")[-1]
|
86 |
+
|
87 |
+
# Save the content to a file
|
88 |
+
with open(filename, "wb") as f:
|
89 |
+
f.write(response.content)
|
90 |
+
|
91 |
+
print(f"Downloaded {filename} successfully!")
|
92 |
+
else:
|
93 |
+
print(f"Failed to download file. Status code: {response.status_code}")
|
94 |
+
|
95 |
+
|
96 |
+
if __name__ == '__main__':
|
97 |
+
app.run(debug=True)
|
static/uploads/input.jpg
ADDED
static/uploads/processed.jpg
ADDED
templates/error.html
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<title>Lost in the Interwebs?</title>
|
6 |
+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
|
7 |
+
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
8 |
+
</head>
|
9 |
+
<body>
|
10 |
+
<div class="container text-center">
|
11 |
+
|
12 |
+
<h1 class="mt-5 py-3 text-danger bg-secondary">Opps</h1>
|
13 |
+
|
14 |
+
<p class="py-5">The page you requested seems to have gotten kidnapped by gremlins. Don't worry, gremlins are pretty lousy at hiding things. Maybe try searching for it, or head back to the homepage using the button below? </p>
|
15 |
+
|
16 |
+
<a cla href="/">Back to Home</a>
|
17 |
+
</div>
|
18 |
+
</body>
|
19 |
+
</html>
|
templates/index.html
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html>
|
3 |
+
|
4 |
+
<head>
|
5 |
+
<title>Upload Image</title>
|
6 |
+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
|
7 |
+
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
8 |
+
<style>
|
9 |
+
|
10 |
+
</style>
|
11 |
+
</head>
|
12 |
+
|
13 |
+
<body>
|
14 |
+
<div class="container">
|
15 |
+
<div class="row">
|
16 |
+
<div class="col-md-4"></div>
|
17 |
+
<div class="col-md-4 mt-5 pt-5">
|
18 |
+
<div class="wrapper mt-5 bg-secondary p-4 rounded">
|
19 |
+
|
20 |
+
<h1>Upload Image</h1>
|
21 |
+
<form method="POST" enctype="multipart/form-data">
|
22 |
+
<input type="file" name="image" class="btn btn-danger">
|
23 |
+
<input type="submit" value="Upload" class="btn btn-danger my-1 mx-auto">
|
24 |
+
</form>
|
25 |
+
<div class="my-3">
|
26 |
+
{% if message %}
|
27 |
+
<h2>{{ message }}</h2>
|
28 |
+
<img src="/static/uploads/processed.jpg" alt="">
|
29 |
+
{% endif %}
|
30 |
+
</div>
|
31 |
+
</div>
|
32 |
+
</div>
|
33 |
+
<div class="col-md-4"></div>
|
34 |
+
</div>
|
35 |
+
|
36 |
+
|
37 |
+
</div>
|
38 |
+
</body>
|
39 |
+
|
40 |
+
</html>
|