Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,103 @@
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
3 |
-
import torch
|
4 |
import cv2
|
5 |
import numpy as np
|
|
|
6 |
import wikipedia
|
7 |
from PIL import Image
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
13 |
midas = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")
|
14 |
midas.to("cpu").eval()
|
15 |
-
|
16 |
-
# β
FIXED: Use correct MiDaS transform attribute
|
17 |
midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms").small_transform
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
labels = results[0].boxes.cls.cpu().numpy()
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Depth estimation
|
39 |
-
input_tensor = midas_transforms(Image.fromarray(
|
40 |
with torch.no_grad():
|
41 |
-
|
42 |
-
|
43 |
-
depth_resized = torch.nn.functional.interpolate(
|
44 |
-
depth_map.unsqueeze(0),
|
45 |
-
size=image_np.shape[:2],
|
46 |
-
mode="bicubic",
|
47 |
-
align_corners=False
|
48 |
-
).squeeze().cpu().numpy()
|
49 |
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
# Wikipedia
|
54 |
try:
|
55 |
-
|
56 |
-
except
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
outputs=[
|
66 |
-
gr.Textbox(label="Tree
|
67 |
-
gr.Image(label="
|
68 |
-
gr.Textbox(label="Tree Species Info")
|
69 |
],
|
70 |
-
title="π³ Tree
|
71 |
-
description="
|
72 |
)
|
73 |
-
demo.launch()
|
74 |
|
|
|
75 |
|
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
|
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
+
import torch
|
6 |
import wikipedia
|
7 |
from PIL import Image
|
8 |
+
import pandas as pd
|
9 |
+
import os
|
10 |
+
import uuid
|
11 |
+
from datetime import datetime
|
12 |
+
from h3 import h3
|
13 |
+
|
14 |
+
# Initialize models
|
15 |
+
model = YOLO("yolov8n.pt") # You can change this to a fine-tuned model
|
16 |
midas = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")
|
17 |
midas.to("cpu").eval()
|
|
|
|
|
18 |
midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms").small_transform
|
19 |
|
20 |
+
# CSV path
|
21 |
+
csv_file = "tree_measurements.csv"
|
22 |
+
if not os.path.exists(csv_file):
|
23 |
+
pd.DataFrame(columns=[
|
24 |
+
"Timestamp", "Latitude", "Longitude", "H3_Index",
|
25 |
+
"Estimated_Height", "Species", "Image_File"
|
26 |
+
]).to_csv(csv_file, index=False)
|
27 |
|
28 |
+
# Convert lat/lon to H3 index
|
29 |
+
def latlon_to_h3(lat, lon, resolution=9):
|
30 |
+
return h3.geo_to_h3(lat, lon, resolution)
|
|
|
31 |
|
32 |
+
# Main processing function
|
33 |
+
def process_tree_image(image, latitude, longitude):
|
34 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
35 |
+
img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
36 |
|
37 |
+
# Object detection
|
38 |
+
results = model(img_cv)
|
39 |
+
detections = results[0].boxes.data.cpu().numpy()
|
40 |
|
41 |
+
if len(detections) == 0:
|
42 |
+
return "No tree detected", image
|
43 |
+
|
44 |
+
# Crop the first detected tree
|
45 |
+
x1, y1, x2, y2, conf, cls = detections[0]
|
46 |
+
tree_crop = img_cv[int(y1):int(y2), int(x1):int(x2)]
|
47 |
|
48 |
# Depth estimation
|
49 |
+
input_tensor = midas_transforms(Image.fromarray(cv2.cvtColor(tree_crop, cv2.COLOR_BGR2RGB))).unsqueeze(0)
|
50 |
with torch.no_grad():
|
51 |
+
depth = midas(input_tensor).squeeze().cpu().numpy()
|
52 |
+
approx_height = round(np.max(depth) - np.min(depth), 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
# Get H3 location index
|
55 |
+
h3_index = latlon_to_h3(latitude, longitude)
|
56 |
|
57 |
+
# Wikipedia species info
|
58 |
try:
|
59 |
+
species_guess = wikipedia.summary("tree", sentences=1)
|
60 |
+
except:
|
61 |
+
species_guess = "Could not retrieve species info"
|
62 |
+
|
63 |
+
# Save image with unique name
|
64 |
+
image_id = f"tree_{uuid.uuid4().hex[:8]}.png"
|
65 |
+
image.save(image_id)
|
66 |
+
|
67 |
+
# Append to CSV
|
68 |
+
new_data = pd.DataFrame([{
|
69 |
+
"Timestamp": timestamp,
|
70 |
+
"Latitude": latitude,
|
71 |
+
"Longitude": longitude,
|
72 |
+
"H3_Index": h3_index,
|
73 |
+
"Estimated_Height": approx_height,
|
74 |
+
"Species": species_guess,
|
75 |
+
"Image_File": image_id
|
76 |
+
}])
|
77 |
+
new_data.to_csv(csv_file, mode='a', index=False, header=False)
|
78 |
+
|
79 |
+
return (
|
80 |
+
f"π Estimated Tree Height: {approx_height} meters\n"
|
81 |
+
f"π H3 Location Index: {h3_index}\n"
|
82 |
+
f"πΏ Species Info: {species_guess}",
|
83 |
+
image
|
84 |
+
)
|
85 |
+
|
86 |
+
# Gradio UI
|
87 |
+
interface = gr.Interface(
|
88 |
+
fn=process_tree_image,
|
89 |
+
inputs=[
|
90 |
+
gr.Image(type="pil", label="πΈ Capture Tree Image"),
|
91 |
+
gr.Number(label="π Latitude"),
|
92 |
+
gr.Number(label="π Longitude")
|
93 |
+
],
|
94 |
outputs=[
|
95 |
+
gr.Textbox(label="π Tree Info"),
|
96 |
+
gr.Image(label="πΌοΈ Captured Image")
|
|
|
97 |
],
|
98 |
+
title="π³ Tree Height & Species Estimator",
|
99 |
+
description="Use your webcam or upload an image of a tree. Enter your GPS location to get height, species info, and H3 geolocation. All data is saved to CSV."
|
100 |
)
|
|
|
101 |
|
102 |
+
interface.launch()
|
103 |
|