├── examples ├── test_img │ ├── bald_guys.jpg │ └── bald_guys_1.jpg ├── faces_clustering │ ├── Launcher.java │ ├── pom.xml │ └── FacesClustering.java └── facial_landmarks │ ├── pom.xml │ └── FacialLandmardsExample.java ├── jni ├── utils.hpp ├── CMakeLists.txt ├── handlers.cpp ├── com_emaraic_jdlib_Jdlib.h ├── utils.cpp ├── handlers.hpp └── Jdlib.cpp ├── Jdlib ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── emaraic │ ├── utils │ ├── Image.java │ ├── FaceDescriptor.java │ └── ImageUtils.java │ └── jdlib │ └── Jdlib.java ├── LICENSE ├── Licenses ├── dlib ├── Intel Math Kernel Library └── openjfx └── README.md /examples/test_img/bald_guys.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tahaemara/jdlib/HEAD/examples/test_img/bald_guys.jpg -------------------------------------------------------------------------------- /examples/test_img/bald_guys_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tahaemara/jdlib/HEAD/examples/test_img/bald_guys_1.jpg -------------------------------------------------------------------------------- /examples/faces_clustering/Launcher.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author Taha Emara 5 | * Website: http://www.emaraic.com 6 | * Email : taha@emaraic.com 7 | * Created on: Dec 28, 2020 8 | */ 9 | public class Launcher { 10 | public static void main(String[] args) { 11 | FacesClustering.main(args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /jni/utils.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // utils.hpp 3 | // Jdlib 4 | // 5 | // Created by Taha Emara on 11/21/20. 6 | // 7 | 8 | #ifndef utils_hpp 9 | #define utils_hpp 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace dlib; 15 | 16 | 17 | std::string convertJStringtoStdString(JNIEnv *env, jstring jStr); 18 | jclass getJavaClass(JNIEnv *env,std::string classname); 19 | jmethodID getJavaMethod(JNIEnv *env, jclass cls, std::string methdname, std::string methdsig); 20 | jobject convertDlibRectangleToJRectangle(JNIEnv *env, rectangle & rect, jclass Rectangle_Class, jmethodID Rectangle_Constructor); 21 | array2d convertBufferedImageToDlibImage(jbyte* bufferPtr, int h, int w); 22 | #endif /* utils_hpp */ 23 | -------------------------------------------------------------------------------- /Jdlib/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | com.emaraic 5 | Jdlib 6 | 1.0-SNAPSHOT 7 | jar 8 | 9 | UTF-8 10 | 1.8 11 | 1.8 12 | 13 | 14 | 15 | 16 | ${basedir}/src/main/resources/ 17 | 18 | **/* 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /jni/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.12) 2 | set(CMAKE_MACOSX_RPATH 1) 3 | set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") 4 | 5 | project(Jdlib) 6 | 7 | set(CMAKE_BUILD_TYPE Release) 8 | 9 | find_package(JNI REQUIRED) 10 | include_directories(${JNI_INCLUDE_DIRS}) 11 | 12 | 13 | set(DLib_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/build/dlib/) 14 | execute_process ( 15 | COMMAND bash -c "git clone https://github.com/davisking/dlib.git" 16 | ) 17 | add_subdirectory(${DLib_LOCATION} dlib_build) 18 | 19 | 20 | add_library(Jdlib SHARED Jdlib.cpp utils.cpp handlers.cpp) 21 | target_link_libraries(Jdlib dlib::dlib ) 22 | 23 | 24 | if(APPLE) 25 | set_target_properties(Jdlib PROPERTIES 26 | LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../Jdlib/src/main/resources/native/macosx) 27 | endif() 28 | 29 | if(UNIX AND NOT APPLE) 30 | set_target_properties(Jdlib PROPERTIES 31 | LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../Jdlib/src/main/resources/native/linux) 32 | endif() -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /Licenses/dlib: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Jdlib/src/main/java/com/emaraic/utils/Image.java: -------------------------------------------------------------------------------- 1 | package com.emaraic.utils; 2 | 3 | import java.awt.image.BufferedImage; 4 | import java.awt.image.DataBufferByte; 5 | 6 | /** 7 | * 8 | * @author Taha Emara 9 | * Website: http://www.emaraic.com 10 | * Email : taha@emaraic.com 11 | * Created on: Nov 14, 2020 12 | */ 13 | public class Image { 14 | 15 | public final int width; 16 | public final int height; 17 | private final boolean hasAlphaChannel; 18 | private int pixelLength; 19 | public final byte[] pixels; 20 | 21 | public Image(BufferedImage image) { 22 | pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 23 | width = image.getWidth(); 24 | height = image.getHeight(); 25 | hasAlphaChannel = image.getAlphaRaster() != null; 26 | pixelLength = 3; 27 | if (hasAlphaChannel) { 28 | pixelLength = 4; 29 | } 30 | } 31 | 32 | short[] getRGB(int x, int y) { 33 | int pos = (y * pixelLength * width) + (x * pixelLength); 34 | short rgb[] = new short[4]; 35 | if (hasAlphaChannel) { 36 | rgb[3] = (short) (pixels[pos++] & 0xFF); // Alpha 37 | } 38 | rgb[2] = (short) (pixels[pos++] & 0xFF); // Blue 39 | rgb[1] = (short) (pixels[pos++] & 0xFF); // Green 40 | rgb[0] = (short) (pixels[pos++] & 0xFF); // Red 41 | return rgb; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Jdlib/src/main/java/com/emaraic/utils/FaceDescriptor.java: -------------------------------------------------------------------------------- 1 | package com.emaraic.utils; 2 | 3 | import java.awt.Point; 4 | import java.awt.Rectangle; 5 | import java.util.ArrayList; 6 | 7 | /** 8 | * 9 | * @author Taha Emara 10 | * Website: http://www.emaraic.com 11 | * Email : taha@emaraic.com 12 | * Created on: Dec 12, 2020 13 | */ 14 | public class FaceDescriptor { 15 | 16 | private final Rectangle faceBox; 17 | private final ArrayList facialLandmarks; 18 | private final float[] faceEmbedding; 19 | private String label; 20 | 21 | public FaceDescriptor(Rectangle faceBoxes, ArrayList facialLandmarks) { 22 | this.faceBox = faceBoxes; 23 | this.facialLandmarks = facialLandmarks; 24 | this.faceEmbedding = null; 25 | } 26 | 27 | 28 | public FaceDescriptor(Rectangle faceBoxes, ArrayList facialLandmarks, float[] faceEmbedding) { 29 | this.faceBox = faceBoxes; 30 | this.facialLandmarks = facialLandmarks; 31 | this.faceEmbedding = faceEmbedding; 32 | this.label = "None"; 33 | } 34 | 35 | public Rectangle getFaceBox() { 36 | return faceBox; 37 | } 38 | 39 | public ArrayList getFacialLandmarks() { 40 | return facialLandmarks; 41 | } 42 | 43 | public float[] getFaceEmbedding() { 44 | return faceEmbedding; 45 | } 46 | 47 | public String getLabel() { 48 | return label; 49 | } 50 | 51 | public void setLabel(String label) { 52 | this.label = label; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /examples/facial_landmarks/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.emaraic 4 | JdlibExamples 5 | 1.0.0 6 | 7 | . 8 | 9 | 10 | maven-assembly-plugin 11 | 12 | 13 | package 14 | 15 | single 16 | 17 | 18 | 19 | 20 | 21 | 22 | FacialLandmardsExample 23 | 24 | 25 | 26 | jar-with-dependencies 27 | 28 | 29 | 30 | 31 | 32 | 33 | 1.8 34 | 1.8 35 | 36 | 37 | 38 | com.emaraic 39 | jdlib 40 | 1.0.1 41 | 42 | 43 | -------------------------------------------------------------------------------- /jni/handlers.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // handlers.cpp 3 | // Jdlib 4 | // 5 | // Created by Taha Emara on 12/18/20. 6 | // 7 | 8 | #include "handlers.hpp" 9 | 10 | FaceDetectorHandler::FaceDetectorHandler(){ 11 | face_detector = dlib::get_frontal_face_detector(); 12 | } 13 | dlib::frontal_face_detector FaceDetectorHandler::getFaceDetector(){ 14 | return face_detector; 15 | } 16 | 17 | FaceDetectorHandler * FaceDetectorHandler::getFaceDetectorHandler(){ 18 | static FaceDetectorHandler *face_detector_handler; 19 | if (!face_detector_handler) 20 | face_detector_handler = new FaceDetectorHandler; 21 | return face_detector_handler; 22 | } 23 | 24 | 25 | 26 | ShapePredictorHandler::ShapePredictorHandler(const std::string & model_path) : model_path(model_path){ 27 | dlib::deserialize(model_path)>>shape_predictor_model; 28 | } 29 | 30 | dlib::shape_predictor ShapePredictorHandler::getShapePredictorModel(){ 31 | return shape_predictor_model; 32 | } 33 | 34 | ShapePredictorHandler * ShapePredictorHandler::getShapePredictorHandler(const std::string & model_path){ 35 | static ShapePredictorHandler *shape_predictor_handler; 36 | if (!shape_predictor_handler) 37 | shape_predictor_handler = new ShapePredictorHandler(model_path); 38 | return shape_predictor_handler; 39 | } 40 | 41 | 42 | FaceEmbeddingHandler::FaceEmbeddingHandler(const std::string & model_path) : model_path(model_path){ 43 | dlib::deserialize(model_path)>>face_embedding_model; 44 | } 45 | 46 | anet_type FaceEmbeddingHandler::getFaceEmbeddingModel(){ 47 | return face_embedding_model; 48 | } 49 | 50 | FaceEmbeddingHandler * FaceEmbeddingHandler::getFaceEmbeddingHandler(const std::string & model_path){ 51 | static FaceEmbeddingHandler *face_embedding_handler; 52 | if (!face_embedding_handler) 53 | face_embedding_handler = new FaceEmbeddingHandler(model_path); 54 | return face_embedding_handler; 55 | } 56 | -------------------------------------------------------------------------------- /jni/com_emaraic_jdlib_Jdlib.h: -------------------------------------------------------------------------------- 1 | /* DO NOT EDIT THIS FILE - it is machine generated */ 2 | #include 3 | /* Header for class com_emaraic_jdlib_Jdlib */ 4 | 5 | #ifndef _Included_com_emaraic_jdlib_Jdlib 6 | #define _Included_com_emaraic_jdlib_Jdlib 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | /* 11 | * Class: com_emaraic_jdlib_Jdlib 12 | * Method: getFaceDectorHandler 13 | * Signature: ()J 14 | */ 15 | JNIEXPORT jlong JNICALL Java_com_emaraic_jdlib_Jdlib_getFaceDectorHandler 16 | (JNIEnv *, jobject); 17 | 18 | /* 19 | * Class: com_emaraic_jdlib_Jdlib 20 | * Method: getShapePredictorHandler 21 | * Signature: (Ljava/lang/String;)J 22 | */ 23 | JNIEXPORT jlong JNICALL Java_com_emaraic_jdlib_Jdlib_getShapePredictorHandler 24 | (JNIEnv *, jobject, jstring); 25 | 26 | /* 27 | * Class: com_emaraic_jdlib_Jdlib 28 | * Method: getFaceEmbeddingHandler 29 | * Signature: (Ljava/lang/String;)J 30 | */ 31 | JNIEXPORT jlong JNICALL Java_com_emaraic_jdlib_Jdlib_getFaceEmbeddingHandler 32 | (JNIEnv *, jobject, jstring); 33 | 34 | /* 35 | * Class: com_emaraic_jdlib_Jdlib 36 | * Method: faceDetect 37 | * Signature: (J[BII)Ljava/util/ArrayList; 38 | */ 39 | JNIEXPORT jobject JNICALL Java_com_emaraic_jdlib_Jdlib_faceDetect 40 | (JNIEnv *, jobject, jlong, jbyteArray, jint, jint); 41 | 42 | /* 43 | * Class: com_emaraic_jdlib_Jdlib 44 | * Method: getFacialLandmarks 45 | * Signature: (JJ[BII)Ljava/util/ArrayList; 46 | */ 47 | JNIEXPORT jobject JNICALL Java_com_emaraic_jdlib_Jdlib_getFacialLandmarks 48 | (JNIEnv *, jobject, jlong, jlong, jbyteArray, jint, jint); 49 | 50 | /* 51 | * Class: com_emaraic_jdlib_Jdlib 52 | * Method: getFaceEmbeddings 53 | * Signature: (JJJ[BII)Ljava/util/ArrayList; 54 | */ 55 | JNIEXPORT jobject JNICALL Java_com_emaraic_jdlib_Jdlib_getFaceEmbeddings 56 | (JNIEnv *, jobject, jlong, jlong, jlong, jbyteArray, jint, jint); 57 | 58 | #ifdef __cplusplus 59 | } 60 | #endif 61 | #endif 62 | -------------------------------------------------------------------------------- /Jdlib/src/main/java/com/emaraic/utils/ImageUtils.java: -------------------------------------------------------------------------------- 1 | package com.emaraic.utils; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.Color; 5 | import java.awt.Graphics2D; 6 | import java.awt.Point; 7 | import java.awt.Rectangle; 8 | import java.awt.image.BufferedImage; 9 | import javax.swing.ImageIcon; 10 | import javax.swing.JFrame; 11 | import javax.swing.JLabel; 12 | import javax.swing.WindowConstants; 13 | 14 | /** 15 | * 16 | * @author Taha Emara 17 | * Website: http://www.emaraic.com 18 | * Email : taha@emaraic.com 19 | * Created on: Dec 10, 2020 20 | */ 21 | public class ImageUtils { 22 | 23 | private static JFrame frame; 24 | private static JLabel label; 25 | 26 | public static void drawRectangle(BufferedImage image, Rectangle rectangle) { 27 | Graphics2D graph = image.createGraphics(); 28 | graph.setColor(Color.GREEN); 29 | graph.draw(rectangle); 30 | graph.dispose(); 31 | } 32 | 33 | public static void showImage(BufferedImage image) { 34 | if (frame == null) { 35 | frame = new JFrame(); 36 | frame.setTitle("Jdlib Viewer"); 37 | frame.setResizable(false); 38 | frame.setSize(image.getWidth(), image.getHeight()); 39 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 40 | label = new JLabel(); 41 | label.setIcon(new ImageIcon(image)); 42 | frame.getContentPane().add(label, BorderLayout.CENTER); 43 | frame.setLocationRelativeTo(null); 44 | frame.pack(); 45 | frame.setVisible(true); 46 | } else { 47 | label.setIcon(new ImageIcon(image)); 48 | } 49 | } 50 | 51 | public static void drawFaceDescriptor(BufferedImage image, FaceDescriptor facedes) { 52 | Graphics2D graph = image.createGraphics(); 53 | graph.setColor(Color.GREEN); 54 | graph.draw(facedes.getFaceBox()); 55 | graph.setColor(Color.RED); 56 | facedes.getFacialLandmarks().stream().forEach((point) -> { 57 | graph.fillOval(point.x, point.y, 3, 3); 58 | }); 59 | graph.dispose(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /examples/faces_clustering/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.emaraic 4 | JdlibExamples 5 | 1.0.0 6 | 7 | . 8 | 9 | 10 | maven-assembly-plugin 11 | 12 | 13 | package 14 | 15 | single 16 | 17 | 18 | 19 | 20 | 21 | 22 | Launcher 23 | 24 | 25 | 26 | jar-with-dependencies 27 | 28 | 29 | 30 | 31 | 32 | 33 | 1.8 34 | 1.8 35 | 36 | 37 | 38 | com.emaraic 39 | jdlib 40 | 1.0.1 41 | 42 | 43 | org.apache.commons 44 | commons-math3 45 | 3.2 46 | 47 | 48 | org.openjfx 49 | javafx-controls 50 | 15.0.1 51 | 52 | 53 | org.openjfx 54 | javafx-swing 55 | 11 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jdlib 2 | Java Wrapper For dlib for Linux and MacOSX. Till now the wrapper contains stubs for face HOG detector, facial landmarks, face embedding models. 3 | 4 | 5 | ## Demo 6 | ### Face Clustering Example 7 | 8 | 9 | ## Using Jdlib 10 | JAR files contains binaries of Jdlib on current supprted platforms (Linux and Mac OS X) inside [releases](https://github.com/tahaemara/jdlib/releases). 11 | 12 | Also you can have everything downloaded and installed automatically with Maven after including the next dependency inside the pom.xml. 13 | 14 | ``` 15 | 16 | com.emaraic 17 | jdlib 18 | 1.0.1 19 | 20 | ``` 21 | 22 | ## Compiling Jdlib 23 | 24 | ### Requirments: 25 | - Dlib installation requirments [Using dlib from C++](http://dlib.net/compile.html) 26 | - JDK 8 or latter 27 | - Maven 28 | 29 | ### Clone project: 30 | ``` 31 | $ git clone https://github.com/tahaemara/jdlib.git 32 | ``` 33 | 34 | ### Compile JNI/C++ code: 35 | ``` 36 | $ cd jdlib/jni 37 | $ mkdir build 38 | $ cd build 39 | $ cmake .. 40 | $ make 41 | ``` 42 | ### Compile Java Package: 43 | 44 | ``` 45 | $ cd ../../Jdlib 46 | $ mvn package 47 | ``` 48 | 49 | After that you will have the JAR file including the binaries for your platform inside Jdlib/target. Then you can use it inside your project as an external jar or install it manually in [local maven](https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html). 50 | 51 | ## Compiling and running examples 52 | 53 | - Download needed models to example folder 54 | ``` 55 | $ cd examples 56 | $ wget http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 57 | $ wget http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2 58 | ``` 59 | - Unzip model 60 | ``` 61 | $ bzip2 -dk shape_predictor_68_face_landmarks.dat.bz2 62 | $ bzip2 -dk dlib_face_recognition_resnet_model_v1.dat.bz2 63 | ``` 64 | - Change Diectory to an example folder such as faces_clustering: 65 | ``` 66 | cd examples/faces_clustering/ 67 | ``` 68 | - Build package via maven 69 | ``` 70 | mvn package 71 | ``` 72 | - Run example 73 | ``` 74 | java -jar target/JdlibExamples-1.0.0-jar-with-dependencies.jar 75 | ``` 76 | -------------------------------------------------------------------------------- /examples/facial_landmarks/FacialLandmardsExample.java: -------------------------------------------------------------------------------- 1 | 2 | import com.emaraic.jdlib.Jdlib; 3 | import com.emaraic.utils.FaceDescriptor; 4 | import com.emaraic.utils.ImageUtils; 5 | import java.awt.Graphics2D; 6 | import java.awt.Image; 7 | import java.awt.image.BufferedImage; 8 | import java.io.File; 9 | import java.io.IOException; 10 | import java.util.ArrayList; 11 | import javax.imageio.ImageIO; 12 | 13 | /** 14 | * This example based on C++ example from http://dlib.net/face_landmark_detection_ex.cpp.html 15 | * This example program shows how to find frontal human faces in an image and 16 | * estimate their pose. The pose takes the form of 68 landmarks. These are 17 | * points on the face such as the corners of the mouth, along the eyebrows, on 18 | * the eyes, and so forth. 19 | * 20 | * @author Taha Emara 21 | * Website: http://www.emaraic.com 22 | * Email : taha@emaraic.com 23 | * Created on: Dec 20, 2020 24 | */ 25 | public class FacialLandmardsExample { 26 | 27 | private static BufferedImage loadImage(String imagepath) { 28 | BufferedImage img = null; 29 | try { 30 | img = ImageIO.read(new File(imagepath)); 31 | } catch (IOException e) { 32 | System.err.println("Error During Loading File: " + imagepath); 33 | } 34 | return img; 35 | } 36 | 37 | public static BufferedImage resize(BufferedImage img, int w, int h) { 38 | Image tempimg = img.getScaledInstance(w, h, Image.SCALE_SMOOTH); 39 | BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 40 | Graphics2D g2d = image.createGraphics(); 41 | g2d.drawImage(tempimg, 0, 0, null); 42 | g2d.dispose(); 43 | return image; 44 | } 45 | 46 | public static void main(String[] args) { 47 | 48 | String facialLandmarksModelPath = "../shape_predictor_68_face_landmarks.dat"; 49 | String imagepath = "../test_img/bald_guys.jpg"; 50 | 51 | Jdlib jdlib = new Jdlib(facialLandmarksModelPath); 52 | 53 | BufferedImage img = loadImage(imagepath); 54 | 55 | ArrayList faces = jdlib.getFaceLandmarks(img); 56 | 57 | for (FaceDescriptor face : faces) { 58 | ImageUtils.drawFaceDescriptor(img, face); 59 | } 60 | 61 | img = resize(img, 800, 800); 62 | 63 | ImageUtils.showImage(img); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /jni/utils.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // utils.cpp 3 | // Jdlib 4 | // 5 | // Created by Taha Emara on 11/21/20. 6 | // 7 | 8 | #include "utils.hpp" 9 | 10 | 11 | std::string convertJStringtoStdString(JNIEnv *env, jstring jstr) { 12 | if (!jstr) 13 | return ""; 14 | 15 | const jclass j_string_class = env->GetObjectClass(jstr); 16 | const jmethodID getBytes = getJavaMethod(env, j_string_class, "getBytes", "(Ljava/lang/String;)[B"); 17 | const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jstr, getBytes, env->NewStringUTF("UTF-8")); 18 | 19 | size_t length = (size_t) env->GetArrayLength(stringJbytes); 20 | jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL); 21 | 22 | std::string ret = std::string((char *)pBytes, length); 23 | env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT); 24 | 25 | env->DeleteLocalRef(stringJbytes); 26 | env->DeleteLocalRef(j_string_class); 27 | return ret; 28 | } 29 | 30 | jclass getJavaClass(JNIEnv *env, std::string classname){ 31 | jclass cls= env->FindClass(classname.c_str()); 32 | if (env->ExceptionCheck()){ 33 | env->ExceptionClear(); 34 | std::cerr<< "Can't find class: " <GetMethodID(cls, methdname.c_str(), methdsig.c_str()); 41 | if (env->ExceptionCheck()){ 42 | env->ExceptionClear(); 43 | std::cerr<< "Can't find method: " << methdname<<" with signature: "<NewObject(Rectangle_Class, Rectangle_Constructor,top_left_x,top_left_y,buttom_right_x-top_left_x,buttom_right_y-top_left_y); 53 | return rect_obj; 54 | } 55 | 56 | array2d convertBufferedImageToDlibImage(jbyte* bytedata, int h, int w){ 57 | array2d img(h,w); 58 | 59 | for (int x = 0; x < w; ++x){ 60 | for (int y = 0; y < h ; ++y) 61 | { 62 | int pos = (y * 3 * w) + (x * 3); 63 | rgb_pixel p; 64 | p.blue = bytedata[pos++] & 0xFF; 65 | p.green = bytedata[pos++] & 0xFF; 66 | p.red = bytedata[pos++] & 0xFF; 67 | assign_pixel(img[y][x],p); 68 | } 69 | } 70 | 71 | return img; 72 | } 73 | -------------------------------------------------------------------------------- /jni/handlers.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // handlers.hpp 3 | // Jdlib 4 | // 5 | // Created by Taha Emara on 12/18/20. 6 | // 7 | 8 | #ifndef handlers_hpp 9 | #define handlers_hpp 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace dlib; 16 | 17 | template