File size: 2,817 Bytes
1a11305 7bc1287 1a11305 b3e3a34 1a11305 b3e3a34 1a11305 b3e3a34 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import io
import time
import cv2
from flask import Flask, render_template, request, redirect, url_for, flash,jsonify
from PIL import Image # For image processing (optional)
import os
import base64
import json
from model import predict
ALLOWED_EXTENSIONS = {'jpg', 'jpeg'}
app = Flask(__name__)
app.secret_key='your_secret_key'
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def home():
return render_template('index.html')
@app.route('/help')
def help():
return render_template('help.html')
@app.route('/about')
def aboutus():
return render_template('about.html')
@app.route('/upload', methods=['POST'])
def upload_image():
if 'file-input' in request.files:
uploaded_file = request.files['file-input']
if uploaded_file and allowed_file(uploaded_file.filename):
filename= uploaded_file.filename
try:
# Read and validate the image (modify as needed)
image_buffer=io.BytesIO(uploaded_file.read())
# print(image_buffer.getvalue())
pred= predict(image_buffer=image_buffer)
return jsonify(pred)
except (IOError, OSError, ValueError) as e:
return jsonify(json.dumps({'error': "Server Error "+ str(e)})), 500
else:
return jsonify(json.dumps({'error': "Invalid file format. Please upload a JPEG, or JPG image."})), 500
if 'file-input-64' in request.form:
try:
# Decode the base64 data (replace with your processing logic)
base64_data = request.form['file-input-64']
image_buffer= io.BytesIO(base64.b64decode(base64_data))
try:
image_buffer.seek(0)
# Use Pillow to open the image data from BytesIO
image = Image.open(image_buffer)
# Save the image as a JPEG with appropriate quality (adjust quality as needed)
# image.save("canvas.jpeg", quality=90, format="JPEG")
image_buffer.seek(0)
pred= predict(image_buffer=image_buffer)
return jsonify(pred)
except (IOError, OSError, ValueError) as e:
return jsonify(json.dumps({'error': "Khali Na Patha"})), 500
except Exception as e:
return jsonify(json.dumps({'error': str(e)})), 500
application = app
# Only run the app if this script is executed directly
if __name__ == '__main__':
application.run() |