├── Custom Object detection live video.ipynb ├── Rename_files.ipynb ├── creating-files-data-and-name.py └── creating-train-and-test-txt-files.py /Custom Object detection live video.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import cv2\n", 10 | "import numpy as np\n", 11 | "import matplotlib.pyplot as plt\n", 12 | "\n", 13 | "#auther - Jay Shankar Bhatt \n", 14 | "# using this code without author's permission other then leaning task is strictly prohibited" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 2, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "## provide the path for testing cofing file and tained model form colab\n", 24 | "net = cv2.dnn.readNetFromDarknet(\"yolov3_custom.cfg\",r\"Downloads\\yolov3_custom_6000.weights\")" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 3, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "### Change here for custom classes for trained model \n", 34 | "\n", 35 | "classes = ['Elon_Musk','Barak_obama','Mark_Zuckerberg']" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 4, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "cap = cv2.VideoCapture(0)\n", 45 | "\n", 46 | "while 1:\n", 47 | " _, img = cap.read()\n", 48 | " img = cv2.resize(img,(1280,720))\n", 49 | " hight,width,_ = img.shape\n", 50 | " blob = cv2.dnn.blobFromImage(img, 1/255,(416,416),(0,0,0),swapRB = True,crop= False)\n", 51 | "\n", 52 | " net.setInput(blob)\n", 53 | "\n", 54 | " output_layers_name = net.getUnconnectedOutLayersNames()\n", 55 | "\n", 56 | " layerOutputs = net.forward(output_layers_name)\n", 57 | "\n", 58 | " boxes =[]\n", 59 | " confidences = []\n", 60 | " class_ids = []\n", 61 | "\n", 62 | " for output in layerOutputs:\n", 63 | " for detection in output:\n", 64 | " score = detection[5:]\n", 65 | " class_id = np.argmax(score)\n", 66 | " confidence = score[class_id]\n", 67 | " if confidence > 0.7:\n", 68 | " center_x = int(detection[0] * width)\n", 69 | " center_y = int(detection[1] * hight)\n", 70 | " w = int(detection[2] * width)\n", 71 | " h = int(detection[3]* hight)\n", 72 | " x = int(center_x - w/2)\n", 73 | " y = int(center_y - h/2)\n", 74 | " boxes.append([x,y,w,h])\n", 75 | " confidences.append((float(confidence)))\n", 76 | " class_ids.append(class_id)\n", 77 | "\n", 78 | "\n", 79 | " indexes = cv2.dnn.NMSBoxes(boxes,confidences,.5,.4)\n", 80 | "\n", 81 | " boxes =[]\n", 82 | " confidences = []\n", 83 | " class_ids = []\n", 84 | "\n", 85 | " for output in layerOutputs:\n", 86 | " for detection in output:\n", 87 | " score = detection[5:]\n", 88 | " class_id = np.argmax(score)\n", 89 | " confidence = score[class_id]\n", 90 | " if confidence > 0.5:\n", 91 | " center_x = int(detection[0] * width)\n", 92 | " center_y = int(detection[1] * hight)\n", 93 | " w = int(detection[2] * width)\n", 94 | " h = int(detection[3]* hight)\n", 95 | "\n", 96 | " x = int(center_x - w/2)\n", 97 | " y = int(center_y - h/2)\n", 98 | "\n", 99 | "\n", 100 | "\n", 101 | " boxes.append([x,y,w,h])\n", 102 | " confidences.append((float(confidence)))\n", 103 | " class_ids.append(class_id)\n", 104 | "\n", 105 | " indexes = cv2.dnn.NMSBoxes(boxes,confidences,.8,.4)\n", 106 | " font = cv2.FONT_HERSHEY_PLAIN\n", 107 | " colors = np.random.uniform(0,255,size =(len(boxes),3))\n", 108 | " if len(indexes)>0:\n", 109 | " for i in indexes.flatten():\n", 110 | " x,y,w,h = boxes[i]\n", 111 | " label = str(classes[class_ids[i]])\n", 112 | " confidence = str(round(confidences[i],2))\n", 113 | " color = colors[i]\n", 114 | " cv2.rectangle(img,(x,y),(x+w,y+h),color,2)\n", 115 | " cv2.putText(img,label + \" \" + confidence, (x,y+400),font,2,color,2)\n", 116 | "\n", 117 | " cv2.imshow('img',img)\n", 118 | " if cv2.waitKey(1) == ord('q'):\n", 119 | " break\n", 120 | " \n", 121 | "cap.release()\n", 122 | "cv2.destroyAllWindows()" 123 | ] 124 | } 125 | ], 126 | "metadata": { 127 | "kernelspec": { 128 | "display_name": "Python 3", 129 | "language": "python", 130 | "name": "python3" 131 | }, 132 | "language_info": { 133 | "codemirror_mode": { 134 | "name": "ipython", 135 | "version": 3 136 | }, 137 | "file_extension": ".py", 138 | "mimetype": "text/x-python", 139 | "name": "python", 140 | "nbconvert_exporter": "python", 141 | "pygments_lexer": "ipython3", 142 | "version": "3.7.6" 143 | }, 144 | "toc": { 145 | "base_numbering": 1, 146 | "nav_menu": {}, 147 | "number_sections": true, 148 | "sideBar": true, 149 | "skip_h1_title": false, 150 | "title_cell": "Table of Contents", 151 | "title_sidebar": "Contents", 152 | "toc_cell": false, 153 | "toc_position": {}, 154 | "toc_section_display": true, 155 | "toc_window_display": false 156 | } 157 | }, 158 | "nbformat": 4, 159 | "nbformat_minor": 2 160 | } 161 | -------------------------------------------------------------------------------- /Rename_files.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import os" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "count = 0\n", 19 | "\n", 20 | "for i in os.listdir():\n", 21 | " os.rename(i,str(count)+ '.'+ i.split('.')[-1])\n", 22 | " count+=1" 23 | ] 24 | } 25 | ], 26 | "metadata": { 27 | "kernelspec": { 28 | "display_name": "Python 3", 29 | "language": "python", 30 | "name": "python3" 31 | }, 32 | "language_info": { 33 | "codemirror_mode": { 34 | "name": "ipython", 35 | "version": 3 36 | }, 37 | "file_extension": ".py", 38 | "mimetype": "text/x-python", 39 | "name": "python", 40 | "nbconvert_exporter": "python", 41 | "pygments_lexer": "ipython3", 42 | "version": "3.7.6" 43 | } 44 | }, 45 | "nbformat": 4, 46 | "nbformat_minor": 4 47 | } 48 | -------------------------------------------------------------------------------- /creating-files-data-and-name.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | """ 4 | Course: Training YOLO v3 for Objects Detection with Custom Data 5 | 6 | Section-3 7 | Labelling new Dataset in YOLO format 8 | File: creating-files-data-and-name.py 9 | """ 10 | 11 | 12 | # Creating files labelled_data.data and classes.names 13 | # for training in Darknet framework 14 | # 15 | # Algorithm: 16 | # Setting up full paths --> Reading file classes.txt --> 17 | # --> Creating file classes.names --> 18 | # --> Creating file labelled_data.data 19 | # 20 | # Result: 21 | # Files classes.names and labelled_data.data needed to train 22 | # in Darknet framework 23 | 24 | 25 | """ 26 | Start of: 27 | Setting up full path to directory with labelled images 28 | """ 29 | 30 | # Full or absolute path to the folder with images 31 | # Find it with Py file getting-full-path.py 32 | # Pay attention! If you're using Windows, yours path might looks like: 33 | # r'C:\Users\my_name\Downloads\video-to-annotate' 34 | # or: 35 | # 'C:\\Users\\my_name\\Downloads\\video-to-annotate' 36 | full_path_to_images = '/home/my_name/Downloads/video-to-annotate' 37 | 38 | """ 39 | End of: 40 | Setting up full path to directory with labelled images 41 | """ 42 | 43 | 44 | """ 45 | Start of: 46 | Creating file classes.names 47 | """ 48 | 49 | # Defining counter for classes 50 | c = 0 51 | 52 | # Creating file classes.names from existing one classes.txt 53 | # Pay attention! If you're using Windows, it might need to change 54 | # this: + '/' + 55 | # to this: + '\' + 56 | # or to this: + '\\' + 57 | with open(full_path_to_images + '/' + 'classes.names', 'w') as names, \ 58 | open(full_path_to_images + '/' + 'classes.txt', 'r') as txt: 59 | 60 | # Going through all lines in txt file and writing them into names file 61 | for line in txt: 62 | names.write(line) # Copying all info from file txt to names 63 | 64 | # Increasing counter 65 | c += 1 66 | 67 | """ 68 | End of: 69 | Creating file classes.names 70 | """ 71 | 72 | 73 | """ 74 | Start of: 75 | Creating file labelled_data.data 76 | """ 77 | 78 | # Creating file labelled_data.data 79 | # Pay attention! If you're using Windows, it might need to change 80 | # this: + '/' + 81 | # to this: + '\' + 82 | # or to this: + '\\' + 83 | with open(full_path_to_images + '/' + 'labelled_data.data', 'w') as data: 84 | # Writing needed 5 lines 85 | # Number of classes 86 | # By using '\n' we move to the next line 87 | data.write('classes = ' + str(c) + '\n') 88 | 89 | # Location of the train.txt file 90 | data.write('train = ' + full_path_to_images + '/' + 'train.txt' + '\n') 91 | 92 | # Location of the test.txt file 93 | data.write('valid = ' + full_path_to_images + '/' + 'test.txt' + '\n') 94 | 95 | # Location of the classes.names file 96 | data.write('names = ' + full_path_to_images + '/' + 'classes.names' + '\n') 97 | 98 | # Location where to save weights 99 | data.write('backup = backup') 100 | 101 | """ 102 | End of: 103 | Creating file labelled_data.data 104 | """ 105 | -------------------------------------------------------------------------------- /creating-train-and-test-txt-files.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | """ 4 | Course: Training YOLO v3 for Objects Detection with Custom Data 5 | 6 | Section-3 7 | Labelling new Dataset in YOLO format 8 | File: creating-train-and-test-txt-files.py 9 | """ 10 | 11 | 12 | # Creating files train.txt and test.txt 13 | # for training in Darknet framework 14 | # 15 | # Algorithm: 16 | # Setting up full paths --> List of paths --> 17 | # --> Extracting 15% of paths to save into test.txt file --> 18 | # --> Writing paths into train and test txt files 19 | # 20 | # Result: 21 | # Files train.txt and test.txt with full paths to images 22 | 23 | 24 | # Importing needed library 25 | import os 26 | 27 | 28 | """ 29 | Start of: 30 | Setting up full path to directory with labelled images 31 | """ 32 | 33 | # Full or absolute path to the folder with images 34 | # Find it with Py file getting-full-path.py 35 | # Pay attention! If you're using Windows, yours path might looks like: 36 | # r'C:\Users\my_name\Downloads\video-to-annotate' 37 | # or: 38 | # 'C:\\Users\\my_name\\Downloads\\video-to-annotate' 39 | full_path_to_images = '/home/my_name/Downloads/video-to-annotate' 40 | 41 | """ 42 | End of: 43 | Setting up full path to directory with labelled images 44 | """ 45 | 46 | 47 | """ 48 | Start of: 49 | Getting list of full paths to labelled images 50 | """ 51 | 52 | # Check point 53 | # Getting the current directory 54 | # print(os.getcwd()) 55 | 56 | # Changing the current directory 57 | # to one with images 58 | os.chdir(full_path_to_images) 59 | 60 | # Check point 61 | # Getting the current directory 62 | # print(os.getcwd()) 63 | 64 | # Defining list to write paths in 65 | p = [] 66 | 67 | # Using os.walk for going through all directories 68 | # and files in them from the current directory 69 | # Fullstop in os.walk('.') means the current directory 70 | for current_dir, dirs, files in os.walk('.'): 71 | # Going through all files 72 | for f in files: 73 | # Checking if filename ends with '.jpeg' 74 | if f.endswith('.jpeg'): 75 | # Preparing path to save into train.txt file 76 | # Pay attention! 77 | # If you're using Windows, it might need to change 78 | # this: + '/' + 79 | # to this: + '\' + 80 | # or to this: + '\\' + 81 | path_to_save_into_txt_files = full_path_to_images + '/' + f 82 | 83 | # Appending the line into the list 84 | # We use here '\n' to move to the next line 85 | # when writing lines into txt files 86 | p.append(path_to_save_into_txt_files + '\n') 87 | 88 | 89 | # Slicing first 15% of elements from the list 90 | # to write into the test.txt file 91 | p_test = p[:int(len(p) * 0.15)] 92 | 93 | # Deleting from initial list first 15% of elements 94 | p = p[int(len(p) * 0.15):] 95 | 96 | """ 97 | End of: 98 | Getting list of full paths to labelled images 99 | """ 100 | 101 | 102 | """ 103 | Start of: 104 | Creating train.txt and test.txt files 105 | """ 106 | 107 | # Creating file train.txt and writing 85% of lines in it 108 | with open('train.txt', 'w') as train_txt: 109 | # Going through all elements of the list 110 | for e in p: 111 | # Writing current path at the end of the file 112 | train_txt.write(e) 113 | 114 | # Creating file test.txt and writing 15% of lines in it 115 | with open('test.txt', 'w') as test_txt: 116 | # Going through all elements of the list 117 | for e in p_test: 118 | # Writing current path at the end of the file 119 | test_txt.write(e) 120 | 121 | """ 122 | End of: 123 | Creating train.txt and test.txt files 124 | """ 125 | --------------------------------------------------------------------------------