barathm111 commited on
Commit
b584001
1 Parent(s): 6f755f5

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +181 -0
  2. index.html +117 -0
  3. strawberryy.h5 +3 -0
  4. styles.css +64 -0
app.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ from PIL import Image
3
+ import numpy as np
4
+ import os
5
+ import tensorflow as tf
6
+ from tensorflow.keras.models import load_model
7
+ from tensorflow.keras.preprocessing import image
8
+ from tensorflow.keras.layers import (
9
+ Input,
10
+ Conv2D,
11
+ MaxPooling2D,
12
+ Flatten,
13
+ Dense,
14
+ BatchNormalization,
15
+ Activation,
16
+ Add,
17
+ Concatenate,
18
+ )
19
+ from tensorflow.keras.optimizers import Adam
20
+ from vit_keras import vit
21
+
22
+ app = Flask(__name__)
23
+
24
+ # Define the upload directory
25
+ UPLOAD_FOLDER = 'uploads'
26
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
27
+
28
+ # Define transitional block function
29
+ def transitional_block(x, filters):
30
+ x = Conv2D(filters, kernel_size=(3, 3), strides=(1, 1), padding='same')(x)
31
+ x = BatchNormalization()(x)
32
+ x = Activation('relu')(x)
33
+ x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)
34
+ return x
35
+
36
+ # Define pointwise convolution block function
37
+ def pointwise_conv_block(x, filters):
38
+ x = Conv2D(filters, kernel_size=(1, 1), strides=(1, 1), padding='same')(x)
39
+ x = BatchNormalization()(x)
40
+ x = Activation('relu')(x)
41
+ return x
42
+
43
+ # Define the ViT model
44
+ input_shape = (224, 224, 3) # Example input shape
45
+ num_classes = 4 # Example number of classes
46
+ vit_model = vit.vit_b32(
47
+ image_size=input_shape[:2],
48
+ include_top=False,
49
+ pretrained=True,
50
+ pretrained_top=False,
51
+ classes=num_classes,
52
+ weights="imagenet21k",
53
+ )
54
+
55
+ # Freeze the layers of the ViT model
56
+ for layer in vit_model.layers:
57
+ layer.trainable = False
58
+
59
+ # Define the modified VGG19 model
60
+ def modified_vgg19(input_tensor):
61
+ # Block 1
62
+ x = Conv2D(64, kernel_size=(3, 3), strides=(1, 1), padding='same')(input_tensor)
63
+ x = BatchNormalization()(x)
64
+ x = Activation('relu')(x)
65
+ x = transitional_block(x, 64)
66
+
67
+ # Block 2
68
+ x = transitional_block(x, 128)
69
+ x = pointwise_conv_block(x, 128)
70
+
71
+ # Block 3
72
+ x = transitional_block(x, 256)
73
+ x = pointwise_conv_block(x, 256)
74
+ x = pointwise_conv_block(x, 256)
75
+ x = pointwise_conv_block(x, 256)
76
+
77
+ # Block 4
78
+ x = transitional_block(x, 512)
79
+ x = pointwise_conv_block(x, 512)
80
+ x = pointwise_conv_block(x, 512)
81
+ x = pointwise_conv_block(x, 512)
82
+ x = pointwise_conv_block(x, 512)
83
+
84
+ # Block 5
85
+ x = transitional_block(x, 512)
86
+ x = pointwise_conv_block(x, 512)
87
+ x = pointwise_conv_block(x, 512)
88
+ x = pointwise_conv_block(x, 512)
89
+ x = pointwise_conv_block(x, 512)
90
+
91
+ x = transitional_block(x, 1024)
92
+ x = pointwise_conv_block(x, 1024)
93
+ x = pointwise_conv_block(x, 1024)
94
+ x = pointwise_conv_block(x, 1024)
95
+ x = pointwise_conv_block(x, 1024)
96
+
97
+ x = Flatten()(x)
98
+ x = Dense(256, activation='relu')(x)
99
+ x = Dense(256, activation='relu')(x)
100
+ output_layer = Dense(4, activation='softmax')(x)
101
+
102
+ return output_layer
103
+ # Your modified VGG19 architecture here
104
+
105
+ # Register custom layers used in modified_vgg19
106
+ tf.keras.utils.get_custom_objects()['transitional_block'] = transitional_block
107
+ tf.keras.utils.get_custom_objects()['pointwise_conv_block'] = pointwise_conv_block
108
+
109
+ # Load the pre-trained model
110
+ path = "C:\\Users\\HI BUDDY\\Desktop\\PoultryApp\\strawberryy.h5"
111
+
112
+ loaded_model = load_model(path, custom_objects={
113
+ 'transitional_block': transitional_block,
114
+ 'pointwise_conv_block': pointwise_conv_block,
115
+ })
116
+
117
+ # Function to preprocess the image
118
+ def preprocess_image(image_path):
119
+ img = image.load_img(image_path, target_size=(224, 224))
120
+ img_array = image.img_to_array(img)
121
+ img_array = np.expand_dims(img_array, axis=0)
122
+ img_array = img_array / 255.0
123
+ return img_array
124
+
125
+ # Function to check if the file has an allowed extension
126
+ def allowed_file(filename):
127
+ ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
128
+ return '.' in filename and \
129
+ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
130
+
131
+ @app.route('/')
132
+ def index():
133
+ return render_template('index.html')
134
+
135
+ @app.route('/predict', methods=['POST'])
136
+ def predict():
137
+ # Check if the 'file' key is in the request
138
+ if 'file' not in request.files:
139
+ return jsonify({'error': 'No file part'})
140
+
141
+ file = request.files['file']
142
+
143
+ # Check if file is empty
144
+ if file.filename == '':
145
+ return jsonify({'error': 'No selected file'})
146
+
147
+ # Check if file is allowed
148
+ if file and allowed_file(file.filename):
149
+ # Create the upload directory if it doesn't exist
150
+ if not os.path.exists(app.config['UPLOAD_FOLDER']):
151
+ os.makedirs(app.config['UPLOAD_FOLDER'])
152
+
153
+ img_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
154
+ file.save(img_path)
155
+
156
+ # Preprocess the image
157
+ img_array = preprocess_image(img_path)
158
+
159
+ #labels
160
+ class_labels = ['klebsila', 'healthy', 'ecoli' , 'pseudomonas', 'staphylococcus', 'streptococcus']
161
+
162
+ # Make predictions
163
+ predictions = loaded_model.predict(img_array)
164
+
165
+ # Set confidence threshold
166
+ confidence_threshold = 0.50 # Adjust as needed #.55 for normal #.85 for microscope
167
+
168
+ # Get the predicted class label and confidence
169
+ max_confidence = np.max(predictions)
170
+
171
+ if max_confidence >= confidence_threshold:
172
+ predicted_class_index = np.argmax(predictions)
173
+ predicted_class_label = class_labels[predicted_class_index]
174
+ return jsonify({'prediction': predicted_class_label})
175
+ else:
176
+ return jsonify({'prediction': "This format is not supported"})
177
+
178
+ return jsonify({'error': 'Invalid file format'})
179
+
180
+ if __name__ == '__main__':
181
+ app.run(debug=True)
index.html ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Image Classifier</title>
7
+ <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
8
+ </head>
9
+ <body>
10
+ <div id="headerContainer" class="header-container">
11
+ <h1>BerryWatch AI</h1>
12
+ </div>
13
+
14
+ <div id="mainContainer" style="display: flex;">
15
+ <div id="inputContainer" style="flex-grow: 1;">
16
+ <input type="file" id="uploadInput">
17
+ <button id="uploadButton">Upload</button>
18
+ <div id="predictionResult"></div>
19
+ <div id="diseaseInfo" style="display: none;">
20
+ <h2>General Precaution</h2>
21
+ <div id="diseaseDescription"></div>
22
+ <div id="prevention"></div>
23
+ <div id="suggestions"></div>
24
+ </div>
25
+ </div>
26
+ <div id="uploadedImageContainer" style="flex-grow: 1; margin-left: 20px;">
27
+ <img id="uploadedImage" src="#" alt="Uploaded Image" style="max-width: 100%; max-height: 300px; display: none;">
28
+ </div>
29
+ </div>
30
+ <script>
31
+ document.getElementById('uploadInput').addEventListener('change', function() {
32
+ const fileInput = document.getElementById('uploadInput');
33
+ const file = fileInput.files[0];
34
+ if (!file) {
35
+ alert('Please select a file.');
36
+ return;
37
+ }
38
+
39
+ // Display uploaded image
40
+ const uploadedImage = document.getElementById('uploadedImage');
41
+ uploadedImage.src = URL.createObjectURL(file);
42
+ uploadedImage.style.display = 'block';
43
+ });
44
+
45
+ document.getElementById('uploadButton').addEventListener('click', async function() {
46
+ const fileInput = document.getElementById('uploadInput');
47
+ const file = fileInput.files[0];
48
+ if (!file) {
49
+ alert('Please select a file.');
50
+ return;
51
+ }
52
+
53
+ const formData = new FormData();
54
+ formData.append('file', file);
55
+
56
+ try {
57
+ const response = await fetch('/predict', {
58
+ method: 'POST',
59
+ body: formData
60
+ });
61
+ const data = await response.json();
62
+ document.getElementById('predictionResult').innerText = data.prediction;
63
+
64
+ // Display additional information based on prediction
65
+ const diseaseInfo = document.getElementById('diseaseInfo');
66
+ const diseaseDescription = document.getElementById('diseaseDescription');
67
+ const prevention = document.getElementById('prevention');
68
+ const suggestions = document.getElementById('suggestions');
69
+
70
+ // Reset previous content
71
+ diseaseDescription.innerHTML = '';
72
+ prevention.innerHTML = '';
73
+ suggestions.innerHTML = '';
74
+
75
+ switch (data.prediction.toLowerCase()) {
76
+ case 'ecoli':
77
+ diseaseDescription.innerHTML = 'Disease caused by E. coli.';
78
+ prevention.innerHTML = 'Prevent contamination of food and water sources.';
79
+ suggestions.innerHTML = 'Use organic fertilizers such as compost.';
80
+ diseaseInfo.style.display = 'block';
81
+ break;
82
+ case 'streptococcus':
83
+ diseaseDescription.innerHTML = 'Disease caused by Streptococcus bacteria.';
84
+ prevention.innerHTML = 'Maintain good hygiene and cleanliness.';
85
+ suggestions.innerHTML = 'Regularly sanitize surfaces and avoid close contact with infected individuals.';
86
+ diseaseInfo.style.display = 'block';
87
+ break;
88
+ case 'staphylococcus':
89
+ diseaseDescription.innerHTML = 'Disease caused by Staphylococcus bacteria.';
90
+ prevention.innerHTML = 'Practice good personal hygiene and wound care.';
91
+ suggestions.innerHTML = 'Use antibiotics as prescribed by a healthcare professional.';
92
+ diseaseInfo.style.display = 'block';
93
+ break;
94
+ case 'klebsiella':
95
+ diseaseDescription.innerHTML = 'Disease caused by Klebsiella bacteria.';
96
+ prevention.innerHTML = 'Prevent transmission through proper sanitation measures.';
97
+ suggestions.innerHTML = 'Avoid overuse of antibiotics.';
98
+ diseaseInfo.style.display = 'block';
99
+ break;
100
+ case 'pseudomonas':
101
+ diseaseDescription.innerHTML = 'Disease caused by Pseudomonas bacteria.';
102
+ prevention.innerHTML = 'Maintain proper hygiene and cleanliness in healthcare settings.';
103
+ suggestions.innerHTML = 'Use appropriate disinfectants and antibiotics.';
104
+ diseaseInfo.style.display = 'block';
105
+ break;
106
+ default:
107
+ // If prediction does not match any specific case, hide the additional info
108
+ diseaseInfo.style.display = 'none';
109
+ break;
110
+ }
111
+ } catch (error) {
112
+ console.error('Error:', error);
113
+ }
114
+ });
115
+ </script>
116
+ </body>
117
+ </html>
strawberryy.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5fdebd3d638467558fd0320d8c8581538fe3cf4db699e1acd0d77704b9c3e141
3
+ size 569303136
styles.css ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ font-family: Arial, sans-serif;
3
+ margin: 0;
4
+ padding: 0;
5
+ display: flex;
6
+ flex-direction: column;
7
+ align-items: center;
8
+ justify-content: center;
9
+ height: 100vh;
10
+ background-color: #c26e82;
11
+ }
12
+
13
+ h1 {
14
+ margin-bottom: 20px;
15
+ }
16
+
17
+ #uploadInput {
18
+ margin-bottom: 20px;
19
+ }
20
+
21
+ #uploadButton {
22
+ padding: 10px 20px;
23
+ background-color: #4caf50;
24
+ color: white;
25
+ border: none;
26
+ border-radius: 5px;
27
+ cursor: pointer;
28
+ }
29
+
30
+ #uploadButton:hover {
31
+ background-color: #45a049;
32
+ }
33
+
34
+ #predictionResult {
35
+ margin-top: 20px;
36
+ font-weight: bold;
37
+ }
38
+
39
+ /* Added styles for uploaded image container */
40
+ #uploadedImageContainer {
41
+ display: flex;
42
+ justify-content: center;
43
+ align-items: center;
44
+
45
+ }
46
+
47
+ /* Added styles for disease information container */
48
+ #diseaseInfo {
49
+ margin-top: 20px;
50
+ padding: 20px;
51
+ background-color: #fff;
52
+ border-radius: 5px;
53
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
54
+ }
55
+
56
+ #diseaseInfo h2 {
57
+ margin-bottom: 10px;
58
+ color: #333;
59
+ }
60
+
61
+ #diseaseInfo div {
62
+ margin-bottom: 10px;
63
+ }
64
+