57 lines
2.1 KiB
Java
57 lines
2.1 KiB
Java
|
|
package it.acxent.face.callable;
|
||
|
|
|
||
|
|
import it.acxent.face.FotoFace;
|
||
|
|
import it.acxent.face.api.FaceRecognitionApi;
|
||
|
|
import it.acxent.face.api.FaceRecognitionApiResult;
|
||
|
|
import it.acxent.face.api.TrainingImage;
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.concurrent.Callable;
|
||
|
|
import org.json.JSONArray;
|
||
|
|
import org.json.JSONObject;
|
||
|
|
|
||
|
|
public class TrainingSendTask implements Callable<FaceRecognitionApiResult> {
|
||
|
|
private final FaceRecognitionApi frApi;
|
||
|
|
|
||
|
|
private final long faceRecognizerId;
|
||
|
|
|
||
|
|
private final List<TrainingImage> imageFilenames;
|
||
|
|
|
||
|
|
public TrainingSendTask(FaceRecognitionApi frApi, long faceRecognizerId, List<TrainingImage> imageFilenames) {
|
||
|
|
this.frApi = frApi;
|
||
|
|
this.faceRecognizerId = faceRecognizerId;
|
||
|
|
this.imageFilenames = imageFilenames;
|
||
|
|
}
|
||
|
|
|
||
|
|
public FaceRecognitionApiResult call() throws Exception {
|
||
|
|
FaceRecognitionApiResult resF = this.frApi.__trainingSendImages(this.faceRecognizerId, this.imageFilenames, false);
|
||
|
|
handleApiResponse(resF);
|
||
|
|
if (resF.isOk() && resF.getResult() != null) {
|
||
|
|
JSONObject jres = (JSONObject)resF.getResult();
|
||
|
|
if (jres.has("noImg")) {
|
||
|
|
List<TrainingImage> missingImages = new ArrayList<>();
|
||
|
|
JSONArray jNoimgA = jres.getJSONArray("noImg");
|
||
|
|
for (int k = 0; k < jNoimgA.length(); k++) {
|
||
|
|
JSONObject jNoImgRow = jNoimgA.getJSONObject(k);
|
||
|
|
FotoFace ff = new FotoFace(this.frApi.getApFull());
|
||
|
|
ff.findByMd5(jNoImgRow.getString("md5"));
|
||
|
|
if (ff.getId_fotoFace() > 0L)
|
||
|
|
missingImages.add(new TrainingImage(ff.getFullFaceImgPath(), ff.getLabel(), ff.getMd5()));
|
||
|
|
}
|
||
|
|
if (!missingImages.isEmpty()) {
|
||
|
|
FaceRecognitionApiResult missingResF = this.frApi.__trainingSendImages(this.faceRecognizerId, missingImages, true);
|
||
|
|
handleApiResponse(missingResF);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return resF;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void handleApiResponse(FaceRecognitionApiResult resF) {
|
||
|
|
if (resF.isOk() && resF.getResult() != null) {
|
||
|
|
JSONObject jres = (JSONObject)resF.getResult();
|
||
|
|
if (jres.has("labels"))
|
||
|
|
FotoFace.updateLabels(this.frApi.getApFull(), jres);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|