└── moving_object_detection.ipynb /moving_object_detection.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "dea49934", 7 | "metadata": { 8 | "id": "dea49934", 9 | "outputId": "f26f9c38-ca36-4fe0-b30f-af94aebf8d18" 10 | }, 11 | "outputs": [ 12 | { 13 | "name": "stdout", 14 | "output_type": "stream", 15 | "text": [ 16 | "Detected object: person with confidence 0.98\n", 17 | "Detected object: cat with confidence 0.52\n", 18 | "Detected object: bottle with confidence 0.75\n", 19 | "Detected object: person with confidence 0.85\n", 20 | "Detected object: sofa with confidence 0.66\n", 21 | "Detected object: cat with confidence 0.74\n", 22 | "Detected object: cat with confidence 0.59\n", 23 | "Detected object: car with confidence 0.52\n", 24 | "Detected object: cat with confidence 0.54\n", 25 | "Detected object: bottle with confidence 0.51\n", 26 | "Detected 10 objects, stopping execution...\n", 27 | "Detected object: tvmonitor with confidence 0.87\n", 28 | "Detected 10 objects, stopping execution...\n", 29 | "Detected object: bottle with confidence 0.58\n", 30 | "Detected 10 objects, stopping execution...\n", 31 | "Program execution stopped.\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "import cv2\n", 37 | "import numpy as np\n", 38 | "\n", 39 | "# Load the pre-trained model and class labels\n", 40 | "net = cv2.dnn.readNetFromCaffe('deploy.prototxt', 'mobilenet_iter_73000.caffemodel')\n", 41 | "classes = ['background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat',\n", 42 | " 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant',\n", 43 | " 'sheep', 'sofa', 'train', 'tvmonitor']\n", 44 | "\n", 45 | "# Initialize the video capture object\n", 46 | "cap = cv2.VideoCapture(0) # 0 for webcam, or provide a video file path\n", 47 | "\n", 48 | "# Initialize the background subtractor\n", 49 | "fgbg = cv2.createBackgroundSubtractorMOG2()\n", 50 | "\n", 51 | "# Counter for detected objects\n", 52 | "object_count = 0\n", 53 | "max_objects = 10 # Number of objects to detect before stopping\n", 54 | "\n", 55 | "while True:\n", 56 | " ret, frame = cap.read()\n", 57 | "\n", 58 | " if not ret:\n", 59 | " break\n", 60 | "\n", 61 | " # Apply the background subtractor to get the foreground mask\n", 62 | " fgmask = fgbg.apply(frame)\n", 63 | "\n", 64 | " # Find contours in the mask\n", 65 | " contours, _ = cv2.findContours(fgmask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n", 66 | "\n", 67 | " for contour in contours:\n", 68 | " if cv2.contourArea(contour) < 500:\n", 69 | " continue\n", 70 | "\n", 71 | " x, y, w, h = cv2.boundingRect(contour)\n", 72 | "\n", 73 | " # Extract the region of interest (ROI)\n", 74 | " roi = frame[y:y+h, x:x+w]\n", 75 | "\n", 76 | " # Prepare the ROI for classification\n", 77 | " blob = cv2.dnn.blobFromImage(roi, 0.007843, (300, 300), 127.5)\n", 78 | " net.setInput(blob)\n", 79 | " detections = net.forward()\n", 80 | "\n", 81 | " # Loop through the detected objects in the ROI\n", 82 | " for i in range(detections.shape[2]):\n", 83 | " confidence = detections[0, 0, i, 2]\n", 84 | " if confidence > 0.5: # Confidence threshold\n", 85 | " idx = int(detections[0, 0, i, 1])\n", 86 | " label = classes[idx]\n", 87 | " label_text = f\"{label}: {confidence:.2f}\"\n", 88 | "\n", 89 | " # Draw bounding box and label on the original frame\n", 90 | " cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)\n", 91 | " cv2.putText(frame, label_text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)\n", 92 | "\n", 93 | " # Print the detected object\n", 94 | " print(f\"Detected object: {label} with confidence {confidence:.2f}\")\n", 95 | "\n", 96 | " # Increment the object counter\n", 97 | " object_count += 1\n", 98 | "\n", 99 | " # Check if the object count has reached the maximum\n", 100 | " if object_count >= max_objects:\n", 101 | " print(\"Detected 10 objects, stopping execution...\")\n", 102 | " break # Break the inner loop\n", 103 | "\n", 104 | " # Check if we should break the outer loop as well\n", 105 | " if object_count >= max_objects:\n", 106 | " break\n", 107 | "\n", 108 | " # Display the resulting frame\n", 109 | " cv2.imshow('Frame', frame)\n", 110 | " cv2.imshow('Foreground Mask', fgmask)\n", 111 | "\n", 112 | " # Exit on pressing 'q'\n", 113 | " if cv2.waitKey(30) & 0xFF == ord('q'):\n", 114 | " break\n", 115 | "\n", 116 | "# Properly release the video capture object and close all windows\n", 117 | "cap.release()\n", 118 | "cv2.destroyAllWindows()\n", 119 | "\n", 120 | "print(\"Program execution stopped.\")\n" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "id": "9e2270d9", 127 | "metadata": { 128 | "id": "9e2270d9" 129 | }, 130 | "outputs": [], 131 | "source": [] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "id": "fbff4701", 137 | "metadata": { 138 | "id": "fbff4701" 139 | }, 140 | "outputs": [], 141 | "source": [] 142 | } 143 | ], 144 | "metadata": { 145 | "kernelspec": { 146 | "display_name": "Python 3 (ipykernel)", 147 | "language": "python", 148 | "name": "python3" 149 | }, 150 | "language_info": { 151 | "codemirror_mode": { 152 | "name": "ipython", 153 | "version": 3 154 | }, 155 | "file_extension": ".py", 156 | "mimetype": "text/x-python", 157 | "name": "python", 158 | "nbconvert_exporter": "python", 159 | "pygments_lexer": "ipython3", 160 | "version": "3.9.12" 161 | }, 162 | "colab": { 163 | "provenance": [] 164 | } 165 | }, 166 | "nbformat": 4, 167 | "nbformat_minor": 5 168 | } --------------------------------------------------------------------------------