{"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":"code","source":"import glob\nimport random\nimport matplotlib.pyplot as plt\nimport cv2\nimport math\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-23T15:16:15.084920Z","iopub.execute_input":"2023-06-23T15:16:15.085317Z","iopub.status.idle":"2023-06-23T15:16:15.094582Z","shell.execute_reply.started":"2023-06-23T15:16:15.085289Z","shell.execute_reply":"2023-06-23T15:16:15.093176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = glob.glob('/kaggle/input/dlsprint2/badlad/labels/yolov8_format/train/*')\ntest_files = glob.glob('/kaggle/input/dlsprint2/badlad/images/test/*')\n","metadata":{"execution":{"iopub.status.busy":"2023-06-23T15:16:34.295269Z","iopub.execute_input":"2023-06-23T15:16:34.296293Z","iopub.status.idle":"2023-06-23T15:16:34.751875Z","shell.execute_reply.started":"2023-06-23T15:16:34.296256Z","shell.execute_reply":"2023-06-23T15:16:34.750469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Train Vs Test Split\nlabels = ['Train Files','Test Files']\nsizes = [len(files),len(test_files)]\n\nfig, axes = plt.subplots(1, 2, figsize=(10, 5))\naxes[0].pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)\naxes[0].set_title('Pie Chart')\naxes[1].bar(labels,sizes)\naxes[1].set_title('Bar Plot')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-23T15:17:08.216872Z","iopub.execute_input":"2023-06-23T15:17:08.217235Z","iopub.status.idle":"2023-06-23T15:17:08.575639Z","shell.execute_reply.started":"2023-06-23T15:17:08.217207Z","shell.execute_reply":"2023-06-23T15:17:08.574206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NumElementsPerFile = []\nNumElements = {0:0,1:0,2:0,3:0}\nfor file in files:\n    with open(file,\"r\") as f:\n        contents = f.readlines()\n        NumElementsPerFile.append(len(contents))\n        #print(contents[1].strip()[0])\n        for elem in contents:\n            elem = int(elem.strip()[0])\n            NumElements[elem] += 1\n            \n               \n    f.close()    \nMap = {0:'paragraph',1:'text_box',2:'image',3:'table'}\nNumElements = {Map[key]: value for key, value in NumElements.items()}","metadata":{"execution":{"iopub.status.busy":"2023-06-23T15:17:44.756627Z","iopub.execute_input":"2023-06-23T15:17:44.756995Z","iopub.status.idle":"2023-06-23T15:20:53.866263Z","shell.execute_reply.started":"2023-06-23T15:17:44.756969Z","shell.execute_reply":"2023-06-23T15:20:53.865301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(10, 5))\naxes[0].hist(NumElementsPerFile, bins=300, edgecolor='black')\naxes[1].set_xlim(0,250)\naxes[0].set_title('Histogram of Count of Categories Per Image')\naxes[1].hist(NumElementsPerFile, bins=1000, edgecolor='black')\naxes[1].set_xlim(0,50)\naxes[1].set_title('Magnified Histogram of Count of Categories Per Image')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-23T15:20:58.110645Z","iopub.execute_input":"2023-06-23T15:20:58.111217Z","iopub.status.idle":"2023-06-23T15:21:00.956807Z","shell.execute_reply.started":"2023-06-23T15:20:58.111186Z","shell.execute_reply":"2023-06-23T15:21:00.955454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = list(NumElements.keys())\nlabels[0],labels[3] = labels[3],labels[0]\nsizes = list(NumElements.values())\nsizes[0],sizes[3] = sizes[3],sizes[0]\nfig, axes = plt.subplots(1, 2, figsize=(10, 5))\naxes[0].pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)\naxes[0].set_title('Pie Chart')\naxes[1].bar(labels,sizes)\naxes[1].set_title('Bar Plot')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-23T15:21:05.013842Z","iopub.execute_input":"2023-06-23T15:21:05.014172Z","iopub.status.idle":"2023-06-23T15:21:05.331509Z","shell.execute_reply.started":"2023-06-23T15:21:05.014151Z","shell.execute_reply":"2023-06-23T15:21:05.330051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def DrawImage(filename):\n    ImageFilename = ImageName(filename)\n    Img = cv2.imread(ImageFilename)\n    height,width,_ = Img.shape\n    \n    with open(filename,\"r\") as f:\n        contents = f.readlines()\n    for elem in contents:\n        Points = elem.split()[1:]\n        X = Points[0::2]\n        Y = Points[1::2]\n        X.append(X[0])\n        Y.append(Y[0])\n    \n        for i in range(len(X)-1):\n            P1 = math.floor(float(X[i])*width)\n            P2 = math.floor(float(Y[i])*height)\n            P3 = math.floor(float(X[i+1])*width)\n            P4 = math.floor(float(Y[i+1])*height)\n\n            Img = cv2.line(Img,(P1,P2),(P3,P4),(0,0,255),2)\n    f.close()  \n    return Img\n        \ndef ImageName(filename):\n    ImageFilename = filename.replace('labels','images')\n    ImageFilename = ImageFilename.replace('yolov8_format/','')\n    ImageFilename = ImageFilename.replace('txt','png')\n    \n    return ImageFilename\n\n\n   \n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-23T15:21:09.931659Z","iopub.execute_input":"2023-06-23T15:21:09.932031Z","iopub.status.idle":"2023-06-23T15:21:09.941714Z","shell.execute_reply.started":"2023-06-23T15:21:09.932003Z","shell.execute_reply":"2023-06-23T15:21:09.940794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Annotating a Single Image\nfig, axes = plt.subplots(1, 2, figsize=(10, 5))\n\nfilename = '/kaggle/input/dlsprint2/badlad/labels/yolov8_format/train/001446aa-115c-4168-b7e1-13da5935a47c.txt'\nimg = cv2.imread(ImageName(filename))\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\nAnnotatedImg = DrawImage(filename)\nAnnotatedImg = cv2.cvtColor(AnnotatedImg, cv2.COLOR_BGR2RGB)\n\naxes[0].imshow(img)\naxes[0].set_axis_off()\naxes[1].imshow(AnnotatedImg)\naxes[1].set_axis_off()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-23T15:21:14.375401Z","iopub.execute_input":"2023-06-23T15:21:14.375865Z","iopub.status.idle":"2023-06-23T15:21:14.662366Z","shell.execute_reply.started":"2023-06-23T15:21:14.375831Z","shell.execute_reply":"2023-06-23T15:21:14.661624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting Multiple Images\nrandom.seed(42)\nrandom_files = random.sample(files, k=10)\nfig, axes = plt.subplots(10, 2, figsize=(20,20))\nfor i,filename in enumerate(random_files):\n    img = cv2.imread(ImageName(filename))\n    \n    \n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n    AnnotatedImg = DrawImage(filename)\n    AnnotatedImg = cv2.cvtColor(AnnotatedImg, cv2.COLOR_BGR2RGB)\n\n    axes[i,0].imshow(img)\n    axes[i,0].set_axis_off()\n    axes[i,1].imshow(AnnotatedImg)\n    axes[i,1].set_axis_off()\nplt.subplots_adjust(wspace= 0.1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-23T15:27:02.517546Z","iopub.execute_input":"2023-06-23T15:27:02.517917Z","iopub.status.idle":"2023-06-23T15:27:07.470043Z","shell.execute_reply.started":"2023-06-23T15:27:02.517891Z","shell.execute_reply":"2023-06-23T15:27:07.469344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}