Keras
Piyush242003's picture
Upload 69 files
b9f3d6d verified
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')
# βœ… Check if file is provided
if not file or file.filename == '':
return jsonify({"error": "No file was uploaded"}), 400
# βœ… Save Image
path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(path)
print(f"Received path in views.py: {path}, Type: {type(path)}")
# βœ… Get Prediction (Expecting a label string)
output = identificationPipeline(path)
# βœ… Ensure the output is a valid string (Label name)
if isinstance(output, np.ndarray):
output = output.tolist() # Convert NumPy array to Python list
elif hasattr(output, 'numpy'):
output = output.numpy().tolist() # Convert TensorFlow tensor to list
elif isinstance(output, dict):
return jsonify(output) # If it's a dictionary, return as JSON
elif not isinstance(output, str):
return jsonify({"error": "Unexpected output format"}), 500
print(f"Output from identificationPipeline: {output}") # Debugging line
# βœ… Read and save the image properly
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')) # Redirect to home if missing data
return render_template('breedIdentification.html', filename=filename, prediction=prediction)