我是一名本科生,我现在使用CoreML框架在iPhone上制作一些视频HumanSeg应用程序,但正如标题所示,我有一个huuuuuge问题。CVPixelBuffer使用UIImage.draw()写作太慢
我有一个UIImage,我必须调整它的大小并将它填充到一个CVPixelBuffer中,以提供MobileNet模型,但是这样的过程只是太慢,耗费大约30ms,这是不可接受的。在我的代码中,方法UIImage.draw(in:CGRect(x:Int,y:Int,width:Int,height:Int))是TOO SLOW,并且花了我20+ ms,这是主要问题。
我的代码如下所示:
func dealRawImage(image : UIImage, dstshape : [Int], pad : UIImage) -> CVPixelBuffer?
{
// decide whether to shrink in height or width
let height = image.size.height
let width = image.size.width
let ratio = width/height
let dst_width = Int(min(CGFloat(dstshape[1]) * ratio, CGFloat(dstshape[0])))
let dst_height = Int(min(CGFloat(dstshape[0])/ratio, CGFloat(dstshape[1])))
let origin = [Int((dstshape[0] - dst_height)/2), Int((dstshape[1] - dst_width)/2)]
// init a pixelBuffer to store the resized & padded image
var pixelBuffer: CVPixelBuffer?
let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue]
CVPixelBufferCreate(kCFAllocatorDefault,
dstshape[1],
dstshape[0],
kCVPixelFormatType_32ARGB,
attrs as CFDictionary,
&pixelBuffer)
// get the pointer of this pixelBuffer
CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer!)
// init a context that contains this pixelBuffer to draw in
let context = CGContext(data: pixelData,
width: dstshape[1],
height: dstshape[0],
bitsPerComponent: 8,
bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer!),
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)!
// push context
UIGraphicsPushContext(context)
context.translateBy(x: 0, y: CGFloat(dstshape[0]))
context.scaleBy(x: 1, y: -1)
pad.draw(in:CGRect(x: 0, y: 0, width: dstshape[1], height: dstshape[0]))
// THIS SINGLE FUNCTION COSTS ME 20+ ms AND IS THE MAJOR ISSUE !
image.draw(in: CGRect(x: origin[1], y: origin[0], width: dst_width, height: dst_height))
UIGraphicsPopContext()
// unlock
CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
return pixelBuffer
}
我只是调用这个函数是这样的:
let input = dealRawImage(image: raw_input_image, dstshape: [224, 224], pad: black_image)
哪里raw_input_image是我从内存中读取的UIImage,dstshape是我想调整形状这个图像,和black_image是一个完全黑色的UIImage用于填充。
我在这个网站上搜索过,但没有发现熟悉的问题。
有没有什么办法可以让这个过程更快并且保存这个项目?我只是不想放弃我的两周工作。
我不确定为什么使用'image.draw()'的行比'pad.draw()'[可能是因为调整大小]慢得多,但是这里有两个问题想到:1)为什么你需要填充你的输入图像? 2)如果你需要速度,为什么你的输入数据在UIImage中? –
噢,我会尝试的一件事是使'image.draw()'使用0,0和全宽和高,并看看是否有更快。 –
谢谢。让我先回答你的问题。 1)我从我的实验室获得训练有素的MoblieNet模型,我的老年人在训练此模型时将黑色填充图像添加到图像中,因此我还需要这样做才能使网络正确运行。 2)如果我需要从iPhone相机或相册中选择图像,我不确定我可以使用什么。我也尝试过使用opencv-swift,但是出现了一些错误,最后我又回到了UIImage。 3)你的意思是使用其他图像格式可能会更快地生成CVPixelBuffer? – AXIHIXA