这是我的代码,我想优化性能如何在 python 中执行并行线程?任何想法?我正在使用这个模型:
“DEEPFACE_MODELS”:“VGG-Face;DeepFace”,“DEEPFACE_BACKENDS”:“opencv;ssd;dlib”任何关于优化性能的建议都会很棒
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)