File size: 11,126 Bytes
8ae2ea0
a261c62
aa4f4c1
8ae2ea0
aa4f4c1
 
a261c62
aa4f4c1
 
0fa81a3
eb93675
 
 
 
 
 
 
a261c62
aa4f4c1
 
 
 
 
eb93675
 
aa4f4c1
eb93675
 
aa4f4c1
eb93675
aa4f4c1
eb93675
aa4f4c1
eb93675
aa4f4c1
eb93675
aa4f4c1
 
eb93675
 
 
 
 
 
 
 
aa4f4c1
 
 
eb93675
 
aa4f4c1
eb93675
 
 
 
 
aa4f4c1
eb93675
 
aa4f4c1
eb93675
aa4f4c1
 
eb93675
aa4f4c1
eb93675
 
 
 
 
 
 
aa4f4c1
eb93675
 
aa4f4c1
 
 
 
 
eb93675
aa4f4c1
 
 
 
 
 
eb93675
 
 
aa4f4c1
eb93675
 
 
 
 
 
 
 
 
aa4f4c1
eb93675
 
aa4f4c1
 
eb93675
aa4f4c1
eb93675
aa4f4c1
 
eb93675
 
 
 
aa4f4c1
 
 
eb93675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa4f4c1
eb93675
 
aa4f4c1
 
eb93675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa4f4c1
eb93675
 
aa4f4c1
eb93675
 
aa4f4c1
eb93675
 
 
 
 
aa4f4c1
eb93675
 
aa4f4c1
eb93675
 
aa4f4c1
eb93675
 
aa4f4c1
eb93675
 
 
 
 
 
aa4f4c1
eb93675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa4f4c1
eb93675
 
 
 
 
aa4f4c1
 
eb93675
 
 
 
 
a261c62
aa4f4c1
eb93675
aa4f4c1
eb93675
aa4f4c1
eb93675
 
aa4f4c1
eb93675
 
aa4f4c1
eb93675
 
