Exemple #1
0
    def post(self, request, *args, **kwargs):
        device_id = request.POST.get('device_id')
        min_temperature = request.POST.get('min_temperature')
        max_temperature = request.POST.get('max_temperature')

        # image_id = str(uuid.uuid4())
        num_imgs = Image_object.objects.filter(device_id=device_id).count()
        if (num_imgs > 1000):  # max buffering images
            num_imgs = 0

        imageid = "{:05d}".format(num_imgs)

        photo_filename = upload_photo_image(request, device_id, imageid)
        thermal_filename = upload_thermal_image(request, device_id, imageid)

        name, ext = os.path.splitext(photo_filename)
        photo_preprocessed_filename = name + '_pre' + ext
        photo_output_filename = name + '_out' + ext

        name, ext = os.path.splitext(thermal_filename)
        thermal_preprocessed_filename = name + '_pre' + ext
        thermal_output_filename = name + '_out' + ext

        ### Check if the image_id is already exist in the database
        image_object = None
        try:
            image_object = Image_object.objects.filter(
                device_id=device_id).get(image_id=imageid)
        except Image_object.DoesNotExist:
            image_object = None

        if image_object:  # update the record in the database
            image_object.device_id = device_id
            image_object.image_id = imageid
            image_object.min_temperature = min_temperature
            image_object.max_temperature = max_temperature
            image_object.photo_filename = photo_filename
            image_object.thermal_filename = thermal_filename
            image_object.photo_preprocessed_filename = photo_preprocessed_filename
            image_object.thermal_preprocessed_filename = thermal_preprocessed_filename
            image_object.photo_output_filename = photo_output_filename
            image_object.thermal_output_filename = thermal_output_filename
            image_object.status = ''
            image_object.date_created = datetime.now()
            image_object.save()
        else:  # create a new record in the database
            image = Image_object()
            image.device_id = device_id
            image.image_id = imageid
            image.min_temperature = min_temperature
            image.max_temperature = max_temperature
            image.photo_filename = photo_filename
            image.thermal_filename = thermal_filename
            image.photo_preprocessed_filename = photo_preprocessed_filename
            image.thermal_preprocessed_filename = thermal_preprocessed_filename
            image.photo_output_filename = photo_output_filename
            image.thermal_output_filename = thermal_output_filename
            image.status = ''
            image.save()

        # detect_faces.s(device_id=device_id, image_id=imageid).delay()
        chain(
            preprocessing_image.s(device_id=device_id, image_id=imageid)
            | detect_faces.s(device_id=device_id, image_id=imageid)
            | form_bounding_boxes.s(device_id=device_id, image_id=imageid)
        ).delay()

        return Response({"status": "ok"}, status=status.HTTP_202_ACCEPTED)