相机人脸检测(FACE_DETECTION)

相机人脸检测(FACE_DETECTION)

HarmonyOS
2024-05-20 21:43:47
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
小小肉蟹

功能场景描述及使用场景

相机人脸检测(FACE_DETECTION)

元数据(Metadata)是对相机返回的图像信息数据的描述和上下文,针对图像信息,提供的更详细的数据,如照片或视频中,识别人像的取景框坐标等信息。

Metadata主要是通过一个TAG(Key),去找对应的Data,用于传递参数和配置信息,减少内存拷贝操作。

使用的核心API

import camera from '@ohos.multimedia.camera';import { BusinessError } from '@ohos.base';

核心代码解释

this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:1920,surfaceHeight:1080}); 
// 获取Surface ID 
this.surfaceId = this.mXComponentController.getXComponentSurfaceId(); 
if (this.cameraManager == null) { 
  this.res = "Get Camera failed" 
  return; 
} 
 
let cameraDevice:camera.CameraDevice|null = this.getFrontCameraDevices(this.cameraManager); 
this.getSupportedOutputCapability(cameraDevice,this.cameraManager).then((outputCapability:camera.CameraOutputCapability|undefined) => { 
  if(this.previewOt===undefined){ 
    this.previewOt = this.getPreviewOutput(this.cameraManager,outputCapability,this.surfaceId); 
  } 
 
  if(this.previewOt===undefined) { 
    return; 
  } 
 
  this.previewOt.on('frameStart', () => { 
    console.info('Preview frame started'); 
  }); 
  this.previewOt.on('frameEnd', () => { 
    console.info('Preview frame ended'); 
  }); 
  this.previewOt.on('error', (previewOutputError: BusinessError) => { 
    console.info(`Preview output error code: ${previewOutputError.code}`); 
  }); 
  this.getCaptureSession(this.cameraManager).then(()=>{ 
    this.startPreviewOutput(this.previewOt); 
  }).catch((error:BusinessError) =>{ 
    console.error("Failed to get getSupportedOutputCapability. error code: " + error.code); 
  }); 
 
}).catch((error:BusinessError) =>{ 
  console.error("Failed to get getSupportedOutputCapability. error code: " + error.code); 
}); 
 
}) 
.width('1080px') 
  .height('1920px') 
  .margin({left:40}) 
 
} 
} 
 
getPreviewOutput(cameraManager:camera.CameraManager|null, cameraOutputCapability: camera.CameraOutputCapability|undefined, surfaceId: string): camera.PreviewOutput | undefined { 
  let previewOutput: camera.PreviewOutput | undefined = undefined; 
 
  if((cameraManager == null) 
    ||(cameraOutputCapability === undefined)){ 
    return undefined; 
  } 
 
  let previewProfilesArray: Array<camera.Profile> = cameraOutputCapability.previewProfiles; 
 
  try { 
    previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId); 
  } catch (error) { 
    let err = error as BusinessError; 
    console.error("Failed to create the PreviewOutput instance. error code: " + err.code); 
  } 
  return previewOutput; 
} 
 
getCameraManager(context:Context):camera.CameraManager{ 
  let cameraManager:camera.CameraManager = camera.getCameraManager(context); 
  return cameraManager; 
} 
startPreviewOutput(previewOutput: camera.PreviewOutput|undefined): void { 
  if(previewOutput === undefined) { 
  return; 
} 
previewOutput.start().then(() => { 
  console.info('Callback returned with previewOutput started.'); 
}).catch((err: BusinessError) => { 
  console.info('Failed to previewOutput start '+ err.code); 
}); 
} 
 
getFrontCameraDevices(cameraManager: camera.CameraManager): camera.CameraDevice|null { 
  let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras(); 
  if (cameraArray != undefined && cameraArray.length <= 0) { 
    console.error("cameraManager.getSupportedCameras error"); 
    return null; 
  } 
  for (let index = 0; index < cameraArray.length; index++) { 
    console.info('cameraId : ' + cameraArray[index].cameraId);  // 获取相机ID 
    console.info('cameraPosition : ' + cameraArray[index].cameraPosition);  // 获取相机位置 
    console.info('cameraType : ' + cameraArray[index].cameraType);  // 获取相机类型 
    console.info('connectionType : ' + cameraArray[index].connectionType);  // 获取相机连接类型 
    if((cameraArray[index].cameraPosition == camera.CameraPosition.CAMERA_POSITION_FRONT)&& 
      (cameraArray[index].connectionType == camera.ConnectionType.CAMERA_CONNECTION_BUILT_IN)){ 
      return cameraArray[index]; 
    } 
  } 
 
  return null; 
 
} 
 
