openpose

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

README

Golang lib for pose detection using tensorflow openpose

Go Reference Go goreleaser GitHub go.mod Go version of a Go module GoReportCard GitHub license GitHub release

Prerequest

  1. libtensorfow 1.x Follow the instruction Install TensorFlow for C
  2. download tenorflow model graph

Demo

demo screen capture

Install

go get -u github.com/bububa/openpose

Camera & Server

Requirements
  • libjpeg-turbo (use -tags jpeg to build without CGo)
  • On Linux/RPi native Go V4L implementation is used to capture images.
Use Opencv4
make cvcamera
On linux/Pi
# use native Go V4L implementation is used to capture images
make linux_camera
Use image/jpeg instead of libjpeg-turbo

use jpeg build tag to build with native Go image/jpeg instead of libjpeg-turbo

go build -o=./bin/cvcamera -tags=cv4,jpeg ./cmd/camera
Usage as Server
Usage of camera:
  -bind string
	Bind address (default ":56000")
  -delay int
	Delay between frames, in milliseconds (default 10)
  -width float
	Frame width (default 640)
  -height float
	Frame height (default 480)
  -index int
	Camera index
  -model string
    mode path

User as lib

import (
    "github.com/bububa/openpose"
)

func main() {
	t := openpose.NewPoseEstimator(modelPath, openpose.MobileNet)
	wd, _ := os.Getwd()
	img, err := loadImage("./golf.jpg")
	if err != nil {
		log.Fatalln(err)
	}
	modelSize := openpose.ModelSizeFaster
	sharpenSigma := 0.0
	t.SetSharpenSigma(sharpenSigma)
	humans, err := t.Estimate(img, modelSize)
	if err != nil {
		log.Fatalln(err)
	}
	outImg := openpose.DrawHumans(img, humans, 2)
    saveImage(outImg, "./out/jpg")
}

Documentation

Index

Constants

View Source
const (
	ThresholdPartConfidence float32 = 0.3
	InterThreashold         float32 = 0.1
	InterMinAboveThreshold  int     = 6
	NMS_Threshold           float64 = 0.1
	MinSubsetCnt            int     = 4
	MinSubsetScore          float32 = 0.8
	ThresholdHumanScore     float32 = 0.4
)
View Source
const (
	DefaultSharpenSigma float64 = 0.0
)
View Source
const TotalBodyParts = 18

Variables

View Source
var CocoColors = [][3]uint8{
	{255, 0, 0}, {255, 85, 0}, {255, 170, 0}, {255, 255, 0}, {170, 255, 0}, {85, 255, 0}, {0, 255, 0}, {0, 255, 85}, {0, 255, 170}, {0, 255, 255}, {0, 170, 255}, {0, 85, 255}, {0, 0, 255}, {85, 0, 255}, {170, 0, 255}, {255, 0, 255}, {255, 0, 170}, {255, 0, 85},
}

CocoColors represents color for coco parts

CocoPairs coco part pairs

View Source
var CocoPairsNetwork = [][2]CocoPart{
	{12, 13}, {20, 21}, {14, 15}, {16, 17}, {22, 23}, {24, 25}, {0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}, {10, 11}, {28, 29}, {30, 31}, {34, 35}, {32, 33}, {36, 37}, {18, 19}, {26, 27},
}

CocoPairNetwork .

View Source
var CocoPairsRender = CocoPairs[0 : len(CocoPairs)-2]

CocoPairsRender represents coco pairs for render

CoordParts represents CocoParts for coordinate

CoordPartsMap represents CocoParts for coordinate in map

PartPairs represents MPIIPart, CocoPart pair list

View Source
var ZP = Point{0, 0}

ZP represents zero position point

View Source
var ZR = Rect(0, 0, 0, 0)

ZR represents zero size rectangle

View Source
var ZS = ASize(0, 0)

ZS returns a new Size with zero

Functions

func BodyPartID

func BodyPartID(humanID int, part CocoPart) string

BodyPartID returns a BodyPart.ID with given CocoPart and index

func ColorFromHex

func ColorFromHex(hexColor string) color.RGBA

ColorFromHex get color from hex

func DrawHumans

