pranaya20 commited on
Commit
7f0ac64
Β·
verified Β·
1 Parent(s): 68109b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -50
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
- # Load YOLO model for tree detection
10
- yolo_model = YOLO("yolov8n.pt")
11
-
12
- # Load MiDaS model for depth estimation
 
 
 
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
- def estimate_tree_height(image):
20
- # Convert to OpenCV format
21
- image_np = np.array(image)
22
- image_bgr = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
 
 
 
23
 
24
- # Run YOLO detection
25
- results = yolo_model(image_bgr)
26
- boxes = results[0].boxes.xyxy.cpu().numpy()
27
- labels = results[0].boxes.cls.cpu().numpy()
28
 
29
- # Filter for trees (assuming class 0 is tree - adjust if needed)
30
- tree_boxes = [box for box, label in zip(boxes, labels) if int(label) == 0]
 
 
31
 
32
- if not tree_boxes:
33
- return "No tree detected", None, None
 
34
 
35
- x1, y1, x2, y2 = map(int, tree_boxes[0])
36
- tree_crop = image_np[y1:y2, x1:x2]
 
 
 
 
37
 
38
  # Depth estimation
39
- input_tensor = midas_transforms(Image.fromarray(image_np)).to("cpu")
40
  with torch.no_grad():
41
- depth_map = midas(input_tensor.unsqueeze(0))[0]
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
- avg_depth = np.mean(depth_resized[y1:y2, x1:x2])
51
- estimated_height_m = avg_depth * 1.8 # scale arbitrarily for demo
52
 
53
- # Wikipedia summary
54
  try:
55
- summary = wikipedia.summary("tree", sentences=2)
56
- except Exception:
57
- summary = "Tree species information not available."
58
-
59
- return f"Estimated Tree Height: {estimated_height_m:.2f} meters", Image.fromarray(tree_crop), summary
60
-
61
- # Gradio Interface
62
- demo = gr.Interface(
63
- fn=estimate_tree_height,
64
- inputs=gr.Image(type="pil"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  outputs=[
66
- gr.Textbox(label="Tree Height Estimate"),
67
- gr.Image(label="Detected Tree"),
68
- gr.Textbox(label="Tree Species Info")
69
  ],
70
- title="🌳 Tree Measurement App",
71
- description="Upload or capture a tree image to estimate its height and get basic species info."
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