cronapp-plugin-mlkit 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cronapp-plugin-mlkit",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "cordova": {
6
6
  "id": "cronapp-plugin-mlkit",
package/plugin.xml CHANGED
@@ -1,5 +1,5 @@
1
1
  <?xml version='1.0' encoding='utf-8'?>
2
- <plugin id="cronapp-plugin-mlkit" version="1.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
2
+ <plugin id="cronapp-plugin-mlkit" version="1.0.3" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
3
3
  <name>CronappMLKitPlugin</name>
4
4
 
5
5
  <js-module name="CronappMLKitPlugin" src="www/cronapp-mlkit.js">
@@ -4,8 +4,11 @@ import android.app.Activity;
4
4
  import android.content.Context;
5
5
  import android.graphics.Bitmap;
6
6
  import android.graphics.BitmapFactory;
7
+ import android.net.Uri;
7
8
  import android.util.Base64;
8
9
 
10
+ import androidx.annotation.NonNull;
11
+
9
12
  import com.google.android.gms.tasks.OnFailureListener;
10
13
  import com.google.android.gms.tasks.OnSuccessListener;
11
14
  import com.google.mlkit.vision.barcode.Barcode;
@@ -35,8 +38,6 @@ import java.io.IOException;
35
38
  import java.io.InputStream;
36
39
  import java.util.List;
37
40
 
