-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9ceb020
Showing
10 changed files
with
4,954 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "e44664ba-13a5-46ff-a3c8-955a41d897e0", | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2022-05-06T07:19:27.886440Z", | ||
"iopub.status.busy": "2022-05-06T07:19:27.886440Z", | ||
"iopub.status.idle": "2022-05-06T07:19:28.357387Z", | ||
"shell.execute_reply": "2022-05-06T07:19:28.357387Z", | ||
"shell.execute_reply.started": "2022-05-06T07:19:27.886440Z" | ||
}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# opencv-python\n", | ||
"import cv2\n", | ||
"# mediapipe人工智能工具包\n", | ||
"import mediapipe as mp\n", | ||
"#进度条库\n", | ||
"from tqdm import tqdm\n", | ||
"#时间库\n", | ||
"import time" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "f288fced-18b7-4657-9465-edbc46b29f62", | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2022-05-06T07:19:28.357387Z", | ||
"iopub.status.busy": "2022-05-06T07:19:28.357387Z", | ||
"iopub.status.idle": "2022-05-06T07:19:28.394519Z", | ||
"shell.execute_reply": "2022-05-06T07:19:28.393512Z", | ||
"shell.execute_reply.started": "2022-05-06T07:19:28.357387Z" | ||
}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"#导入solution\n", | ||
"mp_pose = mp.solutions.pose\n", | ||
"# #导入绘图函数\n", | ||
"mp_drawing = mp.solutions.drawing_utils\n", | ||
"\n", | ||
"#导入模型\n", | ||
"pose = mp_pose.Pose (static_image_mode=True,\n", | ||
" #是静态图片还是连续视频帧\n", | ||
" model_complexity=1,\n", | ||
" #选择人体姿态关键点检测模型,0性能差但快,2性能好但慢,1介于两者之间\n", | ||
" smooth_landmarks=True,\n", | ||
" #是否平滑关键点\n", | ||
" enable_segmentation=True,\n", | ||
" #是否人体抠图\n", | ||
" min_detection_confidence=0.5, #置信度阈值\n", | ||
" min_tracking_confidence=0.5) # 追踪阈值" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "349dbc2a-1859-48d3-9c4e-472704790ca7", | ||
"metadata": {}, | ||
"source": [ | ||
"### 处理单帧的函数" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "c1571e3a-e377-417f-9314-f8e1d9073b5f", | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2022-05-06T07:19:28.396521Z", | ||
"iopub.status.busy": "2022-05-06T07:19:28.395512Z", | ||
"iopub.status.idle": "2022-05-06T07:19:28.410514Z", | ||
"shell.execute_reply": "2022-05-06T07:19:28.409512Z", | ||
"shell.execute_reply.started": "2022-05-06T07:19:28.396521Z" | ||
}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"#处理帧函数\n", | ||
"def process_frame(img):\n", | ||
" # BGR转RGB\n", | ||
" img_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n", | ||
" #将RGB图像输入模型,获取预测结果\n", | ||
" results = pose.process(img_RGB)\n", | ||
" #可视化\n", | ||
" mp_drawing. draw_landmarks (img, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)\n", | ||
" # look img(img)\n", | ||
" #在三维真实物理坐标系中可视化以米为单位的检测结果\n", | ||
" # mp_ _drawing. plot_ landmarks (results. pose_ _wor1d_ landmarks, mp_ pose. POSE CONNECTIONS)\n", | ||
" #\n", | ||
" # BGR转RGB\n", | ||
" #\n", | ||
" #img_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n", | ||
" #\n", | ||
" #将RGB图像输入模型,获取预测结果\n", | ||
" #\n", | ||
" #results = hands.process(img_RGB)\n", | ||
" #\n", | ||
" #if results.multi_ hand landmarks: #如果有检测到手\n", | ||
" #遍历每一只检测出的手\n", | ||
" #\n", | ||
" #for hand_ idx in range(len(results . multi_ hand landmarks)):\n", | ||
" #\n", | ||
" #hand _21 = results. multi hand_ landmarks[hand idx] #获取该手的所有关键点坐标\n", | ||
" #\n", | ||
" #mpDraw. draw landmarks (img, hand_ 21, mp hands . HAND_ CONNECTIONS) #可视化\n", | ||
" return img" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4e5b7d9d-a1d8-418d-81bb-8dc801e1bfb8", | ||
"metadata": {}, | ||
"source": [ | ||
"### 调用摄像头获取每帧(模板)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "7a63842e-5a43-4f00-b68d-84d7917dab1a", | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2022-05-06T07:19:28.411514Z", | ||
"iopub.status.busy": "2022-05-06T07:19:28.411514Z", | ||
"iopub.status.idle": "2022-05-06T07:19:34.614109Z", | ||
"shell.execute_reply": "2022-05-06T07:19:34.613109Z", | ||
"shell.execute_reply.started": "2022-05-06T07:19:28.411514Z" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"#调用摄像头逐帧实时处理模板\n", | ||
"#不需修改任何代码,只需修改process_frame函数即可\n", | ||
"#导入opencv-python\n", | ||
"import cv2\n", | ||
"import time\n", | ||
"#获取摄像头,传入0表示获取系统默认摄像头\n", | ||
"cap = cv2 .VideoCapture(0)\n", | ||
"#打开cap \n", | ||
"cap.open(0)\n", | ||
"#无限循环,直到break被触发\n", | ||
"while cap.isOpened():\n", | ||
" #获取画面\n", | ||
" success, frame = cap.read( )\n", | ||
" if not success:\n", | ||
" print( 'Error' )\n", | ||
" break\n", | ||
" \n", | ||
" ## !!!处理帧函数\n", | ||
" frame = process_frame(frame)\n", | ||
" #展示处理后的三通道图像\n", | ||
" cv2.imshow( \"my_window\",frame )\n", | ||
" if cv2.waitKey(1) in [ord('q'),27]: #按键盘上的q或esc退出(在英文输入法下)\n", | ||
" break\n", | ||
"#关闭摄像头\n", | ||
"cap.release()\n", | ||
"#关闭图像窗口\n", | ||
"cv2.destroyAllWindows()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "718d1661-1a58-49cd-a952-28107c372948", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b96f8aaf-cb15-4f2c-8b94-f1acc1f3fbaf", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"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.8.12" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.