Skip to content

Commit

Permalink
[TF][KILL] more Google Vision face detection
Browse files Browse the repository at this point in the history
  • Loading branch information
thermatk committed Jan 7, 2023
1 parent 90f7a94 commit 093ca61
Showing 1 changed file with 0 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
import androidx.dynamicanimation.animation.SpringAnimation;
import androidx.dynamicanimation.animation.SpringForce;

import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.face.Face;
import com.google.android.gms.vision.face.FaceDetector;

import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.Bitmaps;
import org.telegram.messenger.BuildVars;
Expand Down Expand Up @@ -80,7 +76,6 @@
import org.telegram.ui.Components.Paint.PaintTypeface;
import org.telegram.ui.Components.Paint.Painting;
import org.telegram.ui.Components.Paint.PersistColorPalette;
import org.telegram.ui.Components.Paint.PhotoFace;
import org.telegram.ui.Components.Paint.RenderView;
import org.telegram.ui.Components.Paint.Swatch;
import org.telegram.ui.Components.Paint.UndoStore;
Expand Down Expand Up @@ -156,7 +151,6 @@ public void set(float val) {
}
};

private ArrayList<PhotoFace> faces;
private int originalBitmapRotation;
private BigInteger lcm;

Expand Down Expand Up @@ -1295,11 +1289,6 @@ public void onAnimationEnd(Animator animation) {
private void openStickersView() {
final int wasSelectedIndex = tabsSelectedIndex;
switchTab(1);
postDelayed(() -> {
if (facesBitmap != null) {
detectFaces();
}
}, 350);
StickerMasksAlert stickerMasksAlert = new StickerMasksAlert(getContext(), facesBitmap == null, resourcesProvider) {
@Override
public void onDismissAnimationStart() {
Expand Down Expand Up @@ -1463,63 +1452,10 @@ public void init() {
renderInputView.setVisibility(View.VISIBLE);
}

private int getFrameRotation() {
switch (originalBitmapRotation) {
case 90: return Frame.ROTATION_90;
case 180: return Frame.ROTATION_180;
case 270: return Frame.ROTATION_270;
default: return Frame.ROTATION_0;
}
}

private boolean isSidewardOrientation() {
return originalBitmapRotation % 360 == 90 || originalBitmapRotation % 360 == 270;
}

private void detectFaces() {
queue.postRunnable(() -> {
FaceDetector faceDetector = null;
try {
faceDetector = new FaceDetector.Builder(getContext())
.setMode(FaceDetector.ACCURATE_MODE)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.setTrackingEnabled(false).build();
if (!faceDetector.isOperational()) {
if (BuildVars.LOGS_ENABLED) {
FileLog.e("face detection is not operational");
}
return;
}

Frame frame = new Frame.Builder().setBitmap(facesBitmap).setRotation(getFrameRotation()).build();
SparseArray<Face> faces;
try {
faces = faceDetector.detect(frame);
} catch (Throwable e) {
FileLog.e(e);
return;
}
ArrayList<PhotoFace> result = new ArrayList<>();
Size targetSize = getPaintingSize();
for (int i = 0; i < faces.size(); i++) {
int key = faces.keyAt(i);
Face f = faces.get(key);
PhotoFace face = new PhotoFace(f, facesBitmap, targetSize, isSidewardOrientation());
if (face.isSufficient()) {
result.add(face);
}
}
LPhotoPaintView.this.faces = result;
} catch (Exception e) {
FileLog.e(e);
} finally {
if (faceDetector != null) {
faceDetector.release();
}
}
}, 200);
}

@Override
public void shutdown() {
renderView.shutdown();
Expand Down Expand Up @@ -2622,84 +2558,7 @@ private StickerPosition calculateStickerPosition(TLRPC.Document document) {
baseScale = 0.75f;
}
StickerPosition defaultPosition = new StickerPosition(centerPositionForEntity(), baseScale, rotation);
if (maskCoords == null || faces == null || faces.size() == 0) {
return defaultPosition;
} else {
int anchor = maskCoords.n;

PhotoFace face = getRandomFaceWithVacantAnchor(anchor, document.id, maskCoords);
if (face == null) {
return defaultPosition;
}

Point referencePoint = face.getPointForAnchor(anchor);
float referenceWidth = face.getWidthForAnchor(anchor);
float angle = face.getAngle();
Size baseSize = baseStickerSize();

float scale = (float) (referenceWidth / baseSize.width * maskCoords.zoom);

float radAngle = (float) Math.toRadians(angle);
float xCompX = (float) (Math.sin(Math.PI / 2.0f - radAngle) * referenceWidth * maskCoords.x);
float xCompY = (float) (Math.cos(Math.PI / 2.0f - radAngle) * referenceWidth * maskCoords.x);

float yCompX = (float) (Math.cos(Math.PI / 2.0f + radAngle) * referenceWidth * maskCoords.y);
float yCompY = (float) (Math.sin(Math.PI / 2.0f + radAngle) * referenceWidth * maskCoords.y);

float x = referencePoint.x + xCompX + yCompX;
float y = referencePoint.y + xCompY + yCompY;

return new StickerPosition(new Point(x, y), scale, angle);
}
}

private PhotoFace getRandomFaceWithVacantAnchor(int anchor, long documentId, TLRPC.TL_maskCoords maskCoords) {
if (anchor < 0 || anchor > 3 || faces.isEmpty()) {
return null;
}

int count = faces.size();
int randomIndex = Utilities.random.nextInt(count);
int remaining = count;

PhotoFace selectedFace = null;
for (int i = randomIndex; remaining > 0; i = (i + 1) % count, remaining--) {
PhotoFace face = faces.get(i);
if (!isFaceAnchorOccupied(face, anchor, documentId, maskCoords)) {
return face;
}
}

return selectedFace;
}

private boolean isFaceAnchorOccupied(PhotoFace face, int anchor, long documentId, TLRPC.TL_maskCoords maskCoords) {
Point anchorPoint = face.getPointForAnchor(anchor);
if (anchorPoint == null) {
return true;
}

float minDistance = face.getWidthForAnchor(0) * 1.1f;

for (int index = 0; index < entitiesView.getChildCount(); index++) {
View view = entitiesView.getChildAt(index);
if (!(view instanceof StickerView)) {
continue;
}

StickerView stickerView = (StickerView) view;
if (stickerView.getAnchor() != anchor) {
continue;
}

Point location = stickerView.getPosition();
float distance = (float)Math.hypot(location.x - anchorPoint.x, location.y - anchorPoint.y);
if ((documentId == stickerView.getSticker().id || faces.size() > 1) && distance < minDistance) {
return true;
}
}

return false;
}

private StickerView createSticker(Object parentObject, TLRPC.Document sticker, boolean select) {
Expand Down

0 comments on commit 093ca61

Please sign in to comment.