Spaces:
Sleeping
Sleeping
File size: 16,162 Bytes
059192a |
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
import streamlit as st
import numpy as np
import pandas as pd
import torch
import tensorflow as tf
import cv2
import tempfile
from PIL import Image
from ultralytics import YOLO
from transformers import AutoImageProcessor, AutoModelForImageClassification, pipeline
import matplotlib.pyplot as plt
import time
from datetime import datetime
import os
import requests
from torchvision import transforms
from torchvision.models import mobilenet_v3_large, MobileNet_V3_Large_Weights
import torch.nn.functional as F
# Load Models
species_processor = AutoImageProcessor.from_pretrained("microsoft/resnet-50")
species_model = AutoModelForImageClassification.from_pretrained("microsoft/resnet-50").eval()
yolo_model = YOLO("yolov8x.pt")
threat_model = pipeline("image-classification", model="nateraw/vit-base-beans")
# Habitat Analysis Model
class HabitatAnalyzer:
def __init__(self):
self.CLASSES = ['vegetation', 'water', 'urban', 'barren']
def analyze_vegetation(self, image_array):
ndvi = (image_array[:, :, 3] - image_array[:, :, 0]) / (image_array[:, :, 3] + image_array[:, :, 0] + 1e-8)
return ndvi
def detect_land_changes(self, image1, image2):
return cv2.absdiff(image1, image2)
class SpeciesMonitoringSystem:
def __init__(self):
self.detection_model = mobilenet_v3_large(weights=MobileNet_V3_Large_Weights.DEFAULT)
self.detection_model.eval()
self.species_classes = [
'deer', 'elk', 'moose', 'bear', 'wolf', 'mountain lion', 'bobcat',
'lynx', 'bighorn sheep', 'bison', 'wild boar', 'caribou', 'antelope',
'coyote', 'jaguar', 'leopard', 'tiger', 'lion', 'gorilla', 'chimpanzee',
'fox', 'raccoon', 'beaver', 'badger', 'otter', 'wolverine', 'porcupine',
'skunk', 'opossum', 'armadillo', 'wild cat', 'jackal', 'hyena',
'marten', 'fisher', 'weasel', 'mink', 'coati', 'monkey', 'lemur',
'rabbit', 'squirrel', 'chipmunk', 'rat', 'mouse', 'vole', 'mole',
'shrew', 'bat', 'hedgehog', 'gopher', 'prairie dog', 'muskrat',
'hamster', 'guinea pig', 'ferret', 'chinchilla', 'dormouse',
'eagle', 'hawk', 'falcon', 'owl', 'vulture', 'condor', 'crow', 'raven',
'woodpecker', 'duck', 'goose', 'swan', 'heron', 'crane', 'stork',
'pelican', 'flamingo', 'penguin', 'ostrich', 'emu', 'kiwi', 'peacock',
'pheasant', 'quail', 'grouse', 'turkey', 'cardinal', 'bluejay',
'sparrow', 'finch', 'warbler', 'thrush', 'swallow', 'hummingbird',
'snake', 'lizard', 'turtle', 'tortoise', 'alligator', 'crocodile',
'iguana', 'gecko', 'monitor lizard', 'chameleon', 'python', 'cobra',
'viper', 'rattlesnake', 'boa', 'anaconda', 'skink', 'bearded dragon',
'frog', 'toad', 'salamander', 'newt', 'axolotl', 'caecilian',
'tree frog', 'bullfrog', 'fire salamander', 'spotted salamander',
'salmon', 'trout', 'bass', 'pike', 'catfish', 'carp', 'perch',
'tuna', 'swordfish', 'marlin', 'shark', 'ray', 'eel', 'sturgeon',
'barracuda', 'grouper', 'snapper', 'cod', 'halibut', 'flounder',
'whale', 'dolphin', 'porpoise', 'seal', 'sea lion', 'walrus',
'orca', 'narwhal', 'beluga', 'manatee', 'dugong', 'sea otter',
'butterfly', 'moth', 'beetle', 'ant', 'bee', 'wasp', 'spider',
'scorpion', 'centipede', 'millipede', 'crab', 'lobster', 'shrimp',
'octopus', 'squid', 'jellyfish', 'starfish', 'sea urchin', 'coral',
'snail', 'slug', 'earthworm', 'leech'
]
self.transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
def detect_species(self, image):
img_tensor = self.transform(image).unsqueeze(0)
with torch.no_grad():
outputs = self.detection_model(img_tensor)
probabilities = F.softmax(outputs, dim=1)
top_prob, top_class = torch.topk(probabilities, 3)
results = []
for i in range(3):
species = self.species_classes[top_class[0][i] % len(self.species_classes)]
confidence = top_prob[0][i].item() * 100
results.append((species, confidence))
return results
def count_population(self, image):
gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
_, thresh = cv2.threshold(blur, 127, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
img_with_contours = np.array(image).copy()
cv2.drawContours(img_with_contours, contours, -1, (0, 255, 0), 2)
return len(contours), Image.fromarray(img_with_contours)
def assess_health(self, image):
img_array = np.array(image)
avg_color = np.mean(img_array, axis=(0, 1))
texture_measure = np.std(img_array)
color_variation = np.std(avg_color)
color_score = np.mean(avg_color) / 255 * 100
texture_score = min(100, texture_measure / 2)
variation_score = min(100, color_variation * 2)
health_score = (color_score * 0.4 + texture_score * 0.3 + variation_score * 0.3)
if health_score > 80:
status = "Excellent"
elif health_score > 60:
status = "Good"
elif health_score > 40:
status = "Fair"
else:
status = "Poor"
indicators = {
"Color Vibrancy": color_score,
"Texture Complexity": texture_score,
"Pattern Variation": variation_score
}
return status, health_score, indicators
def detect_threat(image, labels):
results = threat_model(image)
for result in results:
if result['label'] in labels and result['score'] > 0.5:
return f"{result['label']} Detected with confidence {result['score']:.2f}"
return "No Threat Detected"
def detect_land_changes(image1_path, image2_path):
image1 = Image.open(image1_path)
image2 = Image.open(image2_path)
image_array1 = np.array(image1)
image_array2 = np.array(image2)
if image_array1.shape != image_array2.shape:
return "Error: Images must be the same size."
changes = cv2.absdiff(image_array1, image_array2)
col1, col2, col3 = st.columns(3)
with col1:
st.image(image1, caption="Image 1")
with col2:
st.image(image2, caption="Image 2")
with col3:
st.image(changes, caption="Changes Detected")
change_percent = np.sum(changes > 50) / changes.size * 100
st.write(f"Changed Area: {change_percent:.2f}%")
return changes
def main():
habitat_analyzer = HabitatAnalyzer()
st.sidebar.title("Navigation")
option = st.sidebar.radio("Select an Analysis Type:",
["Species Monitoring", "Land Change Detection", "Animal Monitoring", "Threat Detection"])
if option == "Species Monitoring":
st.title("Species Identification")
monitoring_system = SpeciesMonitoringSystem()
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
progress_bar = st.progress(0)
with st.spinner("Analyzing image..."):
col1, col2, col3 = st.columns(3)
progress_bar.progress(30)
species_results = monitoring_system.detect_species(image)
progress_bar.progress(60)
count, marked_image = monitoring_system.count_population(image)
progress_bar.progress(90)
health_status, health_score, health_indicators = monitoring_system.assess_health(image)
with col1:
st.subheader("π Species Detection")
for species, confidence in species_results:
st.write(f"**{species.title()}**")
st.progress(confidence/100)
st.caption(f"Confidence: {confidence:.1f}%")
with col2:
st.subheader("π₯ Population Count")
st.write(f"**Detected Animals:** {count}")
st.image(marked_image, caption="Detection Visualization", use_column_width=True)
with col3:
st.subheader("πͺ Health Assessment")
st.write(f"**Status:** {health_status}")
st.write(f"**Overall Score:** {health_score:.1f}/100")
for indicator, value in health_indicators.items():
st.write(f"**{indicator}:**")
st.progress(value/100)
st.caption(f"{value:.1f}%")
progress_bar.progress(100)
st.sidebar.markdown("---")
st.sidebar.markdown("### Analysis Details")
st.sidebar.text(f"Analyzed at: {time.strftime('%Y-%m-%d %H:%M:%S')}")
st.sidebar.text(f"Image size: {image.size}")
st.markdown("---")
st.subheader("π Export Results")
summary = f"""Wildlife Monitoring Analysis Report
Date: {time.strftime('%Y-%m-%d %H:%M:%S')}
Species Detection Results:
{'-' * 30}
"""
for species, confidence in species_results:
summary += f"\n{species.title()}: {confidence:.1f}% confidence"
summary += f"""\n\nPopulation Count:
{'-' * 30}
Total detected: {count} individuals
Health Assessment:
{'-' * 30}
Status: {health_status}
Overall Score: {health_score:.1f}/100
"""
for indicator, value in health_indicators.items():
summary += f"\n{indicator}: {value:.1f}%"
st.download_button(
label="Download Analysis Report",
data=summary,
file_name="wildlife_analysis_report.txt",
mime="text/plain"
)
elif option == "Land Change Detection":
st.title("π Land Change Detection")
uploaded_file2 = st.file_uploader("Upload first image", type=['tif', 'png', 'jpg'])
uploaded_file3 = st.file_uploader("Upload second image", type=['tif', 'png', 'jpg'])
if uploaded_file2 is not None and uploaded_file3 is not None:
detect_land_changes(uploaded_file2, uploaded_file3)
elif option == "Animal Monitoring":
st.title("Animal Monitoring")
uploaded_file4 = st.file_uploader("Upload Image/Video", type=["jpg", "jpeg", "png", "mp4"])
if uploaded_file4:
if uploaded_file4.type.startswith("image"):
file_bytes = np.asarray(bytearray(uploaded_file4.read()), dtype=np.uint8)
image = cv2.imdecode(file_bytes, 1)
if image is None:
st.error("Error loading image. Please upload a valid image file.")
else:
results = yolo_model(image)
for result in results:
for box in result.boxes.xyxy:
x1, y1, x2, y2 = map(int, box[:4])
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
st.image(image, caption="Detected Animals", channels="BGR")
st.write(f"Estimated Count: {len(results[0].boxes)}")
elif uploaded_file4.type.startswith("video"):
tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(uploaded_file4.read())
cap = cv2.VideoCapture(tfile.name)
if not cap.isOpened():
st.error("Error loading video. Please upload a valid video file.")
else:
stframe = st.empty()
st.write("Processing video...")
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.resize(frame, (640, 480))
results = yolo_model(frame)
for result in results:
for box in result.boxes.xyxy:
x1, y1, x2, y2 = map(int, box[:4])
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
stframe.image(frame, channels="BGR")
time.sleep(0.03)
cap.release()
elif option == "Threat Detection":
st.title("Threat Detection and Prevention")
st.sidebar.header("Choose Threat Detection")
detection_option = st.sidebar.selectbox(
"Select an option",
["Poaching Alerts"]
)
if detection_option in ["Poaching Alerts"]:
uploaded_file7 = st.file_uploader("Upload Image", type=['jpg', 'jpeg', 'png'])
if uploaded_file7:
image = Image.open(uploaded_file7)
st.image(image, caption="Uploaded Image", use_column_width=True)
if detection_option == "Poaching Alerts":
st.subheader("π― Poaching Activity Detection")
with st.spinner("Analyzing image for potential poaching activities..."):
results = yolo_model(image)
poaching_objects = ['person', 'gun', 'knife', 'truck', 'car']
detections = {}
for result in results:
for box in result.boxes:
cls = int(box.cls[0])
conf = float(box.conf[0])
label = result.names[cls]
if label in poaching_objects and conf > 0.3:
detections[label] = conf
if detections:
for obj, conf in detections.items():
st.progress(conf)
st.write(f"{obj.title()}: {conf*100:.1f}% confidence")
annotated_img = np.array(image)
for result in results:
for box in result.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
cv2.rectangle(annotated_img, (x1, y1), (x2, y2), (255, 0, 0), 2)
st.image(annotated_img, caption="Detected Objects", use_column_width=True)
if any(conf > 0.7 for conf in detections.values()):
st.error("β οΈ High-risk poaching activity detected! Alert sent to authorities.")
else:
st.success("No suspicious activities detected.")
if __name__ == "__main__":
main() |