| from flask import Flask, render_template, request, jsonify
|
| from PIL import Image
|
| import numpy as np
|
| import os
|
| import tensorflow as tf
|
| from tensorflow.keras.models import load_model
|
| from tensorflow.keras.preprocessing import image
|
| from tensorflow.keras.layers import (
|
| Input,
|
| Conv2D,
|
| MaxPooling2D,
|
| Flatten,
|
| Dense,
|
| BatchNormalization,
|
| Activation,
|
| Add,
|
| Concatenate,
|
| )
|
| from tensorflow.keras.optimizers import Adam
|
| from vit_keras import vit
|
|
|
| app = Flask(__name__)
|
|
|
|
|
| UPLOAD_FOLDER = 'uploads'
|
| app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
|
|
|
| def transitional_block(x, filters):
|
| x = Conv2D(filters, kernel_size=(3, 3), strides=(1, 1), padding='same')(x)
|
| x = BatchNormalization()(x)
|
| x = Activation('relu')(x)
|
| x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)
|
| return x
|
|
|
|
|
| def pointwise_conv_block(x, filters):
|
| x = Conv2D(filters, kernel_size=(1, 1), strides=(1, 1), padding='same')(x)
|
| x = BatchNormalization()(x)
|
| x = Activation('relu')(x)
|
| return x
|
|
|
|
|
| input_shape = (224, 224, 3)
|
| num_classes = 4
|
| vit_model = vit.vit_b32(
|
| image_size=input_shape[:2],
|
| include_top=False,
|
| pretrained=True,
|
| pretrained_top=False,
|
| classes=num_classes,
|
| weights="imagenet21k",
|
| )
|
|
|
|
|
| for layer in vit_model.layers:
|
| layer.trainable = False
|
|
|
|
|
| def modified_vgg19(input_tensor):
|
|
|
| x = Conv2D(64, kernel_size=(3, 3), strides=(1, 1), padding='same')(input_tensor)
|
| x = BatchNormalization()(x)
|
| x = Activation('relu')(x)
|
| x = transitional_block(x, 64)
|
|
|
|
|
| x = transitional_block(x, 128)
|
| x = pointwise_conv_block(x, 128)
|
|
|
|
|
| x = transitional_block(x, 256)
|
| x = pointwise_conv_block(x, 256)
|
| x = pointwise_conv_block(x, 256)
|
| x = pointwise_conv_block(x, 256)
|
|
|
|
|
| x = transitional_block(x, 512)
|
| x = pointwise_conv_block(x, 512)
|
| x = pointwise_conv_block(x, 512)
|
| x = pointwise_conv_block(x, 512)
|
| x = pointwise_conv_block(x, 512)
|
|
|
|
|
| x = transitional_block(x, 512)
|
| x = pointwise_conv_block(x, 512)
|
| x = pointwise_conv_block(x, 512)
|
| x = pointwise_conv_block(x, 512)
|
| x = pointwise_conv_block(x, 512)
|
|
|
| x = transitional_block(x, 1024)
|
| x = pointwise_conv_block(x, 1024)
|
| x = pointwise_conv_block(x, 1024)
|
| x = pointwise_conv_block(x, 1024)
|
| x = pointwise_conv_block(x, 1024)
|
|
|
| x = Flatten()(x)
|
| x = Dense(256, activation='relu')(x)
|
| x = Dense(256, activation='relu')(x)
|
| output_layer = Dense(4, activation='softmax')(x)
|
|
|
| return output_layer
|
|
|
|
|
|
|
| tf.keras.utils.get_custom_objects()['transitional_block'] = transitional_block
|
| tf.keras.utils.get_custom_objects()['pointwise_conv_block'] = pointwise_conv_block
|
|
|
|
|
| path = "C:\\Users\\HI BUDDY\\Desktop\\PoultryApp\\strawberryy.h5"
|
|
|
| loaded_model = load_model(path, custom_objects={
|
| 'transitional_block': transitional_block,
|
| 'pointwise_conv_block': pointwise_conv_block,
|
| })
|
|
|
|
|
| def preprocess_image(image_path):
|
| img = image.load_img(image_path, target_size=(224, 224))
|
| img_array = image.img_to_array(img)
|
| img_array = np.expand_dims(img_array, axis=0)
|
| img_array = img_array / 255.0
|
| return img_array
|
|
|
|
|
| def allowed_file(filename):
|
| ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
|
| return '.' in filename and \
|
| filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
| @app.route('/')
|
| def index():
|
| return render_template('index.html')
|
|
|
| @app.route('/predict', methods=['POST'])
|
| def predict():
|
|
|
| if 'file' not in request.files:
|
| return jsonify({'error': 'No file part'})
|
|
|
| file = request.files['file']
|
|
|
|
|
| if file.filename == '':
|
| return jsonify({'error': 'No selected file'})
|
|
|
|
|
| if file and allowed_file(file.filename):
|
|
|
| if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
| os.makedirs(app.config['UPLOAD_FOLDER'])
|
|
|
| img_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
|
| file.save(img_path)
|
|
|
|
|
| img_array = preprocess_image(img_path)
|
|
|
|
|
| class_labels = ['klebsila', 'healthy', 'ecoli' , 'pseudomonas', 'staphylococcus', 'streptococcus']
|
|
|
|
|
| predictions = loaded_model.predict(img_array)
|
|
|
|
|
| confidence_threshold = 0.50
|
|
|
|
|
| max_confidence = np.max(predictions)
|
|
|
| if max_confidence >= confidence_threshold:
|
| predicted_class_index = np.argmax(predictions)
|
| predicted_class_label = class_labels[predicted_class_index]
|
| return jsonify({'prediction': predicted_class_label})
|
| else:
|
| return jsonify({'prediction': "This format is not supported"})
|
|
|
| return jsonify({'error': 'Invalid file format'})
|
|
|
| if __name__ == '__main__':
|
| app.run(debug=True)
|
|
|