func DrawHumans(img image.Image, humans []Human, strokeWidth float64) image.Image

func RoundInt

func RoundInt(v float64) int

RoundInt returns int round

Types

type BodyPart

type BodyPart struct {
	// Part CocoPart
	Part CocoPart
	// Point coordinate of body part
	Point Point
	// Score confidence score
	Score float32
}

BodyPart represents body part

func NewBodyPart

func NewBodyPart(part CocoPart, point Point, score float32) BodyPart

NewBodyPart returns a new BodyPart

func (BodyPart) String

func (b BodyPart) String() string

String return BodyPart string representation

type BodyPartPair

type BodyPartPair struct {
	Parts  [2]CocoPart
	Points [2]Point
	Score  float64
}

BodyPartPair ...

type CocoPart

type CocoPart int

CocoPart represents body parts

const (
	// CocoPartNose nose
	CocoPartNose CocoPart = iota
	// CocoPartNeck neck
	CocoPartNeck
	// CocoPartRShoulder right sholder
	CocoPartRShoulder
	// CocoPartRElbow right elbow
	CocoPartRElbow
	// CocoPartRWrist right wrist
	CocoPartRWrist
	// CocoPartLShoulder left sholder
	CocoPartLShoulder
	// CocoPartLElbow left elbow
	CocoPartLElbow
	// CocoPartLWrist left wrist
	CocoPartLWrist
	// CocoPartRHip right hip
	CocoPartRHip
	// CocoPartRKnee right knee
	CocoPartRKnee
	// CocoPartRAnkle right ankle
	CocoPartRAnkle
	// CocoPartLHip left hip
	CocoPartLHip
	// CocoPartLKnee left knee
	CocoPartLKnee
	// CocoPartLAnkle left ankle
	CocoPartLAnkle
	// CocoPartREye right eye
	CocoPartREye
	// CocoPartLEye left eye
	CocoPartLEye
	// CocoPartREar right ear
	CocoPartREar
	// CocoPartLEar left ear
	CocoPartLEar
	// CocoPartBackground background
	CocoPartBackground
)

type Connection

type Connection struct {
	Score       float32
	Coords      [2]image.Point
	Idx         [2]int
	Parts       [2]CocoPart
	Scores      [2]float32
	NormPadding Size
	UPartIdx    map[string]struct{}
}

Connection represents coco part connection

func (Connection) GetUPartIdx

func (c Connection) GetUPartIdx() map[string]struct{}

GetUPartIdx generate uniq partidx string for merge connections

func (Connection) ToBodyParts

func (c Connection) ToBodyParts(parts *[2]BodyPart, rows float64, cols float64)

ToBodyPart returns BodyParts based on Connection

type Human

type Human struct {
	Parts map[CocoPart]BodyPart
	Score float32
}

Human represents human structure

func NewHuman

func NewHuman() *Human

NewHuman returns a new Human with given BodyPartPairs

func (Human) GetFaceBox

func (h Human) GetFaceBox(imgW float64, imgH float64, mode int) Rectangle

GetFaceBox returns upper body box compared to img size (w, h)

func (Human) GetMaxScore

func (h Human) GetMaxScore() float32

GetMaxScore returns the max score of body parts

func (Human) GetUpperBodyBox

func (h Human) GetUpperBodyBox(imgW float64, imgH float64) Rectangle

GetUpperBodyBox returns upper body box compared to img size (w, h)

func (Human) HasPart

func (h Human) HasPart(part CocoPart) bool

HasPart returns specific part exists in humnan or not

func (Human) PartCount

func (h Human) PartCount() int

PartCount returns total number of body parts

func (*Human) Reset

func (h *Human) Reset()

Reset reset human

type MPIIPart

type MPIIPart int

MPIIPart MPII human parts

