項目上一直需要人眼跟蹤功能,目前用的是開源的neven狈涮,比較老了狐胎,效果不好。
考慮使用neven做人臉檢測歌馍,加一個cnn網(wǎng)絡(luò)做人臉對齊握巢。
使用ncnn框架,需要將ios camera輸出的CMSampleBufferRef轉(zhuǎn)化成Mat松却。
neven輸入為灰度圖像镜粤,而cnn輸入為rgb彩色圖像,camera只能輸出一種模式玻褪,rgba或者yuv420肉渴。
考慮到bgra轉(zhuǎn)gray計算量小于yuv轉(zhuǎn)bgr,camera優(yōu)先輸出bgra
在原有項目上修改的代碼带射,在camera的相關(guān)類里面同规,怎么設(shè)置輸出bgra都不行,后來才發(fā)現(xiàn)代碼不規(guī)范在啟動預(yù)覽前設(shè)置成yuv420.
yuv420轉(zhuǎn)cv::Mat
-(int)ProcessWithSampleBufferRef:(CMSampleBufferRef)sampleBufferRef{
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBufferRef);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
uint8_t yuv = (uint8_t)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
int wid = (int)CVPixelBufferGetWidth(pixelBuffer);
int hei = (int)CVPixelBufferGetHeight(pixelBuffer);
cv::Mat img = cv::Mat(hei*1.5, wid, CV_8UC1, yuv);
cv::Mat img2;
cv::cvtColor(img, img2, cv::COLOR_YUV420sp2RGB);
CVPixelBufferUnlockBaseAddress(pixelBuffer,0);
return 0;
}
bgra轉(zhuǎn)cv::Mat:
uint8_t bgra = (uint8_t)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
cv::Mat img = cv::Mat(hei, wid, CV_8UC4, bgra);