Search code examples
pythonreact-nativeface-recognitionflask-restful

GET Face recognition python script via flask route to react native app


I am creating an application using react native and flask as backend. I have various routes in my app.py and another python file for capturing face and recognition. I need to call this file in app.py, cretae route and GET that route(embed) in my react-native frontend. I tried below:

**This is my app.py file where all my routes are configured.**
from flask_restful import Api
from flask_pymongo import PyMongo
from flask import Flask, request, json, Response
from pymongo import MongoClient
from flask import jsonify
import pymongo
import recognition

@app.route('/candidates', methods=['GET'])
def candidates():
     cand = mongo.db.Candidates
     candidates =[]
     candidate = cand.find()
     for j in candidate:
            j.pop('_id')
            candidates.append(j)
     print(candidates)
     return jsonify(candidates)

@app.route('/getFace', methods=['GET'])
def get_face():
    return recognition

But with this the camera starts immediately and my other route dont work then. Can someone please suggest on how do I achieve this flow?

[![Folder Structure for Project][1]][1]


  [1]: https://i.sstatic.net/Att32.png

Solution

  • I managed to achieve this as follow:
    
    1. Capture face via video/camera from react native and post to flask as file.
    2. Create a python script with opencv face recognition which will accept video/image from frontend and save as frames while returning count of frames and identified face names.
    3.In app.py file create an api to call the script file and check the return value and send response based on the values returned.
    
    import auth.  //auth is the recognition script
    @app.route('/getFace', methods=['POST'])
    def get_face():
      
          frame = request.files['frame']
          
          print(frame)
          face_names = auth.face_rec()
          if(face_names):
              
              message = "FACE FOUND"
              print(message)
              return message
          else:
              message = "FACE NOT FOUND"
              print(message)
              return message