File size: 7,719 Bytes
51999bc
 
 
503f165
51999bc
503f165
51999bc
 
 
 
 
 
 
 
f9c8e59
 
69e59ef
51999bc
 
 
 
503f165
 
 
 
51999bc
 
 
 
 
 
 
 
 
 
503f165
51999bc
 
 
 
 
 
 
 
 
503f165
51999bc
8fa301f
51999bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9c8e59
51999bc
 
 
 
 
f758e02
51999bc
 
bef2827
51999bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f758e02
 
51999bc
f758e02
 
51999bc
 
f758e02
69e59ef
51999bc
69e59ef
 
 
 
 
51999bc
69e59ef
f758e02
51999bc
 
69e59ef
 
51999bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503f165
8fa301f
51999bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503f165
51999bc
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import streamlit as st
from PIL import Image
import os
from deepforest import main
from deepforest import get_data
import matplotlib.pyplot as plt
# from predict import extract_features, predict_similarity, compare_features, extract_features_cp
import os, re
import streamlit as st
import pandas as pd
from PIL import Image
import tempfile
from inference import split_image_from_dataframe
from datetime import datetime
from predict import extract_features, predict_similarity, compare_features, extract_features_cp




import rasterio
import geopandas as gpd

model = main.deepforest()
model.use_release()

# Set the page configuration
st.set_page_config(page_title="Wise-Vision", page_icon=":deciduous_tree:")

# Title and description
st.title("🌳 Wise-Vision")
st.subheader("AI + Environment Hackathon 2024")

# Sidebar information
st.sidebar.title("About")
st.sidebar.info(
    """
    This app is designed for the AI + Environment Hackathon 2024.
    Upload a panoramic image and specify a folder path to detect tree species in the image.
    Upload a word file to integrate knowledge into the image.
    Output will be a panoramic image with identified trees and knowledge symbols.
    """
)

st.sidebar.title("Contact")
st.sidebar.info(
    """
    For more information, contact us at:
    [[email protected]]
    """
)


script_dir = os.path.dirname(os.path.abspath(__file__))

# Create a new folder within the script directory for storing cropped images
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
output_folder_name = f"output_{timestamp}"
output_image_folder = os.path.join(script_dir, output_folder_name)
os.makedirs(output_image_folder, exist_ok=True)
output_image_folder = os.path.abspath(output_image_folder)
# Define paths for the image and Excel file within the new folder
cropped_image_path = os.path.join(output_image_folder, f"panoramic_{timestamp}.png")
excel_output_path = os.path.join(output_image_folder, f"results_{timestamp}.xlsx")

# Input: Upload panoramic image
uploaded_image = st.file_uploader("Upload a panoramic image", type=['png', 'jpeg', 'JPG'])

# Input: Folder path for tree species detection

def extract_treespecies_features(folder_path):
    image_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith(('png', 'jpg', 'jpeg', '.JPG'))]

    species_feature_list = [{"feature": extract_features(file), "file_name": file} for file in image_files]
    return species_feature_list


# print(species_feature_list[:2])
def perform_inference(cropped_images, species_feature_list, img_df):

    for img_idx, item in enumerate(cropped_images):
        image = item["image"]
        feature_cp = extract_features_cp(image)
        row_results = []
        species_result = []
        emoji = []
        species_context = []
        for idx, species in enumerate(species_feature_list):
            # euclidean_dist, cos_sim = compare_features(feature_cp, species["feature"])
        # print(f'Euclidean Distance: {euclidean_dist}')
        # print(f'Cosine Similarity: {cos_sim}')

        # Predict similarity
            is_similar = predict_similarity(feature_cp, species["feature"], threshold=0.92)
        # print(species)
        # print(f'Are the images similar? {"Yes" if is_similar else "No"}')

            result = "Yes" if is_similar else "No"

            if result == "Yes":
                item[f"result_{idx}"] = result
                item[f"file_name_{idx}"] = species["file_name"]
                row_results.append(species["file_name"])
                # # Regular expression to match the tree species name
                # species_pattern = r'identified_species\\([^\\]+) -'

                # # Search for the pattern in the file path
                # match = re.search(species_pattern, species["file_name"])

                # Extract and print the tree species name if found
              
                    # species_info = retriever.invoke(f"Scientific name:{tree_species}")
                    
                    # ans = generate_image(species_info, client)
                    # emoji.append(ans)
                    # text_context = [doc.page_content for doc in species_info]
                    # text_context = ", ".join(text_context)
                    # species_context.append(text_context)
                    # print(ans)
                    # species_result.append(tree_species)
    
        img_df.at[img_idx, "species_identified"] = ", ".join(species_result) if species_result else "No similar species found"            
        img_df.at[img_idx, "result_file_path"] = ", ".join(row_results) if row_results else ""
        # img_df.at[img_idx, "emoji"] = ", ".join(emoji) if emoji else ""
        # img_df.at[img_idx, "retreived context"] = ", ".join(species_context) if species_context else ""
        
        
    return cropped_images


# Function to simulate tree species detection

# Display uploaded image and detected tree species
if uploaded_image is not None:
    with tempfile.NamedTemporaryFile(delete=False, suffix='.JPG') as temp_file:
        temp_file.write(uploaded_image.read())
        temp_file_path = temp_file.name
    # Open and display the image
    # image = Image.open(uploaded_image)
    sample_image_path = get_data(temp_file_path)
    boxes = model.predict_image(path=sample_image_path, return_plot=False)
    img_actual = model.predict_image(path=sample_image_path, return_plot=True, color=(137, 0, 0), thickness=9)
    st.image(img_actual, caption='Segmented Panoramic Image', channels ='RGB', use_column_width=True)
    st.success("Sample Dataframe:")
    st.dataframe(boxes.head())
    plt.imshow(img_actual[:,:,::-1])
    # plt.show(img[:,:,::-1])
    plt.savefig(cropped_image_path)
    # if st.button("Next Step"):
  
    accuracy_threshold = st.slider("Accuracy threshold for cropping images:",min_value=0.1, max_value=1.0, value=0.4)
    images_list = split_image_from_dataframe(boxes, temp_file_path, output_folder_name)
    image_width = 200
    st.success("Sample Images:")
    # Display the images in a row
    col1, col2, col3 = st.columns(3)

    with col1:
        st.image(images_list[3]["image"], caption="Sample 1", width=image_width)

    with col2:
        st.image(images_list[4]["image"], caption="Sample 2", width=image_width)

    with col3:
        st.image(images_list[5]["image"], caption="Sample 3", width=image_width)
    
    folder_path = './identified_species'

    species_feature_list = extract_treespecies_features(folder_path)
    final_result = perform_inference(images_list, species_feature_list, boxes)
    st.success("Final Data:")
    st.dataframe(boxes)
    boxes.to_excel(excel_output_path)
    for index, row in boxes.iterrows():
        species_identified = row['species_identified']
        if species_identified !="No similar species found":
            cropped_image_path = row['cropped_image_path']
            result_file_path = row['result_file_path']
            if type(result_file_path) == list:
                result_file_path = result_file_path[0]
                

            result_file_path = result_file_path.split(',')[0]
            st.write(species_identified)
            col1, col2 = st.columns(2)
            with col1:
                st.image(cropped_image_path, caption='Cropped Image')
            with col2:
                st.image(result_file_path, caption='Species Match')
            
            

            
            

    
    # Detect tree species
    # detected_species = detect_tree_species(image, folder_path)
    
    # Display detected tree species
    # st.write("### Detected Tree Species:")
    # for species in detected_species:
    #     st.write(f"- {species}")