{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**Using YOLOv7, we detect cars in images and then apply the OpenCV GrabCut algorithm to extract the car within the bounding box obtained from YOLOv7.**","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport torch\nimport os\nimport random\nfrom collections import defaultdict\nimport pandas as pd\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:48:28.256535Z","iopub.execute_input":"2023-01-19T12:48:28.257569Z","iopub.status.idle":"2023-01-19T12:48:28.266491Z","shell.execute_reply.started":"2023-01-19T12:48:28.257521Z","shell.execute_reply":"2023-01-19T12:48:28.265330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/WongKinYiu/yolov7.git","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:45:56.744878Z","iopub.execute_input":"2023-01-19T12:45:56.745513Z","iopub.status.idle":"2023-01-19T12:46:01.723649Z","shell.execute_reply.started":"2023-01-19T12:45:56.745471Z","shell.execute_reply":"2023-01-19T12:46:01.722519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:46:01.727176Z","iopub.execute_input":"2023-01-19T12:46:01.727996Z","iopub.status.idle":"2023-01-19T12:46:03.853656Z","shell.execute_reply.started":"2023-01-19T12:46:01.727963Z","shell.execute_reply":"2023-01-19T12:46:03.852529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\nmodel = torch.hub.load('/kaggle/working/yolov7','custom', '/kaggle/working/yolov7.pt', source = 'local')","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:46:03.855884Z","iopub.execute_input":"2023-01-19T12:46:03.856312Z","iopub.status.idle":"2023-01-19T12:46:09.588912Z","shell.execute_reply.started":"2023-01-19T12:46:03.856271Z","shell.execute_reply":"2023-01-19T12:46:09.587764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = model('/kaggle/working/images/train/6131a03dd028_10.jpg').pandas().xyxy[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:46:09.591378Z","iopub.execute_input":"2023-01-19T12:46:09.591787Z","iopub.status.idle":"2023-01-19T12:46:14.907135Z","shell.execute_reply.started":"2023-01-19T12:46:09.591748Z","shell.execute_reply":"2023-01-19T12:46:14.906110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/working/images/train/'\nimages = os.listdir(path)\nname_dic = defaultdict(list)\nfinal_result = pd.DataFrame()","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:46:14.908498Z","iopub.execute_input":"2023-01-19T12:46:14.909306Z","iopub.status.idle":"2023-01-19T12:46:14.918557Z","shell.execute_reply.started":"2023-01-19T12:46:14.909266Z","shell.execute_reply":"2023-01-19T12:46:14.917546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 1\nwhile i <= 12:\n    img = random.choice(images)\n    res = model(path+str(img)).pandas().xyxy[0]\n    name_dic['image_name'].append(img)\n    final_result = final_result.append(res.iloc[0:1])\n    i += 1","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:46:14.919980Z","iopub.execute_input":"2023-01-19T12:46:14.920486Z","iopub.status.idle":"2023-01-19T12:46:15.589471Z","shell.execute_reply.started":"2023-01-19T12:46:14.920446Z","shell.execute_reply":"2023-01-19T12:46:15.588528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_result.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:46:15.591109Z","iopub.execute_input":"2023-01-19T12:46:15.591488Z","iopub.status.idle":"2023-01-19T12:46:15.600142Z","shell.execute_reply.started":"2023-01-19T12:46:15.591448Z","shell.execute_reply":"2023-01-19T12:46:15.599073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(name_dic['image_name'])","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:46:15.604538Z","iopub.execute_input":"2023-01-19T12:46:15.604969Z","iopub.status.idle":"2023-01-19T12:46:15.612511Z","shell.execute_reply.started":"2023-01-19T12:46:15.604933Z","shell.execute_reply":"2023-01-19T12:46:15.611580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name_df = pd.DataFrame(name_dic)\nname_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:46:15.613739Z","iopub.execute_input":"2023-01-19T12:46:15.614629Z","iopub.status.idle":"2023-01-19T12:46:15.629148Z","shell.execute_reply.started":"2023-01-19T12:46:15.614594Z","shell.execute_reply":"2023-01-19T12:46:15.628095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df = pd.concat([name_df, final_result.reset_index(drop = True)], axis = 1)\nfinal_df","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:46:15.630346Z","iopub.execute_input":"2023-01-19T12:46:15.631282Z","iopub.status.idle":"2023-01-19T12:46:15.648523Z","shell.execute_reply.started":"2023-01-19T12:46:15.631249Z","shell.execute_reply":"2023-01-19T12:46:15.647664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,10))\nfor ind in final_df.index:\n    img = cv2.imread(path+str(final_df['image_name'][ind]))\n    img = cv2.rectangle(img, (int(final_df['xmin'][ind]),int(final_df['ymin'][ind])), (int(final_df['xmax'][ind]), int(final_df['ymax'][ind])), (255,0,0), 2)\n    ax = plt.subplot(3,4,ind+1)  #(nrows, ncolumns, index)\n    plt.imshow(img, cmap = 'gray') ","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:48:33.374201Z","iopub.execute_input":"2023-01-19T12:48:33.374812Z","iopub.status.idle":"2023-01-19T12:48:39.958432Z","shell.execute_reply.started":"2023-01-19T12:48:33.374765Z","shell.execute_reply":"2023-01-19T12:48:39.957587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,10))\nfor ind in final_df.index:\n    img = cv2.imread(path+str(final_df['image_name'][ind]))\n    mask = np.zeros(img.shape[:2], np.uint8)\n    \n    bgmodel = np.zeros((1,65), np.float64)\n    fgmodel = np.zeros((1,65),np.float64)\n    \n    rect = (int(final_df['xmin'][ind]), int(final_df['ymin'][ind]), int(final_df['xmax'][ind]-final_df['xmin'][ind]), int(final_df['ymax'][ind]-final_df['ymin'][ind]))\n    cv2.grabCut(img, mask, rect, bgmodel, fgmodel, 5, cv2.GC_INIT_WITH_RECT)\n    \n    # If mask==2 or mask== 1, mask2 get 0, other wise it gets 1 as 'uint8' type.\n    mask2 = np.where((mask == 2)|(mask == 0), 0, 1).astype('uint8')\n\n    # adding additional dimension for rgb to the mask, by default it gets 1\n    # multiply it with input image to get the segmented image\n    image = img * mask2[:, :, np.newaxis]\n    ax = plt.subplot(3,4,ind+1)  #(nrows, ncolumns, index)\n    plt.imshow(image, cmap = 'gray')","metadata":{"execution":{"iopub.status.busy":"2023-01-19T12:48:50.229490Z","iopub.execute_input":"2023-01-19T12:48:50.229879Z","iopub.status.idle":"2023-01-19T12:51:20.084260Z","shell.execute_reply.started":"2023-01-19T12:48:50.229842Z","shell.execute_reply":"2023-01-19T12:51:20.083324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}