1.前提是已經安裝過TensorFlow-gpu版本了
TensorFlow-gpu版本安裝教程參考https://blog.csdn.net/weixin_41320468/article/details/104511105?utm_medium=distribute.pc_relevant.none-task-blog-utm_term-2&spm=1001.2101.3001.4242
1).先安裝anaconda3
2).創建一個tensorflow-gpu的環境:conda create -n tensorflow_gpuenv
3).下載並安裝cuda10,添加環境變量
4).下載並解壓cudnn7.4,復制那三個文件到cuda下面
5).激活環境:conda activate tensorflow_gpuenv
6).在該環境下安裝tensoflow-gpu1.14(使用清華鏡像): pip install --ignore-installed --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==1.14.0
7).檢測是否成功:
activate tensorflow_gpuenv
python #進入python
#下面測試tensorflow-gpu是否安裝成功
import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
2.下載tf-pose-estimation源碼
下載地址:https://github.com/ildoonet/tf-pose-estimation

3.requirements文件里面有所需要的模塊
1)在這里,先安裝git
conda activate tensorflow_gpuenv//進入TensorFlow環境
conda install git//安裝git
E:
cd E:\tf-pose-estimation-master\tf-pose-estimation-master//進入到下載的tf-openpose-estimation-master文件目錄
pip install -r requirements.txt


如上所示,安裝成功。
4.安裝opencv
首先要看一下自己電腦支持的版本
python
import pip._internal
print(pip._internal.pep425tags.get_supported())
如下所示:

則去https://www.lfd.uci.edu/~gohlke/pythonlibs/下載下面對應的文件,64位電腦就win_amd64,32位電腦就win32.

下載放置目錄,等待安裝

pip install E:\tf-pose-estimation-master\tf-pose-estimation-master\opencv_python-4.4.0-cp37-cp37m-win_amd64.whl
如下所示便表示安裝成功

5.Build c++ library
先要下載swig,這一步會比較麻煩。目前到現在為止我都不知道咋安裝成功的.
1.swig下載地址
http://prdownloads.sourceforge.net/swig/swigwin-3.0.12.zip
2.解壓放置目錄
E:\swigwin-3.0.12或者C:\swigwin-3.0.12

3.添加環境變量
下面這兩個是,分別是在用戶變量中和系統變量中創建的

下面這兩個是在用戶變量和系統變量的path中添加的。我只知道,我的是兩個都弄了才成功的,網上是說進行下面的一個就行,如添加C:\swigwin-3.0.12


經過一番騷操作,竟然成功了。過程如下:
在進入E:\tf-pose-estimation-master\tf-pose-estimation-master\tf_pose\pafprocess下執行如下代碼
swig -python -c++ pafprocess.i && python setup.py build_ext --inplace(我這個時候好像是沒有激活tensorflow環境)
(這里是因為我這個base的環境本身就包含了很多包的,可以通過conda list查看當前環境已經安裝的東西,缺啥或者需要啥就裝啥。)


如上,則表示成功。反之,如果出現下面結果,那就是環境變量有問題。

6.調用攝像頭或者圖片檢測
完成5之后就算大工搞成了,但是還差一點。
//調用攝像頭顯示
python run_webcam.py --model=mobilenet_thin --resize=432x368 --camera=0
//調用圖片
python run.py --model=mobilenet_thin --resize=432x368 --image=./images/p1.jpg
如果能成功顯示最好,但是也有可能出現下面問題:

出現的原因...算了我太懶了。
直接簡單粗暴解決-.-找到對應錯誤文件

把這一行注釋掉

如果需要調用本地視頻,則需要修改run_video.py文件,修改后代碼如下
import argparse
import logging
import time
import cv2
import numpy as np
from tf_pose import common
from tf_pose.estimator import TfPoseEstimator
from tf_pose.networks import get_graph_path, model_wh
logger = logging.getLogger('TfPoseEstimator-Video')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
fps_time = 0
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='tf-pose-estimation Video')
parser.add_argument('--video', type=str, default='./videos/appledancing.mp4') #改為自己的輸入視頻
parser.add_argument('--resolution', type=str, default='432x368', help='network input resolution. default=432x368')
parser.add_argument('--model', type=str, default='mobilenet_thin', help='cmu / mobilenet_thin / mobilenet_v2_large / mobilenet_v2_small')
parser.add_argument('--resize-out-ratio', type=float, default=4.0,help='if provided, resize heatmaps before they are post-processed. default=1.0')
parser.add_argument('--show-process', type=bool, default=False,help='for debug purpose, if enabled, speed for inference is dropped.')
parser.add_argument('--showBG', type=bool, default=True, help='False to show skeleton only.')
args = parser.parse_args()
logger.debug('initialization %s : %s' % (args.model, get_graph_path(args.model)))
w, h = model_wh(args.resolution)
e = TfPoseEstimator(get_graph_path(args.model), target_size=(w, h))
cap = cv2.VideoCapture(args.video)
ret_val, image = cap.read()
video_size = (image.shape[1],image.shape[0])
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2')
#video_size = (2560,1920)
outVideo = cv2.VideoWriter('save.avi', fourcc, 30, video_size)#這個參數30原本是10的,默認是10FPS保存文件的,可以根據實際的FPS大小,調整一下,比如我台式機是30FPS,那么就調整成30.
#filename = '/home/yasin/save.avi' #更改為自己的保存路徑
if cap.isOpened() is False:
print("Error opening video stream or file")
while cap.isOpened():
ret_val, image = cap.read()
if not ret_val:
break
logger.debug('image process+')
#image = common.read_imgfile(args.image, None, None)
humans = e.inference(image, resize_to_default=(w > 0 and h > 0), upsample_size=args.resize_out_ratio)
logger.debug('postprocess+')
image = TfPoseEstimator.draw_humans(image, humans, imgcopy=False)
logger.debug('show+')
# image_res = cv2.resize(image,(640,480))
cv2.putText(image, "FPS: %f" % (1.0 / (time.time() - fps_time)), (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0, 255, 0), 2)
cv2.imshow('tf-pose-estimation result', image)
fps_time = time.time()
outVideo.write(image)
# video_write = cv2.VideoWriter(filename, fourcc, 10, video_size)
# video_write.write(image)
if cv2.waitKey(1) == 27:
break
logger.debug('finished+')
cap.release()
outVideo.release()
cv2.destroyAllWindows()
''' while cap.isOpened():
ret_val, image = cap.read()
humans = e.inference(image,resize_to_default=(w > 0 and h > 0))
if not args.showBG:
image = np.zeros(image.shape)
image = TfPoseEstimator.draw_humans(image, humans, imgcopy=False)
cv2.putText(image, "FPS: %f" % (1.0 / (time.time() - fps_time)), (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.imshow('tf-pose-estimation result', image)
fps_time = time.time()
if cv2.waitKey(1) == 27:
break
cv2.destroyAllWindows()
logger.debug('finished+')
'''
至此,就能完美運行了,打完收工ovo。
運行python代碼可能遇到的錯誤
TabError: inconsistent use of tabs and spaces in indentation
這是因為沒有正確使用Tab和Space的問題。
