Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import torch
|
5 |
+
import torchvision.transforms as transforms
|
6 |
+
from PIL import Image
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
|
9 |
+
# Load Pretrained AI Model (You can replace this with your custom model)
|
10 |
+
model = torch.hub.load("pytorch/vision:v0.10.0", "resnet18", pretrained=True)
|
11 |
+
model.eval()
|
12 |
+
|
13 |
+
# Function to analyze deforestation using AI model
|
14 |
+
def analyze_deforestation(image):
|
15 |
+
image = Image.fromarray(image).convert("RGB")
|
16 |
+
transform = transforms.Compose([
|
17 |
+
transforms.Resize((224, 224)),
|
18 |
+
transforms.ToTensor(),
|
19 |
+
])
|
20 |
+
img_tensor = transform(image).unsqueeze(0)
|
21 |
+
|
22 |
+
# AI Prediction
|
23 |
+
with torch.no_grad():
|
24 |
+
output = model(img_tensor)
|
25 |
+
|
26 |
+
# Simulated deforestation risk (random value for demo)
|
27 |
+
deforestation_risk = np.random.uniform(0.3, 0.9)
|
28 |
+
|
29 |
+
# Identify locations for seed-dropping (For simplicity, we use random dots)
|
30 |
+
h, w = image.size
|
31 |
+
num_seeds = int(deforestation_risk * 20) # More risk = More seeds
|
32 |
+
seed_locations = np.random.randint(0, min(h, w), (num_seeds, 2))
|
33 |
+
|
34 |
+
# Plot Results
|
35 |
+
plt.figure(figsize=(6, 6))
|
36 |
+
plt.imshow(image)
|
37 |
+
for loc in seed_locations:
|
38 |
+
plt.scatter(loc[0], loc[1], color="red", s=20, marker="o") # Mark seed locations
|
39 |
+
plt.title(f"Deforestation Risk: {deforestation_risk:.2f} | Seeds: {num_seeds}")
|
40 |
+
plt.axis("off")
|
41 |
+
plt.savefig("output.png")
|
42 |
+
|
43 |
+
return "output.png", f"Risk Level: {deforestation_risk:.2f} | Seeds Dropped: {num_seeds}"
|
44 |
+
|
45 |
+
# Create Gradio UI
|
46 |
+
interface = gr.Interface(
|
47 |
+
fn=analyze_deforestation,
|
48 |
+
inputs=gr.Image(type="numpy"),
|
49 |
+
outputs=["image", "text"],
|
50 |
+
title="🌱 Plant Me AI - Drone Seed Dispersal 🌍",
|
51 |
+
description="Upload a satellite image of a forest area. The AI will analyze deforestation risks and predict seed-dropping locations for drone-based planting."
|
52 |
+
)
|
53 |
+
|
54 |
+
# Launch the web app
|
55 |
+
interface.launch(debug=True)
|