|
|
from flask import Flask,render_template, request, jsonify, redirect, url_for |
|
|
import os |
|
|
import cv2 |
|
|
from App.dog_vision import identificationPipeline |
|
|
import numpy as np |
|
|
|
|
|
app = Flask(__name__) |
|
|
UPLOAD_FOLDER ='Static/Upload' |
|
|
PREDICT_FOLDER = './Static/Predict' |
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER |
|
|
|
|
|
@app.route('/', methods=['GET', 'POST']) |
|
|
def index(): |
|
|
if request.method == 'POST': |
|
|
file = request.files.get('image_name') |
|
|
|
|
|
|
|
|
if not file or file.filename == '': |
|
|
return jsonify({"error": "No file was uploaded"}), 400 |
|
|
|
|
|
|
|
|
path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename) |
|
|
file.save(path) |
|
|
|
|
|
print(f"Received path in views.py: {path}, Type: {type(path)}") |
|
|
|
|
|
|
|
|
output = identificationPipeline(path) |
|
|
|
|
|
|
|
|
if isinstance(output, np.ndarray): |
|
|
output = output.tolist() |
|
|
elif hasattr(output, 'numpy'): |
|
|
output = output.numpy().tolist() |
|
|
elif isinstance(output, dict): |
|
|
return jsonify(output) |
|
|
elif not isinstance(output, str): |
|
|
return jsonify({"error": "Unexpected output format"}), 500 |
|
|
|
|
|
print(f"Output from identificationPipeline: {output}") |
|
|
|
|
|
|
|
|
image = cv2.imread(path) |
|
|
if image is None: |
|
|
return jsonify({"error": "Uploaded file is not a valid image format or is corrupted"}), 400 |
|
|
|
|
|
pred_filename = 'image.jpg' |
|
|
pred_path = os.path.join(PREDICT_FOLDER, pred_filename) |
|
|
cv2.imwrite(pred_path, image) |
|
|
|
|
|
return redirect(url_for('breedIdentification', filename=file.filename, prediction=output)) |
|
|
|
|
|
return render_template('index.html') |
|
|
|
|
|
|
|
|
@app.route("/breedIdentification/") |
|
|
def breedIdentification(): |
|
|
filename = request.args.get('filename') |
|
|
prediction = request.args.get('prediction') |
|
|
|
|
|
if not filename or not prediction: |
|
|
return redirect(url_for('index')) |
|
|
|
|
|
return render_template('breedIdentification.html', filename=filename, prediction=prediction) |