async getSupportedOutputCapability(cameraDevice: camera.CameraDevice|null, cameraManager: camera.CameraManager): Promise<camera.CameraOutputCapability | undefined> { 
  if(cameraDevice == null){ 
  return; 
} 
// 创建相机输入流 
try { 
  if(this.cameraInput === undefined){ 
    this.cameraInput = cameraManager.createCameraInput(cameraDevice); 
  } 
} catch (error) { 
  let err = error as BusinessError; 
  console.error('Failed to createCameraInput errorCode = ' + err.code); 
} 
if (this.cameraInput === undefined) { 
  return undefined; 
} 
// 监听cameraInput错误信息 
this.cameraInput.on('error', cameraDevice, (error: BusinessError) => { 
  console.info(`Camera input error code: ${error.code}`); 
}); 
// 打开相机 
await this.cameraInput.open(); 
// 获取相机设备支持的输出流能力 
this.cameraOutputCapability  = cameraManager.getSupportedOutputCapability(cameraDevice); 
if (!this.cameraOutputCapability) { 
  console.error("cameraManager.getSupportedOutputCapability error"); 
  return undefined; 
} 
console.info("outputCapability: " + JSON.stringify(this.cameraOutputCapability)); 
return this.cameraOutputCapability; 
} 
 
 
async getCaptureSession(cameraManager: camera.CameraManager|null): Promise<void> { 
  this.startMetadataOutput(cameraManager) 
  if((this.captureSession != undefined)||(cameraManager == null)){ 
  return; 
} 
try { 
  this.captureSession = cameraManager.createCaptureSession(); 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to create the CaptureSession instance. error: ${JSON.stringify(err)}`); 
} 
 
if(this.captureSession === undefined){ 
  return; 
} 
 
try { 
  this.captureSession.beginConfig(); 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to beginConfig. error: ${JSON.stringify(err)}`); 
} 
 
try { 
  if(this.cameraInput != undefined){ 
    this.captureSession.addInput(this.cameraInput); 
  } 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to addInput. error: ${JSON.stringify(err)}`); 
} 
 
try { 
  this.captureSession.addOutput(this.previewOt); 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to add previewOutput. error: ${JSON.stringify(err)}`); 
} 
 
try { 
  this.captureSession.addOutput(this.metadataOutput); 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to add metadataOutput. error: ${JSON.stringify(err)}`); 
} 
 
try { 
  await this.captureSession.commitConfig(); 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to commitConfig. error: ${JSON.stringify(err)}`); 
} 
 
try { 
  await this.captureSession.start() 
} catch (error) { 
  let err = error as BusinessError; 
  console.error(`Failed to start. error: ${JSON.stringify(err)}`); 
} 
} 
 
startMetadataOutput(cameraManager: camera.CameraManager|null): void { 
  if (this.cameraOutputCapability === undefined){ 
  return; 
} 
if(cameraManager == null) { 
  return; 
} 
let metadataObjectTypes: Array<camera.MetadataObjectType> = this.cameraOutputCapability.supportedMetadataObjectTypes; 
try { 
  this.metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes); 
} catch (error) { 
  let err = error as BusinessError; 
  console.info('Failed to createMetadataOutput, error code: '+ err.code); 
} 
if(this.metadataOutput === undefined){ 
  return; 
} 
this.metadataOutput.start().then(() => { 
  console.info('Callback returned with metadataOutput started.'); 
}).catch((err: BusinessError) => { 
  console.info('Failed to metadataOutput start, error code: '+ err.code); 
}); 
this.metadataOutput.on('metadataObjectsAvailable', (err: BusinessError, metadataObjectArr: Array<camera.MetadataObject>) => { 
  console.info(`metadata output metadataObjectsAvailable`); 
  this.res="Face Detected"; 
  // this.stopMetadataOutput(); 
  console.info("metadataObjectArr topLeftX",metadataObjectArr[0].boundingBox.topLeftX) 
  console.info("metadataObjectArr topLeftY",metadataObjectArr[0].boundingBox.topLeftY) 
  console.info("metadataObjectArr width",metadataObjectArr[0].boundingBox.width) 
  console.info("metadataObjectArr height",metadataObjectArr[0].boundingBox.height) 
}); 
this.metadataOutput.on('error', (metadataOutputError: BusinessError) => { 
  console.info(`Metadata output error code: ${metadataOutputError.code}`); 
}); 
 
} 
 
stopMetadataOutput(): void { 
  if(this.metadataOutput === undefined){ 
  return; 
} 
this.metadataOutput.stop().then(() => { 
  console.info('Callback returned with metadataOutput stopped.'); 
 
}).catch((err: BusinessError) => { 
  console.info('Failed to metadataOutput stop '+ err.code); 
}); 
} 
aboutToDisappear(){ 
  if(this.captureSession != undefined){ 
    this.captureSession.release().then(()=>{ 
      console.error("release session"); 
    }) 
  } 
 
  if(this.previewOt != undefined){ 
    this.previewOt.release().then(()=>{ 
      console.error("release previewOt"); 
    }) 
  } 
 
  if(this.cameraInput != undefined) { 
    this.cameraInput.close().then(()=>{ 
      console.error("release cameraInput"); 
    }) 
  } 
 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.

注明适配的版本信息

本示例为Stage模型,支持API version 10。

本示例需要使用DevEco Studio 4.0 Release版本进行编译运行。

分享
微博
QQ
微信
回复
2024-05-22 15:31:42
相关问题
HarmonyOS 人脸检测
811浏览 • 1回复 待解决
HarmonyOS 人脸活体检测调用
921浏览 • 1回复 待解决
HarmonyOS 人脸活体检测问题
914浏览 • 1回复 待解决
HarmonyOS 人脸活体检测Vision Kit
993浏览 • 1回复 待解决
HarmonyOS 相机人脸监听的相关问题
905浏览 • 1回复 待解决
HarmonyOS 是否有人脸活体检测API支持
675浏览 • 1回复 待解决
如何检测相机的可用状态?
761浏览 • 0回复 待解决
如何检测当前相机服务的状态
3127浏览 • 1回复 待解决
HarmonyOS人脸活体认证
922浏览 • 1回复 待解决
HarmonyOS FiDO人脸认证失败
886浏览 • 1回复 待解决