This commit is contained in:
2025-04-14 17:15:38 +10:00
parent 91dbc67c71
commit e90d776f53
20 changed files with 1590 additions and 0 deletions

246
LBPTOP.ipynb Normal file
View File

@@ -0,0 +1,246 @@
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-14T04:06:31.135934Z",
"start_time": "2025-04-14T04:06:30.845929Z"
}
},
"cell_type": "code",
"source": [
"import cv2\n",
"import numpy as np\n",
"from skimage.feature import local_binary_pattern\n"
],
"id": "9157e102c51206bb",
"outputs": [],
"execution_count": 1
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-14T04:06:31.144988Z",
"start_time": "2025-04-14T04:06:31.138940Z"
}
},
"cell_type": "code",
"source": [
"def extract_lbp_top(video_path, radius=2, n_points=8, method='uniform', block_size=10):\n",
" cap = cv2.VideoCapture(video_path)\n",
" frames = []\n",
"\n",
" # 读取所有帧\n",
" while True:\n",
" ret, frame = cap.read()\n",
" if not ret:\n",
" break\n",
" gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n",
" frames.append(gray)\n",
" cap.release()\n",
"\n",
" frames = np.array(frames)\n",
" T, H, W = frames.shape # 时间、空间维度\n",
"\n",
" # 使用滑动窗口计算 XT, YT 平面\n",
" hist_xy = np.zeros((n_points + 2,))\n",
" hist_xt = np.zeros((n_points + 2,))\n",
" hist_yt = np.zeros((n_points + 2,))\n",
"\n",
" # LBP on XY plane\n",
" for t in range(0, T, block_size):\n",
" if t >= T:\n",
" break\n",
" lbp = local_binary_pattern(frames[t], n_points, radius, method)\n",
" hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, n_points + 3), density=True)\n",
" hist_xy += hist\n",
"\n",
" # LBP on XT plane\n",
" for y in range(0, H, block_size):\n",
" if y >= H:\n",
" break\n",
" xt_plane = frames[:, y, :]\n",
" lbp = local_binary_pattern(xt_plane, n_points, radius, method)\n",
" hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, n_points + 3), density=True)\n",
" hist_xt += hist\n",
"\n",
" # LBP on YT plane\n",
" for x in range(0, W, block_size):\n",
" if x >= W:\n",
" break\n",
" yt_plane = frames[:, :, x]\n",
" lbp = local_binary_pattern(yt_plane, n_points, radius, method)\n",
" hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, n_points + 3), density=True)\n",
" hist_yt += hist\n",
"\n",
" # 拼接三个平面的直方图作为最终特征向量\n",
" feature_vector = np.concatenate([hist_xy, hist_xt, hist_yt])\n",
" feature_vector /= np.linalg.norm(feature_vector) # 归一化\n",
"\n",
" return feature_vector"
],
"id": "d9d5bd6f6a9e114a",
"outputs": [],
"execution_count": 2
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-14T04:06:31.297545Z",
"start_time": "2025-04-14T04:06:31.294247Z"
}
},
"cell_type": "code",
"source": [
"def getFeature(video_path = r\"D:\\DESKTOP\\2025\\44\\a1\\dataset\\org\\1.mp4\"):\n",
"\n",
" feature = extract_lbp_top(video_path)\n",
" # print(\"Video feature shape (for copyright):\", feature.shape)\n",
" return feature"
],
"id": "404b8b3562026f1f",
"outputs": [],
"execution_count": 3
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-14T04:06:32.509232Z",
"start_time": "2025-04-14T04:06:31.302931Z"
}
},
"cell_type": "code",
"source": [
"import sqlite3\n",
"import io\n",
"import os\n",
"import tqdm\n",
"\n",
"# 注册适配器与转换器numpy数组 <-> BLOB\n",
"def adapt_array(arr):\n",
" out = io.BytesIO()\n",
" np.save(out, arr)\n",
" out.seek(0)\n",
" return sqlite3.Binary(out.read())\n",
"\n",
"def convert_array(text):\n",
" out = io.BytesIO(text)\n",
" out.seek(0)\n",
" return np.load(out)\n",
"\n",
"# 注册自定义类型处理\n",
"sqlite3.register_adapter(np.ndarray, adapt_array)\n",
"sqlite3.register_converter(\"array\", convert_array)\n",
"\n",
"# 创建数据库连接(带类型检测)\n",
"conn = sqlite3.connect(\"LBPTOP.db\", detect_types=sqlite3.PARSE_DECLTYPES)\n",
"cursor = conn.cursor()\n",
"\n",
"# 创建表\n",
"cursor.execute(\"\"\"\n",
"CREATE TABLE IF NOT EXISTS data (\n",
" id INTEGER PRIMARY KEY,\n",
" array BLOB,\n",
" group_path TEXT,\n",
" full_path TEXT\n",
")\n",
"\"\"\")\n",
"\n",
"\n",
"def add_to_db(array_to_store,group_path,full_path):\n",
" cursor.execute(\"INSERT INTO data (array,group_path,full_path) VALUES (?,?,?)\", (array_to_store,group_path,full_path,))\n",
" conn.commit()\n",
"\n",
"# # 读取数组\n",
"# cursor.execute(\"SELECT array FROM data WHERE id=1\")\n",
"# fetched_array = cursor.fetchone()[0]\n",
"#\n",
"# print(\"原始数组:\\n\", array_to_store)\n",
"# print(\"读取的数组:\\n\", fetched_array)\n",
"\n",
"folder_path = r\"D:\\DESKTOP\\2025\\44\\a1\\dataset\"\n",
"\n",
"all_files = []\n",
"names = [str(x)+\".mp4\" for x in range(10)]\n",
"print(names)\n",
"\n",
"\n",
"# 遍历文件夹\n",
"for group_path in os.listdir(folder_path):\n",
" full_path = os.path.join(folder_path, group_path)\n",
" if os.path.isdir(full_path):\n",
" # 遍历子文件夹\n",
" for video_path in os.listdir(full_path):\n",
" if os.path.basename(video_path) in names:\n",
" video_full_path = os.path.join(full_path, video_path)\n",
" if os.path.isfile(video_full_path):\n",
" # 处理视频文件\n",
" all_files.append((group_path,video_full_path))\n",
"print(len(all_files))\n"
],
"id": "4dc69bad309cb6b1",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['0.mp4', '1.mp4', '2.mp4', '3.mp4', '4.mp4', '5.mp4', '6.mp4', '7.mp4', '8.mp4', '9.mp4']\n",
"70\n"
]
}
],
"execution_count": 4
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-14T04:20:52.302187Z",
"start_time": "2025-04-14T04:06:32.521053Z"
}
},
"cell_type": "code",
"source": [
"for group_path, video_full_path in tqdm.tqdm(all_files):\n",
" # 读取视频特征\n",
" feature = getFeature(video_full_path)\n",
" # 将特征存储到数据库\n",
" add_to_db(feature,group_path,video_full_path)\n",
" # print(f\"已处理并存储: {video_full_path}\")\n",
"\n",
"conn.close()\n"
],
"id": "3cd83672f6901125",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 70/70 [14:19<00:00, 12.28s/it]\n"
]
}
],
"execution_count": 5
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}