Spaces:
Runtime error
Runtime error
import gradio as gr | |
from PIL import Image | |
import torch | |
import numpy as np | |
import cv2 | |
from plantcv import plantcv as pcv | |
from skimage.feature import local_binary_pattern | |
from io import BytesIO | |
from skimage.feature import hog | |
import base64 # import the base64 module | |
import openpyxl | |
import pandas as pd | |
from gradio import themes | |
import gradio as gr | |
import os | |
theme = gr.themes.Base( | |
primary_hue="violet", | |
secondary_hue="green", | |
).set( | |
body_background_fill_dark='*checkbox_label_background_fill' | |
) | |
def save_to_file(df, file_path, folder): | |
# Create the folder if it doesn't exist | |
os.makedirs(folder, exist_ok=True) | |
# Save DataFrame to Excel file | |
with pd.ExcelWriter(os.path.join(folder, file_path)) as writer: | |
df.to_excel(writer, index=False) | |
print(f"File saved at {os.path.join(folder, file_path)}") | |
HF_TOKEN = os.getenv('HF_TOKEN') | |
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "test") | |
def image_processing(image,input_type,input_choice, save_path): | |
#Initialize output image | |
output_image = np.zeros((512,512)) | |
array = np.array(image) | |
array = array.astype(np.float32) | |
gray_img = cv2.cvtColor(array, cv2.COLOR_RGB2GRAY) | |
if input_type == "Tips": | |
img1 = pcv.morphology.skeletonize(mask=gray_img) | |
output_image = pcv.morphology.find_tips(skel_img=img1, mask=None, label="default") | |
non_zero_indices = np.nonzero(output_image) | |
# Create a new Excel workbook and worksheet | |
workbook = openpyxl.Workbook() | |
worksheet = workbook.active | |
# Write the non-zero indices to the worksheet | |
for row, col in zip(*non_zero_indices): | |
worksheet.cell(row=row+1, column=1, value=row) | |
worksheet.cell(row=row+1, column=2, value=col) | |
# Save the workbook | |
#excel_tips = 'tip_pts_mask_indices.xlsx' | |
workbook.save(save_path) | |
# Create a DataFrame from the branch points mask | |
df = pd.DataFrame(output_image) | |
save_to_file(df, os.path.splitext(save_path)[0] + "_mask.xlsx", "statistics") | |
elif input_type == "Branches": | |
img1 = pcv.morphology.skeletonize(mask=gray_img) | |
output_image = pcv.morphology.find_branch_pts(skel_img=img1, mask=None, label="default") | |
non_zero_indices = np.nonzero(output_image) | |
# Create a new Excel workbook and worksheet | |
workbook = openpyxl.Workbook() | |
worksheet = workbook.active | |
# Write the non-zero indices to the worksheet | |
for row, col in zip(*non_zero_indices): | |
worksheet.cell(row=row+1, column=1, value=row) | |
worksheet.cell(row=row+1, column=2, value=col) | |
# Save the workbook | |
#excel_tips = 'tip_pts_mask_indices.xlsx' | |
workbook.save(save_path) | |
# Create a DataFrame from the branch points mask | |
df = pd.DataFrame(output_image) | |
save_to_file(df, os.path.splitext(save_path)[0] + "_mask.xlsx", "statistics") | |
elif input_type == "Both": | |
img1 = pcv.morphology.skeletonize(mask=gray_img) | |
tips = pcv.morphology.find_tips(skel_img=img1, mask=None, label="default") | |
branches = pcv.morphology.find_branch_pts(skel_img=img1, mask=None, label="default") | |
output_image = np.zeros_like(img1) | |
output_image[tips > 0] = 255 | |
output_image[branches > 0] = 128 | |
non_zero_indices = np.nonzero(output_image) | |
# Create a new Excel workbook and worksheet | |
workbook = openpyxl.Workbook() | |
worksheet = workbook.active | |
# Write the non-zero indices to the worksheet | |
for row, col in zip(*non_zero_indices): | |
worksheet.cell(row=row+1, column=1, value=row) | |
worksheet.cell(row=row+1, column=2, value=col) | |
# Save the workbook | |
#excel_tips = 'tip_pts_mask_indices.xlsx' | |
workbook.save(save_path) | |
# Create a DataFrame from the branch points mask | |
df = pd.DataFrame(output_image) | |
save_to_file(df, os.path.splitext(save_path)[0] + "_mask.xlsx", "statistics") | |
elif input_type == "sort": | |
image = pcv.morphology.skeletonize(mask=gray_img) | |
img1,edge_objects = pcv.morphology.prune(skel_img=image, size=70, mask=None) | |
#output_image = leaf(skel_img=img1,objects=edge_objects, mask=None) | |
elif input_type == "sift transform": | |
image = pcv.morphology.skeletonize(mask=gray_img) | |
sift = cv2.SIFT_create() | |
kp, des= sift.detectAndCompute(image, None) | |
output_image = cv2.drawKeypoints(image, kp, des) | |
np.savez(os.path.join("features", "sift_descriptors.npz"), descriptors=des) | |
elif input_type == "lbp transform": | |
radius = 1 # LBP feature radius | |
n_points = 8 * radius # number of LBP feature points | |
output_image = local_binary_pattern(gray_img, n_points, radius) | |
# Save the LBP transformed image as a NumPy array in .npz format | |
np.savez(os.path.join("features", "lbp_transform.npz"),lbp=output_image) | |
elif input_type == "hog transform": | |
image = pcv.morphology.skeletonize(mask=array) | |
fd,output_image = hog(gray_img, orientations=10, pixels_per_cell=(16, 16), | |
cells_per_block=(1, 1), visualize=True, multichannel=False, channel_axis=-1) | |
np.savez(os.path.join("features", "hog_transform.npz"),hog=output_image) | |
elif input_type == "compute all": | |
img1 = pcv.morphology.skeletonize(mask=gray_img) | |
if input_choice == "compute_tips": | |
output_image = pcv.morphology.find_branch_pts(skel_img=img1, mask=None, label="default") | |
non_zero_indices = np.nonzero(output_image) | |
# Create a new Excel workbook and worksheet | |
workbook = openpyxl.Workbook() | |
worksheet = workbook.active | |
# Write the non-zero indices to the worksheet | |
for row, col in zip(*non_zero_indices): | |
worksheet.cell(row=row+1, column=1, value=row) | |
worksheet.cell(row=row+1, column=2, value=col) | |
# Save the workbook | |
#excel_tips = 'tip_pts_mask_indices.xlsx' | |
workbook.save(save_path) | |
# Create a DataFrame from the branch points mask | |
df = pd.DataFrame(output_image) | |
save_to_file(df, os.path.splitext(save_path)[0] + "_mask.xlsx", "statistics") | |
elif input_choice == "compute_branch": | |
output_image = pcv.morphology.find_tips(skel_img=img1, mask=None, label="default") | |
non_zero_indices = np.nonzero(output_image) | |
# Create a new Excel workbook and worksheet | |
workbook = openpyxl.Workbook() | |
worksheet = workbook.active | |
# Write the non-zero indices to the worksheet | |
for row, col in zip(*non_zero_indices): | |
worksheet.cell(row=row+1, column=1, value=row) | |
worksheet.cell(row=row+1, column=2, value=col) | |
# Save the workbook | |
#excel_tips = 'tip_pts_mask_indices.xlsx' | |
workbook.save(save_path) | |
# Create a DataFrame from the branch points mask | |
df = pd.DataFrame(output_image) | |
save_to_file(df, os.path.splitext(save_path)[0] + "_mask.xlsx", "statistics") | |
elif input_choice == "compute_both": | |
img1 = pcv.morphology.skeletonize(mask=gray_img) | |
tips = pcv.morphology.find_tips(skel_img=img1, mask=None, label="default") | |
branches = pcv.morphology.find_branch_pts(skel_img=img1, mask=None, label="default") | |
output_image = np.zeros_like(img1) | |
output_image[tips > 0] = 255 | |
output_image[branches > 0] = 128 | |
elif input_choice == "compute_sift": | |
image = pcv.morphology.skeletonize(mask=gray_img) | |
sift = cv2.SIFT_create() | |
kp, des= sift.detectAndCompute(image, None) | |
output_image = cv2.drawKeypoints(image, kp, des) | |
np.savez(os.path.join("features", "sift_descriptors.npz"), descriptors=des) | |
elif input_choice == "compute_lbp": | |
radius = 1 # LBP feature radius | |
n_points = 8 * radius # number of LBP feature points | |
output_image = local_binary_pattern(gray_img, n_points, radius) | |
np.savez(os.path.join("features", "lbp_transform.npz"),lbp=output_image) | |
elif input_choice == "compute_hog": | |
image = pcv.morphology.skeletonize(mask=array) | |
fd,output_image = hog(gray_img, orientations=10, pixels_per_cell=(16, 16), | |
cells_per_block=(1, 1), visualize=True, multichannel=False, channel_axis=-1) | |
np.savez(os.path.join("features", "hog_transform.npz"),hog=output_image) | |
# Convert the resulting NumPy array back to a PIL image object for Gradio output | |
img2 = Image.fromarray(output_image) | |
if img2.mode == 'F': | |
img2 = img2.convert('RGB') | |
# Return the processed image as a Gradio output | |
return img2 | |
def flagging_callback(example, label): | |
hf_writer.add(example, label) | |
body = ( | |
"<center>" | |
"<a href='https://precisiongreenhouse.tamu.edu/'><img src='https://peepleslab.engr.tamu.edu/wp-content/uploads/sites/268/2023/04/AgriLife_Logo-e1681857158121.png' width=1650></a>" | |
"<br>" | |
"This demo extracts the plant statistics and the image features and also stores them. " | |
"<br>" | |
"<a href ='https://precisiongreenhouse.tamu.edu/'>The Texas A&M Plant Growth and Phenotyping Facility Data Analysis Pipeline</a>" | |
"</center>" | |
) | |
examples = [["img1.png"],["img2.png"],["img3.png"]] | |
iface = gr.Interface( | |
fn=image_processing, | |
inputs=[gr.inputs.Image(label="Input Image"), | |
gr.components.Dropdown(["Tips", "Branches","Both","sort","sift transform","lbp transform","hog transform","compute all"], label="Choose the operation to be performed"), | |
gr.components.Dropdown(["compute_tips","compute_branch","compute_both","compute_sift","compute_lbp","compute_hog"],label="choose from compute all"), | |
gr.inputs.Textbox(default="results.xlsx", label="Enter file name to save (.xlsx extension will be added automatically)"), | |
], | |
outputs=gr.outputs.Image(type="pil", label="Processed Image"), | |
description=body, | |
layout="vertical", | |
allow_flagging=False, | |
allow_screenshot=False, | |
theme=theme, | |
examples=examples, | |
flagging_callback=flagging_callback | |
) | |
iface.launch() |