First PoC

This commit is contained in:
Ruben van de Ven 2022-10-31 14:37:33 +01:00
commit 9ceb71286c
5 changed files with 249 additions and 0 deletions

41
Dockerfile Normal file
View file

@ -0,0 +1,41 @@
# modified from https://hub.docker.com/r/cwaffles/openpose
FROM nvidia/cuda:11.4.0-cudnn8-devel-ubuntu20.04
#get deps
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
python3-dev python3-pip python3-setuptools git g++ wget make libprotobuf-dev protobuf-compiler libopencv-dev \
libgoogle-glog-dev libboost-all-dev libhdf5-dev libatlas-base-dev build-essential libgflags-dev \
liblmdb-dev
RUN apt-get install -y cmake
#for python api
RUN pip3 install --upgrade pip
RUN pip3 install numpy opencv-python
#get openpose
WORKDIR /openpose
RUN git clone https://github.com/CMU-Perceptual-Computing-Lab/openpose.git .
#build it
WORKDIR /openpose/build
RUN cmake -DBUILD_PYTHON=ON .. && make -j `nproc`
# fetch models
WORKDIR /openpose/models
RUN ./getModels.sh
# Install python
WORKDIR /openpose/build/python/openpose
RUN make install
RUN cp /openpose/build/python/openpose/pyopenpose.cpython-38-x86_64-linux-gnu.so /usr/local/lib/python3.8/dist-packages/
WORKDIR /usr/local/lib/python3.8/dist-packages
RUN ln -s pyopenpose.cpython-38-x86_64-linux-gnu.so pyopenpose
#ENV LD_LIBRARY_PATH=/openpose/build/python/openpose
RUN apt-get install -y ffmpeg
WORKDIR /openpose

12
README.md Normal file
View file

@ -0,0 +1,12 @@
# openpose-docker-python
Run python applications which use the openpose python module
Based on https://github.com/esemeniuc/openpose-docker
## Usage
```bash
docker build -t openpose-ubuntu20 .
docker run --rm --gpus all -v `pwd`/data:/data --user $(id -u):$(id -g) -v `pwd`/app:/app --workdir=/app -e HOME=/app -it openpose-ubuntu20 python3 break_on_scale.py --steps 500
```

81
app/01_body_from_image.py Normal file
View file

@ -0,0 +1,81 @@
# From Python
# It requires OpenCV installed for Python
import sys
import cv2
import os
from sys import platform
import argparse
# Import Openpose (Windows/Ubuntu/OSX)
dir_path = os.path.dirname(os.path.realpath(__file__))
try:
# Windows Import
if platform == "win32":
# Change these variables to point to the correct folder (Release/x64 etc.)
sys.path.append(dir_path + '/../../python/openpose/Release');
os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;'
import pyopenpose as op
else:
# Change these variables to point to the correct folder (Release/x64 etc.)
#sys.path.append('../../python');
# If you run `make install` (default path is `/usr/local/python` for Ubuntu), you can also access the OpenPose/python module from there. This will install OpenPose and the python library at your desired installation path. Ensure that this is in your python path in order to use it.
# sys.path.append('/usr/local/python')
import pyopenpose as op
except ImportError as e:
print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
raise e
# Flags
parser = argparse.ArgumentParser()
parser.add_argument("--image_path", default="/openpose/examples/media/COCO_val2014_000000000192.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).")
args = parser.parse_known_args()
# Custom Params (refer to include/openpose/flags.hpp for more parameters)
params = dict()
params["model_folder"] = "/openpose/models/"
# params["face"] = True
params["hand"] = True
# params["heatmaps_add_parts"] = True
# params["heatmaps_add_bkg"] = True
# params["heatmaps_add_PAFs"] = True
# params["heatmaps_scale"] = 3
# params["upsampling_ratio"] = 1
# params["body"] = 1
# Add others in path?
for i in range(0, len(args[1])):
curr_item = args[1][i]
if i != len(args[1])-1: next_item = args[1][i+1]
else: next_item = "1"
if "--" in curr_item and "--" in next_item:
key = curr_item.replace('-','')
if key not in params: params[key] = "1"
elif "--" in curr_item and "--" not in next_item:
key = curr_item.replace('-','')
if key not in params: params[key] = next_item
# Construct it from system arguments
# op.init_argv(args[1])
# oppython = op.OpenposePython()
# Starting OpenPose
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()
# Process Image
datum = op.Datum()
imageToProcess = cv2.imread(args[0].image_path)
datum.cvInputData = imageToProcess
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
# Display Image
print("Body keypoints: \n" + str(datum.poseKeypoints))
print("Face keypoints: \n" + str(datum.faceKeypoints))
print("Left hand keypoints: \n" + str(datum.handKeypoints[0]))
print("Right hand keypoints: \n" + str(datum.handKeypoints[1]))
cv2.imwrite("/data/result_body.jpg",datum.cvOutputData)
print(dir(datum))
# cv2.imshow("OpenPose 1.7.0 - Tutorial Python API", datum.cvOutputData)
# cv2.waitKey(0)

