{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Duke MTMC BBox Visualize\n", "\n", "- read in all images, resized\n", "- mean images\n", "- camera, ID, frame, left, top, width, height, wx, wy, feetX, feetyY\n", "- read in all bboxes\n", "- draw normalized bboxes" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%reload_ext autoreload\n", "%autoreload 2\n", "\n", "import os\n", "from os.path import join\n", "import math\n", "from glob import glob\n", "from random import randint\n", "\n", "import cv2 as cv\n", "import numpy as np\n", "import pandas as pd\n", "from PIL import Image, ImageDraw\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import scipy.io as sio\n", "import h5py\n", "from pathlib import Path\n", "from sklearn import preprocessing\n", "from tqdm import tqdm_notebook as tqdm\n", "\n", "import sys\n", "sys.path.append('/work/megapixels_dev/megapixels/')\n", "from app.settings import app_cfg as cfg\n", "from app.utils import file_utils, im_utils\n", "from app.models.bbox import BBox" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "fp_det_trainval = '/data_store/datasets/people/duke_mtmc/dataset/trainvalRaw.mat'\n", "fp_dir_processed = '/data_store/datasets/people/duke_mtmc/processed/'\n", "fp_dir_vids = '/data_store/datasets/people/duke_mtmc/dataset/videos/'\n", "fp_out_bboxes = '/data_store/datasets/people/duke_mtmc/processed/duke_mtmc_bboxes_01.png'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "cam_dets = {}\n", "with h5py.File(fp_det_trainval, 'r') as f:\n", " print(f.keys())\n", " for k, v in f.items():\n", " cam_dets[k] = np.array(v)\n", "train_data = cam_dets['trainData']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "mat_keys = ['camera', 'id', 'frame', 'left', 'top', 'width', 'height', 'wx', 'wy', 'feetX', 'feetY']" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "detections = {}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "7eaff9a503e8426380c71e84a262f448", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=4994653), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "total = len(train_data[0])\n", "for i in tqdm(range(0, total)):\n", " camera_str = str(int(train_data[0][i]))\n", " obj = {\n", " 'camera': int(train_data[0][i]),\n", " 'id': int(train_data[1][i]),\n", " 'frame': int(train_data[2][i]),\n", " 'left': int(train_data[3][i]),\n", " 'top': int(train_data[4][i]),\n", " 'width': int(train_data[5][i]),\n", " 'height': int(train_data[6][i]),\n", " 'wx': train_data[7][i],\n", " 'wy': train_data[8][i],\n", " 'feetX': train_data[9][i],\n", " 'feetY': train_data[10][i]\n", " }\n", " if camera_str not in detections.keys():\n", " detections[camera_str] = []\n", " detections[camera_str].append(obj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Average of Video" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "5ee08cb23c844e009d5ca5bd2ef39d09", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=10), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0f4bafaeaed7461d8ebba036890db289", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=10), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c1a936c427b0425eb910be1d7115669b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=10), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9a0e24e13f0c44f4a2574ad5ae3be63c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=10), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d3bdb03e99804938b655e9083d5823df", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=10), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "43ce9c80dddd42239e718dbd40ba1e07", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=9), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c445c047d97e4825a41b163782f795b5", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=9), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "77584ca01ac5496689dc6d2955c3d3ed", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=10), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "for cam in range(1,9):\n", " frames = []\n", " # skip frames\n", " dec = 480\n", " fps_video_cam = glob(join(fp_dir_vids, f'camera{cam}/*.mp4'))\n", "\n", " for fp_video_cam in tqdm(fps_video_cam):\n", " pos = 0\n", " vid = cv.VideoCapture(fp_video_cam)\n", " nframes = int(vid.get(cv.CAP_PROP_FRAME_COUNT))\n", "\n", " # read video frames\n", " while vid.isOpened():\n", " pos += dec\n", " if pos >= nframes:\n", " break\n", " vid.set(cv.CAP_PROP_POS_FRAMES, pos)\n", " read, frame = vid.read()\n", " if not read:\n", " break\n", " frames.append(frame)\n", " \n", " vid.release()\n", " # save average for each camera\n", " im_mean = np.mean(frames, axis=0).astype(np.uint8)\n", " fp_out_cam_avg = join(fp_dir_processed, f'duke_mtmc_cam{cam}_average.png')\n", " cv.imwrite(fp_out_cam_avg, im_mean)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(cv.cvtColor(im_mean, cv.COLOR_BGR2RGB))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "37f7ad3c034f4b49ade20a711f76ce26", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=1300168), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "# create list of bboxes\n", "dim = (1920, 1080)\n", "bboxes = []\n", "for d in tqdm(detections[str(cam)]):\n", " bboxes.append(BBox.from_xywh_dim(d['left'], d['top'], d['width'], d['height'], dim))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fcdf5a38255540c38c544ccf6b459fa1", "version_major": 2, "version_minor": 0 }, "text/plain": [ "HBox(children=(IntProgress(value=0, max=1300168), HTML(value='')))" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "im_saliency = np.zeros([dim[1], dim[0]]).astype(np.float32)\n", "for i, bbox in enumerate(tqdm(bboxes)):\n", " bbox = bbox.to_dim(dim)\n", " pt1, pt2 = tuple(map(int, bbox.pt_tl)), tuple(map(int, bbox.pt_br))\n", " im_saliency[pt1[1]:pt2[1], pt1[0]:pt2[0]] += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "im_saliency_mean = (im_saliency / (np.max(im_saliency) / 1)).astype(np.float32)\n", "im_saliency_mean = 255 * im_saliency_mean\n", "im_saliency_mean = np.clip(im_saliency_mean, 0, 255).astype(np.uint8)\n", "im_cm = cv.applyColorMap(im_saliency_mean, cv.COLORMAP_JET)\n", "im_cm = cv.blur(im_cm, ksize=(13, 13))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(cv.cvtColor((im_cm).astype(np.uint8), cv.COLOR_BGR2RGB))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# save files\n", "fp_out_saliency = join(fp_dir_processed, f'duke_mtmc_saliency_cam{cam}.png')\n", "cv.imwrite(fp_out_saliency, im_saliency_mean)\n", "#im_saliency_mean = cv.blur(im_saliency_mean, ksize=(5,5))\n", "#fp_out_saliency_blur = join(fp_dir_processed, f'duke_mtmc_saliency_cam{cam}_blur.png')\n", "cv.imwrite(fp_out_saliency_blur, im_saliency_mean)\n", "# wave" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'im_cm' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# preview overlay (not for production)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0malpha\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0.5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mim_comp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maddWeighted\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mim_mean\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0malpha\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mim_cm\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0malpha\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcvtColor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mim_comp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCOLOR_BGR2RGB\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'im_cm' is not defined" ] } ], "source": [ "# preview overlay (not for production)\n", "alpha = 0.5\n", "im_comp = cv.addWeighted(im_mean, alpha, im_cm, 1 - alpha, 0)\n", "plt.imshow(cv.cvtColor(im_comp, cv.COLOR_BGR2RGB))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "import blend_modes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "blend_modes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Old Viz" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i, bbox in enumerate(tqdm(bboxes)):\n", " if i % 60:\n", " continue\n", " bbox = bbox.to_dim(size_draw)\n", " pt1, pt2 = tuple(map(int, bbox.pt_tl)), tuple(map(int, bbox.pt_br))\n", " im_draw_next = im_bg.copy()\n", " cv.rectangle(im_draw_next, pt1, pt2, clr, stroke_weight, cv.LINE_AA)\n", " im_bg = cv.addWeighted(im_draw_next, alpha, im_bg, 1 - alpha, 0)\n", " if i > limit:\n", " break\n", "\n", "cv.imwrite(fp_out_bboxes, im_bg)\n", "plt.imshow(cv.cvtColor(im_bg, cv.COLOR_BGR2RGB))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# draw it\n", "clr = (255, 255, 255)\n", "size_orig = (5184, 3456)\n", "scale = size_orig[0]/1920\n", "size_draw = (int(size_orig[0]/scale), int(size_orig[1]/scale))\n", "im_draw = np.zeros([size_draw[1], size_draw[0], 3]).astype(np.uint8)\n", "im_bg = im_draw.copy()\n", "limit = 2000000\n", "stroke_weight = 1\n", "alpha = 0.125\n", "\n", "for i, roi in enumerate(tqdm(rois.items())):\n", " fname, bbox = roi # expand\n", " bbox = bbox.to_dim(size_draw)\n", " pt1, pt2 = tuple(map(int, bbox.pt_tl)), tuple(map(int, bbox.pt_br))\n", " im_draw_next = im_bg.copy()\n", " cv.rectangle(im_draw_next, pt1, pt2, clr, stroke_weight, cv.LINE_AA)\n", " im_bg = cv.addWeighted(im_draw_next, alpha, im_bg, 1 - alpha, 0)\n", " if i > limit:\n", " break\n", "\n", "\n", "plt.imshow(cv.cvtColor(im_bg, cv.COLOR_BGR2RGB))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Average Image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "im_comp = np.empty_like(im_utils.resize(cv.imread(fp_im), width=1920) / 255)\n", "for fp_im in tqdm(fp_ims[:100]):\n", " im = im_utils.resize(cv.imread(fp_im), width=1920) / 255\n", " im_comp += im" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "im_mean = np.mean(ims, axis=0).astype(np.uint8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(im_mean)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = np.array([.1, .2])\n", "b = np.array([.1, .2])\n", "c = a + b\n", "print(c)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "im = im.astype(np.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "megapixels", "language": "python", "name": "megapixels" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }