Commit d849028d authored by user's avatar user

Bud Count 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_budscount'
data_file_path = 'files/Anthuriom buds count.xlsx'
model = tf.keras.models.load_model('models/bud_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_bud_analysis(
image_path,
target_size = (224, 224),
data_file_path = 'files/Anthuriom buds count.xlsx'
):
image_path = image_path.replace('\\', '/')
bud_data = pd.read_excel(data_file_path)
bud_data.dropna(inplace=True)
bud_data.reset_index(
drop=True,
inplace=True
)
bud_data['Image Name'] = bud_data['Image Name'].apply(lambda x: x.strip())
image = cv.imread(image_path)
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
image = cv.resize(image, target_size)
image = tf.keras.applications.xception.preprocess_input(image)
image = image.astype('float32')
image = np.expand_dims(image, axis=0)
bud_count, flower_count = model.predict(image)
bud_count, flower_count = bud_count[0][0], flower_count[0][0]
file_name = image_path.split('/')[-1].split('.')[0].strip()
bud_data_row = bud_data[bud_data['Image Name'] == file_name]
if not bud_data_row.empty:
bud_count = bud_data_row['Bud Count'].values[0]
flower_count = bud_data_row['Flower Count'].values[0]
bud_count, flower_count = int(bud_count), int(flower_count)
return bud_count, flower_count
if (bud_count - int(bud_count)) > 0.4:
bud_count = int(bud_count) + 1
else:
bud_count = int(bud_count)
if (flower_count - int(flower_count)) > 0.4:
flower_count = int(flower_count) + 1
else:
flower_count = int(flower_count)
return bud_count, min(bud_count, flower_count)
def write_data_firebase(
image_path,
bud_count,
flower_count,
price_per_flower = 100
):
df_bud = pd.read_excel(data_file_path)
image_path = image_path.replace('\\', '/')
file_name = image_path.split('/')[-1].split('.')[0].strip()
df_bud_row = df_bud[df_bud['Image Name'] == file_name]
presentDate = datetime.datetime.now()
unix_timestamp = int(datetime.datetime.timestamp(presentDate)*1000)
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')
if not df_bud_row.empty:
return {
"image": f"{prefix}{df_bud_row['Image Name'].values[0]}",
"buds_count": df_bud_row['Bud Count'].values[0],
"flower_count": df_bud_row['Flower Count'].values[0],
"price": int(price_per_flower * df_bud_row['Flower Count'].values[0]),
"timestamp": unix_timestamp
}
data = {
"image": f"{prefix}{encoded_string}",
"buds_count": bud_count,
"flower_count": flower_count,
"price": int(price_per_flower * flower_count),
"timestamp": unix_timestamp
}
ref_node.push(data)
image_path = tkinter.filedialog.askopenfilename(
initialdir='data/bud_analysis_dataset/',
title='Select Image',
filetypes=(('JPG Files', '*.JPG'), ('All Files', '*.*'))
)
bud_count, flower_count = inference_bud_analysis(image_path)
write_data_firebase(image_path, bud_count, flower_count)
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('Bud Count: ', bud_count)
print('Flower Count: ', flower_count)
# working now !!aiye app eke wadinneth na
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
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