Skip to content

Commit

Permalink
add:android
Browse files Browse the repository at this point in the history
  • Loading branch information
EdVince committed Sep 24, 2023
1 parent f75a9ad commit 593419f
Show file tree
Hide file tree
Showing 423 changed files with 165,432 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

[DragGan](https://github.com/XingangPan/DragGAN) implemented by [ncnn](https://github.com/Tencent/ncnn) framework based on C++

You can download the model in [model_zoo](https://github.com/EdVince/model_zoo/releases/tag/DragGan-NCNN).
You can download the model and app in [model_zoo](https://github.com/EdVince/model_zoo/releases/tag/DragGan-NCNN).

## Demo

![image](./resources/android.jpg)

![image](./resources/666.gif)

![image](./resources/123456789.gif)
Expand Down
28 changes: 28 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "29.0.2"
ndkVersion '22.1.7171670'

defaultConfig {
applicationId "com.tencent.makeup"
archivesBaseName = "$applicationId"

ndk {
moduleName "ncnn"
abiFilters "armeabi-v7a", "arm64-v8a"
}
minSdkVersion 24
}

externalNativeBuild {
cmake {
version "3.10.2"
path file('src/main/jni/CMakeLists.txt')
}
}
dependencies {
implementation 'com.android.support:support-v4:24.0.0'
}
}
17 changes: 17 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tencent.makeup"
android:versionCode="1"
android:versionName="1.1">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:label="@string/app_name" >
<activity android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
847 changes: 847 additions & 0 deletions android/app/src/main/assets/generator.param

Large diffs are not rendered by default.

Binary file added android/app/src/main/assets/mapping.bin
Binary file not shown.
55 changes: 55 additions & 0 deletions android/app/src/main/assets/mapping.param
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
7767517
53 56
Input /mapping/Cast_output_0 0 1 /mapping/Cast_output_0
Split splitncnn_input0 1 3 /mapping/Cast_output_0 /mapping/Cast_output_0_splitncnn_0 /mapping/Cast_output_0_splitncnn_1 /mapping/Cast_output_0_splitncnn_2
MemoryData /mapping/fc0/Reshape_output_0 0 1 /mapping/fc0/Reshape_output_0 0=512
MemoryData /mapping/fc0/Transpose_output_0 0 1 /mapping/fc0/Transpose_output_0 0=512 1=512
MemoryData /mapping/fc1/Reshape_output_0 0 1 /mapping/fc1/Reshape_output_0 0=512
MemoryData /mapping/fc1/Transpose_output_0 0 1 /mapping/fc1/Transpose_output_0 0=512 1=512
MemoryData /mapping/fc2/Reshape_output_0 0 1 /mapping/fc2/Reshape_output_0 0=512
MemoryData /mapping/fc2/Transpose_output_0 0 1 /mapping/fc2/Transpose_output_0 0=512 1=512
MemoryData /mapping/fc3/Reshape_output_0 0 1 /mapping/fc3/Reshape_output_0 0=512
MemoryData /mapping/fc3/Transpose_output_0 0 1 /mapping/fc3/Transpose_output_0 0=512 1=512
MemoryData /mapping/fc4/Reshape_output_0 0 1 /mapping/fc4/Reshape_output_0 0=512
MemoryData /mapping/fc4/Transpose_output_0 0 1 /mapping/fc4/Transpose_output_0 0=512 1=512
MemoryData /mapping/fc5/Reshape_output_0 0 1 /mapping/fc5/Reshape_output_0 0=512
MemoryData /mapping/fc5/Transpose_output_0 0 1 /mapping/fc5/Transpose_output_0 0=512 1=512
MemoryData /mapping/fc6/Reshape_output_0 0 1 /mapping/fc6/Reshape_output_0 0=512
MemoryData /mapping/fc6/Transpose_output_0 0 1 /mapping/fc6/Transpose_output_0 0=512 1=512
MemoryData /mapping/fc7/Reshape_output_0 0 1 /mapping/fc7/Reshape_output_0 0=512
MemoryData /mapping/fc7/Transpose_output_0 0 1 /mapping/fc7/Transpose_output_0 0=512 1=512
MemoryData G.mapping.w_avg 0 1 G.mapping.w_avg 0=512
BinaryOp /mapping/Mul 2 1 /mapping/Cast_output_0_splitncnn_2 /mapping/Cast_output_0_splitncnn_1 /mapping/Mul_output_0 0=2
Reduction /mapping/ReduceMean 1 1 /mapping/Mul_output_0 /mapping/ReduceMean_output_0 0=3 1=0 -23303=1,0 4=1 5=1
BinaryOp /mapping/Add 1 1 /mapping/ReduceMean_output_0 /mapping/Add_output_0 0=0 1=1 2=1.000000e-08
UnaryOp /mapping/Sqrt 1 1 /mapping/Add_output_0 /mapping/Sqrt_output_0 0=5
BinaryOp /mapping/Div 1 1 /mapping/Sqrt_output_0 /mapping/Div_output_0 0=8 1=1 2=1.000000e+00
BinaryOp /mapping/Mul_1 2 1 /mapping/Cast_output_0_splitncnn_0 /mapping/Div_output_0 /mapping/Mul_1_output_0 0=2
Gemm /mapping/fc0/Add_output_0 3 1 /mapping/Mul_1_output_0 /mapping/fc0/Transpose_output_0 /mapping/fc0/Reshape_output_0 /mapping/fc0/Add_output_0 0=1.000000e+00 1=1.000000e+00 2=0 3=0
ReLU /mapping/fc0/LeakyRelu 1 1 /mapping/fc0/Add_output_0 /mapping/fc0/LeakyRelu_output_0 0=2.000000e-01
BinaryOp /mapping/fc0/Mul_2 1 1 /mapping/fc0/LeakyRelu_output_0 /mapping/fc0/Mul_2_output_0 0=2 1=1 2=1.414214e+00
Gemm /mapping/fc1/Add_output_0 3 1 /mapping/fc0/Mul_2_output_0 /mapping/fc1/Transpose_output_0 /mapping/fc1/Reshape_output_0 /mapping/fc1/Add_output_0 0=1.000000e+00 1=1.000000e+00 2=0 3=0
ReLU /mapping/fc1/LeakyRelu 1 1 /mapping/fc1/Add_output_0 /mapping/fc1/LeakyRelu_output_0 0=2.000000e-01
BinaryOp /mapping/fc1/Mul_2 1 1 /mapping/fc1/LeakyRelu_output_0 /mapping/fc1/Mul_2_output_0 0=2 1=1 2=1.414214e+00
Gemm /mapping/fc2/Add_output_0 3 1 /mapping/fc1/Mul_2_output_0 /mapping/fc2/Transpose_output_0 /mapping/fc2/Reshape_output_0 /mapping/fc2/Add_output_0 0=1.000000e+00 1=1.000000e+00 2=0 3=0
ReLU /mapping/fc2/LeakyRelu 1 1 /mapping/fc2/Add_output_0 /mapping/fc2/LeakyRelu_output_0 0=2.000000e-01
BinaryOp /mapping/fc2/Mul_2 1 1 /mapping/fc2/LeakyRelu_output_0 /mapping/fc2/Mul_2_output_0 0=2 1=1 2=1.414214e+00
Gemm /mapping/fc3/Add_output_0 3 1 /mapping/fc2/Mul_2_output_0 /mapping/fc3/Transpose_output_0 /mapping/fc3/Reshape_output_0 /mapping/fc3/Add_output_0 0=1.000000e+00 1=1.000000e+00 2=0 3=0
ReLU /mapping/fc3/LeakyRelu 1 1 /mapping/fc3/Add_output_0 /mapping/fc3/LeakyRelu_output_0 0=2.000000e-01
BinaryOp /mapping/fc3/Mul_2 1 1 /mapping/fc3/LeakyRelu_output_0 /mapping/fc3/Mul_2_output_0 0=2 1=1 2=1.414214e+00
Gemm /mapping/fc4/Add_output_0 3 1 /mapping/fc3/Mul_2_output_0 /mapping/fc4/Transpose_output_0 /mapping/fc4/Reshape_output_0 /mapping/fc4/Add_output_0 0=1.000000e+00 1=1.000000e+00 2=0 3=0
ReLU /mapping/fc4/LeakyRelu 1 1 /mapping/fc4/Add_output_0 /mapping/fc4/LeakyRelu_output_0 0=2.000000e-01
BinaryOp /mapping/fc4/Mul_2 1 1 /mapping/fc4/LeakyRelu_output_0 /mapping/fc4/Mul_2_output_0 0=2 1=1 2=1.414214e+00
Gemm /mapping/fc5/Add_output_0 3 1 /mapping/fc4/Mul_2_output_0 /mapping/fc5/Transpose_output_0 /mapping/fc5/Reshape_output_0 /mapping/fc5/Add_output_0 0=1.000000e+00 1=1.000000e+00 2=0 3=0
ReLU /mapping/fc5/LeakyRelu 1 1 /mapping/fc5/Add_output_0 /mapping/fc5/LeakyRelu_output_0 0=2.000000e-01
BinaryOp /mapping/fc5/Mul_2 1 1 /mapping/fc5/LeakyRelu_output_0 /mapping/fc5/Mul_2_output_0 0=2 1=1 2=1.414214e+00
Gemm /mapping/fc6/Add_output_0 3 1 /mapping/fc5/Mul_2_output_0 /mapping/fc6/Transpose_output_0 /mapping/fc6/Reshape_output_0 /mapping/fc6/Add_output_0 0=1.000000e+00 1=1.000000e+00 2=0 3=0
ReLU /mapping/fc6/LeakyRelu 1 1 /mapping/fc6/Add_output_0 /mapping/fc6/LeakyRelu_output_0 0=2.000000e-01
BinaryOp /mapping/fc6/Mul_2 1 1 /mapping/fc6/LeakyRelu_output_0 /mapping/fc6/Mul_2_output_0 0=2 1=1 2=1.414214e+00
Gemm /mapping/fc7/Add_output_0 3 1 /mapping/fc6/Mul_2_output_0 /mapping/fc7/Transpose_output_0 /mapping/fc7/Reshape_output_0 /mapping/fc7/Add_output_0 0=1.000000e+00 1=1.000000e+00 2=0 3=0
ReLU /mapping/fc7/LeakyRelu 1 1 /mapping/fc7/Add_output_0 /mapping/fc7/LeakyRelu_output_0 0=2.000000e-01
BinaryOp /mapping/fc7/Mul_2 1 1 /mapping/fc7/LeakyRelu_output_0 /mapping/Tile_output_0 0=2 1=1 2=1.414214e+00
Split splitncnn_0 1 2 /mapping/Tile_output_0 /mapping/Tile_output_0_splitncnn_0 /mapping/Tile_output_0_splitncnn_1
BinaryOp /mapping/Sub 2 1 /mapping/Tile_output_0_splitncnn_1 G.mapping.w_avg /mapping/Sub_output_0 0=1
BinaryOp /mapping/Mul_3 1 1 /mapping/Sub_output_0 /mapping/Mul_3_output_0 0=2 1=1 2=3.000000e-01
BinaryOp /mapping/Sub_2 2 1 /mapping/Tile_output_0_splitncnn_0 /mapping/Mul_3_output_0 /mapping/Sub_2_output_0 0=1
275 changes: 275 additions & 0 deletions android/app/src/main/java/com/tencent/makeup/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License 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.

package com.tencent.makeup;
import android.annotation.SuppressLint;
import android.content.Context;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends Activity
{
private ImageView imageView;
private EditText seedText;

private Bitmap showBitmap;

private StableDiffusion sd = new StableDiffusion();
/** Called when the activity is first created. */
@SuppressLint("MissingInflatedId")
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

imageView = (ImageView) findViewById(R.id.resView);
seedText = (EditText) findViewById(R.id.seed);
showBitmap = Bitmap.createBitmap(512,512,Bitmap.Config.ARGB_8888);

boolean ret_init = sd.Init(getAssets());

Button buttonGenImage = (Button) findViewById(R.id.gen);
buttonGenImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
int seed = Integer.parseInt(seedText.getText().toString());
sd.gen(showBitmap, seed);
final Bitmap styledImage = showBitmap.copy(Bitmap.Config.ARGB_8888,true);
imageView.post(new Runnable() {
public void run() {
imageView.setImageBitmap(styledImage);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
});
}
});

imageView.setOnTouchListener(new View.OnTouchListener() {
boolean shouldProcessTouch = true;
long lastTouchTime = 0;
final long TOUCH_DELAY = 500; // 设置触摸延迟时间为500毫秒

@Override
public boolean onTouch(View v, MotionEvent event) {
long now = System.currentTimeMillis();

if (now - lastTouchTime < TOUCH_DELAY) {
shouldProcessTouch = false; // 如果距离上次触摸时间小于延迟时间,则不处理触摸事件
} else {
shouldProcessTouch = true;
lastTouchTime = now;
}

if(!shouldProcessTouch){
return true;
}

int x = (int) event.getX();
int y = (int) event.getY();

int[] location = new int[2];
v.getLocationOnScreen(location);

int relativeX = x - location[0];
int relativeY = y - location[1];

relativeX = 512 * relativeX / v.getWidth();
relativeY = 512 * relativeY / v.getWidth();

// 处理点击事件,使用 x 和 y
Log.e("MainActivity", "("+x+","+y+") ("+relativeX+","+relativeY+")");
sd.setPoint(showBitmap,relativeX,relativeY);
final Bitmap styledImage = showBitmap.copy(Bitmap.Config.ARGB_8888,true);
imageView.post(new Runnable() {
public void run() {
imageView.setImageBitmap(styledImage);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
});

return true;
}
});

Button buttonClean = (Button) findViewById(R.id.clean);
buttonClean.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
sd.clean(showBitmap);
final Bitmap styledImage = showBitmap.copy(Bitmap.Config.ARGB_8888,true);
imageView.post(new Runnable() {
public void run() {
imageView.setImageBitmap(styledImage);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
});
}
});

