OpenPose实现抖音很火的人体骨架和视频动态舞
1 说明
=====
1.1 OpenPose 是基于卷积神经网络和监督学习并以 caffe 为框架写成的开源库。
1.2 可以实现人的面部表情、躯干和四肢甚至手指的跟踪,适用多人且具有较好的鲁棒性。
1.3 是世界上第一个基于深度学习的实时多人二维姿态估计,为机器理解人类提供了一个高质量的信息维度。
1.4 代码来源:https://github.com/spmallick/learnopencv
2 效果图
======
2.1 图片
2.2 视频
视频节选
3 准备
=====
3.1 对源代码进行修改、注释、运行,提高可读性和可操作性,适合小白,入门秒懂。
3.2 环境:python3.8+opencv4.4.0+深度deepin-linux操作系统。
3.3 模型下载地址,官网也有太慢了。网友提供,谢谢 https://blog.csdn.net/GL_a_/article/details/81661821
3.4 文档结构
说明是自己单独的,output.avi是视频生成的效果图
4 图片人体骨架
============
4.1 代码:OpenPoseImage.py#打开终端输入:本机 #python3.8 OpenPoseImage.py --device cpu --image_file single.jpeg #第1步:导入模块 import cv2 import time import numpy as np import argparse #终端参数设置 parser = argparse.ArgumentParser(description="Run keypoint detection") #启动cpu运行 parser.add_argument("--device", default="cpu", help="Device to inference on") parser.add_argument("--image_file", default="single.jpeg", help="Input image") args = parser.parse_args() #第3步:模型设置 #模型下载地址:https://blog.csdn.net/GL_a_/article/details/81661821 #国内的,快 #模型判定 MODE = "COCO" #if MODE is "COCO": if MODE=="COCO": #当前目录下的文件夹 protoFile = "pose/coco/pose_deploy_linevec.prototxt" #或者直接下载,太慢了,文件很大 #http://posefs1.perception.cs.cmu.edu/OpenPose/models/pose/coco/pose_iter_440000.caffemodel weightsFile = "pose/coco/pose_iter_440000.caffemodel" #缺少 nPoints = 18 POSE_PAIRS = [ [1,0],[1,2],[1,5],[2,3],[3,4],[5,6],[6,7],[1,8],[8,9],[9,10],[1,11],[11,12],[12,13],[0,14],[0,15],[14,16],[15,17]] #附加 #elif MODE is "MPI" : elif MODE=="MPI" : protoFile = "pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt" #或者直接下载,太慢了,文件很大 #http://posefs1.perception.cs.cmu.edu/OpenPose/models/pose/mpi/pose_iter_160000.caffemodel weightsFile = "pose/mpi/pose_iter_160000.caffemodel" #缺少 nPoints = 15 POSE_PAIRS = [[0,1], [1,2], [2,3], [3,4], [1,5], [5,6], [6,7], [1,14], [14,8], [8,9], [9,10], [14,11], [11,12], [12,13] ] #第4步:opencv读取图片 frame = cv2.imread(args.image_file) frameCopy = np.copy(frame) frameWidth = frame.shape[1] frameHeight = frame.shape[0] threshold = 0.1 #第5步:模型读取和深度学习设置骨架和骨架点 #读取模型 net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile) #默认启动cpu if args.device == "cpu": net.setPreferableBackend(cv2.dnn.DNN_TARGET_CPU) print("Using CPU device") #附加设置,启动GPU elif args.device == "gpu": net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA) print("Using GPU device") #时间设置 t = time.time() # input image dimensions for the network inWidth = 368 inHeight = 368 inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False) net.setInput(inpBlob) output = net.forward() print("time taken by network : {:.3f}".format(time.time() - t)) H = output.shape[2] W = output.shape[3] #骨架点 # Empty list to store the detected keypoints points = [] for i in range(nPoints): # confidence map of corresponding body"s part. probMap = output[0, i, :, :] # Find global maxima of the probMap. minVal, prob, minLoc, point = cv2.minMaxLoc(probMap) # Scale the point to fit on the original image x = (frameWidth * point[0]) / W y = (frameHeight * point[1]) / H if prob > threshold : cv2.circle(frameCopy, (int(x), int(y)), 8, (0, 255, 255), thickness=-1, lineType=cv2.FILLED) cv2.putText(frameCopy, "{}".format(i), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, lineType=cv2.LINE_AA) # Add the point to the list if the probability is greater than the threshold points.append((int(x), int(y))) else : points.append(None) #骨架 # Draw Skeleton,画骨架 for pair in POSE_PAIRS: partA = pair[0] partB = pair[1] if points[partA] and points[partB]: cv2.line(frame, points[partA], points[partB], (0, 255, 255), 2) cv2.circle(frame, points[partA], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED) #显示生成图片 cv2.imshow("Output-Keypoints", frameCopy) cv2.imshow("Output-Skeleton", frame) #输出生成图片 #cv2.imwrite("Output-Keypoints.jpg", frameCopy) #cv2.imwrite("Output-Skeleton.jpg", frame) print("Total time taken : {:.3f}".format(time.time() - t)) cv2.waitKey(0)
4.2 操作和效果图
5 视频骨架测试
============
5.1 代码OpenPoseVideo.py:#打开终端,需要一定的时间 #python3.8 OpenPoseVideo.py --device cpu --video_file sample_video.mp4 import cv2 import time import numpy as np import argparse parser = argparse.ArgumentParser(description="Run keypoint detection") parser.add_argument("--device", default="cpu", help="Device to inference on") parser.add_argument("--video_file", default="sample_video.mp4", help="Input Video") args = parser.parse_args() MODE = "MPI" #注意源代码是is,python3改为== #if MODE is "COCO": if MODE=="COCO": protoFile = "pose/coco/pose_deploy_linevec.prototxt" weightsFile = "pose/coco/pose_iter_440000.caffemodel" nPoints = 18 POSE_PAIRS = [ [1,0],[1,2],[1,5],[2,3],[3,4],[5,6],[6,7],[1,8],[8,9],[9,10],[1,11],[11,12],[12,13],[0,14],[0,15],[14,16],[15,17]] #elif MODE is "MPI" : elif MODE=="MPI" : protoFile = "pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt" weightsFile = "pose/mpi/pose_iter_160000.caffemodel" nPoints = 15 POSE_PAIRS = [[0,1], [1,2], [2,3], [3,4], [1,5], [5,6], [6,7], [1,14], [14,8], [8,9], [9,10], [14,11], [11,12], [12,13] ] inWidth = 368 inHeight = 368 threshold = 0.1 input_source = args.video_file cap = cv2.VideoCapture(input_source) hasFrame, frame = cap.read() #生成本目录下的视频 vid_writer = cv2.VideoWriter("output.avi",cv2.VideoWriter_fourcc("M","J","P","G"), 10, (frame.shape[1],frame.shape[0])) net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile) if args.device == "cpu": net.setPreferableBackend(cv2.dnn.DNN_TARGET_CPU) print("Using CPU device") elif args.device == "gpu": net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA) print("Using GPU device") while cv2.waitKey(1) < 0: t = time.time() hasFrame, frame = cap.read() frameCopy = np.copy(frame) if not hasFrame: cv2.waitKey() break frameWidth = frame.shape[1] frameHeight = frame.shape[0] inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False) net.setInput(inpBlob) output = net.forward() H = output.shape[2] W = output.shape[3] # Empty list to store the detected keypoints points = [] for i in range(nPoints): # confidence map of corresponding body"s part. probMap = output[0, i, :, :] # Find global maxima of the probMap. minVal, prob, minLoc, point = cv2.minMaxLoc(probMap) # Scale the point to fit on the original image x = (frameWidth * point[0]) / W y = (frameHeight * point[1]) / H if prob > threshold : cv2.circle(frameCopy, (int(x), int(y)), 8, (0, 255, 255), thickness=-1, lineType=cv2.FILLED) cv2.putText(frameCopy, "{}".format(i), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, lineType=cv2.LINE_AA) # Add the point to the list if the probability is greater than the threshold points.append((int(x), int(y))) else : points.append(None) # Draw Skeleton for pair in POSE_PAIRS: partA = pair[0] partB = pair[1] if points[partA] and points[partB]: cv2.line(frame, points[partA], points[partB], (0, 255, 255), 3, lineType=cv2.LINE_AA) cv2.circle(frame, points[partA], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED) cv2.circle(frame, points[partB], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED) cv2.putText(frame, "time taken = {:.2f} sec".format(time.time() - t), (50, 50), cv2.FONT_HERSHEY_COMPLEX, .8, (255, 50, 0), 2, lineType=cv2.LINE_AA) cv2.imshow("Output-Skeleton", frame) vid_writer.write(frame) vid_writer.release()
5.2 因为需要一定的时间,过程省略,效果图如文章开头。
合格妈妈的入园前的准备我不知道我算不算,算不算一个好妈妈?孩子快一岁的时候我在网上买的餐桌椅,还有勺子,到吃饭的时候直些把碗和勺子放在餐椅上他自己吃饭,每次都是饭凉的差不多,给他吃,我婆婆说孩子太小了,
揭开固体饮料的神秘面纱最近在家带宝宝没事的时候,刷微信看到了一条科普怎么理解固体饮料科普怎么理解固体饮料怎么理解固体饮料,大家会发现很多产品都写着固体饮料,既然是饮料,又怎么会有功效。这是国家食品法的硬
生活中的三三两两狗血剧想听故事吗?在这里我给大伙们讲个故事吧!有一个少年叫小明,有一年高考结束,小明认识一位女生小初,相对彼此互有好感,就开始了恋爱的征途。在高考分分数线出来,小明考到一个职业学院,小明
痛揭泛滥的网贷,加速毁掉了垮掉的一代网络有多发达快速,网贷信息就有多泛滥如洪水猛兽。据统计,有百分之八十的年轻人有网贷,这是多大的一块蛋糕呀!造成这些的原因实质就是社会导向的金钱至上主义,攀比,欲望,说白了就是社会的
2岁9个多月宝宝不会说话,医生叫做康复训练我的孩子2岁9个月了,只会喊妈妈,爸爸,并且只会无意识的喊,而不是对着我喊妈妈!从孩子满了2岁开始,我就特别焦虑,因为孩子不会说话,我慢慢发现,他不听指令,好像沉浸在自己的世界里,
全职宝妈带娃没收入,要怎么赚钱,大家帮帮忙?5月28号刨腹产生下二胎男宝宝,第一胎刨腹产,第二胎也要刨腹产。从宝宝出生到现在已经4个多月了。5月份辞职回家,预产期6月8号。提前一个多月辞职,没办法公司不能请假只能辞职。没想到
为啥影视圈这些货这么能闹腾?蹭顶流印某天公开致歉,早干嘛去啦?不知道那是文物墙吗?刚摁下吴某烦,扰乱整个社会秩序,真是无德,败坏社会风气,缺爹少娘的管教,也难怪从小在加拿大长大,外国资本主义教育的结果,有钱有一切,
揭秘86版西游记白龙马临终凄凉惨死,反思人性冷漠86版西游记所有的演员都有不错的归宿,当然而后的声名带来的荣誉和名利更是不可胜收,尤其四位主演那是名利双收呀!却唯独这匹杨导借调的军马白龙马,晚景凄凉,惨死后分人而食。有好多人会有
孩子爸爸又把孩子狠狠揍一顿刚刚,孩子爸爸又把孩子狠狠揍一顿,不是打手心,打屁股,真的是狠狠的,狠狠的一脚一脚踢屁股!踢了五六脚后,我拦着,我们又吵一架,这两年来,因为他打孩子不知道吵了多少架!他说我惯着孩子
2021。10。8阴阴冷的天气,熬夜的身体,没有问候的工作日,小心翼翼的沟通,没有谁会留意你的期待,你的心情,似过客一般,无所谓,各自忙碌望着背影,和七日前一样,依旧失落!晚上Fr写了会儿作业,就叫困
2021。10。9晴放学路上征询Fr意见想不想看长津湖,她兴奋的说想,还说今天在学校老师聊起电影话题,完全插不上嘴,只能羡慕的听着别人讨论,于是立马拍板定了晚上730的票,匆忙吃了饭准点赶到影院,影片