Skip to content

Commit 2154faa

Browse files
committed
.
1 parent a6d2143 commit 2154faa

File tree

4 files changed

+54
-36
lines changed

4 files changed

+54
-36
lines changed

app/src/main/java/com/example/vcvyc/myapplication/FaceDetect.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ private static Bitmap convert2rgb565(Bitmap bitmap) {
5656
canvas.drawBitmap(bitmap, 0, 0, paint);
5757
return convertedBitmap;
5858
}
59-
//按照rect的大小裁剪出人脸
60-
public static Bitmap crop(Bitmap bitmap,Rect rect){
61-
Bitmap cropped=Bitmap.createBitmap(bitmap,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top);
62-
return cropped;
63-
}
6459
public static Rect detectBiggestFace(Bitmap _bitmap){
6560
Rect[] rects=FaceDetect.detectFaces(_bitmap);
6661
if (rects.length==0) return null;

app/src/main/java/com/example/vcvyc/myapplication/MainActivity.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,35 @@ public double compareFaces(){
7171
if (rect1==null) return -1;
7272
Rect rect2 = FaceDetect.detectBiggestFace(bitmap2);
7373
if (rect2==null) return -2;*/
74+
Bitmap bm1=Utils.copyBitmap(bitmap1);
75+
Bitmap bm2=Utils.copyBitmap(bitmap2);
7476
Vector<Box> boxes=mtcnn.detectFaces(bitmap1,40);
7577
Vector<Box> boxes1=mtcnn.detectFaces(bitmap2,40);
7678
if (boxes.size()==0) return -1;
7779
if (boxes1.size()==0)return -2;
78-
for (int i=0;i<boxes.size();i++) Utils.drawBox(bitmap1,boxes.get(i));
79-
for (int i=0;i<boxes1.size();i++) Utils.drawBox(bitmap2,boxes1.get(i));
80+
for (int i=0;i<boxes.size();i++) Utils.drawBox(bitmap1,boxes.get(i),1+bitmap1.getWidth()/500 );
81+
for (int i=0;i<boxes1.size();i++) Utils.drawBox(bitmap2,boxes1.get(i),1+bitmap2.getWidth()/500 );
8082
Log.i("Main","[*]boxNum"+boxes1.size());
8183
Rect rect1=boxes.get(0).transform2Rect();
8284
Rect rect2=boxes1.get(0).transform2Rect();
85+
//MTCNN检测到的人脸框,再上下左右扩展margin个像素点,再放入facenet中。
86+
int margin=20; //20这个值是facenet中设置的。自己应该可以调整。
87+
Utils.rectExtend(bitmap1,rect1,margin);
88+
Utils.rectExtend(bitmap2,rect2,margin);
89+
//要比较的两个人脸,加厚Rect
90+
Utils.drawRect(bitmap1,rect1,1+bitmap1.getWidth()/100 );
91+
Utils.drawRect(bitmap2,rect2,1+bitmap2.getWidth()/100 );
8392
//(2)裁剪出人脸(只取第一张)
84-
Bitmap face1=FaceDetect.crop(bitmap1,rect1);
85-
Bitmap face2=FaceDetect.crop(bitmap2,rect2);
93+
Bitmap face1=Utils.crop(bitmap1,rect1);
94+
Bitmap face2=Utils.crop(bitmap2,rect2);
8695
//(显示人脸)
8796
imageView1.setImageBitmap(bitmap1);
8897
imageView2.setImageBitmap(bitmap2);
8998
//(3)特征提取
9099
FaceFeature ff1=facenet.recognizeImage(face1);
91100
FaceFeature ff2=facenet.recognizeImage(face2);
101+
bitmap1=bm1;
102+
bitmap2=bm2;
92103
//(4)比较
93104
return ff1.compare(ff2);
94105
}

app/src/main/java/com/example/vcvyc/myapplication/Utils.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414

1515
import java.util.Vector;
1616

17+
import static java.lang.Math.max;
18+
import static java.lang.Math.min;
19+
1720
public class Utils {
1821
//复制图片,并设置isMutable=true
1922
public static Bitmap copyBitmap(Bitmap bitmap){
2023
return bitmap.copy(bitmap.getConfig(),true);
2124
}
2225
//在bitmap中画矩形
23-
public static void drawRect(Bitmap bitmap,Rect rect){
26+
public static void drawRect(Bitmap bitmap,Rect rect,int thick){
2427
try {
2528
Canvas canvas = new Canvas(bitmap);
2629
Paint paint = new Paint();
2730
int r=255;//(int)(Math.random()*255);
2831
int g=0;//(int)(Math.random()*255);
2932
int b=0;//(int)(Math.random()*255);
3033
paint.setColor(Color.rgb(r, g, b));
31-
paint.setStrokeWidth(1+bitmap.getWidth()/500 );
34+
paint.setStrokeWidth(thick);
3235
paint.setStyle(Paint.Style.STROKE);
3336
canvas.drawRect(rect, paint);
3437
//Log.i("Util","[*]draw rect");
@@ -37,17 +40,17 @@ public static void drawRect(Bitmap bitmap,Rect rect){
3740
}
3841
}
3942
//在图中画点
40-
public static void drawPoints(Bitmap bitmap, Point[] landmark){
43+
public static void drawPoints(Bitmap bitmap, Point[] landmark,int thick){
4144
for (int i=0;i<landmark.length;i++){
4245
int x=landmark[i].x;
4346
int y=landmark[i].y;
4447
//Log.i("Utils","[*] landmarkd "+x+ " "+y);
45-
drawRect(bitmap,new Rect(x-1,y-1,x+1,y+1));
48+
drawRect(bitmap,new Rect(x-1,y-1,x+1,y+1),thick);
4649
}
4750
}
48-
public static void drawBox(Bitmap bitmap,Box box){
49-
drawRect(bitmap,box.transform2Rect());
50-
drawPoints(bitmap,box.landmark);
51+
public static void drawBox(Bitmap bitmap,Box box,int thick){
52+
drawRect(bitmap,box.transform2Rect(),thick);
53+
drawPoints(bitmap,box.landmark,thick);
5154
}
5255
//Flip alone diagonal
5356
//对角线翻转。data大小原先为h*w*stride,翻转后变成w*h*stride
@@ -102,7 +105,19 @@ public static Vector<Box> updateBoxes(Vector<Box> boxes){
102105
b.addElement(boxes.get(i));
103106
return b;
104107
}
105-
//
108+
//按照rect的大小裁剪出人脸
109+
public static Bitmap crop(Bitmap bitmap,Rect rect){
110+
Bitmap cropped=Bitmap.createBitmap(bitmap,rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top);
111+
return cropped;
112+
}
113+
//rect上下左右各扩展pixels个像素
114+
public static void rectExtend(Bitmap bitmap,Rect rect,int pixels){
115+
rect.left=max(0,rect.left-pixels);
116+
rect.right=min(bitmap.getWidth()-1,rect.right+pixels);
117+
rect.top=max(0,rect.top-pixels);
118+
rect.bottom=min(bitmap.getHeight()-1,rect.bottom+pixels);
119+
}
120+
106121
static public void showPixel(int v){
107122
Log.i("MainActivity","[*]Pixel:R"+((v>>16)&0xff)+"G:"+((v>>8)&0xff)+ " B:"+(v&0xff));
108123
}

app/src/main/res/layout/activity_main.xml

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,27 @@
88

99
<ImageView
1010
android:id="@+id/imageView"
11-
android:layout_width="298dp"
12-
android:layout_height="352dp"
13-
android:layout_marginEnd="20dp"
14-
android:layout_marginStart="8dp"
15-
android:layout_marginTop="8dp"
16-
app:layout_constraintEnd_toStartOf="@+id/imageView2"
17-
app:layout_constraintHorizontal_bias="0.228"
11+
android:layout_width="557dp"
12+
android:layout_height="264dp"
13+
android:layout_marginStart="16dp"
14+
android:layout_marginTop="24dp"
1815
app:layout_constraintStart_toStartOf="parent"
1916
app:layout_constraintTop_toBottomOf="@+id/textView"
2017
app:srcCompat="?attr/actionBarDivider" />
2118

2219
<ImageView
2320
android:id="@+id/imageView2"
24-
android:layout_width="254dp"
25-
android:layout_height="354dp"
26-
android:layout_marginEnd="8dp"
27-
android:layout_marginTop="8dp"
21+
android:layout_width="572dp"
22+
android:layout_height="256dp"
23+
android:layout_marginEnd="12dp"
2824
app:layout_constraintEnd_toEndOf="parent"
29-
app:layout_constraintTop_toBottomOf="@+id/textView"
25+
app:layout_constraintTop_toBottomOf="@+id/imageView"
3026
app:srcCompat="?attr/actionBarDivider" />
3127

3228
<TextView
3329
android:id="@+id/textView"
3430
android:layout_width="527dp"
35-
android:layout_height="237dp"
31+
android:layout_height="102dp"
3632
tools:layout_editor_absoluteX="16dp"
3733
tools:layout_editor_absoluteY="16dp" />
3834

@@ -42,19 +38,20 @@
4238
android:layout_height="100dp"
4339
android:layout_marginEnd="8dp"
4440
android:layout_marginStart="8dp"
45-
android:layout_marginTop="48dp"
41+
android:layout_marginTop="8dp"
4642
android:text="开始比较【点击图片从本机选择】"
4743
app:layout_constraintEnd_toEndOf="parent"
44+
app:layout_constraintHorizontal_bias="0.254"
4845
app:layout_constraintStart_toEndOf="@+id/textView2"
49-
app:layout_constraintTop_toBottomOf="@+id/imageView" />
46+
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
5047

5148
<TextView
5249
android:id="@+id/textView2"
5350
android:layout_width="309dp"
54-
android:layout_height="115dp"
55-
android:layout_marginStart="8dp"
56-
android:layout_marginTop="48dp"
51+
android:layout_height="118dp"
52+
android:layout_marginStart="16dp"
53+
android:layout_marginTop="8dp"
5754
android:text="点击图片从本地上传"
5855
app:layout_constraintStart_toStartOf="parent"
59-
app:layout_constraintTop_toBottomOf="@+id/imageView" />
56+
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
6057
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)