@yoonit/nativescript-camera
Yoonit Camera 拥有自定义视图,显示前后摄像头的预览层,并检测其中的人脸和读取二维码。
npm i --save @yoonit/nativescript-camera

NativeScript Yoonit Camera

NativeScript Version Downloads

Android iOS MIT license

A NativeScript 插件,用于提供

  • 现代 Android Camera API Camera X
  • 相机预览(前 & 后)
  • PyTorch 集成(Android)
  • 计算机视觉管道
  • 人脸检测、捕获和图像裁剪
  • 理解人脸
  • 帧捕获
  • 捕获定时图像
  • 二维码扫描

更多关于...

插件的核心是本地层。本地层的每个更改都会反映在这里。这个插件,Yoonit Camera,我们可以说是许多 Yoonit 本地库的集合

所有这些本地库都可以独立使用。

目录

安装

npm i -s @yoonit/nativescript-camera  

使用

所有 @yoonit/nativescript-camera 提供的功能都是通过 YoonitCamera 组件访问的,该组件包括相机预览。下面是我们提供的基本使用代码,更多详细信息,请参阅 方法事件Demo Vue

VueJS 插件

main.js

import Vue from 'nativescript-vue'  
import YoonitCamera from '@yoonit/nativescript-camera/vue'

Vue.use(YoonitCamera)

之后,您可以使用 this.$yoo.camera 在整个项目中访问相机对象

Vue 组件

App.vue

<template>
<Page @loaded="onLoaded">
<YoonitCamera
ref="yooCamera"
lens="front"
captureType="face"
imageCapture=true
imageCaptureAmount=10
imageCaptureInterval=500
detectionBox=true
@faceDetected="doFaceDetected"
@imageCaptured="doImageCaptured"
@endCapture="doEndCapture"
@qrCodeContent="doQRCodeContent"
@status="doStatus"
@permissionDenied="doPermissionDenied"
/>
</Page>
</template>

<script>
export default {
data: () => ({}),

methods: {
async onLoaded() {

console.log('[YooCamera] Getting Camera view')
this.$yoo.camera.registerElement(this.$refs.yooCamera)

console.log('[YooCamera] Getting permission')
if (await this.$yoo.camera.requestPermission()) {

console.log('[YooCamera] Permission granted, start preview')
this.$yoo.camera.preview()
}
},

doFaceDetected({
x,
y,
width,
height,
leftEyeOpenProbability,
rightEyeOpenProbability,
smilingProbability,
headEulerAngleX,
headEulerAngleY,
headEulerAngleZ
}) {
console.log(
'[YooCamera] doFaceDetected',
`
x: ${x}
y: ${y}
width: ${width}
height: ${height}
leftEyeOpenProbability: ${leftEyeOpenProbability}
rightEyeOpenProbability: ${rightEyeOpenProbability}
smilingProbability: ${smilingProbability}
headEulerAngleX: ${headEulerAngleX}
headEulerAngleY: ${headEulerAngleY}
headEulerAngleZ: ${headEulerAngleZ}
`

)
if (!x || !y || !width || !height) {
this.imagePath = null
}
},

doImageCaptured({
type,
count,
total,
image: {
path,
source
},
inferences,
darkness,
lightness,
sharpness
}) {
if (total === 0) {
console.log('[YooCamera] doImageCreated', `${type}: [${count}] ${path}`)
this.imageCreated = `${count}`
} else {
console.log('[YooCamera] doImageCreated', `${type}: [${count}] of [${total}] - ${path}`)
this.imageCreated = `${count} de ${total}`
}
console.log('[YooCamera] Mask Pytorch', inferences)

console.log('[YooCamera] Image Quality Darkness:', darkness)
console.log('[YooCamera] Image Quality Lightness', lightness)
console.log('[YooCamera] Image Quality Sharpness', sharpness)
this.imagePath = source
},

doEndCapture() {
console.log('[YooCamera] doEndCapture')
},

doQRCodeContent({ content }) {
console.log('[YooCamera] doQRCodeContent', content)
},

doStatus({ status }) {
console.log('[YooCamera] doStatus', status)
},

doPermissionDenied() {
console.log('[YooCamera] doPermissionDenied')
}
}
}
</script>

