|
|
|
from pyngrok import ngrok, conf |
|
import getpass |
|
|
|
|
|
from flask import Flask, render_template, request, redirect, url_for, send_from_directory |
|
import os |
|
|
|
from utils import cottonmodel, sugarcanemodel, tomatomodel, get_model_details |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
public_url = ngrok.connect(5000).public_url |
|
print(" * ngrok tunnel \"{}\" -> \"http://127.0.0.1:{}/\"".format(public_url, 5000)) |
|
|
|
|
|
app.config["BASE_URL"] = public_url |
|
|
|
|
|
|
|
|
|
|
|
|
|
UPLOAD_FOLDER = 'static/uploaded_images' |
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER |
|
|
|
|
|
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'} |
|
|
|
|
|
def allowed_file(filename): |
|
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS |
|
|
|
|
|
@app.route('/') |
|
def home(): |
|
model_data = get_model_details() |
|
return render_template('index.html', model_data=model_data) |
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/cropmodel/') |
|
def cropmodel(): |
|
|
|
if request.method == 'GET': |
|
|
|
modelname = request.args.get('modelname') |
|
|
|
|
|
if modelname: |
|
|
|
|
|
|
|
__model_data = get_model_details() |
|
model_data = __model_data[modelname] |
|
|
|
|
|
|
|
return render_template('modelresult.html', modelname=modelname, model_data=model_data) |
|
|
|
else: |
|
msg = "Model Name not provided in the request." |
|
return render_template('modelresult.html', msg=msg) |
|
|
|
|
|
return render_template('nongetresult.html') |
|
|
|
|
|
|
|
|
|
@app.route('/upload', methods=['POST']) |
|
def upload_file(): |
|
if request.method == 'POST': |
|
|
|
if 'file' not in request.files: |
|
return redirect(request.url) |
|
|
|
modelname = request.form.get('modelname') |
|
|
|
|
|
file = request.files['file'] |
|
|
|
if file.filename == '': |
|
return redirect(request.url) |
|
|
|
if file and allowed_file(file.filename): |
|
|
|
filename = 'imageFile.jpeg' |
|
|
|
|
|
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) |
|
|
|
|
|
defalut_image_path = 'static/uploaded_images/imageFile.jpeg' |
|
|
|
__model_data = get_model_details() |
|
model_data = __model_data[str(modelname)] |
|
|
|
if modelname == 'cottonmodel': |
|
result = cottonmodel(defalut_image_path) |
|
|
|
elif modelname == 'sugarcanemodel': |
|
result = sugarcanemodel(defalut_image_path) |
|
|
|
elif modelname == 'tomatomodel': |
|
result = tomatomodel(defalut_image_path) |
|
|
|
else: |
|
result = 'None' |
|
model_data = 'None' |
|
|
|
|
|
|
|
print("--- result : ", result) |
|
|
|
return render_template('modelresult.html', modelname=modelname, result=result, model_data=model_data) |
|
|
|
|
|
else: |
|
return "Invalid file type. Allowed extensions are: png, jpg, jpeg" |
|
|
|
return redirect(url_for('cropmodel')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True) |
|
|
|
|
|
app.run(port=5000, use_reloader=False) |
|
|
|
|
|
|
|
|
|
|