反应本机相机人脸检测

问题描述 投票:0回答:2

使用 react-native-camera 尝试面部识别不起作用。

<RNCamera
faceDetectionMode={RNCamera.Constants.FaceDetection.Mode.fast}
onFacesDetected={(face)=>{
   console.log(face)
 }}
/>

我收到错误

TypeError: cannot read property fast of undefined" triggered by faceDetectionMode={RNCamera.Constants.FaceDetection.Mode.fast}

当我打印出来时

console.log('RNCAMERA',RNCamera.Constants)

{"AutoFocus": {"off": 0, "on": 2},.....,  "FaceDetection": {}, .....}

人脸检测为空。

我正在使用 [电子邮件受保护]

我还按照这些步骤使其正常工作。目前仅测试ios。

pod 'react-native-camera', path: '../node_modules/react-native- camera', subspecs: ['FaceDetectorMLKit']

cd ios && pod install && cd ..

我在 pod install 上遇到的错误是

Unable to find a specification for `MLImage (= 1.0.0-beta1)` depended upon by `MLKitVision`

这是一个错误吗?

react-native face-detection react-native-camera
2个回答
0
投票

请检查React原生人脸检测 在此输入链接描述


-1
投票
//This code will detect and count face

import { View, Text, ActivityIndicator, StyleSheet, DeviceEventEmitter, Alert } from 'react-native'
import React, { useEffect, useRef, useState } from 'react'
import { Camera, useCameraDevices } from 'react-native-vision-camera';
import { showFloatingBubble, hideFloatingBubble, requestPermission, initialize } from "react-native-floating-bubble"
import FaceDetection, {
  FaceDetectorContourMode,
  FaceDetectorLandmarkMode,
} from 'react-native-face-detection';
const App = () => {
  //REf Variables
  const camera = useRef<Camera>(null);

  //Hooks
  const [image, setImage] = useState<string | undefined>();
  const [showCamera, setShowCamera] = useState<boolean>(false);

  //Camera Vars
  const devices = useCameraDevices('wide-angle-camera');
  const device = devices.front;
  const options1 = {
    landmarkMode: FaceDetectorLandmarkMode.ALL,
    contourMode: FaceDetectorContourMode.ALL,
    rotation: 0,
    cameraOrientation: 'landscapeRight',
  };

  const handleInterval = async () => {
    if (camera.current !== null) {
      try {
        const buffer = await camera.current.takePhoto({});
        const faces = await FaceDetection.processImage(buffer.path, options1);
        if (faces.length > 0) {
          alert(`Total No of faces are ${faces.length}`);
          // console.log("Total No of faces are ${faces.length}===============",`Total No of faces are ${faces.length}`)
        }
      } catch (error) {
        console.log('Error in handleInterval:', error);
      }
    }
  };

  useEffect(() => {
    const intervalId = setInterval(handleInterval, 5000);

    return () => clearInterval(intervalId);
  }, []);

  useEffect(() => {

    const getPermissions = async () => {
      try {
        const newCameraPermission = await Camera.requestCameraPermission();
        console.log('permission==============', newCameraPermission);
      } catch (error) {
        console.log('Error in getPermissions:', error);
      }
    };
    requestPermission()
    .then(() => console.log("Permission Granted"))
    .catch(() => console.log("Permission is not granted"))
    
// Initialize bubble manage
initialize()
    .then(() => console.log("Initialized the bubble mange"))
  showFloatingBubble(10, 10)
    .then(() => {      
  
  });
  
  getPermissions();
  DeviceEventEmitter.addListener("floating-bubble-press", (e) => {
    setShowCamera(prevShowCamera => !prevShowCamera)
  //   Alert.alert(
  //     'Alert Title',
  //     'alertMessage',
  // )
  //  console.log("camera===============","I am pressed")
  });

  }, []);


  if (device == null) return <ActivityIndicator />;

  return (
    <>
      {showCamera && 
      <Camera
        style={StyleSheet.absoluteFill}
        device={device}
        ref={camera}
        isActive={true}
        photo={true}
      />
      }
    </>
  )
}

export default App
© www.soinside.com 2019 - 2024. All rights reserved.