API

属性

属性 输入/格式 默认值 描述
lens "front""back" "front" 要使用的相机镜头 "front" 或 "back"。
captureType "none""front""frame""qrcode" "none" 相机的捕获类型。
imageCapture 布尔值 false 启用/禁用保存图像捕获。
imageCaptureAmount 数字 0 图像捕获数量目标。
imageCaptureInterval 数字 1000 图像捕获时间间隔(毫秒)。
imageCaptureWidth "NNpx" "200px" 图像捕获宽度(像素)。
imageCaptureHeight "NNpx" "200px" 图像捕获高度(像素)。
colorEncoding "RGB""YUV" "RGB" 仅限 Android。图像捕获颜色编码类型:"RGB""YUV"
detectionBox 布尔值 false 显示/隐藏人脸检测框。
detectionBoxColor 字符串 #ffffff 设置检测框颜色。
detectionMinSize "NN%" "0%" 要捕获的人脸最小尺寸百分比。
detectionMaxSize "NN%" "100%" 要捕获的人脸最大尺寸百分比。
detectionTopSize "NN%" "100%" 表示百分比。正值放大检测的上侧,负值缩小检测的上侧。使用 detectionBox 来获得可视结果。
detectionRightSize "NN%" "100%" 表示百分比。正值放大检测的右侧,负值缩小检测的右侧。使用 detectionBox 来获得可视结果。
detectionBottomSize "NN%" "100%" 表示百分比。正值放大检测的下侧,负值缩小检测的下侧。使用 detectionBox 来获得可视结果。
detectionLeftSize "NN%" "100%" 表示百分比。正值放大检测的左侧,负值缩小检测的左侧。使用 detectionBox 来获得可视结果。
roi 布尔值 false 启用/禁用兴趣区域的捕获。
roiTopOffset "NN%" "0%" 百分比距离,表示顶部面边界框与相机预览顶部之间的距离。
roiRightOffset "NN%" "0%" 百分比距离,表示右侧面边界框与相机预览右侧之间的距离。
roiBottomOffset "NN%" "0%" 百分比距离,表示底部面边界框与相机预览底部之间的距离。
roiLeftOffset "NN%" "0%" 百分比距离,表示左侧面边界框与相机预览左侧之间的距离。
roiAreaOffset 布尔值 false 启用/禁用兴趣区域区域偏移的显示。
roiAreaOffsetColor 字符串 '#ffffff73' 设置兴趣区域区域偏移的显示颜色。
faceContours 布尔值 false 启用/禁用检测到的面部上的点列表的显示。
faceContoursColor 字符串 '#FFFFFF' 设置面部轮廓颜色。
computerVision (仅限 Android) 布尔值 false 启用/禁用计算机视觉模型。
torch 布尔值 false 启用/禁用设备手电筒。仅适用于 "back" 相机镜头。

方法

功能 参数 有效值 返回类型 描述
requestPermission - - promise 请求用户给予访问相机的权限。
hasPermission - - 布尔值 返回应用程序是否有相机权限。
preview - - void 如果有权限,则开始相机预览。
startCapture type: string
  • "none"
  • "face"
  • "qrcode"
  • "frame"
