1

this is my code and I want to optimize the performance how can I do this stuff parallel thread in python? any idea? i am useing this models :
"DEEPFACE_MODELS": "VGG-Face;DeepFace", "DEEPFACE_BACKENDS": "opencv;ssd;dlib" any suggestion about optimizing performance would be great

import os
from logging import getLogger
from os import listdir

from deepface import DeepFace
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from project import settings
from project.apps.recognition.serializers import RecognizeSerializer
from project.settings import VALUES

logger = getLogger(__name__)


def build_models():
    model_names = VALUES["DEEPFACE_MODELS"].split(';')
    backends = VALUES["DEEPFACE_BACKENDS"].split(';')
    metric = VALUES["DEEPFACE_METRIC"]
    models = {}
    for model_name in model_names:
        models[model_name] = DeepFace.build_model(model_name)
    return models, backends, metric


models, backends, metric = build_models()


class RecognitionAPI(APIView):
    def post(self, request, *args, **kwargs):
        serializer = RecognizeSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        validated_data = serializer.validated_data
        filename = serializer.save()
        user_directory = os.path.join(settings.MEDIA_ROOT, validated_data.get('user_id'))
        pairs = []
        for user_pic in listdir(user_directory):
            pic_path = os.path.join(user_directory, user_pic)
            if os.path.exists(pic_path):
                pairs.append([filename, pic_path])

        number_of_recognition = 0
        number_of_not_recognition = 0
        for backend in backends:
            try:
                for model_name in models.keys():
                    verification = DeepFace.verify(pairs, model=models[model_name], model_name=model_name,
                                                   detector_backend=backend, distance_metric=metric,
                                                   enforce_detection=False)
                    for info in verification.values():
                        if info['verified']:
                            number_of_recognition += 1
                        else:
                            number_of_not_recognition += 1
            except ValueError:
                continue
        if (number_of_recognition / (number_of_recognition + number_of_not_recognition)) >= \
                VALUES["RECOGNITION_THRESHOLD"]:
            return Response(status=status.HTTP_200_OK)
        return Response(status=status.HTTP_403_FORBIDDEN)

  • you need some sort of distributed task queue like [celery](https://docs.celeryproject.org/en/stable/index.html), to process cpu bound tasks without overloading a django view – Ersain Feb 09 '22 at 09:23
  • Use a `ThreadPoolExecutor` and submit the `Deepface.verify` tasks into it with all the arguments along with it. Finally wait for them to complete using `wait`. But multithreading may not be the first choice if the task is CPU-bound rather than being IO-bound. – theoctober19th Feb 09 '22 at 09:24
  • @theoctober19th could you explain more ? – sina jamshidi Feb 09 '22 at 09:48

0 Answers0