Commit 6572bc42 authored by Patrik Tomov's avatar Patrik Tomov
Browse files

Fixed registration when average face skip

parent dff4563d
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ package cz.fidentis.web.batchprocessing.registration;

import cz.fidentis.web.batchprocessing.registration.dto.AddAverageFaceToProjectDto;
import cz.fidentis.web.batchprocessing.registration.dto.BatchRegistrationTaskDto;
import cz.fidentis.web.humanface.dto.HumanFaceDto;
import cz.fidentis.web.batchprocessing.registration.dto.RegistrationResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
@@ -30,7 +30,7 @@ public class BatchRegistrationApi {
    @PostMapping("/execute")
    @Operation(summary = "Executes registration of mupltiple faces")
    @PreAuthorize("isOwner(#dto.taskId, 'TASK')")
    public ResponseEntity<HumanFaceDto> execute(@Valid @RequestBody BatchRegistrationTaskDto dto) {
    public ResponseEntity<RegistrationResponseDto> execute(@Valid @RequestBody BatchRegistrationTaskDto dto) {
        return ResponseEntity.ok(batchRegistrationService.execute(dto));
    }

+3 −3
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ package cz.fidentis.web.batchprocessing.registration;

import cz.fidentis.web.batchprocessing.registration.dto.AddAverageFaceToProjectDto;
import cz.fidentis.web.batchprocessing.registration.dto.BatchRegistrationTaskDto;
import cz.fidentis.web.humanface.dto.HumanFaceDto;
import cz.fidentis.web.batchprocessing.registration.dto.RegistrationResponseDto;

/**
 * @author Patrik Tomov
@@ -14,9 +14,9 @@ public interface BatchRegistrationService {
     * Method that executes registration for multiple faces of task
     *
     * @param dto with registration parameters
     * @return {@code HumanFaceDto} new average human face
     * @return {@code RegistrationResponseDto} object with registration results
     */
    HumanFaceDto execute(BatchRegistrationTaskDto dto);
    RegistrationResponseDto execute(BatchRegistrationTaskDto dto);

    /**
     * Export average face as OBJ file
+24 −13
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@ import cz.fidentis.analyst.engines.face.batch.registration.BatchFaceRegistration
import cz.fidentis.web.async.WebSocketProgressHandler;
import cz.fidentis.web.batchprocessing.registration.dto.AddAverageFaceToProjectDto;
import cz.fidentis.web.batchprocessing.registration.dto.BatchRegistrationTaskDto;
import cz.fidentis.web.batchprocessing.registration.dto.RegistrationResponseDto;
import cz.fidentis.web.batchprocessing.registration.dto.RegistrationResultDto;
import cz.fidentis.web.batchprocessing.service.BatchBaseService;
import cz.fidentis.web.fileupload.FileUpload;
import cz.fidentis.web.fileupload.FileUploadRepo;
@@ -64,12 +66,18 @@ public class BatchRegistrationServiceImpl extends BatchBaseService implements Ba

    @Override
    @Transactional
    public HumanFaceDto execute(BatchRegistrationTaskDto dto) {
    public RegistrationResponseDto execute(BatchRegistrationTaskDto dto) {
        HumanFace selectedFace = getSelectedFace(dto.getSelectedFile().getId(), dto.getTaskId());
        HumanFaceDto newAvgFace = null;
        RegistrationResultDto resultDto = executeRegistration(dto, selectedFace);
        if (resultDto == null) {
            return new RegistrationResponseDto(null, false);
        }

        HumanFace newAvgFace = executeRegistration(dto, selectedFace);

        return newAvgFace != null ? createOrUpdateAverageFaceFile(dto, newAvgFace) : null;
        if (resultDto.getHumanFace() != null) {
            newAvgFace = createOrUpdateAverageFaceFile(dto, resultDto.getHumanFace());
        }
        return new RegistrationResponseDto(newAvgFace, resultDto.isCompletedSuccessfully());
    }

    @Override
@@ -96,14 +104,16 @@ public class BatchRegistrationServiceImpl extends BatchBaseService implements Ba
        fileUpload.setProject(project);
    }

    private HumanFace executeRegistration(BatchRegistrationTaskDto dto, HumanFace selectedFace) {
    private RegistrationResultDto executeRegistration(BatchRegistrationTaskDto dto, HumanFace selectedFace) {
        HumanFace newAvgFace;
        RegistrationResultDto resultDto;
        dto.setCalculationProgress(0);
        double prevPSM = 0;
        int iter = 0;
        while (true) {
            iter++;
            newAvgFace = averageAndRegisterIteration(dto);
            resultDto = averageAndRegisterIteration(dto);
            newAvgFace = resultDto.getHumanFace();
            if (newAvgFace == null) {
                return null;
            }
@@ -123,7 +133,7 @@ public class BatchRegistrationServiceImpl extends BatchBaseService implements Ba
            selectedFace = newAvgFace;
            prevPSM = newPSM;
        }
        return newAvgFace;
        return resultDto;
    }

    private HumanFaceDto createOrUpdateAverageFaceFile(BatchRegistrationTaskDto dto, HumanFace newAvgFace) {
@@ -163,18 +173,18 @@ public class BatchRegistrationServiceImpl extends BatchBaseService implements Ba
        return distance;
    }

    private HumanFace averageAndRegisterIteration(BatchRegistrationTaskDto dto) {
    private RegistrationResultDto averageAndRegisterIteration(BatchRegistrationTaskDto dto) {
        try {
            return doInBackground(dto).join();
        } catch (CompletionException | IOException ex) {
            Logger.print(ex.getCause().toString());
            return null;
            return new RegistrationResultDto(null, false);
        }
    }

    @Async
    protected CompletableFuture<HumanFace> doInBackground(BatchRegistrationTaskDto dto) throws IOException {
        HumanFace avgFace = null;
    protected CompletableFuture<RegistrationResultDto> doInBackground(BatchRegistrationTaskDto dto) throws IOException {
        HumanFace avgFace;

        BatchFaceRegistrationConfig config = new BatchFaceRegistrationConfig(
                dto.getRegistrationTaskDto().getRegistrationStrategy(),
@@ -197,7 +207,7 @@ public class BatchRegistrationServiceImpl extends BatchBaseService implements Ba
        for (int i = 0; i < numFaces; i++) {
            if (webSocketProgressHandler.isInterrupted(dto.getCalculationWebSocketSessionId())) {
                webSocketProgressHandler.clearInterrupt(dto.getCalculationWebSocketSessionId());
                return CompletableFuture.completedFuture(null);
                return CompletableFuture.completedFuture(new RegistrationResultDto(null, false));
            }
            HumanFace superimposedFace = getHumanFaceAtIndexFromArray(i, dto.getFileIds(), dto.getTaskId());
            HumanFaceEntity faceEntity = humanFaceService.getHumanFaceEntityByFileUploadIdAndTaskId(dto.getFileIds().get(i), dto.getTaskId(), false);
@@ -215,8 +225,9 @@ public class BatchRegistrationServiceImpl extends BatchBaseService implements Ba

        if (config.avgFaceStrategy() != BatchFaceRegistrationServices.AverageFaceStrategy.NONE) {
            avgFace = createAvgFace(batchFaceRegistration.getAverageMesh());
            return CompletableFuture.completedFuture(new RegistrationResultDto(avgFace, true));
        }
        return CompletableFuture.completedFuture(avgFace);
        return CompletableFuture.completedFuture(new RegistrationResultDto(null, true));
    }

    private File createTempFileForAverageFace() throws IOException {
+20 −0
Original line number Diff line number Diff line
package cz.fidentis.web.batchprocessing.registration.dto;

import cz.fidentis.web.humanface.dto.HumanFaceDto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author Patrik Tomov
 * @since  09.12.2024
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class RegistrationResponseDto {
    private HumanFaceDto humanFace;
    private boolean completedSuccessfully;
}
+20 −0
Original line number Diff line number Diff line
package cz.fidentis.web.batchprocessing.registration.dto;

import cz.fidentis.analyst.data.face.HumanFace;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author Patrik Tomov
 * @since  09.12.2024
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class RegistrationResultDto {
    private HumanFace humanFace;
    private boolean completedSuccessfully;
}
Loading