-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #3 Implement face features extraction
- Loading branch information
Showing
13 changed files
with
463 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
modules/core/src/main/scala/fr/janalyse/sotohp/store/dao/DaoFaceFeatures.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package fr.janalyse.sotohp.store.dao | ||
|
||
import zio.json.JsonCodec | ||
|
||
case class DaoFaceFeatures( | ||
photoId: String, | ||
someoneId: Option[String], | ||
box: DaoBoundingBox, | ||
features: Array[Float] | ||
) derives JsonCodec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
modules/model/src/main/scala/fr/janalyse/sotohp/model/FaceFeatures.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package fr.janalyse.sotohp.model | ||
|
||
import wvlet.airframe.ulid.ULID | ||
|
||
case class FaceFeatures( | ||
photoId: PhotoId, | ||
someoneId: Option[SomeoneId], | ||
box: BoundingBox, | ||
features: Array[Float] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
modules/processor/src/main/java/fr/janalyse/sotohp/processor/FeatureComparison.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package fr.janalyse.sotohp.processor; | ||
|
||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
// code coming from : https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/face/FeatureComparison.java | ||
|
||
import ai.djl.ModelException; | ||
import ai.djl.modality.cv.Image; | ||
import ai.djl.modality.cv.ImageFactory; | ||
import ai.djl.translate.TranslateException; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
public final class FeatureComparison { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(FeatureComparison.class); | ||
|
||
private FeatureComparison() {} | ||
|
||
public static void main(String[] args) throws IOException, ModelException, TranslateException { | ||
Path imageFile1 = Paths.get("src/test/resources/kana1.jpg"); | ||
Image img1 = ImageFactory.getInstance().fromFile(imageFile1); | ||
Path imageFile2 = Paths.get("src/test/resources/kana2.jpg"); | ||
Image img2 = ImageFactory.getInstance().fromFile(imageFile2); | ||
|
||
float[] feature1 = FeatureExtraction.predict(img1); | ||
float[] feature2 = FeatureExtraction.predict(img2); | ||
|
||
logger.info(Float.toString(calculSimilar(feature1, feature2))); | ||
} | ||
|
||
public static float calculSimilar(float[] feature1, float[] feature2) { | ||
float ret = 0.0f; | ||
float mod1 = 0.0f; | ||
float mod2 = 0.0f; | ||
int length = feature1.length; | ||
for (int i = 0; i < length; ++i) { | ||
ret += feature1[i] * feature2[i]; | ||
mod1 += feature1[i] * feature1[i]; | ||
mod2 += feature2[i] * feature2[i]; | ||
} | ||
return (float) ((ret / Math.sqrt(mod1) / Math.sqrt(mod2) + 1) / 2.0f); | ||
} | ||
} |
Oops, something went wrong.