Commit 544afd82 authored by Vimalasooriya P.G.N.T's avatar Vimalasooriya P.G.N.T

Merge branch 'IT19970646' into 'master'

Insect detection part

See merge request !1
parents 9ec3bb04 5f7ec4f9
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (fertilizerProjBackend)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/fertilizerProjBackend.iml" filepath="$PROJECT_DIR$/.idea/fertilizerProjBackend.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/fertilizerProjBackend" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="effc1f58-4f9a-4fe0-8698-49968642257d" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/fertilizerProjBackend/index.py" beforeDir="false" afterPath="$PROJECT_DIR$/fertilizerProjBackend/index.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/fertilizerProjBackend/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/fertilizerProjBackend/main.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/fertilizerProjBackend" />
</component>
<component name="ProjectId" id="2PrWlojrV5VD7b4PxkFMqEBbvR1" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"last_opened_file_path": "C:/Users/user/Desktop/Project/fertilizerProjBackend",
"settings.editor.selected.configurable": "preferences.fileTypes"
}
}]]></component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="effc1f58-4f9a-4fe0-8698-49968642257d" name="Changes" comment="" />
<created>1684215837501</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1684215837501</updated>
</task>
<servers />
</component>
</project>
\ No newline at end of file
from flask import Flask
# Define a flask app
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
import os
import flask
import json
from flask import render_template, request
from flask import jsonify
from werkzeug.utils import secure_filename
from app import app
from main import getColorPrediction, getInsectPrediction, getLeafPrediction, predictFertilizer
@app.route('/', methods=['GET'])
def index():
# Main page
return render_template('index.html')
@app.route('/predictInsect', methods=['POST'])
def submit_file():
if request.method == 'POST':
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected for uploading'})
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
label, acc = getInsectPrediction(filename)
response = {
'label': label,
'probability': acc
}
return jsonify(response)
else:
return jsonify({'error': 'Invalid request method'})
@app.route('/predictColor', methods=['POST'])
def submit_file_color():
if request.method == 'POST':
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected for uploading'})
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
label, acc = getColorPrediction(filename)
response = {
'label': label,
'probability': acc
}
return jsonify(response)
else:
return jsonify({'error': 'Invalid request method'})
@app.route('/predictLeaf', methods=['POST'])
def submit_file_leaf():
if request.method == 'POST':
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected for uploading'})
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
label, acc = getLeafPrediction(filename)
response = {
'label': label,
'probability': acc
}
return jsonify(response)
else:
return jsonify({'error': 'Invalid request method'})
@app.route('/predictFertilizer', methods=['POST'])
def submit_yield():
msg_received = flask.request.get_json()
rsp = predictFertilizer(msg_received)
return json.dumps(rsp.__dict__)
if __name__ == '__main__':
app.run(debug=True, port=4000)
import tensorflow as tf
import numpy as np
import pandas as pd
from keras_preprocessing.image import load_img
from keras_preprocessing.image import img_to_array
from teachable_machine import TeachableMachine
from sklearn.linear_model import LinearRegression
from response import Response
# Model Saved Path
INSECT_MODEL_PATH = 'models/insect_type_model.h5'
INSECT_MODEL_LABELS_PATH = 'models/insect_type_model_labels.txt'
TYPE_MODEL_PATH = 'models/paddy_or_other_pest_model.h5'
TYPE_MODEL_LABELS_PATH = 'models/paddy_or_other_pest_model_labels.txt'
LEAF_MODEL_PATH = 'models/healthy_disease_leaf_model.h5'
LEAF_MODEL_LABELS_PATH = 'models/healthy_disease_leaf_model_labels.txt'
COLOR_MODEL_PATH = 'models/colors_level_model.h5'
COLOR_MODEL_LABELS_PATH = 'models/colors_level_model_labels.txt'
# Define the mapping from class indices to class labels
type_class_labels = {0: 'PaddyPest', 1: 'OtherPest'}
class_labels = {0: 'rice leaf roller', 1: 'rice leaf caterpillar ', 2: 'paddy stem maggot', 3: 'asiatic rice borer',
4: 'yellow rice borer', 5: 'rice gall midge ', 6: 'Rice Stemfly', 7: 'brown plant hopper',
8: 'white backed plant hopper',
9: 'rice leafhopper', 10: 'Paddy Bug'}
color_labels = {0: 'ColorL2', 1: 'ColorL3', 2: 'ColorL4', 3: 'ColorL5', }
leaf_labels = {0: 'Healthy', 1: 'BrownSpot', 2: 'Hispa', 3: 'LeafBlast', 4: 'Bacterialleafblight'}
def getInsectPrediction(filename):
typeModel = TeachableMachine(model_path=TYPE_MODEL_PATH,
labels_file_path=TYPE_MODEL_LABELS_PATH)
image_path = 'uploads/' + filename
result = typeModel.classify_image(image_path)
print("class_index", result["class_index"])
print("class_name:::", result["class_name"])
print("class_confidence:", result["class_confidence"])
typeLabel = str(result["class_name"]).rstrip()
if typeLabel == 'PaddyPest':
insectModel = TeachableMachine(model_path=INSECT_MODEL_PATH,
labels_file_path=INSECT_MODEL_LABELS_PATH)
image_path = 'uploads/' + filename
result = insectModel.classify_image(image_path)
print("class_index", result["class_index"])
print("class_name:::", result["class_name"])
print("class_confidence:", result["class_confidence"])
insect_label = str(result["class_name"]).rstrip()
probability = str(result["class_confidence"])
return insect_label, probability
else:
print('Not a paddy pest')
return typeLabel, 0.0
def getColorPrediction(filename):
colorModel = TeachableMachine(model_path=COLOR_MODEL_PATH,
labels_file_path=COLOR_MODEL_LABELS_PATH)
image_path = 'uploads/' + filename
result = colorModel.classify_image(image_path)
print("class_index", result["class_index"])
print("class_name:::", result["class_name"])
print("class_confidence:", result["class_confidence"])
colorLabel = str(result["class_name"])
probability = str(result["class_confidence"])
return colorLabel, probability
def getLeafPrediction(filename):
model = TeachableMachine(model_path=LEAF_MODEL_PATH,
labels_file_path=LEAF_MODEL_LABELS_PATH)
image_path = 'uploads/' + filename
result = model.classify_image(image_path)
print("class_index", result["class_index"])
print("class_name:::", result["class_name"])
print("class_confidence:", result["class_confidence"])
label = str(result["class_name"])
probability = str(result["class_confidence"])
return label, probability
def predictFertilizer(msg_received):
try:
type = msg_received["type"]
year = msg_received["year"]
month = msg_received["month"]
keyWord = msg_received["keyWord"]
except:
p1 = Response(1, "Required data missing.", None)
return p1
type = msg_received["type"]
year = msg_received["year"]
month = msg_received["month"]
keyWord = msg_received["keyWord"]
data = ()
if type == 'month':
data = predictFertilizerMonthly(int(year), int(month), keyWord)
elif type == 'year':
data = predictFertilizerYearly(int(year), keyWord).to_dict()
else:
data = predictFertilizer(int(year), int(month), keyWord)
p1 = Response(0, "Success.", data)
return p1
def predictFertilizerMonthly(year, predMonth, predKeyWord):
year = year - 1
# Read the rainfall data into a DataFrame
df = pd.read_csv('csvFiles/fertilizer1.csv')
# Convert the date column to datetime and set it as the index
df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)
# Create a new DataFrame that contains only the crop data
crop_df = df[[predKeyWord]]
# Add a column for the month of each data point
crop_df['month'] = crop_df.index.month
# Add a column for the year of each data point
crop_df['year'] = crop_df.index.year
# Create a pivot table that shows the total rainfall for each month and year
rainfall_pivot = crop_df.pivot_table(values=predKeyWord, index='year', columns='month', aggfunc='sum')
# Create a new DataFrame that contains only the data for the previous year
prev_year_df = rainfall_pivot.loc[year]
# Drop the December rainfall data (since we're trying to predict it)
prev_year_df.drop(predMonth, inplace=True)
# Flatten the DataFrame to create two arrays (X and y) for the linear regression model
X = prev_year_df.index.values.reshape(-1, 1)
y = prev_year_df.values.reshape(-1, 1)
# Create a linear regression model and fit it to the data
model = LinearRegression()
model.fit(X, y)
# Predict the rainfall for December of the current year
current_year = pd.Timestamp.now().year
X_pred = [[current_year]]
y_pred = model.predict(X_pred)
# Print the predicted rainfall for December of the current year
print('Our guess for this year\'s December ' + predKeyWord + ' is:', y_pred[0][0])
return y_pred[0][0]
def predictFertilizerYearly(year, predKeyWord):
# Read the rainfall data into a DataFrame
df = pd.read_csv('csvFiles/fertilizer1.csv')
# Convert the date column to datetime and set it as the index
df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)
# Create a new DataFrame that contains only the crop data
crop_df = df[[predKeyWord]]
# Add a column for the month of each data point
crop_df['month'] = crop_df.index.month
# Add a column for the year of each data point
crop_df['year'] = crop_df.index.year
# Create a pivot table that shows the total rainfall for each month and year
rainfall_pivot = crop_df.pivot_table(values=predKeyWord, index='year', columns='month', aggfunc='sum')
# Create a new DataFrame that contains only the data for the previous year
prev_year_df = rainfall_pivot.loc[year - 1]
# Drop the December rainfall data (since we're using it as the target variable)
# prev_year_df.drop(predMonth, inplace=True)
# Create a DataFrame to store the predictions
predictions_df = pd.DataFrame(columns=['month', predKeyWord])
# Loop over each month and make a prediction for the next year's rainfall
for month in range(1, 13):
# Create a new DataFrame that contains only the data for the current month
month_df = prev_year_df[[month]]
print(month_df)
# Flatten the DataFrame to create two arrays (X and y) for the linear regression model
X = month_df.index.values.reshape(-1, 1)
y = month_df.values.reshape(-1, 1)
# Create a linear regression model and fit it to the data
model = LinearRegression()
model.fit(X, y)
# Predict the rainfall for the next year
next_year = year
X_pred = [[next_year]]
y_pred = model.predict(X_pred)
# Add the prediction to the predictions DataFrame
predictions_df = predictions_df.append({'month': month, predKeyWord: y_pred[0][0]}, ignore_index=True)
# Print the predicted rainfall for each month of the next year
return predictions_df
This diff is collapsed.
class Response:
def __init__(self, code, message, data):
self.code = code
self.message = message
self.data = data
from keras.models import load_model
from PIL import Image, ImageOps
import numpy as np
class TeachableMachine(object):
'''
Create your TeachableMachine object to run your trained AI models.
'''
def __init__(self, model_path='keras_model.h5', labels_file_path='labels.txt', model_type='h5') -> None:
self._model_type = model_type.lower()
self._labels_file_path = labels_file_path
self._supported_types = ('keras', 'Keras', 'h5', 'h5py')
np.set_printoptions(suppress=True)
try:
self._model = load_model(model_path, compile=False)
except IOError as e:
print('LoadingModelError: Error while loading Teachable Machine model')
raise IOError(e)
except:
print("LoadingModelError: Error while loading Teachable Machine model")
raise FileNotFoundError
try:
self._labels_file = open(self._labels_file_path, "r").readlines()
except IOError as e:
print('LoadingLabelsError: Error while loading labels.txt file')
raise IOError(e)
except:
print("LoadingLabelsError: Error while loading labels.txt file")
raise FileNotFoundError
self._object_creation_status = self._model_type in self._supported_types
if self._object_creation_status:
print('Teachable Machine Object is created successfully.')
else:
raise 'NotSupportedType: Your model type is not supported, try to use types such as "keras" or "h5".'
def classify_image(self, frame_path: str):
'''To deploy your Teachable Machine Model on a computer/PC
with .h5 extension using TensorFlow.
Parameters:
* (str) frame_path: Provide path of the image to be classified.
Returns:
* class_name: Name of the highest predicted class according to labels.txt file
* class_index: Index or ID of the highest predicted class according to labels.txt file
* predictions: All prediction values for all classes.
'''
try:
frame = Image.open(frame_path)
if frame.mode != "RGB":
frame = frame.convert("RGB")
except FileNotFoundError as e:
print("ImageNotFound: Error in image file.")
raise FileNotFoundError(e)
except TypeError as e:
print(
"ImageTypeError: Error while converting image to RGB format, image type is not supported")
try:
if self._object_creation_status:
return self._get_image_classification(frame)
except BaseException as e:
print('Error in classification process, retrain your model.')
raise e
def _get_image_classification(self, image):
data = self._form_image(image)
prediction = self._model.predict(data)
class_index = np.argmax(prediction)
class_name = self._labels_file[class_index]
class_confidence = prediction[0][class_index]
return {
"class_name": class_name[2:],
"highest_class_name": class_name[2:],
"highest_class_id": class_index,
"class_index": class_index,
"class_id": class_index,
"predictions": prediction,
"all_predictions": prediction,
"class_confidence": class_confidence,
"highest_class_confidence": class_confidence,
}
def _form_image(self, image):
image_data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
crop_size = (224, 224)
image = ImageOps.fit(image, crop_size, Image.Resampling.LANCZOS)
image_array = np.asarray(image)
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
image_data[0] = normalized_image_array
return image_data
Subproject commit 6ab1067a166b8de8b41ec98432f6b257192c5ae8
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