{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import os\n\nimport pydicom as dicom\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport cv2\n\nprint(\"default backend: \", matplotlib.get_backend())\nmatplotlib.use('Agg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# path\ndf = pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/train.csv\")\ndf = df.sort_values(by=\"patient_id\").reset_index(drop=True)\n\ntest_df = pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/test.csv\")\ntest_df = test_df.sort_values(by=\"patient_id\").reset_index(drop=True)\n\nTEST_IMG_PATH = \"/kaggle/input/siim-isic-melanoma-classification/test/\"\nTRAIN_IMG_PATH = \"/kaggle/input/siim-isic-melanoma-classification/train/\"\n\n# image resolution\nWIDTH = 500\nHEIGHT = 320\nN_CHANNELS = 3\n\n# framrate\nFRAMERATE = 2","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"plot and save np.array as image file with meta annotation","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot(img_path, img_name, annotate,\n         w=WIDTH, h=HEIGHT, dpi=80):\n    \n    # data\n    ds = dicom.dcmread(img_path)\n    image = cv2.resize(ds.pixel_array, (w, h))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n    annotate = [str(key) + \": \" + str(value)+\"\\n\"\n                    for key, value in annotate.items()]\n\n    figsize = w / float(dpi), h / float(dpi)\n\n    # plot\n    fig = plt.figure(figsize=figsize)\n    ax = fig.add_axes([0, 0, 1, 1])\n  \n    ax.imshow(image, interpolation='nearest')\n    ax.axis('off')\n    \n    # annotate\n    ax.text(280, 75, \"\".join(annotate), \n            color=\"black\", weight=\"bold\",\n            fontsize=7,\n            bbox=dict(boxstyle=\"round\",\n                        facecolor='#BF02F6', \n                        alpha=0.3, \n                        edgecolor='black'\n                        )\n                )\n    # save\n    plt.savefig(img_name, bbox_inches='tight', \n                transparent=False, pad_inches=0.006, dpi=dpi)\n    \n    return plt.close()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Writing Video from images(np arrays)","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n\ntrain_video_name = 'train_data.avi'\ntest_video_name = 'test_data.avi'\n\ndef make_video(df, data_path, \n               video_name, framerate=FRAMERATE,\n               width=WIDTH, height=HEIGHT):\n    \n    # Define the codec and create VideoWrite object\n    fourcc = cv2.VideoWriter_fourcc(*'XVID')\n    video = cv2.VideoWriter(video_name, fourcc, framerate, (width, height))\n\n    for i in range(len(df)):\n            \n        meta_data = df.iloc[i, :].to_dict()\n        img_path = data_path + meta_data[\"image_name\"] + \".dcm\"\n\n        img_name = \"skin.png\"\n\n        # create image file\n        plot(img_path, img_name, meta_data)\n        \n        # write as np array remove as image\n        video.write(cv2.imread(img_name))\n        os.remove(img_name)\n        \n\n    cv2.destroyAllWindows()\n    return video.release()\n\nmake_video(df, TRAIN_IMG_PATH, train_video_name)\n#make_video(test_df, TEST_IMG_PATH, test_video_name)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"adding sound","execution_count":null},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"!pip install youtube_dl -qq ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"# https://stackoverflow.com/questions/27473526/download-only-audio-from-youtube-video-using-youtube-dl-in-python-script\nfrom __future__ import unicode_literals\nimport youtube_dl\n\n\nydl_opts = {\n    'format': 'bestaudio/best',\n    'postprocessors': [{\n        'key': 'FFmpegExtractAudio',\n        'preferredcodec': 'mp3',\n        'preferredquality': '192',\n    }],\n}\nwith youtube_dl.YoutubeDL(ydl_opts) as ydl:\n    ydl.download(['https://www.youtube.com/watch?v=DnCJBZVTiJ4']);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true,"_kg_hide-input":true},"cell_type":"code","source":"# convert from avi to mp4 \n!ffmpeg -i train_data.avi -strict -2 train_data.mp4\n\n# adding sound\n!ffmpeg  -stream_loop -1 -i \"Deus Ex - Human Revolution - Detroit Limb Clinic (1 Hour of Music & Ambience)-DnCJBZVTiJ4.mp3\" -c copy -v 0 -f nut - | ffmpeg -thread_queue_size 10K -i - -i train_data.mp4 -c copy -map 1:v -map 0:a -shortest -y train_data_with_sound.mp4\n              \n# piece of video\n#!ffmpeg -ss 00:00:00 -i stats_with_sound.mp4 -to 00:01:00 -c copy stats_clip.mp4   ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"time codes","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# from num_images to timecode\ndef get_timecode(n_images, framerate=FRAMERATE):\n    seconds = n_images / framerate\n    return '{h:02d}:{m:02d}:{s:02d}' \\\n            .format(h=int(seconds/3600),\n                    m=int(seconds/60%60),\n                    s=int(seconds%60))\n\nunique_patients = np.unique(df[\"patient_id\"].values, \n                             return_index=True)\n\ntimecodes = [f\"{n} - {get_timecode(t)}\" for n, t \n                    in zip(unique_patients[0], unique_patients[1])]\n#timecodes[:10]\n\nwith open('timecodes.txt', 'w') as f:\n    f.write(f\"Timestamps:\\nPatient id    Time\\n\")\n    for item in timecodes:\n        f.write(f\"\\n\\n{item}\")        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%HTML\n<video width=\"800\" height=\"600\" controls>\n  <source src=\"train_data_with_sound.mp4\" type=\"video/mp4\">\n</video>","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"!head -n 20 timecodes.txt","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### [High resolution video](https://www.youtube.com/watch?v=CZmCkOhjv14)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"### Full list of time codes\n","execution_count":null},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"!cat timecodes.txt","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}