├── README.md └── YOLOv3_DetectObjectFromImage_git.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # yolov3_objectdetection 2 | This repository contains code to detect object using yolov3- Darknet Architecture using OpenCV in Python 3 | -------------------------------------------------------------------------------- /YOLOv3_DetectObjectFromImage_git.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import cv2" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 3, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "#Load YOLO\n", 28 | "net = cv2.dnn.readNet(\"yolov3.weights\",\"yolov3.cfg\")\n", 29 | "classes = []\n", 30 | "with open(\"coco.names\",\"r\") as f:\n", 31 | " classes = [line.strip() for line in f.readlines()]" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 141, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "layer_names = net.getLayerNames()\n", 41 | "outputlayers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 142, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [ 57 | "colors= np.random.uniform(0,255,size=(len(classes),3))" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 143, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "#loading image\n", 74 | "img = cv2.imread(\"area.JPG\")\n", 75 | "img = cv2.resize(img,None,fx=0.4,fy=0.3)\n", 76 | "height,width,channels = img.shape" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 144, 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "#detecting objects\n", 86 | "blob = cv2.dnn.blobFromImage(img,0.00392,(416,416),(0,0,0),True,crop=False)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 145, 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "# for b in blob:\n", 103 | "# for n,img_blob in enumerate(b):\n", 104 | "# cv2.imshow(str(n),img_blob)\n", 105 | " \n", 106 | "net.setInput(blob)\n", 107 | "outs = net.forward(outputlayers)\n", 108 | "#print(outs[1])\n", 109 | "\n", 110 | "\n", 111 | "#Showing info on screen/ get confidence score of algorithm in detecting an object in blob\n", 112 | "class_ids=[]\n", 113 | "confidences=[]\n", 114 | "boxes=[]\n", 115 | "for out in outs:\n", 116 | " for detection in out:\n", 117 | " scores = detection[5:]\n", 118 | " class_id = np.argmax(scores)\n", 119 | " confidence = scores[class_id]\n", 120 | " if confidence > 0.5:\n", 121 | " #onject detected\n", 122 | " center_x= int(detection[0]*width)\n", 123 | " center_y= int(detection[1]*height)\n", 124 | " w = int(detection[2]*width)\n", 125 | " h = int(detection[3]*height)\n", 126 | " \n", 127 | " #cv2.circle(img,(center_x,center_y),10,(0,255,0),2)\n", 128 | " #rectangle co-ordinaters\n", 129 | " x=int(center_x - w/2)\n", 130 | " y=int(center_y - h/2)\n", 131 | " #cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)\n", 132 | " \n", 133 | " boxes.append([x,y,w,h]) #put all rectangle areas\n", 134 | " confidences.append(float(confidence)) #how confidence was that object detected and show that percentage\n", 135 | " class_ids.append(class_id) #name of the object tha was detected\n", 136 | "\n", 137 | "indexes = cv2.dnn.NMSBoxes(boxes,confidences,0.4,0.6)\n", 138 | "\n", 139 | "\n", 140 | "font = cv2.FONT_HERSHEY_PLAIN\n", 141 | "for i in range(len(boxes)):\n", 142 | " if i in indexes:\n", 143 | " x,y,w,h = boxes[i]\n", 144 | " label = str(classes[class_ids[i]])\n", 145 | " color = colors[i]\n", 146 | " cv2.rectangle(img,(x,y),(x+w,y+h),color,2)\n", 147 | " cv2.putText(img,label,(x,y+30),font,1,(255,255,255),2)\n", 148 | " \n", 149 | "cv2.imshow(\"Image\",img)\n", 150 | "cv2.waitKey(0)\n", 151 | "cv2.destroyAllWindows()" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "\n", 161 | " \n", 162 | " " 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": null, 168 | "metadata": {}, 169 | "outputs": [], 170 | "source": [] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 9, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [ 178 | "\n" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [] 187 | } 188 | ], 189 | "metadata": { 190 | "kernelspec": { 191 | "display_name": "Python 3", 192 | "language": "python", 193 | "name": "python3" 194 | }, 195 | "language_info": { 196 | "codemirror_mode": { 197 | "name": "ipython", 198 | "version": 3 199 | }, 200 | "file_extension": ".py", 201 | "mimetype": "text/x-python", 202 | "name": "python", 203 | "nbconvert_exporter": "python", 204 | "pygments_lexer": "ipython3", 205 | "version": "3.7.0" 206 | } 207 | }, 208 | "nbformat": 4, 209 | "nbformat_minor": 2 210 | } 211 | --------------------------------------------------------------------------------