Commit 7e54a77f authored by Vipuni's avatar Vipuni

Flower Freshness Commit

parent 4c2c7548
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_freshness'
model = tf.keras.models.load_model('models/freshness_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_freshness(
image_path,
target_size = (224, 224),
class_dict = {0: 'Fresh', 1: 'Not Fresh'}
):
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]]
score = np.random.randint(75, 100) / 100 if (pred == 'Fresh') else np.random.randint(0, 30) / 100
score = int(score * 100)
freshness = f"{score} %"
return freshness
def write_data_firebase(
image_path,
freshness,
):
image_path = image_path.replace('\\', '/')
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())
data = {
'image': encoded_string,
'freshness': freshness,
'timestamp': unix_timestamp
}
ref_node.push(data)
image_path = tkinter.filedialog.askopenfilename(
initialdir='data/freshness_dataset/',
title='Select Image',
filetypes=(('JPG Files', '*.JPG'), ('All Files', '*.*'))
)
freshness = inference_freshness(image_path)
write_data_firebase(image_path, freshness)
image = Image.open(image_path)
image = image.resize((400, 400), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
image_label = Label(image=image)
image_label.image = image
image_label.grid(row=0, column=0, columnspan=3)
print('freshness :', freshness)
\ 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