我想通过使用视觉检测面部点来绘制面部的矢量图像,我已经创建了一个代码,其中我得到了一些点,我已经下载了代码来绘制线条,但我无法理解它并自定义线条。请帮助我理解代码,如果有人可以帮助我如何使用我拥有的人脸检测代码在脸上画点,我的人脸检测代码是:
func drawOnImage(source: UIImage,
boundingRect: CGRect,
faceLandmarkRegions: [VNFaceLandmarkRegion2D]) -> UIImage {
UIGraphicsBeginImageContextWithOptions(source.size, false, 1)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: source.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(CGBlendMode.colorBurn)
context.setLineJoin(.round)
context.setLineCap(.round)
context.setShouldAntialias(true)
context.setAllowsAntialiasing(true)
let rectWidth = source.size.width * boundingRect.size.width
let rectHeight = source.size.height * boundingRect.size.height
//draw image
let rect = CGRect(x: 0, y:0, width: source.size.width, height: source.size.height)
context.draw(source.cgImage!, in: rect)
//draw bound rect
var fillColor = UIColor.green
fillColor.setFill()
context.addRect(CGRect(x: boundingRect.origin.x * source.size.width, y:boundingRect.origin.y * source.size.height, width: rectWidth, height: rectHeight))
context.drawPath(using: CGPathDrawingMode.stroke)
//draw overlay
fillColor = UIColor.red
fillColor.setStroke()
context.setLineWidth(2.0)
for faceLandmarkRegion in faceLandmarkRegions {
var points: [CGPoint] = []
for i in 0..<faceLandmarkRegion.pointCount {
let point = faceLandmarkRegion.normalizedPoints[i]
let p = CGPoint(x: CGFloat(point.x), y: CGFloat(point.y))
points.append(p)
}
let mappedPoints = points.map { CGPoint(x: boundingRect.origin.x * source.size.width + $0.x * rectWidth, y: boundingRect.origin.y * source.size.height + $0.y * rectHeight) }
context.addLines(between: mappedPoints)
context.drawPath(using: CGPathDrawingMode.stroke)
}
let coloredImg : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return coloredImg
}
字符串
我想画一个矢量图,在脸上画点和线
1条答案
按热度按时间omjgkv6w1#
伙计们,我已经实现了我想要的输出使用ARKit提供的面罩功能。我现在可以在检测到的脸上添加面具我在这里分享代码。
字符串
在类中声明属性
型
将下面的代码放入类的**ViewDidLoad()**方法中
型
在您的类中添加以下两个Delegate方法,并从ARSCNViewDelegate继承您的类
型
现在,当您运行代码时,您将能够看到检测到的面部上有一个矢量线的面部遮罩