From 46619bac76995295f895a58da45d21412bfaac7b Mon Sep 17 00:00:00 2001 From: 725921 <3407246504@qq.com> Date: Thu, 18 Dec 2025 20:19:54 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=97=A0=E4=BA=BA=E6=9C=BA=E5=AF=BC?= =?UTF-8?q?=E8=88=AA=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了主程序代码 --- src/UAV_navigation_system/main.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/UAV_navigation_system/main.py diff --git a/src/UAV_navigation_system/main.py b/src/UAV_navigation_system/main.py new file mode 100644 index 0000000000..e24185760c --- /dev/null +++ b/src/UAV_navigation_system/main.py @@ -0,0 +1,19 @@ +import torch +import cv2 + +print("✅ PyTorch version:", torch.__version__) +print("✅ OpenCV version:", cv2.__version__) +print("🎉 All set to build the drone navigation system!") +print("✅ Virtual environment is working!") + + +# src/main.py + +import torch + +def main(): + print("Welcome to Drone Navigation Vision Project!") + print("Torch version:", torch.__version__) + +if __name__ == "__main__": + main() From b5aad2878cf9eb4e86e853bcd02934036d477dd9 Mon Sep 17 00:00:00 2001 From: 725921 <3407246504@qq.com> Date: Fri, 19 Dec 2025 13:06:32 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=97=A0=E4=BA=BA=E6=9C=BA=E5=AF=BC?= =?UTF-8?q?=E8=88=AA=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了导航系统的代码 --- .../drone_nav_with_vision.py | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/UAV_navigation_system/drone_nav_with_vision.py diff --git a/src/UAV_navigation_system/drone_nav_with_vision.py b/src/UAV_navigation_system/drone_nav_with_vision.py new file mode 100644 index 0000000000..cfd02296cd --- /dev/null +++ b/src/UAV_navigation_system/drone_nav_with_vision.py @@ -0,0 +1,134 @@ +import cv2 +import numpy as np +from tensorflow.keras.models import load_model +from tensorflow.keras.preprocessing.image import img_to_array +import random # For simulating low battery and emergency conditions +import time # For the timeout condition + +# DroneBattery Class to manage battery +class DroneBattery: + def __init__(self, max_capacity=100, current_charge=100): + self.max_capacity = max_capacity + self.current_charge = current_charge + + def display_battery_status(self): + print(f"Battery Status: {self.current_charge}%") + + def charge_battery(self, charge_rate=10): + while self.current_charge < self.max_capacity: + self.current_charge += charge_rate + if self.current_charge > self.max_capacity: + self.current_charge = self.max_capacity + print(f"Charging... {self.current_charge}%") + time.sleep(1) + print("Battery fully charged!") + + def discharge_battery(self, discharge_rate=10): + while self.current_charge > 0: + self.current_charge -= discharge_rate + if self.current_charge < 0: + self.current_charge = 0 + print(f"Discharging... {self.current_charge}%") + time.sleep(1) + print("Battery completely drained!") + + def is_battery_low(self): + return self.current_charge < 20 + +# ✅ Load and compile model +print("📦 Loading model...") +model = load_model(r"C:\Users\hp\DroneNavVision\data\best_model.h5") +model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # ✅ Compile step added + +class_names = ['Animal', 'City', 'Fire', 'Forest', 'Vehicle', 'Water'] + +def check_emergency(): + emergency_conditions = ['low_battery', 'emergency'] + return random.choice(emergency_conditions) + +# Handle low battery condition +def handle_low_battery(drone_battery): + print("🔋 Low battery! Returning to base.") + drone_battery.charge_battery(charge_rate=15) + exit() + +def preprocess_frame(frame): + resized = cv2.resize(frame, (128, 128)) + img_array = img_to_array(resized) / 255.0 + return np.expand_dims(img_array, axis=0) + +def decide_navigation(predicted_class): + if predicted_class == 'Fire': + print("🔥 Fire detected! Navigate away.") + elif predicted_class == 'Animal': + print("🦌 Animal ahead. Hovering.") + elif predicted_class == 'Forest': + print("🌲 Forest zone detected. Reduce speed.") + elif predicted_class == 'Water': + print("🌊 Water body detected. Maintain altitude and avoid descent.") + elif predicted_class == 'Vehicle': + print("🚗 Vehicle detected. Hover and wait.") + elif predicted_class == 'City': + print("🏙️ Urban area detected. Enable obstacle avoidance and slow navigation.") + else: + print("✅ Clear path. Continue normal navigation.") + +def main(): + print("🚁 Starting the drone vision process...") + start_time = time.time() + + # Use the correct video source based on your setup + VIDEO_SOURCE = "http://192.168.1.3:4747/video" # Or set your IP stream (e.g., "rtsp:///stream") + + cap = cv2.VideoCapture(VIDEO_SOURCE, cv2.CAP_FFMPEG) + cap.set(cv2.CAP_PROP_BUFFERSIZE, 10) # Increase buffer size + cap.set(cv2.CAP_PROP_FPS, 30) # Set the desired FPS (adjust as needed) + + + # Check if video stream is opened successfully + if not cap.isOpened(): + print("❌ Failed to open video source. Check connection or URL.") + return + else: + print("✅ Video source opened successfully.") + + + drone_battery = DroneBattery() + + while True: + print("📸 Processing frame...") + + # Check if battery is low + if drone_battery.is_battery_low(): + handle_low_battery(drone_battery) + + elapsed_time = time.time() - start_time + if elapsed_time > 300: + print("⏰ Timeout reached! Stopping the drone.") + break + + ret, frame = cap.read() + if not ret: + print("❌ Failed to capture frame.") + break + + processed = preprocess_frame(frame) + pred = model.predict(processed) + predicted_class = class_names[np.argmax(pred)] + confidence = np.max(pred) * 100 + + cv2.putText(frame, f"{predicted_class} ({confidence:.2f}%)", (10, 30), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + cv2.imshow("Drone Vision Feed", frame) + + decide_navigation(predicted_class) + + if cv2.waitKey(1) & 0xFF == ord('q'): + print("🛑 Manual stop initiated by the user.") + break + + cap.release() + cv2.destroyAllWindows() + +if __name__ == "__main__": + main() From 43038104b72dbbcea85ef71f7eeea24ae0887b2e Mon Sep 17 00:00:00 2001 From: 725921 <3407246504@qq.com> Date: Fri, 19 Dec 2025 15:23:29 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=97=A0=E4=BA=BA=E6=9C=BA=E5=AF=BC?= =?UTF-8?q?=E8=88=AA=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了训练代码 --- src/UAV_navigation_system/cnn_train.py | 90 ++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/UAV_navigation_system/cnn_train.py diff --git a/src/UAV_navigation_system/cnn_train.py b/src/UAV_navigation_system/cnn_train.py new file mode 100644 index 0000000000..1cdb59b787 --- /dev/null +++ b/src/UAV_navigation_system/cnn_train.py @@ -0,0 +1,90 @@ +# cnn_train.py + +import os +import numpy as np +import tensorflow as tf +from tensorflow.keras.preprocessing.image import ImageDataGenerator +from tensorflow.keras.models import Sequential +from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout +from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint + +# Paths +base_dir = os.path.abspath("../data") # ../data folder contains 'train' and 'test' +train_dir = os.path.join(base_dir, "train") +test_dir = os.path.join(base_dir, "test") + +# Parameters +img_size = (128, 128) +batch_size = 32 +epochs = 70 + +# Image Preprocessing with stronger augmentation +train_datagen = ImageDataGenerator( + rescale=1./255, + rotation_range=30, + width_shift_range=0.1, + height_shift_range=0.1, + shear_range=0.2, + zoom_range=0.2, + horizontal_flip=True +) + +test_datagen = ImageDataGenerator(rescale=1./255) + +train_gen = train_datagen.flow_from_directory( + train_dir, + target_size=img_size, + batch_size=batch_size, + class_mode="categorical" +) + +test_gen = test_datagen.flow_from_directory( + test_dir, + target_size=img_size, + batch_size=batch_size, + class_mode="categorical" +) + +# CNN Model - More Layers +from tensorflow.keras.applications import MobileNetV2 +from tensorflow.keras.layers import GlobalAveragePooling2D + +# Load pre-trained base model +base_model = MobileNetV2(input_shape=(128, 128, 3), include_top=False, weights='imagenet') +base_model.trainable = False # Freeze the base model + +# Transfer learning model +model = tf.keras.Sequential([ + base_model, + GlobalAveragePooling2D(), + Dense(128, activation="relu"), + Dropout(0.5), + Dense(train_gen.num_classes, activation="softmax") +]) + + +# Compile Model +model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) +model.summary() + +# Callbacks for early stopping and saving the best model +early_stop = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True) + +checkpoint = ModelCheckpoint( + filepath=os.path.join(base_dir, "best_model.h5"), + monitor='val_accuracy', + save_best_only=True, + verbose=1 +) + +# Train Model +history = model.fit( + train_gen, + epochs=epochs, + validation_data=test_gen, + callbacks=[early_stop, checkpoint] +) + +# Save Final Model +model.save(os.path.join(base_dir, "cnn_model.h5")) +print("Model training complete and saved.") From aef66f89cf4fb10ed4ea613f85b6fe9f3c208e8c Mon Sep 17 00:00:00 2001 From: 725921 <3407246504@qq.com> Date: Fri, 19 Dec 2025 15:58:06 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=97=A0=E4=BA=BA=E6=9C=BA=E5=AF=BC?= =?UTF-8?q?=E8=88=AA=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 卷积神经网络(CNN)模型评估脚本 --- src/UAV_navigation_system/cnn_evaluate.py | 58 +++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/UAV_navigation_system/cnn_evaluate.py diff --git a/src/UAV_navigation_system/cnn_evaluate.py b/src/UAV_navigation_system/cnn_evaluate.py new file mode 100644 index 0000000000..097967f99d --- /dev/null +++ b/src/UAV_navigation_system/cnn_evaluate.py @@ -0,0 +1,58 @@ +# cnn_evaluate.py + +import os +import tensorflow as tf +from tensorflow.keras.preprocessing.image import ImageDataGenerator +import numpy as np +from sklearn.metrics import classification_report, confusion_matrix +import matplotlib.pyplot as plt +import seaborn as sns + +# Paths +base_dir = os.path.abspath("../data") +test_dir = os.path.join(base_dir, "test") + +# Match image size with training +img_size = (128, 128) +batch_size = 32 + +# Load and preprocess test data +test_datagen = ImageDataGenerator(rescale=1./255) +test_generator = test_datagen.flow_from_directory( + test_dir, + target_size=img_size, + batch_size=batch_size, + class_mode='categorical', + shuffle=False # Required for accurate predictions alignment +) + +# Load trained model +model_path = os.path.join(base_dir, "best_model.h5") +model = tf.keras.models.load_model(model_path) + +# Evaluate +loss, accuracy = model.evaluate(test_generator) +print(f"Test Loss: {loss:.4f}") +print(f"Test Accuracy: {accuracy:.4f}") + +# ---------- 🧠 Add this section below to analyze predictions ---------- +# Predict labels +Y_pred = model.predict(test_generator) +y_pred = np.argmax(Y_pred, axis=1) + +# Classification report +print("\nClassification Report:") +print(classification_report(test_generator.classes, y_pred, target_names=test_generator.class_indices.keys())) + +# Confusion matrix +cm = confusion_matrix(test_generator.classes, y_pred) +plt.figure(figsize=(8, 6)) +sns.heatmap(cm, annot=True, fmt="d", + xticklabels=test_generator.class_indices.keys(), + yticklabels=test_generator.class_indices.keys(), + cmap="Blues") +plt.title("Confusion Matrix") +plt.xlabel("Predicted") +plt.ylabel("Actual") +plt.tight_layout() +plt.show()