-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserversidescript.py
More file actions
215 lines (171 loc) · 7.83 KB
/
serversidescript.py
File metadata and controls
215 lines (171 loc) · 7.83 KB
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
207
208
209
210
211
212
213
214
215
import os
import io
import shutil
import numpy as np
import cv2
import tensorflow as tf
from tensorflow.keras.models import load_model
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google.oauth2.service_account import Credentials
import streamlit as st
# Define constants
SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
FOLDER_ID = st.secrets["FOLDER_ID"]
TEMP_PATH = './temp/'
STORAGE_PATH = './storage/'
MODEL_PATH = 'leaf_classifier_model.h5'
def authenticate_google_drive():
"""Authenticate using a service account from Streamlit secrets."""
credentials = Credentials.from_service_account_info(
st.secrets["gcp_service_account"],
scopes=SCOPES
)
return build('drive', 'v3', credentials=credentials)
def list_files_in_folder(drive_service, folder_id):
"""List all files in a specific Google Drive folder."""
query = f"'{folder_id}' in parents"
response = drive_service.files().list(q=query, fields="files(id, name)").execute()
return response.get('files', [])
def download_file(drive_service, file_id, file_name, download_path):
"""Download a file from Google Drive."""
request = drive_service.files().get_media(fileId=file_id)
file_path = os.path.join(download_path, file_name)
with io.BytesIO() as fh:
downloader = MediaIoBaseDownload(fh, request)
done = False
while not done:
status, done = downloader.next_chunk()
print(f"Download Progress: {int(status.progress() * 100)}%")
# Write the downloaded content to a local file
with open(file_path, 'wb') as f:
fh.seek(0)
f.write(fh.read())
print(f"File downloaded: {file_path}")
return file_path
def crop_individual_leaves(image_path, output_dir, base_filename):
"""Crop individual leaves from bunch images."""
# Load image
image = cv2.imread(image_path)
if image is None:
print(f"Error loading image: {image_path}")
return []
# Create grayscale for processing without modifying original image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur to reduce noise
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# Apply threshold to create binary image
_, thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Filter contours by area to remove noise
min_contour_area = 1000 # Adjust based on your images
valid_contours = [cnt for cnt in contours if cv2.contourArea(cnt) > min_contour_area]
# Crop and save individual leaves
cropped_paths = []
for i, contour in enumerate(valid_contours):
# Get bounding rectangle
x, y, w, h = cv2.boundingRect(contour)
# Add padding around the bounding box
padding = 10
x_start = max(0, x - padding)
y_start = max(0, y - padding)
x_end = min(image.shape[1], x + w + padding)
y_end = min(image.shape[0], y + h + padding)
# Crop the image (keeping original BGR format)
cropped = image[y_start:y_end, x_start:x_end]
# Skip if cropped image is too small
if cropped.shape[0] < 20 or cropped.shape[1] < 20:
continue
# Make the cropped image square
height, width, _ = cropped.shape
max_dim = max(height, width)
square_image = np.zeros((max_dim, max_dim, 3), dtype=np.uint8) # Black background
y_offset = (max_dim - height) // 2
x_offset = (max_dim - width) // 2
square_image[y_offset:y_offset + height, x_offset:x_offset + width] = cropped
# Resize to 224x224
resized_image = cv2.resize(square_image, (224, 224))
# Save cropped image directly to output directory
filename = f"{base_filename}_leaf_{i}.jpg"
output_path = os.path.join(output_dir, filename)
cv2.imwrite(output_path, resized_image) # Save in original BGR format
cropped_paths.append(output_path)
return cropped_paths
def process_image(image_path, output_dir, model):
"""Process an image using the leaf classifier model."""
# Get base filename without extension
base_filename = os.path.splitext(os.path.basename(image_path))[0]
# Load image
img = cv2.imread(image_path)
if img is None:
print(f"Error loading image: {image_path}")
return []
# Create a copy for model input (needs RGB conversion for the model)
img_for_model = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# For model input, resize to model's expected size
img_size = (128, 128) # Keep the model's expected input size
img_resized = cv2.resize(img_for_model, img_size)
img_array = np.expand_dims(img_resized / 255.0, axis=0)
# Classify as single leaf or bunch of leaves
bunch_prediction = model.predict(img_array)[0][0]
processed_paths = []
if bunch_prediction < 0.5: # Single leaf
# Make the image square first (using original BGR image)
height, width, _ = img.shape
max_dim = max(height, width)
square_image = np.zeros((max_dim, max_dim, 3), dtype=np.uint8) # Black background
y_offset = (max_dim - height) // 2
x_offset = (max_dim - width) // 2
square_image[y_offset:y_offset + height, x_offset:x_offset + width] = img
# Resize to 224x224
output_image = cv2.resize(square_image, (224, 224))
# Save single leaf image directly to output folder
output_path = os.path.join(output_dir, f"{base_filename}.jpg")
cv2.imwrite(output_path, output_image) # Save in original BGR format
processed_paths.append(output_path)
else: # Bunch of leaves
# Crop individual leaves and save directly to output folder
processed_paths = crop_individual_leaves(image_path, output_dir, base_filename)
return processed_paths
def main():
"""Main function to download, process, and store leaf images."""
# Authenticate and create the Drive service
drive_service = authenticate_google_drive()
# Ensure the temp and storage directories exist
if not os.path.exists(TEMP_PATH):
os.makedirs(TEMP_PATH)
if not os.path.exists(STORAGE_PATH):
os.makedirs(STORAGE_PATH)
# Load the leaf classifier model
print("Loading leaf classifier model...")
model = load_model(MODEL_PATH)
# List all files in the specified folder
print("Fetching files from Google Drive folder...")
files = list_files_in_folder(drive_service, FOLDER_ID)
if not files:
print("No files found in the specified folder.")
return
try:
# Download and process each file
for file in files:
file_id = file['id']
file_name = file['name']
# Skip non-image files
if not file_name.lower().endswith(('.jpg', '.jpeg', '.png')):
print(f"Skipping non-image file: {file_name}")
continue
print(f"Downloading {file_name}...")
file_path = download_file(drive_service, file_id, file_name, TEMP_PATH)
print(f"Processing {file_name}...")
processed_paths = process_image(file_path, STORAGE_PATH, model)
print(f"Processed {len(processed_paths)} images from {file_name}")
finally:
# Clean up: remove the temp directory
print("Cleaning up temporary files...")
if os.path.exists(TEMP_PATH):
shutil.rmtree(TEMP_PATH)
print(f"Removed temporary directory: {TEMP_PATH}")
print("Processing complete! All images have been saved to:", STORAGE_PATH)
if __name__ == '__main__':
main()