Button buttonDrag = (Button) findViewById(R.id.drag);
buttonDrag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
new Thread(new Runnable() {
@Override
public void run() {
for (int it = 0; it < 200; it++) {
boolean bk = sd.drag(showBitmap, it);
final Bitmap styledImage = showBitmap.copy(Bitmap.Config.ARGB_8888, true);
final int finalIt = it;
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Step:"+ finalIt, Toast.LENGTH_SHORT).show();
imageView.setImageBitmap(styledImage);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
});
if(bk){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Finish!", Toast.LENGTH_SHORT).show();
}
});
break;
}
}
}
}).start();
}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
try {
final Bitmap tmp = decodeUri(selectedImage);
showBitmap = Bitmap.createScaledBitmap(tmp,512,512,false);
imageView.setImageBitmap(showBitmap);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
break;
default:
break;
}
}

private void copy(Context myContext, String ASSETS_NAME, String savePath, String saveName) {
String filename = savePath + "/" + saveName;
File dir = new File(savePath);
if (!dir.exists())
dir.mkdir();
try {
if (!(new File(filename)).exists()) {
InputStream is = myContext.getResources().getAssets().open(ASSETS_NAME);
FileOutputStream fos = new FileOutputStream(filename);
byte[] buffer = new byte[7168];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException
{
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

// The new size we want to scale to
final int REQUIRED_SIZE = 512;

// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}

// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);

// Rotate according to EXIF
int rotate = 0;
try
{
ExifInterface exif = new ExifInterface(getContentResolver().openInputStream(selectedImage));
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
}
catch (IOException e)
{
Log.e("MainActivity", "ExifInterface IOException");
}

Matrix matrix = new Matrix();
matrix.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
}
Loading

0 comments on commit 593419f

Please sign in to comment.