Skip to content

Commit

Permalink
Merge pull request #42 from tucan9389/multi-pose
Browse files Browse the repository at this point in the history
[PR] Implement building bipartite graph, calculate cost values of each edge, assign and make humans
  • Loading branch information
tucan9389 committed May 1, 2020
2 parents d84aa66 + 32521ae commit 98d29ba
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 78 deletions.
2 changes: 1 addition & 1 deletion PoseEstimation-TFLiteSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@
712A7FC02424BDD800B043F9 /* StillImageLineViewController.swift */,
71A1ED4024574F2E001F796C /* StillImageHeatmapViewController.swift */,
7105C919241CE9B6001A4325 /* LiveImageViewController.swift */,
7105C929241D011F001A4325 /* View */,
7105C92B241D0150001A4325 /* MLModel */,
71DD577D2446D7A40024C146 /* Algorithm */,
7105C92A241D0144001A4325 /* Extension */,
7105C929241D011F001A4325 /* View */,
7105C938241D29C5001A4325 /* Video */,
7105C91E241CE9B7001A4325 /* Assets.xcassets */,
7105C920241CE9B7001A4325 /* LaunchScreen.storyboard */,
Expand Down
11 changes: 6 additions & 5 deletions PoseEstimation-TFLiteSwift/NonMaximumnonSuppression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import Foundation

class NonMaximumnonSuppression {
typealias MaximumPoint = (row: Int, col: Int, val: Float32)
typealias MaximumPoint = (col: Int, row: Int, val: Float32)

static func process(_ heatmap: TFLiteFlatArray<Float32>, partIndex: Int, width: Int, height: Int) -> [MaximumPoint] {
let filterSize = 3
var lastMaximumColumns: [MaximumPoint?] = (0..<width).map { _ in nil }
var results: [MaximumPoint] = []
results.reserveCapacity(20)

for row in (0..<height) {
for col in (0..<width) {
Expand Down Expand Up @@ -48,8 +49,8 @@ class NonMaximumnonSuppression {
lastMaximumColumns[targetColumn] = nil
}
}
results.append((row: lastMaximumPoint.row,
col: lastMaximumPoint.col,
results.append((col: lastMaximumPoint.col,
row: lastMaximumPoint.row,
val: lastMaximumPoint.val))
}
}
Expand All @@ -66,8 +67,8 @@ class NonMaximumnonSuppression {
lastMaximumColumns[targetColumn] = nil
}
}
results.append((row: lastMaximumPoint.row,
col: lastMaximumPoint.col,
results.append((col: lastMaximumPoint.col,
row: lastMaximumPoint.row,
val: lastMaximumPoint.val))
}

Expand Down
363 changes: 303 additions & 60 deletions PoseEstimation-TFLiteSwift/OpenPose/OpenPosePoseEstimator.swift

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions PoseEstimation-TFLiteSwift/PoseConfidenceMapDrawingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ class PoseConfidenceMapDrawingView: UIView {
var componentOfVector = outputChannelIndexes.reduce(0.0) { value, outputChannelIndex in
return value + output[0, row, col, outputChannelIndex]
}
componentOfVector = min(max(componentOfVector, -1.0), 1.0)
componentOfVector = min(max(componentOfVector, -1.0), 1.0) // -1.0 ~ 1.0
let drawingAreaRect = CGRect(x: oneAreaWidth*CGFloat(col), y: oneAreaHeight*CGFloat(row),
width: oneAreaWidth, height: oneAreaHeight)
drawRect(with: drawingAreaRect, componentOfVector: componentOfVector)
let areaFillColor = DrawingConstant.Area.areaColor(CGFloat(componentOfVector))
drawRect(with: drawingAreaRect, fillColor: areaFillColor)
}
}
}

func drawRect(with rect: CGRect, componentOfVector: Float32 = 0.0) {
func drawRect(with rect: CGRect, fillColor: UIColor) {
guard let startingPoint = rect.points.first else { return }
let rectPath = UIBezierPath()
rectPath.move(to: startingPoint)
Expand All @@ -51,7 +52,7 @@ class PoseConfidenceMapDrawingView: UIView {
rectPath.lineWidth = DrawingConstant.Line.width
DrawingConstant.Area.lineColor.setStroke()
rectPath.stroke()
DrawingConstant.Area.areaColor(CGFloat(componentOfVector)).setFill()
fillColor.setFill()
rectPath.fill()
}
}
Expand Down
16 changes: 16 additions & 0 deletions PoseEstimation-TFLiteSwift/PoseEstimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ struct Keypoint {
let score: Float
}

struct KeypointElement: Equatable {
let col: Int
let row: Int
let val: Float32

init(element: (col: Int, row: Int, val: Float32)) {
col = element.col
row = element.row
val = element.val
}

static func == (lhs: KeypointElement, rhs: KeypointElement) -> Bool {
return lhs.col == rhs.col && lhs.row == rhs.row
}
}

struct PoseEstimationOutput {

var outputs: [TFLiteFlatArray<Float32>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ class StillImageHeatmapViewController: UIViewController {
}

@IBAction func didChangePAF(_ sender: Any) {
updateHeatmapOverlayView()
if case .paf = selectedChannel {
updateHeatmapOverlayView()
} else {
guard let allButton = pairButtons?.first else { return }
selectChannel(allButton)
}
}

@objc func selectChannel(_ button: UIButton) {
Expand All @@ -148,11 +153,13 @@ class StillImageHeatmapViewController: UIViewController {
updateChannelButton(with: partName, on: partButtons)
updateChannelButton(with: nil, on: pairButtons)
selectedChannel = .confidenceMap(part: partName)
UIView.animate(withDuration: 0.23) { self.pafSegment?.alpha = 0.5 }
} else if pairButtons?.contains(button) == true {
let pairName = buttonTitle.components(separatedBy: "(").first ?? buttonTitle
updateChannelButton(with: nil, on: partButtons)
updateChannelButton(with: pairName, on: pairButtons)
selectedChannel = .paf(pair: pairName)
UIView.animate(withDuration: 0.23) { self.pafSegment?.alpha = 1.0 }
}

updateHeatmapOverlayView()
Expand Down
9 changes: 2 additions & 7 deletions PoseEstimation-TFLiteSwift/StillImageLineViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class StillImageLineViewController: UIViewController {
button.addTarget(self, action: #selector(selectPart), for: .touchUpInside)
}

threshold = 0.1
thresholdSlider?.isContinuous = false // `changeThreshold` will be called when touch up on slider
threshold = 0.1 // initial threshold for part (not for pair)
}

func updatePartButton(on targetPartName: String) {
Expand All @@ -109,12 +110,6 @@ class StillImageLineViewController: UIViewController {

@objc func selectPart(_ button: UIButton) {
guard let partName = button.title(for: .normal) else { return }
// guard partName != "ALL" else {
// let alert = UIAlertController(title: "Error", message: "Not support 'ALL' case on multi pose estimation", preferredStyle: .alert)
// alert.addAction(UIAlertAction(title: "Ok", style: .cancel))
// present(alert, animated: true)
// return
// }

select(on: partName)
if let image = imageView?.image {
Expand Down

0 comments on commit 98d29ba

Please sign in to comment.