|
|
|
|
|
import pandas as pd |
|
|
from ultralytics import YOLO |
|
|
from flask import Flask, render_template, request, send_from_directory |
|
|
from werkzeug.utils import secure_filename |
|
|
import os |
|
|
import pandas as pd |
|
|
import shutil |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
os.mkdir('/tmp') |
|
|
except: |
|
|
pass |
|
|
|
|
|
def clr_pred(): |
|
|
""" |
|
|
for removing the static folder (for memoery saving in web) |
|
|
""" |
|
|
dir = '/tmp/static' |
|
|
try: |
|
|
shutil.rmtree(dir) |
|
|
print("Cleared Previous Predictions") |
|
|
except: |
|
|
pass |
|
|
|
|
|
def clr_old_upload(): |
|
|
""" |
|
|
for removing the old images uploaded file (for memoery saving in web) |
|
|
""" |
|
|
dir = '/tmp/UPLOAD_FOLDER' |
|
|
try: |
|
|
shutil.rmtree(dir) |
|
|
print("Cleared UPLOAD_FOLDER") |
|
|
except: |
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
def mk_csv_folder(): |
|
|
try: |
|
|
os.mkdir("/tmp/static/CSV_File") |
|
|
print("made csv_folder") |
|
|
except: |
|
|
pass |
|
|
|
|
|
|
|
|
def mk_uploaded_folder(): |
|
|
try: |
|
|
os.mkdir("/tmp/UPLOAD_FOLDER") |
|
|
print("made UPLOAD_FOLDER") |
|
|
except: |
|
|
pass |
|
|
|
|
|
|
|
|
def mk_pred_folder(): |
|
|
try: |
|
|
os.mkdir("/tmp/static") |
|
|
print("made static folder") |
|
|
except: |
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
model = YOLO('best.pt') |
|
|
|
|
|
|
|
|
def Use_yolo(img_path): |
|
|
""" |
|
|
:param img_path: |
|
|
:return: image with bbox , csv file |
|
|
""" |
|
|
all_data =[] |
|
|
|
|
|
results = model(img_path, conf=0.1, verbose=False) |
|
|
model.predict(img_path, save=True, conf=0.2, show_labels=True, |
|
|
project='/tmp/static', name="Image_Prediction") |
|
|
|
|
|
boxes = results[0].boxes.xyxy.tolist() |
|
|
classes = results[0].boxes.cls.tolist() |
|
|
confidences = results[0].boxes.conf.tolist() |
|
|
names = results[0].names |
|
|
|
|
|
if not boxes: |
|
|
|
|
|
all_data.append({ |
|
|
'Class': 'No Object Found', |
|
|
'x_min': 'Nan', |
|
|
'y_min': 'Nan', |
|
|
'x_max': 'Nan', |
|
|
'y_max': 'Nan' |
|
|
}) |
|
|
else: |
|
|
|
|
|
for box, cls, conf in zip(boxes, classes, confidences): |
|
|
x_min, y_min, x_max, y_max = box |
|
|
detected_class = names[int(cls)] |
|
|
|
|
|
|
|
|
all_data.append({ |
|
|
'Class': detected_class, |
|
|
'x_min': int(x_min), |
|
|
'y_min': int(y_min), |
|
|
'x_max': int(x_max), |
|
|
'y_max': int(y_max), |
|
|
'Confidence/Probability Score': conf |
|
|
}) |
|
|
|
|
|
sub = pd.DataFrame(all_data) |
|
|
|
|
|
sub.to_csv("/tmp/static/CSV_File/WBC_File.csv", index=False) |
|
|
|
|
|
|
|
|
app = Flask(__name__, static_folder="/tmp") |
|
|
app.config['UPLOAD_FOLDER'] = '/tmp/UPLOAD_FOLDER' |
|
|
|
|
|
@app.route('/') |
|
|
def home(): |
|
|
return render_template('index.html') |
|
|
|
|
|
@app.route('/index') |
|
|
def home_click(): |
|
|
return render_template('index.html') |
|
|
|
|
|
@app.route('/document') |
|
|
def document_click(): |
|
|
df = pd.read_csv("Documents/results.csv") |
|
|
|
|
|
data = df.to_dict(orient='records') |
|
|
|
|
|
return render_template('document.html', data=data, columns=df.columns) |
|
|
|
|
|
|
|
|
@app.route('/wbc_info') |
|
|
def wbc_info_click(): |
|
|
return render_template('wbc_info.html') |
|
|
|
|
|
|
|
|
@app.route('/upload', methods=['GET', 'POST']) |
|
|
def upload(): |
|
|
try: |
|
|
if request.method == 'POST': |
|
|
|
|
|
clr_old_upload() |
|
|
clr_pred() |
|
|
mk_pred_folder() |
|
|
mk_uploaded_folder() |
|
|
mk_csv_folder() |
|
|
show_csv_heading = False |
|
|
f = request.files['fileInput'] |
|
|
|
|
|
f.save(os.path.join(app.config["UPLOAD_FOLDER"], secure_filename(f.filename) )) |
|
|
imageList = os.listdir("/tmp/UPLOAD_FOLDER") |
|
|
|
|
|
for image in imageList: |
|
|
|
|
|
Use_yolo("/tmp/UPLOAD_FOLDER/"+image) |
|
|
|
|
|
pred_image_list = os.listdir("/tmp/static/Image_Prediction") |
|
|
|
|
|
df = pd.read_csv("/tmp/static/CSV_File/WBC_File.csv") |
|
|
|
|
|
data = df.to_dict(orient='records') |
|
|
show_csv_heading = True |
|
|
return render_template("index.html", pred_image_list= pred_image_list, data=data, columns=df.columns, show_csv_heading=show_csv_heading) |
|
|
|
|
|
except: |
|
|
return render_template("error.html") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
app.run(host='0.0.0.0', port=7860) |
|
|
|