Posture detection by openpose from USB camera image using python code

Purpose

It is a memorandum when posture detection by openpose is performed from a USB camera image using python code.

Preparation

Ubuntu16.04 PC Webcam

Refer to the article Openpose PYTHON API and proceed with the preparation until the test code (1_extract_pose.py) is moved.

The version of CUDA / cuDNN uses the following.  CUDA ver8.0,  cuDNN ver6.0 I tried running OpenPose. was used as a reference.

code

test.py


# From Python
# It requires OpenCV installed for Python
import sys
import cv2
import os
from sys import platform
import cv2

# Remember to add your installation path here
# Option a
dir_path = os.path.dirname(os.path.realpath(__file__))
if platform == "win32": sys.path.append(dir_path + '/../../python/openpose/');
else: sys.path.append('../../python');
# Option b
# 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')

# Parameters for OpenPose. Take a look at C++ OpenPose example for meaning of components. Ensure all below are filled
try:
    from openpose import *
except:
    raise Exception('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
params = dict()
params["logging_level"] = 3
params["output_resolution"] = "-1x-1"
params["net_resolution"] = "-1x368"
params["model_pose"] = "BODY_25"
params["alpha_pose"] = 0.6
params["scale_gap"] = 0.3
params["scale_number"] = 1
params["render_threshold"] = 0.05
# If GPU version is built, and multiple GPUs are available, set the ID here
params["num_gpu_start"] = 0
params["disable_blending"] = False
# Ensure you point to the correct path where models are located
#params["default_model_folder"] = dir_path + "/../../../models/"
params["default_model_folder"] = "../../../models/"
# Construct OpenPose object allocates GPU memory
openpose = OpenPose(params)

cap = cv2.VideoCapture(0)

while 1:
    # Read new image

    ret, img = cap.read()
    # img = cv2.imread("../../../examples/media/COCO_val2014_000000000192.jpg ")

    # Output keypoints and the image with the human skeleton blended on it
    keypoints, output_image = openpose.forward(img, True)

    # Print the human pose keypoints, i.e., a [#people x #keypoints x 3]-dimensional numpy object with the keypoints of all the people on that image
    print(keypoints)
    # Display the image
    cv2.imshow("output", output_image)
    cv2.waitKey(15)

cap.release()
cv2.destroyAllWindows()

test

It is OK if the result of posture detection by openpose is output from the image input from the WEB camera below.

/openpose/build/examples/tutorial_python$ python test.py 

Coding Error measures

Prototxt file not found: /usr/local/python/openpose/../../../models error

$ python 1_extract_pose.py
Error:
Prototxt file not found: /usr/local/python/openpose/../../../models/pose/body_25/pose_deploy.prototxt.
Possible causes:
	1. Not downloading the OpenPose trained models.
	2. Not running OpenPose from the same directory where the `model` folder is located.
	3. Using paths with spaces.

Because models cannot be referenced from the runtime directory It's OK if you modify 1_extract_pose.py so that openpose / models can refer to it.

#params["default_model_folder"] = dir_path + "/../../../models/"
params["default_model_folder"] = "../../../models/"

reference

Openpose PYTHON API

Recommended Posts

Posture detection by openpose from USB camera image using python code
Do a search by image from the camera roll using Pythonista3
Image acquisition from camera with Python + OpenCV
[Python] Specify the range from the image by dragging the mouse
View images in OpenCV from Python using an external USB camera on your MacBook
Read QR code from image file with Python (Mac)
Image collection by calling Bing Image Search API v5 from Python
Shoot time-lapse from a PC camera using Python and OpenCV
Flatten using Python yield from
Stop Omxplayer from Python code
[Python] I tried using OpenPose
Image processing by python (Pillow)
[Image processing] Edge detection using Python and OpenCV makes Poo naked!
Using Rstan from Python with PypeR
[Python] Using OpenCV with Python (Image transformation)
Notes on using MeCab from Python
[Python] Using OpenCV with Python (Edge Detection)
OS determination by Makefile using Python
Using Cloud Storage from Python3 (Introduction)
Execute Python code from C # GUI
Python error detection run from Powershell
Check python code styles using pep8
Run Ansible from Python using API
Precautions when using phantomjs from python
Access spreadsheets using OAuth 2.0 from Python
[VS Code] ~ Tips when using python ~
Try using Amazon DynamoDB from Python
[EV3 x Python] Stream the camera image to your PC using mjpg-streamer.
Avoid run-time error ModuleNotFoundError for executables generated from Python code using Pyinstaller
Avoid run-time error ModuleNotFoundError for executables generated from Python code using Pyinstaller
[Python] for statement error