Skip to content

Commit

Permalink
Resolves #1
Browse files Browse the repository at this point in the history
  • Loading branch information
jmformenti committed Nov 27, 2022
1 parent 0e188de commit 1e14202
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ Face identification implemented using cosine distance between calculated embeddi

Commands to build the project:

git clone https://github.com/jmformenti/face-recognition-java.git
cd face-recognition-java
mvn package
```
git lfs install
git clone https://github.com/jmformenti/face-recognition-java.git
cd face-recognition-java
mvn package
```

A fatjar `face-recognition-java-${VERSION}.jar` will be placed in target dir.

Expand All @@ -21,12 +24,14 @@ A fatjar `face-recognition-java-${VERSION}.jar` will be placed in target dir.
1. Prepare data, one root directory with one subdirectory with images of each person (for example, see `src/test/resources/images/train`).

2. Generate embeddings file.

```
java -jar target/face-recognition-java-${VERSION}.jar embed -p /path/to/root/images -e embeddings.dat
```
3. Recognize faces in one image.

```
java -jar target/face-recognition-java-${VERSION}.jar predict -e embeddings.dat -p /path/to/image
```
# Pretrained models
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>org.atypical</groupId>
<artifactId>face-recognition-java</artifactId>
<version>0.1.0</version>
<version>0.1.1</version>

<properties>
<java.version>11</java.version>
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/org/atypical/face/model/FaceEmbeddingModel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.atypical.face.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand All @@ -19,7 +20,6 @@
import org.atypical.face.util.ImageUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.FileSystemUtils;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
Expand Down Expand Up @@ -52,26 +52,27 @@ public class FaceEmbeddingModel {
public void init() throws ModelException, IOException {
Resource resource = new ClassPathResource("models/pytorch/vggface2/vggface2.pt");

Path tempModelDir = extractModeltoTempDir(resource);
File tempModelDir = extractModeltoTempDir(resource).toFile();

Criteria<Image, double[]> criteria = Criteria.builder() //
.setTypes(Image.class, double[].class) //
.optTranslator(new FaceTranslator()) //
.optEngine("PyTorch") //
.optModelUrls(tempModelDir.toString()) //
.optModelUrls(tempModelDir.toURI().toString()) //
.optModelName("vggface2") //
.build();

this.model = ModelZoo.loadModel(criteria);
this.predictor = model.newPredictor();

FileSystemUtils.deleteRecursively(tempModelDir);
deleteFolderOnExit(tempModelDir);

initKryo();
}

private Path extractModeltoTempDir(Resource resource) throws IOException {
Path tempModelDir = Files.createTempDirectory(resource.getFilename());

IOUtils.copy(resource.getInputStream(),
new FileOutputStream(tempModelDir.resolve(resource.getFilename()).toFile()));
return tempModelDir;
Expand Down Expand Up @@ -167,6 +168,13 @@ public static double cosine(NDArray u, NDArray v) {
return Math.abs(1 - uv / uu.mul(vv).sqrt().toDoubleArray()[0]);
}

private void deleteFolderOnExit(File folder) {
folder.deleteOnExit();
for (File f : folder.listFiles()) {
f.deleteOnExit();
}
}

class FaceTranslator implements Translator<Image, double[]> {

public FaceTranslator() {
Expand Down

0 comments on commit 1e14202

Please sign in to comment.