{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f9a03d7c",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import os\n",
"import pandas as pd\n",
"import numpy as np\n",
"import plotly.express as px\n",
"import cv2\n",
"from tqdm.notebook import tqdm \n",
"from utils import *"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7e671b5a",
"metadata": {},
"outputs": [],
"source": [
"path_video_OpenPifPaf_detection = r'..\\Inference\\test\\videos\\turn180_1d.mp4.openpifpaf.json'\n",
"\n",
"video_json = []\n",
"for line in open(path_video_OpenPifPaf_detection, 'r'):\n",
" video_json.append(json.loads(line))\n",
"\n",
"keypoints_3d_path = r'karussel_24kps.csv'\n",
"keypoints_3d = pd.read_csv(keypoints_3d_path, index_col=0).astype(float)\n",
"\n",
"axes_3d = np.float32([[0,0,0],\n",
" [1,0,0],\n",
" [0,1,0],\n",
" [0,0,1]])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1beeef98",
"metadata": {},
"outputs": [],
"source": [
"def pict_points(size=100):\n",
" s = size//2\n",
" return np.float32([[0,0,0],\n",
" [-s,-s, 0],\n",
" [s, -s, 0],\n",
" [s, s, 0],\n",
" [-s, s, 0]])\n",
"\n",
"from functions_shield import *\n",
"logo, pts_pict = readShield(\"coca-cola.png\")"
]
},
{
"cell_type": "markdown",
"id": "99354896",
"metadata": {},
"source": [
"### video"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3164afcb",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d11eec7ddd3a434082a3c453f81bc877",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/375 [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"name_video = path_video_OpenPifPaf_detection[:-16]\n",
"video = cv2.VideoCapture(name_video)\n",
"frame_width = int(video.get(3))\n",
"frame_height = int(video.get(4))\n",
"imageSize = (frame_height, frame_width)\n",
"fps = int(video.get(5))\n",
"num_frames = int(video.get(7))\n",
"out = cv2.VideoWriter(name_video[:-4]+'_'+'logo'+'.mp4', cv2.VideoWriter_fourcc(*'XVID'), fps, imageSize[::-1])\n",
"\n",
"imgpts_list = []\n",
"box_pict = pict_points(425)\n",
"poses = []\n",
"number = 0\n",
"for n, frame in enumerate(tqdm(video_json)):\n",
" fl, img = video.read()\n",
" num = frame['frame']\n",
" anns = frame['predictions']\n",
" score = 0\n",
" ann = []\n",
" for i in anns:\n",
" if i['score']>score:\n",
" score = i['score']\n",
" ann = i\n",
" rvec = []\n",
" if ann != []:\n",
" keypoints_2d, bbox, score = encode(ann)\n",
" conf_keypoints_2d = keypoints_2d[keypoints_2d['conf']>0.35]\n",
" if len(conf_keypoints_2d)>5:\n",
" rvec, tvec, camMatrx, dist = fit((1920, 1080), conf_keypoints_2d, keypoints_3d, focus=1)\n",
"\n",
" imgpts = cv.projectPoints(box_pict, rvec, tvec, camMatrx, dist)[0][:, 0].astype(int)\n",
" imgpts_list.append(imgpts)\n",
" \n",
" imgpts = imgpts_filt_list[number]\n",
" img = overlaying(logo, img, pts_pict, imgpts[1:])\n",
" \n",
" for p in imgpts[1:]:\n",
" img = cv.circle(img, p.astype(int), 5, (0,0,0), -1)\n",
"# for p in conf_keypoints_2d[['x', 'y']].values:\n",
"# img = cv.circle(img, p.astype(int), 5, (0,0,0), -1)\n",
" number += 1\n",
" \n",
" out.write(img)\n",
" \n",
"video.release()\n",
"out.release()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ada19ded",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"hovertemplate": "variable=x0
index=%{x}
value=%{y}
index=%{x}
value=%{y}
index=%{x}
value=%{y}
index=%{x}
value=%{y}
index=%{x}
value=%{y}
index=%{x}
value=%{y}
index=%{x}
value=%{y}
index=%{x}
value=%{y}
index=%{x}
value=%{y}
index=%{x}
value=%{y}