void 设置捕获类型为 "none"、"face"、"qrcode" 或 "frame"。默认值是 "none"
stopCapture - - void 停止任何类型的捕获。
destroy - - void 销毁预览。
toggleLens - - void 切换相机镜头朝向 "front"/"back"。
setCameraLens lens: string "front""back" void 设置相机使用 "front" 或 "back" 镜头。默认值是 "front"
getLens - - 字符串 返回 "front" 或 "back"。
setImageCapture enable: boolean truefalse void 启用/禁用保存图像捕获。默认值是 false
setImageCaptureAmount amount: Int 任何正 Int void 对于值 0,保存无限图像。当捕获图像数量达到时,触发事件 onEndCapture。默认值是 0
setImageCaptureInterval interval: number 任何正数,表示以毫秒为单位的时间 void 设置图像捕获时间间隔(以毫秒为单位)。
setImageCaptureWidth width: string 值格式必须是 NNpx void 设置图像捕获宽度(以像素为单位)。
setImageCaptureHeight height: string 值格式必须是 NNpx void 设置图像捕获高度(以像素为单位)。
setImageCaptureColorEncoding colorEncoding: string "YUV""RGB" void 仅适用于 android。设置图像捕获颜色编码类型: "RGB""YUV"
setDetectionBox enable: boolean truefalse void 设置显示/隐藏面部检测框。
setDetectionBoxColor color: string 十六进制 void 设置检测框颜色。
setDetectionMinSize percentage: string 值格式必须是 NN% void 设置捕获的面部最小大小百分比。
setDetectionMaxSize percentage: string 值格式必须是 NN% void 设置捕获的面部最大大小百分比。
setDetectionTopSize percentage: string 值格式必须是 NN% void 表示百分比。正值放大检测的上侧,负值缩小检测的上侧。使用 setDetectionBox 来获得可视结果。
setDetectionRightSize percentage: string 值格式必须是 NN% void 表示百分比。正值扩大检测的右侧,负值减小检测的右侧。使用 setDetectionBox 获取可视结果。
setDetectionBottomSize percentage: string 值格式必须是 NN% void 表示百分比。正值扩大检测的底部,负值减小检测的底部。使用 setDetectionBox 获取可视结果。
setDetectionLeftSize percentage: string 值格式必须是 NN% void 表示百分比。正值扩大检测的左侧,负值减小检测的左侧。使用 setDetectionBox 获取可视结果。
setROI enable: boolean truefalse void 启用/禁用人脸兴趣区域捕获。
setROITopOffset percentage: string 值格式必须是 NN% void 百分比距离,表示顶部面边界框与相机预览顶部之间的距离。
setROIRightOffset percentage: string 值格式必须是 NN% void 百分比距离,表示右侧面边界框与相机预览右侧之间的距离。
setROIBottomOffset percentage: string 值格式必须是 NN% void 百分比距离,表示底部面边界框与相机预览底部之间的距离。
setROILeftOffset percentage: string 值格式必须是 NN% void 百分比距离,表示左侧面边界框与相机预览左侧之间的距离。
setROIMinSize percentage: string 值格式必须是 NN% void 设置 ROI 内相关的最小人脸大小。
setROIAreaOffset enable: boolean truefalse void 启用/禁用兴趣区域区域偏移的显示。
setROIAreaOffsetColor color: string 十六进制颜色 void 设置兴趣区域区域偏移的显示颜色。
setFaceContours enable: boolean truefalse void 启用/禁用检测到的面部上的点列表的显示。
setFaceContoursColor color: string 十六进制颜色 void 设置面部轮廓颜色。
setComputerVision (仅限 Android) enable: boolean truefalse void 启用/禁用计算机视觉模型。
setComputerVisionLoadModels (仅限 Android) modelPaths: Array<string> 有效的系统路径文件到 PyTorch 计算机视觉模型 void 设置捕获图像时使用的模型。了解更多,请点击此处
computerVisionClearModels (仅限 Android) - - void 清除使用 setComputerVisionLoadModels 添加的模型。
setTorch enable: boolean truefalse void 启用/禁用设备手电筒。仅适用于 "back" 相机镜头。

事件