38
- import androidx.annotation.NonNull;
39
-
40
41
  public class CronappMLKitPlugin extends CordovaPlugin {
41
42
 
42
43
  protected static Context applicationContext = null;
@@ -170,8 +171,11 @@ public class CronappMLKitPlugin extends CordovaPlugin {
170
171
  try {
171
172
  InputImage image = getImage(message);
172
173
  FaceDetectorOptions faceDetectorOptions = new FaceDetectorOptions.Builder()
173
- .setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_ALL)
174
-
174
+ .setClassificationMode(options.optInt("classificationMode", FaceDetectorOptions.CLASSIFICATION_MODE_ALL))
175
+ .setPerformanceMode(options.optInt("performanceMode", FaceDetectorOptions.PERFORMANCE_MODE_FAST))
176
+ .setContourMode(options.optInt("contourMode", FaceDetectorOptions.CONTOUR_MODE_NONE))
177
+ .setMinFaceSize((float) options.optDouble("minFaceSize", 0.1F))
178
+ .setLandmarkMode(options.optInt("landmarkMode", FaceDetectorOptions.LANDMARK_MODE_NONE))
175
179
  .build();
176
180
  FaceDetector detector = FaceDetection.getClient(faceDetectorOptions);
177
181
  detector.process(image)
@@ -223,7 +227,7 @@ public class CronappMLKitPlugin extends CordovaPlugin {
223
227
  InputImage image = InputImage.fromBitmap(bitMap, 0);
224
228
  return image;
225
229
  } else {
226
- Uri uri = Uri.parse("/sdcard/Download/face.jpg");
230
+ Uri uri = Uri.parse(message);
227
231
  InputImage image = InputImage.fromFilePath(applicationContext, uri);
228
232
  return image;
229
233
  }
@@ -1,12 +1,15 @@
1
1
  package io.cronapp.mlkit;
2
2
 
3
3
  import android.graphics.Point;
4
+ import android.graphics.PointF;
4
5
  import android.graphics.Rect;
5
6
  import android.text.TextUtils;
6
7
 
7
8
  import com.google.mlkit.vision.barcode.Barcode;
8
9
  import com.google.mlkit.vision.common.InputImage;
9
10
  import com.google.mlkit.vision.face.Face;
11
+ import com.google.mlkit.vision.face.FaceContour;
12
+ import com.google.mlkit.vision.face.FaceLandmark;
10
13
  import com.google.mlkit.vision.label.ImageLabel;
11
14
  import com.google.mlkit.vision.text.Text;
12
15
 
@@ -184,7 +187,7 @@ public class Utils {
184
187
  JSONArray array = new JSONArray();
185
188
  for (Face face : faces) {
186
189
 
187
- array.put(FaceToJsonConverter.faceToJson(face));
190
+ array.put(parseFace(face));
188
191
  }
189
192
  return array;
190
193
  }
@@ -258,18 +261,13 @@ public class Utils {
258
261
  date.getHours(), date.getMinutes(), date.getSeconds());
259
262
  }
260
263
 
261
- public static JSONObject parseFaces(Face face) {
264
+ public static JSONObject parseFace(Face face) {
262
265
 
263
266
  JSONObject faceData = new JSONObject();
264
267
 
265
268
  try {
266
269
  faceData.put("faceId", face.getTrackingId());
267
- JSONObject boundingBox = new JSONObject();
268
- boundingBox.put("left", face.getBoundingBox().left);
269
- boundingBox.put("top", face.getBoundingBox().top);
270
- boundingBox.put("right", face.getBoundingBox().right);
271
- boundingBox.put("bottom", face.getBoundingBox().bottom);
272
- faceData.put("boundingBox", boundingBox);
270
+ faceData.put("boundingBox", parseBoundingBox(face.getBoundingBox()));
273
271
  faceData.put("headEulerAngleX", face.getHeadEulerAngleX());
274
272
  faceData.put("headEulerAngleY", face.getHeadEulerAngleY());
275
273
  faceData.put("headEulerAngleZ", face.getHeadEulerAngleZ());
@@ -289,8 +287,25 @@ public class Utils {
289
287
  landmarksArray.put(landmarkData);
290
288
  }
291
289
  faceData.put("landmarks", landmarksArray);
290
+
291
+ JSONArray contoursArray = new JSONArray();
292
+
293
+ for (FaceContour contour : face.getAllContours()) {
294
+ JSONObject obj = new JSONObject();
295
+ JSONArray points = new JSONArray();
296
+ obj.put("type", contour.getFaceContourType());
297
+ for (PointF point: contour.getPoints()) {
298
+ JSONObject objPoint = new JSONObject();
299
+ objPoint.put("x", point.x);
300
+ objPoint.put("y", point.y);
301
+ points.put(objPoint);
302
+ }
303
+ obj.put("points", points);
304
+ contoursArray.put(obj);
305
+ }
306
+ faceData.put("contours", contoursArray);
292
307
  } catch (Exception e) {
293
- e.printStackTrace();
308
+ throw new RuntimeException(e);
294
309
  }
295
310
 
296
311
  return faceData;
@@ -43,6 +43,7 @@ extension Text {
43
43
 
44
44
  extension Face {
45
45
  func toJSON(with image: UIImage) -> [AnyHashable: Any] {
46
+ // Cria o dicionário de resposta
46
47
  var response: [AnyHashable: Any] = [:]
47
48
 
48
49
  response["faceId"] = trackingID
@@ -74,6 +75,25 @@ extension Face {
74
75
  }
75
76
  response["landmarks"] = landmarksArray
76
77
 
78
+ var contoursArray: [[AnyHashable: Any]] = []
79
+ for contour in contours {
80
+ var contourData: [AnyHashable: Any] = [:]
81
+ contourData["type"] = contour.type.rawValue
82
+
83
+ var pointsArray: [[String: CGFloat]] = []
84
+ for point in contour.points {
85
+ let pointData = [
86
+ "x": point.x,
87
+ "y": point.y
88
+ ]
89
+ pointsArray.append(pointData)
90
+ }
91
+ contourData["points"] = pointsArray
92
+
93
+ contoursArray.append(contourData)
94
+ }
95
+ response["contours"] = contoursArray
96
+
77
97
  return response
78
98
  }
79
99
  }
@@ -35,6 +35,40 @@ class CronappMLKitPlugin: CDVPlugin {
35
35
  }
36
36
  }
37
37
 
38
+ func createFaceDetectorOptions(from options: AnyObject?) -> FaceDetectorOptions {
39
+ let defaultClassificationMode = FaceDetectorClassificationMode.all
40
+ let defaultPerformanceMode = FaceDetectorPerformanceMode.fast
41
+ let defaultContourMode = FaceDetectorContourMode.none
42
+ let defaultLandmarkMode = FaceDetectorLandmarkMode.none
43
+ let defaultMinFaceSize: Float = 0.1
44
+
45
+ let optionsDict = options as? [String: Any] ?? [:]
46
+
47
+ let classificationModeRaw = optionsDict["classificationMode"] as? Int ?? defaultClassificationMode.rawValue
48
+ let classificationMode = FaceDetectorClassificationMode(rawValue: classificationModeRaw) ?? defaultClassificationMode
49
+
50
+ let performanceModeRaw = optionsDict["performanceMode"] as? Int ?? defaultPerformanceMode.rawValue
51
+ let performanceMode = FaceDetectorPerformanceMode(rawValue: performanceModeRaw) ?? defaultPerformanceMode
52
+
53
+ let contourModeRaw = optionsDict["contourMode"] as? Int ?? defaultContourMode.rawValue
54
+ let contourMode = FaceDetectorContourMode(rawValue: contourModeRaw) ?? defaultContourMode
55
+
56
+ let minFaceSize = optionsDict["minFaceSize"] as? Float ?? defaultMinFaceSize
57
+
58
+ let landmarkModeRaw = optionsDict["landmarkMode"] as? Int ?? defaultLandmarkMode.rawValue
59
+ let landmarkMode = FaceDetectorLandmarkMode(rawValue: landmarkModeRaw) ?? defaultLandmarkMode
60
+
61
+ // Configura e retorna as opções do detector de rosto
62
+ let faceDetectorOptions = FaceDetectorOptions()
63
+ faceDetectorOptions.classificationMode = classificationMode
64
+ faceDetectorOptions.performanceMode = performanceMode
65
+ faceDetectorOptions.contourMode = contourMode
66
+ faceDetectorOptions.minFaceSize = CGFloat(minFaceSize)
67
+ faceDetectorOptions.landmarkMode = landmarkMode
68
+
69
+ return faceDetectorOptions
70
+ }
71
+
38
72
 
39
73
  @objc(faceDetector:)
40
74
  func faceDetector(command: CDVInvokedUrlCommand) {
@@ -43,21 +77,31 @@ class CronappMLKitPlugin: CDVPlugin {
43
77
  self.commandDelegate.send(pluginResult, callbackId: command.callbackId)
44
78
  return
45
79
  }
46
- getImage(imageURL: imageURL) { (image, error) in
47
- if let error = error {
48
- let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: error.localizedDescription)
49
- self.commandDelegate.send(pluginResult, callbackId: command.callbackId)
50
- } else {
51
- let faceDetector = FaceDetector.faceDetector()
52
- let visionImage = VisionImage(image: image!)
53
- faceDetector.process(visionImage) { (faces, error) in
54
- if let error = error {
55
- let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: error.localizedDescription)
56
- self.commandDelegate.send(pluginResult, callbackId: command.callbackId)
57
- } else {
58
- let barcodesDict = faces?.compactMap({ $0.toJSON(with: image!) })
59
- let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: barcodesDict)
60
- self.commandDelegate.send(pluginResult, callbackId: command.callbackId)
80
+ let options = command.arguments[1]
81
+
82
+ // Verifique se o argumento é um dicionário ou algo que possa ser convertido
83
+ if let optionsDict = options as? [String: Any] {
84
+
85
+ getImage(imageURL: imageURL) { (image, error) in
86
+ if let error = error {
87
+ let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: error.localizedDescription)
88
+ self.commandDelegate.send(pluginResult, callbackId: command.callbackId)
89
+ } else {
90
+ let options = FaceDetectorOptions()
91
+ options.performanceMode = .fast
92
+ options.classificationMode = .all
93
+
94
+ let faceDetector = FaceDetector.faceDetector(options: self.createFaceDetectorOptions(from: optionsDict as AnyObject))
95
+ let visionImage = VisionImage(image: image!)
96
+ faceDetector.process(visionImage) { (faces, error) in
97
+ if let error = error {
98
+ let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: error.localizedDescription)
99
+ self.commandDelegate.send(pluginResult, callbackId: command.callbackId)
100
+ } else {
101
+ let barcodesDict = faces?.compactMap({ $0.toJSON(with: image!) })
102
+ let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: barcodesDict)
103
+ self.commandDelegate.send(pluginResult, callbackId: command.callbackId)
104
+ }
61
105
  }
62
106
  }
63
107
  }