Spaces:
Sleeping
Sleeping
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) | |