File size: 2,490 Bytes
bf53f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from django.shortcuts import render

# Create your views here.
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json
from rest_framework.views import APIView
from django.core.files.storage import default_storage
# from .demo import run_inference  # Import from demo.py
from age_estimator.mivolo.demo_copy import main
import os


# @csrf_exempt
class AgeEstimation(APIView):
    def post(self, request):
        # Save the uploaded video file
        try:    
            video_file = request.FILES['video_file']


# def age_estimation_view(request):
#     if request.method == "POST":
#         try:
#             data = json.loads(request.body)
            # video_path = data.get("video_path")
            output_folder = 'output'
            detector_weights = 'age_estimator/mivolo/models/yolov8x_person_face.pt'
            checkpoint = 'age_estimator/mivolo/models/model_imdb_cross_person_4.22_99.46.pth.tar'
            # detector_weights = data.get("detector_weights")
            # checkpoint = data.get("checkpoint")
            # device = data.get("device", "cpu")
            # with_persons = data.get("with_persons", False)
            # disable_faces = data.get("disable_faces", False)
            # draw = data.get("draw", False)
            device = 'cpu'
            with_persons = True
            disable_faces = False
            draw = True
            file_name = default_storage.save(video_file.name, video_file)
            video_file_path = os.path.join(default_storage.location, file_name)

            # Check for required parameters
            if not video_file_path or not detector_weights or not checkpoint:
                return JsonResponse({"error": "Missing required fields: 'video_path', 'detector_weights', or 'checkpoint'"}, status=400)

            # Run the inference function from demo.py
            absolute_age, lower_bound, upper_bound = main(
                video_file_path, output_folder, detector_weights, checkpoint, device, with_persons, disable_faces, draw
            )

            # print(absolute_age)
            # Return the result as a JSON response
            return JsonResponse({
                # "absolute_age": absolute_age,
                "age_range": f"{lower_bound} - {upper_bound}"
            })

        except Exception as e:
            return JsonResponse({"error": str(e)}, status=500)

        return JsonResponse({"error": "Invalid request method"}, status=400)