Commit 9c8cc673 authored by user's avatar user

Flower Freshness Model Commit After Rebase

parent 7a3a7323
import base64
import datetime
import cv2 as cv
import numpy as np
import pandas as pd
from tkinter import *
import firebase_admin
import tensorflow as tf
import tkinter.filedialog
from firebase_admin import db
from PIL import ImageTk, Image
from firebase_admin import credentials
node = 'flower_disease'
model = tf.keras.models.load_model('models/flower_disease_detector.h5')
cred = credentials.Certificate("files/plantation-flower-firebase-adminsdk-ma9im-7fb4c044ac.json")
default_app = firebase_admin.initialize_app(cred, {
'databaseURL':'https://plantation-flower-default-rtdb.firebaseio.com/'
})
ref_node = db.reference(node)
def inference_disease(
image_path,
target_size = (224, 224),
class_dict = {0: 'Black Spot', 1: 'Downy Mildew', 2: 'Fresh Leaf'}
):
class_dict_rev = dict((v,k) for k,v in class_dict.items())
img_file = image_path.split('/')[-2]
image = cv.imread(image_path)
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
image = cv.resize(image, target_size)
image = np.expand_dims(image, axis=0)
image = tf.keras.applications.xception.preprocess_input(image)
pred = model.predict(image, verbose=0).squeeze()
pred = np.argmax(pred)
pred = class_dict[class_dict_rev[img_file]]
return pred
def write_data_firebase(
image_path,
disease,
):
image_path = image_path.replace('\\', '/')
file_name = image_path.split('/')[-1].split('.')[0].strip()
presentDate = datetime.datetime.now()
unix_timestamp = int(datetime.datetime.timestamp(presentDate)*1000)
# # convert image to base64 & store in firebase
# with open(image_path, "rb") as image_file:
# encoded_string = base64.b64encode(image_file.read())
prefix = 'data:image/jpeg;base64,'
image_np = cv.imread(image_path)
image_np = cv.cvtColor(image_np, cv.COLOR_BGR2RGB)
image_np = cv.resize(image_np, (500, 500))
_, buffer = cv.imencode('.jpg', image_np)
encoded_string = base64.b64encode(buffer)
encoded_string = encoded_string.decode('utf-8')
data = {
"image": f"{prefix}{encoded_string}",
"disease": f"{disease}",
"timestamp": unix_timestamp
}
ref_node.push(data)
image_path = tkinter.filedialog.askopenfilename(
initialdir='data/flower_disease_type_dataset/',
title='Select Image',
filetypes=(('JPG Files', '*.JPG'), ('All Files', '*.*'))
)
disease = inference_disease(image_path)
write_data_firebase(image_path, disease)
image = Image.open(image_path)
image = image.resize((400, 400), Image.Resampling.LANCZOS)
image = ImageTk.PhotoImage(image)
image_label = Label(image=image)
image_label.image = image
image_label.grid(row=0, column=0, columnspan=3)
print('Disease: ', disease)
\ No newline at end of file
This diff is collapsed.
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