Initial commit

This commit is contained in:
2024-03-24 20:09:46 +01:00
commit f836e7c7ba
10 changed files with 1670 additions and 0 deletions

View File

@ -0,0 +1,151 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"from tqdm import tqdm, trange\n",
"from PIL import Image, ImageChops"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"VIDEO_FILE = \"diagnostik_aorta_gefaesse_fall8.mp4\"\n",
"cap = cv2.VideoCapture(VIDEO_FILE)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def _load_frames_sequentially(cap: cv2.VideoCapture, frame_interval: int):\n",
" frames = []\n",
" for i in range(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))):\n",
" res, frame = cap.read()\n",
" if i % frame_interval == 0 and res:\n",
" frames.append(frame)\n",
" return frames"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def _load_frames_random_access(cap: cv2.VideoCapture, frame_interval: int):\n",
" frames = []\n",
" total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n",
" i = 0\n",
" while i < total_frames:\n",
" cap.set(cv2.CAP_PROP_POS_FRAMES, i)\n",
" res, frame = cap.read()\n",
" if res:\n",
" frames.append(frame)\n",
" i += frame_interval\n",
" return frames"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def _load_frames_ra_grab(cap: cv2.VideoCapture, frame_interval: int):\n",
" frames = []\n",
" total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n",
" i = 0\n",
" res = cap.grab()\n",
" while i < total_frames:\n",
" if res:\n",
" frames.append(cap.retrieve()[1])\n",
" for _ in range(frame_interval):\n",
" res = cap.grab()\n",
" i += frame_interval\n",
" return frames"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def load_frames(cap: cv2.VideoCapture, frame_interval: int, method: str):\n",
" if method == \"sequentially\":\n",
" return _load_frames_sequentially(cap, frame_interval)\n",
" elif method == \"random-access\":\n",
" return _load_frames_random_access(cap, frame_interval)\n",
" elif method == \"ra-grab\":\n",
" return _load_frames_ra_grab(cap, frame_interval)\n",
" else:\n",
" raise ValueError(\"Unknown method\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"method='ra-grab', duration=6.1883978843688965\n",
"method='random-access', duration=14.68180513381958\n"
]
}
],
"source": [
"import time\n",
"\n",
"for method in (\"ra-grab\", \"random-access\"):\n",
" cap = cv2.VideoCapture(VIDEO_FILE)\n",
" start = time.time()\n",
" frames = load_frames(cap, 30, method)\n",
" print(f\"{method=}, duration={time.time() - start}\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Resultat: Random Access und nur Dekodieren wenn benötigt ist am schnellsten"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.10.1"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because one or more lines are too long

253
experimental/slides.ipynb Executable file
View File

@ -0,0 +1,253 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"from tqdm import tqdm\n",
"from PIL import Image, ImageChops"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"VIDEO_FILE = \"thorax_wiederholung_theorie_2_edit.mp4\""
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"cap = cv2.VideoCapture(VIDEO_FILE)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"60"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fps = int(cap.get(cv2.CAP_PROP_FPS))\n",
"fps"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 36788/36788 [03:05<00:00, 198.49it/s]\n"
]
}
],
"source": [
"frames = []\n",
"total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n",
"\n",
"for i in tqdm(range(total_frames)):\n",
" ret, frame = cap.read()\n",
" if i % fps == 0 and ret:\n",
" frames.append(frame)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"images = [ Image.fromarray(frame[:,:,::-1]) for frame in frames]"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"thumbnails = [ img.copy() for img in images]\n",
"for img in thumbnails:\n",
" img.thumbnail((64,64))"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"def compare(frame1, frame2):\n",
" diff = frame1 - frame2\n",
" return diff.sum() / (255 * np.prod(diff.shape))"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"def compare(image1, image2):\n",
" diff = ImageChops.difference(image1, image2)\n",
" return diff.histogram()"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(compare(thumbnails[0], thumbnails[10]))"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 613/613 [00:06<00:00, 95.81it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 73 slides.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"diffs = []\n",
"threshold = 0.23\n",
"\n",
"selected_frames = [frames[0]]\n",
"\n",
"compare_frame = frames[0]\n",
"for frame in tqdm(frames[1:]):\n",
" diff = compare(compare_frame, frame)\n",
" diffs.append(diff)\n",
" if diff > threshold:\n",
" compare_frame = frame\n",
" selected_frames.append(frame)\n",
"\n",
"print(f\"Found {len(selected_frames)} slides.\")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"73"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(selected_frames)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"73it [00:24, 3.02it/s]\n"
]
}
],
"source": [
"from PIL import Image\n",
"from os import makedirs\n",
"\n",
"imgdir = VIDEO_FILE.removesuffix('.mp4')\n",
"\n",
"makedirs(imgdir, exist_ok=True)\n",
"\n",
"\n",
"for index,frame in tqdm(enumerate(selected_frames)):\n",
" im = Image.fromarray(frame[:,:,::-1], mode=\"RGB\")\n",
" im.save(f\"{imgdir}/{index:02}.png\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.10.1"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

22
experimental/slides.py Executable file
View File

@ -0,0 +1,22 @@
import argparse
from pathlib import Path
import cv2
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("video", type=Path, help="Video file")
parser.add_argument("--interval", "-i", type=int, default=1, help="Frame intervals")
args = parser.parse_args()
cap = cv2.VideoCapture(args.video.as_posix())
fps = cap.get(cv2.CAP_PROP_FPS)
frames = []
f = 0
while f < cap.get(cv2.CAP_PROP_FRAME_COUNT):
cap.set(cv2.CAP_PROP_POS_FRAMES, f)
frames.append(cap.read())
f += int(fps)
print(len(frames))