const (
	// MPIIPartRAnkle right ankle
	MPIIPartRAnkle MPIIPart = iota
	// MPIIPartRKnee right knee
	MPIIPartRKnee

	MPIIPartRHip
	// MPIIPartLHip left hip
	MPIIPartLHip
	// MPIIPartLKnee left knee
	MPIIPartLKnee
	// MPIIPartLAnkle left ankle
	MPIIPartLAnkle
	// MPIIPartRWrist right wrist
	MPIIPartRWrist
	// MPIIPartRElbow right elbow
	MPIIPartRElbow
	// MPIIPartRShoulder right sholder
	MPIIPartRShoulder
	// MPIIPartLShoulder left sholder
	MPIIPartLShoulder
	// MPIIPartLElbow left elbow
	MPIIPartLElbow
	// MPIIPartLWrist left wrist
	MPIIPartLWrist
	// MPIIPartNeck neck
	MPIIPartNeck
	// MPIIPartHead head
	MPIIPartHead
)

type ModelSize

type ModelSize [2]int

ModelSize size for training image

var (
	ModelSizeBest    ModelSize = [2]int{1312, 736}
	ModelSizeBetter  ModelSize = [2]int{656, 368}
	ModelSizeCMU     ModelSize = [2]int{640, 360}
	ModelSizeDefault ModelSize = [2]int{432, 368}
	ModelSizeFaster  ModelSize = [2]int{336, 288}
	ModelSizeFatest  ModelSize = [2]int{304, 240}
)

type ModelType

type ModelType int

ModelType represents type of mode graph

const (
	// CMU cmu model graph
	CMU ModelType = iota
	// MobileNetmodelnet model graph
	MobileNet
)

type PartPair

type PartPair struct {
	MPIIPart MPIIPart
	CocoPart CocoPart
}

PartPair represents pose part MPIIPart, CocoPart pair

type Point

type Point struct {
	// X coordinate of body part
	X float64
	// Y coordinate of body part
	Y float64
}

Point represents coordinate

func BodyPartsPoints

func BodyPartsPoints(parts []BodyPart) ([]Point, []bool)

BodyPartsPoints return body part points

func Pt

func Pt(x float64, y float64) Point

Pt returns a new Point

func (Point) IsZero

func (p Point) IsZero() bool

IsZero check if the Point is zero

type PoseEstimator

type PoseEstimator struct {
	// contains filtered or unexported fields
}

PoseEstimator represents pose estimator instance

func NewPoseEstimator

func NewPoseEstimator(modelPath string, modelType ModelType) *PoseEstimator

NewPoseEstimator returns a new TensorFlow PoseEstimator instance.

func (*PoseEstimator) Estimate

func (t *PoseEstimator) Estimate(img image.Image, modelSize ModelSize) ([]Human, error)

Estimate returns estimated Humans in an image

func (*PoseEstimator) LoadModel

func (t *PoseEstimator) LoadModel() error

LoadModel load tensorfow model

func (*PoseEstimator) ModelLoaded

func (t *PoseEstimator) ModelLoaded() bool

ModelLoaded tests if the TensorFlow model is loaded.

func (*PoseEstimator) SetSharpenSigma

func (t *PoseEstimator) SetSharpenSigma(sigma float64)

SetSharpenSigma set sharpen sigma for image preprocessing

type Rectangle

type Rectangle struct {
	X int
	Y int
	W int
	H int
}

Rectangle represents rectangle

func Rect

func Rect(x, y, w, h int) Rectangle

Rect returns a new Rectangle

func (Rectangle) Area

func (r Rectangle) Area() int

Area returns rectangle area size

func (Rectangle) Intersect

func (r Rectangle) Intersect(s Rectangle) Rectangle

Intersect returns the largest rectangle contained by both r and s. If the two rectangles do not overlap then the zero rectangle will be returned.

type Size

type Size struct {
	W float64
	H float64
}

Size represents Size

func ASize

func ASize(w, h float64) Size

ASize returns a Size

func ImagePreprocess

func ImagePreprocess(img image.Image, modelSize ModelSize, sharpenSigma float64) (image.Image, Size)

ImagePreprocess preprocess image for model

func (Size) IsZero

func (s Size) IsZero() bool

IsZero check if the Size is zero

Directories

Path Synopsis
cmd
camera/server
Package server implement server
Package server implement server
camera/server/handlers
Package handlers include server handlers
Package handlers include server handlers
Package combinations a package with combinations functions from python itertools
Package combinations a package with combinations functions from python itertools
Package gaussian gaussian filter for mat
Package gaussian gaussian filter for mat

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL