diff --git a/Detection Models/Object Detection using YOLO/Readme.md b/Detection Models/Object Detection using YOLO/Readme.md new file mode 100644 index 000000000..3d6e91e16 --- /dev/null +++ b/Detection Models/Object Detection using YOLO/Readme.md @@ -0,0 +1,36 @@ +# Object Detection using YOLO +## Introduction +The task of object detection involves identifying and localizing multiple objects within an image or video. In this project, we use the YOLO (You Only Look Once) algorithm, a state-of-the-art object detection model, to detect and classify objects from an image. YOLO is known for its high speed and accuracy, making it suitable for real-time object detection applications. + +## Algorithms Used +**YOLO (You Only Look Once):** +YOLO is a deep learning-based object detection algorithm that frames object detection as a single regression problem, straight from image pixels to bounding box coordinates and class probabilities. It divides an image into a grid and predicts bounding boxes and probabilities for each grid cell. YOLO is fast and efficient, making it ideal for real-time detection tasks. + +**Convolutional Neural Networks (CNNs):** +YOLO relies on CNNs to extract features from images and classify objects. The network is pre-trained on a large dataset (COCO) and then fine-tuned on new datasets for specific tasks. + +**Non-Maximum Suppression (NMS):** +This algorithm is used to filter overlapping bounding boxes. NMS ensures that the best prediction is chosen by suppressing weaker, overlapping boxes, reducing redundancy and improving detection accuracy. + +## Performance Analysis + +**Accuracy:** The accuracy of object detection models like YOLO depends on factors such as image quality, resolution, and object size. In this code, a confidence threshold of 0.7 is used to filter low-confidence predictions, ensuring that only highly confident detections are displayed. + +**Speed:** YOLO is known for its real-time detection capabilities. Using a model like YOLOv3, detection speed is optimized. Inference time is generally under a second, making YOLO suitable for video streams or high-throughput image detection tasks. + +**Challenges:** False positives or missed detections may occur if objects are small or partially obscured. Model performance can vary with different confidence thresholds or non-maximum suppression settings. + +## Result +The model successfully processes an input image and detects objects within it. Detected objects are highlighted with bounding boxes, and the class names are displayed with confidence scores. The result is visualized using matplotlib, which shows the image with detected objects after filtering through non-maximum suppression. + +## Future Work + +**Custom Dataset Fine-tuning:** + +Fine-tuning the YOLO model on a custom dataset specific to certain use cases (e.g., bird species detection) could lead to improved accuracy in specialized domains. + +**Integration with Real-Time Systems:** +Implementing the YOLO model in a real-time system, such as live video streams, can make it useful for applications like surveillance, traffic monitoring, or wildlife observation. + +**Improved Data Augmentation:** +Data augmentation techniques like image rotation, flipping, and cropping could be applied to the training set to increase the model’s robustness to variations in lighting, angles, and object positions. \ No newline at end of file diff --git a/Detection Models/Object Detection using YOLO/Result/Output.png b/Detection Models/Object Detection using YOLO/Result/Output.png new file mode 100644 index 000000000..4138d7bee Binary files /dev/null and b/Detection Models/Object Detection using YOLO/Result/Output.png differ diff --git a/Detection Models/Object Detection using YOLO/Result/input.png b/Detection Models/Object Detection using YOLO/Result/input.png new file mode 100644 index 000000000..e6608f136 Binary files /dev/null and b/Detection Models/Object Detection using YOLO/Result/input.png differ diff --git a/Detection Models/Object Detection using YOLO/main.py b/Detection Models/Object Detection using YOLO/main.py new file mode 100644 index 000000000..1904c79da --- /dev/null +++ b/Detection Models/Object Detection using YOLO/main.py @@ -0,0 +1,78 @@ +import cv2 +import numpy as np +import matplotlib.pyplot as plt + +# Load YOLO model +# The yolo model can be downloaded directly from the official website of YOLO algorithm +yolo = cv2.dnn.readNet("C:\\Users\\billa\\OneDrive\\Desktop\\Programs\\ML_DL\\yolov3.weights", + "C:\\Users\\billa\\OneDrive\\Desktop\\Programs\\ML_DL\\yolov3.cfg") + +# Load class names +classes = [] +with open("C:\\Users\\billa\\OneDrive\\Desktop\\Programs\\ML_DL\\coco (1).names", 'r') as f: + classes = f.read().splitlines() + +# Load image +img = cv2.imread("C:\\Users\\billa\\OneDrive\\Desktop\\Programs\\ML_DL\\ggg.jpg") +if img is None: + print("Error loading image.") +height, width = img.shape[:2] # Get image height and width + +# Prepare the image for YOLO +blob = cv2.dnn.blobFromImage(img, 1/255, (416, 416), (0, 0, 0), swapRB=True, crop=False) +yolo.setInput(blob) + +# Get output layer names and run forward pass +output_layers_names = yolo.getUnconnectedOutLayersNames() +layer_output = yolo.forward(output_layers_names) + +# Initialize lists +boxes = [] +confidences = [] +class_ids = [] + +# Process each detection +for output in layer_output: + for detection in output: + scores = detection[5:] + class_id = np.argmax(scores) + confidence = scores[class_id] + if confidence > 0.7: # Increased confidence threshold + center_x = int(detection[0] * width) + center_y = int(detection[1] * height) + w = int(detection[2] * width) + h = int(detection[3] * height) + + # Calculate top-left corner coordinates + x = int(center_x - w / 2) + y = int(center_y - h / 2) + + # Append detection information + boxes.append([x, y, w, h]) + confidences.append(float(confidence)) + class_ids.append(class_id) + +# Perform Non-Maximum Suppression +indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) + +# Font for displaying labels +font = cv2.FONT_HERSHEY_PLAIN +# Random colors for each box +colors = np.random.randint(0, 255, size=(len(boxes), 3), dtype='uint8') + +# Check if any boxes are returned +if len(indexes) > 0: + indexes = indexes.flatten() # Flatten the list of indexes + + # Draw bounding boxes and labels + for i in indexes: + x, y, w, h = boxes[i] + label = str(classes[class_ids[i]]) + color = [int(c) for c in colors[i]] + cv2.rectangle(img, (x, y), (x + w, y + h), color, 2) + cv2.putText(img, label, (x, y - 10), font, 2, (255, 255, 255), 2) + +# Display the image with matplotlib +plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) +plt.axis('off') # Hide axis +plt.show()