Commit ccb09856 authored by Gunathilaka.M.A.G.T 's avatar Gunathilaka.M.A.G.T

app file uploading

parent b231231b
import cv2
import numpy as np
import base64
from io import BytesIO
from PIL import Image
import tensorflow as tf
from flask_cors import CORS
from flask import Flask, request, jsonify
print(tf.__version__)
print(tf.keras.__version__)
app = Flask(__name__)
CORS(app)
class_dict_defect = {
'defective': 0,
'qualified': 1
}
class_dict_defect_rev = {
0: 'defective',
1: 'qualified'
}
model_defects = tf.keras.models.load_model('models/courier-defect-detector.h5')
@app.route('/defects', methods=['POST'])
def defects():
if request.method == 'POST':
data = request.json
base64_image = data['image']
image_bytes = base64.b64decode(base64_image)
image = np.array(Image.open(BytesIO(image_bytes)))
filename = "uploads/image.jpg"
cv2.imwrite(filename, image)
result = inference_model(filename)
return jsonify({
"status": "success",
"defectiveness": result,
}), 200
return jsonify({
"status": "unsuccess",
"defectiveness": None,
}), 400
def inference_model(filename):
try:
img = cv2.imread(filename)
img = cv2.resize(img, (224, 224))
if img is not None and not img.size == 0:
img = tf.keras.applications.xception.preprocess_input(img)
img = np.expand_dims(img, axis=0)
pred = model_defects.predict(img)
pred = pred.squeeze() > 0.5
pred = pred.squeeze()
return class_dict_defect_rev[int(pred)]
else:
print("Invalid or empty image:", filename)
return None
except Exception as e:
print(f"Error processing image: {str(e)}")
return None
if __name__ == '__main__':
app.run(debug=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment