基本問題描述:首先聲明,人臉遮擋代碼參考他人,若侵權,請告知;
前提條件:首先在openpose已經安轉好的基礎上,即可以使用Python調用;驗證方法
export PYTHONPATH=/usr/local/python:$PYTHONPATH
Python 3.5.2 (default, Jul 10 2019, 11:58:48)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import openpose
>>>
然後寫了C++主函數進行Python代碼調用,完成openpose的使用;
算法的主要功能爲,利用openpose的功能一些自動跟蹤人體結構的運動感,進行人臉跟蹤和人臉遮擋~~
(畢竟Python調用openpose方便,而在硬件嵌入式上,主程序仍是C++執行;)
CXXFLAGS=`python3-config --cflags` `python3-config --ldflags`
cpp_python:cpp_python.o
g++ -o cpp_python -g cpp_python.o $(CXXFLAGS)
cpp_python.o:cpp_python.cpp
g++ -o cpp_python.o -c cpp_python.cpp $(CXXFLAGS)
clean:
rm cpp_python cpp_python.o
Makefile 文件
#include <Python.h>
#include <iostream>
#include <string>
using namespace std;
void img_processing();
int great_function_from_python(string name_video);
int main()
{
Py_Initialize();
//python 2.7 char str[] = "Python";
//python 3.5需要用wchar_t
wchar_t str[] = L"Python";
string name="./a.mp4";
Py_SetProgramName(str);
if(!Py_IsInitialized())
cout << "init faild/n" << endl;
// 下面可以只是方便調試,可以不用。
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyRun_SimpleString("import cv2");
PyRun_SimpleString("import numpy as np");
//PyRun_SimpleString("print ("Hello Python!")\n");
//img_processing();
cout<<"enter paramter"<<endl;
int sum = great_function_from_python(name);
cout<<"leave paramter"<<endl;
cout << "sum:" << sum << endl;
Py_Finalize();
return 0;
}
// 不需要傳入參數和返回參數
void img_processing()
{
PyObject * pModule = NULL;
PyObject * pFunc = NULL;
pModule = PyImport_ImportModule("face_from_video_openpose");
if (pModule == NULL) {
printf("ERROR importing module");
}
pFunc = PyObject_GetAttrString(pModule, "openpose_face_mask");
if(pFunc != NULL) {
PyObject_CallObject(pFunc, NULL);
}
else {
cout << "pFunc returned NULL" <<endl;
}
}
//需要輸入參數和返回參數
int great_function_from_python(string name_video)
{
cout<<name_video<<endl;
int res;
PyObject *pModule,*pFunc;
PyObject *pArgs, *pValue;
PyObject *arg;
/* import */
pModule =PyImport_ImportModule("face_from_video_openpose");
/* great_module.great_function */
pFunc = PyObject_GetAttrString(pModule, "openpose_face_mask");
//PyObject* args = Py_BuildValue("openpose",name_video.c_str());//給python函數參數賦值
/* build args */
pArgs = PyTuple_New(1);
//PyTuple_SetItem(pArgs,0, PyLong_FromLong(a));
arg=PyUnicode_FromString(name_video.c_str());
PyTuple_SetItem(pArgs,0, arg);
/* call */
pValue = PyObject_CallObject(pFunc, pArgs);
//pValue = PyObject_CallObject(pFunc, args);
res = PyLong_AsLong(pValue);
return res;
//https://docs.python.org/3/genindex-P.html
}
cpp_python.cpp 主文件
# It requires OpenCV installed for Python
import sys
import cv2
import os
import argparse
from openpose import pyopenpose as op
import numpy as np
def openpose_face_mask(video_name):
print (video_name)
dir_path = os.path.dirname(os.path.realpath(__file__))
# Change these variables to point to the correct folder (Release/x64 etc.)
sys.path.append('../../python');
# Custom Params (refer to include/openpose/flags.hpp for more parameters)
params = dict()
params["model_folder"] = "./"
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()
datum = op.Datum()
#video_writer = cv2.VideoWriter("./pose_estimation_demo.mp4", cv2.VideoWriter_fourcc('D', 'I', 'V', 'X'), 15, (640, 480), True)
cap=cv2.VideoCapture(video_name)
face_list_point=[0,1,15,16,17,18]
while True:
ret,frame=cap.read()
if ret==False:
print ('erro')
face_list=[]
datum.cvInputData = frame
opWrapper.emplaceAndPop([datum])
print (datum.poseKeypoints)
if datum.poseKeypoints.ndim!=3:
continue
for human_pose_keypoints in datum.poseKeypoints:
keypoints_x_y_scorec = np.array(human_pose_keypoints.ravel(), dtype=float)
x_list=[]
y_list=[]
for item_id,item in enumerate(face_list_point):
x_list.append(keypoints_x_y_scorec[item*3])
y_list.append(keypoints_x_y_scorec[item*3+1])
while 0 in x_list:
x_list.remove(0)
while 0 in y_list:
y_list.remove(0)
if len(x_list) == 0 or len(y_list) == 0:
continue
min_x = int(min(x_list))-10
if min_x < 0:
min_x = 0
max_x = int(max(x_list))+10
min_y = int(min(y_list))-20
if min_y < 0:
min_y = 0
max_y = int(max(y_list))
if (min_x == max_x) or (min_y == max_y):
continue
frame[min_y:max_y, min_x:max_x] = cv2.GaussianBlur(frame[min_y:max_y, min_x:max_x], (25, 25), 300, 300)
#datum.cvInputData = frame
#opWrapper.emplaceAndPop([datum])
#print("Body keypoints: \n" + str(datum.poseKeypoints))
#video_writer.write(frame)
cv2.imshow("OpenPose 1.5.0 - Tutorial Python API", frame)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
return 0
#openpose_face_mask(video_name)
face_from_video_openpose.py 人臉遮擋文件(最新版本openpose)
附一張圖片