事件 参数 描述
imageCaptured { type: string, count: number, total: number, image: object = { path: string, source: any, binary: any }, inferences: [{ ['model name']: model output }], darkness: number, lightness: number, sharpness: number } 必须已启动人脸/帧捕获类型。当人脸图像文件保存时发出。
  • type: "face" 或 "frame"
  • count: 当前索引
  • total: 总创建数
  • image.path: 人脸/帧图像路径
  • image.source: 块文件
  • image.binary: 块文件
  • inferences: 包含模型输出的数组
  • darkness: 图像暗度分类。
  • lightness: 图像亮度分类。
  • sharpness: 图像锐度分类。
    faceDetected { x: number, y: number, width: number, height: number, leftEyeOpenProbability: number, rightEyeOpenProbability: number, smilingProbability: number, headEulerAngleX: number, headEulerAngleY: number, headEulerAngleZ: number } 必须已启动人脸捕获类型。发出人脸分析,如果没有更多人脸检测,所有参数都为 null。
    endCapture - 必须已启动人脸/帧捕获类型。当创建的图像文件数等于设置的图像数时发出(请参阅 setImageCaptureAmount 方法)。
    qrCodeContent { content: string } 必须已启动二维码捕获类型(请参阅 startCapture)。当相机读取 QR 码时发出。
    status { type: 'error'/'message', status: string } 从本地发出消息错误。通常用于调试目的。
    permissionDenied - 尝试 preview 时发出,但没有相机权限。

    人脸分析

    人脸分析是 onFaceDetected 发出的响应。这里指定所有参数。

    属性 类型 描述
    x 数字 人脸在屏幕上的 x 位置。
    y 数字 人脸在屏幕上的 y 位置。
    width 数字 人脸在屏幕上的 width 位置。
    height 数字 人脸在屏幕上的 height 位置。
    leftEyeOpenProbability 数字 左眼睁开概率。
    rightEyeOpenProbability 数字 右眼睁开概率。
    smilingProbability 数字 微笑概率。
    headEulerAngleX 数字 指示垂直头部方向的度数角度。参见 头部运动
    headEulerAngleY 数字 指示水平头部方向的度数角度。参见 头部运动
    headEulerAngleZ 数字 指示倾斜头部方向的度数角度。参见 头部运动

    头部运动

    这里解释上述 gif 和如何达到 "结果"。每个 "运动"(垂直、水平和倾斜)是一个状态,基于指示头部方向的度数角度;

    头部方向 属性 速度v < -36° -36° < 速度v < -12° -12° < 速度v < 12° 12° < 速度v < 36° 36° < 速度v
    垂直 headEulerAngleX 超级向下 向下 前方 向上 超级向上
    水平 headEulerAngleY 超级向左 向左 前方 向右 超级向右
    倾斜 headEulerAngleZ 超级向右 向右 前方 向左 超级向左

    图像质量

    图像质量是根据三个属性:暗度、亮度和锐度进行分类的。结果可在imageCapture事件中获取。让我们看看每个参数的规格

    阈值 分类
    暗度
    暗度 > 0.7 太暗
    暗度 <= 0.7 可接受
    亮度
    亮度 > 0.65 太亮
    亮度 <= 0.65 可接受
    锐度
    锐度 >= 0.1591 模糊
    锐度 < 0.1591 可接受

    消息

    预定义的消息常量由status事件使用。

    消息 描述
    INVALID_MINIMUM_SIZE 人脸/二维码的宽度百分比相对于屏幕宽度小于设置的。
    INVALID_MAXIMUM_SIZE 人脸/二维码的宽度百分比相对于屏幕宽度大于设置的。
    INVALID_OUT_OF_ROI 人脸边界框超出设置的感兴趣区域。

    贡献并使其更好

    克隆仓库,更改所需内容并发送PR。

    对于提交消息,我们使用常规提交

    贡献总是受欢迎的,一些已经做出贡献的人!


    Cyberlabs AI前端团队用❤编写代码