112
app/break_on_scale.py Normal file
View file

@ -0,0 +1,112 @@
# From Python
import sys
import cv2
import os
import argparse
import subprocess
import numpy as np
import pyopenpose as op
# Flags
parser = argparse.ArgumentParser()
parser.add_argument("--image_path", default="/data/human.jpg", help="Process an image. Read all standard formats (jpg, png, bmp, etc.).")
parser.add_argument("--steps", default=100, type=int, help="Frames to render by stretching")
args, unknown_args = parser.parse_known_args()
# Custom Params (refer to include/openpose/flags.hpp for more parameters)
# here: https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/fc7813cfbb93552905a7190e6805fd01a9c101a2/include/openpose/flags.hpp
params = dict()
params["model_folder"] = "/openpose/models/"
# params["face"] = True
# params["hand"] = True
# params["heatmaps_add_parts"] = True
# params["heatmaps_add_bkg"] = True
# params["heatmaps_add_PAFs"] = True
# params["heatmaps_scale"] = 3
# params["upsampling_ratio"] = 1
# params["body"] = 1
# Add others in path?
for i in range(0, len(unknown_args)):
curr_item = unknown_args[i]
if i != len(unknown_args)-1: next_item = unknown_args[i+1]
else: next_item = "1"
if "--" in curr_item and "--" in next_item:
key = curr_item.replace('-','')
if key not in params: params[key] = "1"
elif "--" in curr_item and "--" not in next_item:
key = curr_item.replace('-','')
if key not in params: params[key] = next_item
# Construct it from system arguments
# op.init_argv(unknown_args)
# oppython = op.OpenposePython()
original_image = cv2.imread(args.image_path)
# Starting OpenPose
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()
# for i in range(args.steps):
# factor = (i/args.steps)
# mask = np.zeros(original_image.shape[:2], dtype="uint8")
# cv2.rectangle(mask, (0, 0), (original_image.shape[1], int(original_image.shape[0] - original_image.shape[0] * factor)), 255, -1)
# # cv2.imshow("Rectangular Mask", mask)
# frame = cv2.bitwise_and(original_image, original_image, mask=mask)
# # Process Image
# datum = op.Datum()
# datum.cvInputData = frame
# opWrapper.emplaceAndPop(op.VectorDatum([datum]))
# # Display Image
# # print("Body keypoints: \n" + str(datum.poseKeypoints))
# # print("Face keypoints: \n" + str(datum.faceKeypoints))
# # print("Left hand keypoints: \n" + str(datum.handKeypoints[0]))
# # print("Right hand keypoints: \n" + str(datum.handKeypoints[1]))
# print(i, datum.poseKeypoints is not None)
# cv2.imwrite(f"/data/out/result_body_obscure{i:03d}.jpg",datum.cvOutputData)
# subprocess.call([
# 'ffmpeg', '-i', '/data/out/result_body_obscure%3d.jpg', '-y','/data/out/result_body_obscure.mp4'
# ])
for i in range(args.steps):
scale = 1 - (i/args.steps)
frame = np.zeros(original_image.shape, dtype="uint8")
width = int(original_image.shape[1] * scale)
height = int(original_image.shape[0])
partial_frame = cv2.resize(original_image, (width, height))
# frame[y_start:y_end,x_start:x_end] = partial_frame
offset = int((frame.shape[1] - width) / 2)
# print(offset)
frame[0:height,offset:(offset+width)] = partial_frame
# Process Image
datum = op.Datum()
# print(frame.shape)
datum.cvInputData = frame
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
# Display Image
# print("Body keypoints: \n" + str(datum.poseKeypoints))
# print("Face keypoints: \n" + str(datum.faceKeypoints))
# print("Left hand keypoints: \n" + str(datum.handKeypoints[0]))
# print("Right hand keypoints: \n" + str(datum.handKeypoints[1]))
print(i, datum.poseKeypoints is not None)
cv2.imwrite(f"/data/out/result_body_scale{i:03d}.jpg",datum.cvOutputData)
subprocess.call([
'ffmpeg', '-i', '/data/out/result_body_scale%3d.jpg', '-y','/data/out/result_body_scale.mp4'
])
print("done")
# cv2.imshow("OpenPose 1.7.0 - Tutorial Python API", datum.cvOutputData)
# cv2.waitKey(0)

3
data/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*
!.gitignore