Spaces:
Sleeping
Sleeping
Create inference.py
Browse files- inference.py +103 -0
inference.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from deepforest import main
|
2 |
+
from deepforest import get_data
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from predict import extract_features, predict_similarity, compare_features, extract_features_cp
|
5 |
+
import os
|
6 |
+
import streamlit as st
|
7 |
+
model = main.deepforest()
|
8 |
+
model.use_release()
|
9 |
+
|
10 |
+
# quit()
|
11 |
+
# print(img.head())
|
12 |
+
import pandas as pd
|
13 |
+
from PIL import Image
|
14 |
+
|
15 |
+
|
16 |
+
def split_image_from_dataframe(dataframe, panoramic_image, output_folder_name):
|
17 |
+
"""
|
18 |
+
Splits an image into multiple images based on coordinates provided in a dataframe.
|
19 |
+
|
20 |
+
Parameters:
|
21 |
+
dataframe (pd.DataFrame): DataFrame containing image path and coordinates.
|
22 |
+
"""
|
23 |
+
cropped_images_info = []
|
24 |
+
cropped_image_paths = []
|
25 |
+
for i, row in dataframe.iterrows():
|
26 |
+
image_path = row['image_path']
|
27 |
+
left, top, right, bottom = row['xmin'], row['ymin'], row['xmax'], row['ymax']
|
28 |
+
|
29 |
+
image = Image.open(panoramic_image)
|
30 |
+
|
31 |
+
cropped_image = image.crop((left, top, right, bottom))
|
32 |
+
cropped_image_dict = {
|
33 |
+
f'image': cropped_image,
|
34 |
+
f'position': (left, top, right, bottom)
|
35 |
+
}
|
36 |
+
cropped_images_info.append(cropped_image_dict)
|
37 |
+
cropped_image_paths.append(f'{output_folder_name}/cropped_image_{i}.png')
|
38 |
+
cropped_image.save(f'{output_folder_name}/cropped_image_{i}.png') # Save each cropped image
|
39 |
+
|
40 |
+
dataframe['cropped_image_path'] = cropped_image_paths
|
41 |
+
return cropped_images_info
|
42 |
+
|
43 |
+
|
44 |
+
# print(images_list)
|
45 |
+
# quit()
|
46 |
+
# Load images from folder
|
47 |
+
def extract_treespecies_features(folder_path):
|
48 |
+
image_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith(('png', 'jpg', 'jpeg', '.JPG'))]
|
49 |
+
|
50 |
+
species_feature_list = [{"feature": extract_features(file), "file_name": file} for file in image_files]
|
51 |
+
return species_feature_list
|
52 |
+
|
53 |
+
|
54 |
+
# print(species_feature_list[:2])
|
55 |
+
def perform_inference(images_list, species_feature_list):
|
56 |
+
for idx, item in enumerate(images_list):
|
57 |
+
image = item["image"]
|
58 |
+
feature_cp = extract_features_cp(image)
|
59 |
+
for idx, species in enumerate(species_feature_list):
|
60 |
+
euclidean_dist, cos_sim = compare_features(feature_cp, species["feature"])
|
61 |
+
# print(f'Euclidean Distance: {euclidean_dist}')
|
62 |
+
# print(f'Cosine Similarity: {cos_sim}')
|
63 |
+
|
64 |
+
# Predict similarity
|
65 |
+
is_similar = predict_similarity(feature_cp, species["feature"], threshold=0.8)
|
66 |
+
# print(species)
|
67 |
+
# print(f'Are the images similar? {"Yes" if is_similar else "No"}')
|
68 |
+
|
69 |
+
result = "Yes" if is_similar else "No"
|
70 |
+
if result == "Yes":
|
71 |
+
item[f"result_{idx}"] = result
|
72 |
+
item[f"file_name_{idx}"] = species["file_name"]
|
73 |
+
|
74 |
+
return images_list
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
if __name__ == '__main__':
|
80 |
+
pan_image = "D:/Downloads/image/plant_images/plant_images/drone_igapo_flooded_forest/DJI_20240504124024_0037_D.JPG"
|
81 |
+
|
82 |
+
sample_image_path = get_data(pan_image)
|
83 |
+
# img = model.predict_image(path=sample_image_path, return_plot=False)
|
84 |
+
# from PIL import Image
|
85 |
+
# print(img)
|
86 |
+
img_df = ""
|
87 |
+
# img_actual = model.predict_image(path=sample_image_path, return_plot=True, color=(0, 165, 255), thickness=9)
|
88 |
+
img_actual = model.predict_tile(raster_path=sample_image_path, return_plot=True, patch_size=100,patch_overlap=0.25)
|
89 |
+
# im = Image.open('Foto.jpg')
|
90 |
+
# im.save('Foto.png')
|
91 |
+
#predict_image returns plot in BlueGreenRed (opencv style), but matplotlib likes RedGreenBlue, switch the channel order. Many functions in deepforest will automatically perform this flip for you and give a warning.
|
92 |
+
plt.imshow(img_actual[:,:,::-1])
|
93 |
+
# plt.show(img[:,:,::-1])
|
94 |
+
plt.savefig("cropped_test3/panoramic_2.png")
|
95 |
+
quit()
|
96 |
+
images_list = split_image_from_dataframe(img_df, pan_image)
|
97 |
+
folder_path = 'D:/Downloads/image/plant_images/plant_images/drone_igapo_flooded_forest/identified_species'
|
98 |
+
|
99 |
+
species_feature_list = extract_treespecies_features()
|
100 |
+
final_result = perform_inference(images_list, species_feature_list)
|
101 |
+
print(final_result)
|
102 |
+
|
103 |
+
|