aa4f4c1
eb93675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa4f4c1
eb93675
 
 
aa4f4c1
eb93675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import gradio as gr
import torch
import cv2
import numpy as np
from PIL import Image
from transformers import pipeline, AutoImageProcessor, AutoModelForImageClassification
import wikipedia
import folium
import tempfile
import os
import logging
import warnings
warnings.filterwarnings("ignore")

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class TreeAnalyzer:
    def __init__(self):
        self.setup_models()
        
    def setup_models(self):
        """Initialize models optimized for HF Spaces"""
        logger.info("Loading models for HF Spaces...")
        
        # Load depth estimation model
        self.midas = None
        try:
            self.midas = torch.hub.load('intel-isl/MiDaS', 'MiDaS_small', trust_repo=True)
            self.midas.eval()
            self.midas_transforms = torch.hub.load('intel-isl/MiDaS', 'transforms', trust_repo=True)
            self.transform = self.midas_transforms.small_transform
            logger.info("βœ“ MiDaS loaded")
        except Exception as e:
            logger.error(f"MiDaS failed: {e}")
            
        # Load plant classification model
        self.plant_classifier = None
        models_to_try = [
            "google/vit-base-patch16-224",
            "microsoft/resnet-50",
            "facebook/convnext-tiny-224"
        ]
        
        for model_name in models_to_try:
            try:
                self.plant_classifier = pipeline(
                    "image-classification",
                    model=model_name,
                    return_top_k=10
                )
                logger.info(f"βœ“ Loaded classifier: {model_name}")
                break
            except Exception as e:
                logger.warning(f"Failed to load {model_name}: {e}")
                continue
    
    def estimate_tree_height(self, image):
        """Estimate tree height using depth estimation"""
        if self.midas is None:
            return "Height estimation not available (MiDaS model failed to load)"
            
        try:
            # Convert and resize image
            img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
            h, w = img_cv.shape[:2]
            
            # Resize for memory efficiency
            if h > 384 or w > 384:
                scale = min(384/h, 384/w)
                new_h, new_w = int(h*scale), int(w*scale)
                img_cv = cv2.resize(img_cv, (new_w, new_h))
            
            # Process with MiDaS
            input_batch = self.transform(img_cv)
            
            with torch.no_grad():
                prediction = self.midas(input_batch)
                prediction = torch.nn.functional.interpolate(
                    prediction.unsqueeze(1),
                    size=(img_cv.shape[0], img_cv.shape[1]),
                    mode="bicubic",
                    align_corners=False,
                ).squeeze()
            
            depth_map = prediction.cpu().numpy()
            
            # Simple height estimation
            h_img, w_img = depth_map.shape
            center_region = depth_map[h_img//4:3*h_img//4, w_img//4:3*w_img//4]
            
            if center_region.size > 0:
                depth_range = np.max(center_region) - np.min(center_region)
                height_ratio = center_region.shape[0] / h_img
                estimated_height = max(1.5, min(50.0, (depth_range * height_ratio * 30)))
                
                return f"Estimated height: {estimated_height:.1f} meters\n(Approximate estimate based on image depth analysis)"
            else:
                return "Could not estimate height from this image"
                
        except Exception as e:
            logger.error(f"Height estimation error: {e}")
            return f"Height estimation failed: {str(e)}"
    
    def identify_tree_species(self, image):
        """Identify tree species with better filtering"""
        if self.plant_classifier is None:
            return "Species identification not available (classifier failed to load)", []
            
        try:
            # Resize image for processing
            if image.size[0] > 224 or image.size[1] > 224:
                image = image.resize((224, 224), Image.Resampling.LANCZOS)
            
            # Get predictions
            predictions = self.plant_classifier(image)
            
            # Enhanced plant/tree keywords
            plant_keywords = [
                # Trees
                'tree', 'oak', 'pine', 'maple', 'birch', 'cedar', 'fir', 'palm', 'willow',
                'cherry', 'apple', 'spruce', 'poplar', 'ash', 'elm', 'beech', 'sycamore',
                'acacia', 'eucalyptus', 'magnolia', 'chestnut', 'walnut', 'hickory',
                'cypress', 'juniper', 'redwood', 'bamboo', 'mahogany', 'teak',
                # Plants and botanical terms
                'plant', 'leaf', 'leaves', 'branch', 'bark', 'forest', 'wood', 'botanical',
                'flora', 'foliage', 'evergreen', 'deciduous', 'conifer', 'hardwood',
                'softwood', 'timber', 'shrub', 'bush', 'vine', 'fern', 'moss',
                # Specific species indicators
                'quercus', 'pinus', 'acer', 'betula', 'fagus', 'tilia', 'fraxinus',
                'platanus', 'castanea', 'juglans', 'carya', 'ulmus', 'salix'
            ]
            
            # Process and score predictions
            species_candidates = []
            for pred in predictions:
                label = pred['label'].lower()
                confidence = pred['score']
                
                # Calculate plant relevance score
                plant_score = sum(1 for keyword in plant_keywords if keyword in label)
                is_plant_related = plant_score > 0
                
                # Get Wikipedia info
                wiki_info = self.get_wikipedia_info(pred['label'])
                
                species_candidates.append({
                    'species': pred['label'],
                    'confidence': confidence,
                    'plant_score': plant_score,
                    'is_plant_related': is_plant_related,
                    'wiki_info': wiki_info
                })
            
            # Sort by plant relevance and confidence
            species_candidates.sort(key=lambda x: (x['plant_score'], x['confidence']), reverse=True)
            
            # Return top candidates
            final_results = species_candidates[:3]
            
            if any(result['is_plant_related'] for result in final_results):
                return "Species identification completed", final_results
            else:
                return "Possible species identified (may not be plants)", final_results
                
        except Exception as e:
            logger.error(f"Species identification error: {e}")
            return f"Species identification failed: {str(e)}", []
    
    def get_wikipedia_info(self, species_name):
        """Get Wikipedia information with better error handling"""
        try:
            # Clean species name
            clean_name = species_name.split(',')[0].split('(')[0].strip()
            
            search_queries = [
                clean_name,
                f"{clean_name} tree",
                f"{clean_name} plant",
                f"{clean_name} species"
            ]
            
            for query in search_queries:
                try:
                    results = wikipedia.search(query, results=2)
                    if results:
                        for result in results:
                            try:
                                page = wikipedia.page(result, auto_suggest=False)
                                summary = wikipedia.summary(result, sentences=2, auto_suggest=False)
                                return {
                                    'title': page.title,
                                    'summary': summary,
                                    'url': page.url
                                }
                            except:
                                continue
                except:
                    continue
            
            return {
                'title': 'No information found',
                'summary': f'Wikipedia information not available for {species_name}',
                'url': None
            }
            
        except Exception as e:
            return {
                'title': 'Error',
                'summary': f'Could not retrieve information: {str(e)}',
                'url': None
            }

def analyze_tree(image, latitude, longitude):
    """Main analysis function"""
    if image is None:
        return "Please upload an image", "", "", "", ""
    
    try:
        analyzer = TreeAnalyzer()
        
        # Height estimation
        height_result = analyzer.estimate_tree_height(image)
        
        # Species identification
        species_status, species_info = analyzer.identify_tree_species(image)
        
        # Format species results
        species_text = ""
        if species_info:
            for i, info in enumerate(species_info, 1):
                species_text += f"## {i}. {info['species']}\n"
                species_text += f"**Confidence:** {info['confidence']:.3f}\n"
                species_text += f"**Plant-related:** {'Yes' if info['is_plant_related'] else 'Uncertain'}\n"
                
                wiki = info['wiki_info']
                species_text += f"**Wikipedia:** {wiki['title']}\n"
                species_text += f"{wiki['summary']}\n"
                if wiki['url']:
                    species_text += f"πŸ”— [Read more]({wiki['url']})\n"
                species_text += "\n---\n"
        else:
            species_text = "No species information could be determined from this image."
        
        # Location info
        location_result = ""
        map_html = ""
        
        if latitude is not None and longitude is not None:
            try:
                location_result = f"Coordinates: {latitude:.6f}, {longitude:.6f}"
                
                # Create map
                m = folium.Map(location=[latitude, longitude], zoom_start=15)
                folium.Marker(
                    [latitude, longitude],
                    popup=f"Tree Location<br>{latitude:.6f}, {longitude:.6f}",
                    tooltip="Tree Location"
                ).add_to(m)
                
                # Save map
                map_file = tempfile.NamedTemporaryFile(delete=False, suffix='.html', mode='w')
                m.save(map_file.name)
                map_file.close()
                
                with open(map_file.name, 'r', encoding='utf-8') as f:
                    map_html = f.read()
                os.unlink(map_file.name)
                
            except Exception as e:
                location_result = f"Error processing location: {str(e)}"
                map_html = "<p>Could not generate map</p>"
        else:
            location_result = "No GPS coordinates provided"
            map_html = "<p>