Skip to content

Commit

Permalink
增加相似度阈值调整接口及功能
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhongsheng committed Aug 2, 2024
1 parent befc02c commit 986e491
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 27 deletions.
29 changes: 26 additions & 3 deletions app/src/main/java/com/zbgd/lighttrack/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import android.view.Surface
import android.view.TextureView
import android.view.View
import android.widget.Button
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.zbgd.lighttrack.databinding.ActivityMainBinding
import java.util.Collections
import kotlin.math.abs
import kotlin.math.log


class MainActivity : AppCompatActivity() {
Expand All @@ -36,6 +36,9 @@ class MainActivity : AppCompatActivity() {

private lateinit var textureView: AutoFitTextureView
private lateinit var overlayView: OverlayView
private lateinit var seekBar: SeekBar
private lateinit var tvThreshold: TextView

private lateinit var btnSetTarget: Button
private lateinit var btnTrack: Button
private lateinit var cameraDevice: CameraDevice
Expand Down Expand Up @@ -67,6 +70,8 @@ class MainActivity : AppCompatActivity() {
overlayView = findViewById(R.id.overlayView)
btnSetTarget = findViewById(R.id.btnSetTarget)
btnTrack = findViewById(R.id.btnTrack)
seekBar = findViewById(R.id.seekBar)
tvThreshold = findViewById(com.zbgd.lighttrack.R.id.tv_threshold)
var r = lightTrack.Init(assets)
if (r){
Log.i(TAG, "lightTrack init successfully")
Expand All @@ -77,6 +82,24 @@ class MainActivity : AppCompatActivity() {

cameraManager = getSystemService(CAMERA_SERVICE) as CameraManager
cameraId = cameraManager.cameraIdList[0]
seekBar.setProgress(30)
seekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
var variable = progress / 100.0f;
// Log.i(TAG, "lightTrack init successfully ${variable}")
lightTrack.SetSimilarityThreshold(variable)
tvThreshold.setText(variable.toString())

}

override fun onStartTrackingTouch(seekBar: SeekBar) {
// 可以在这里处理滑块开始滑动的事件
}

override fun onStopTrackingTouch(seekBar: SeekBar) {
// 可以在这里处理滑块停止滑动的事件
}
})

textureView.surfaceTextureListener = object : TextureView.SurfaceTextureListener {
override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
Expand Down
75 changes: 54 additions & 21 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,64 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="center">
android:orientation="vertical"
android:paddingHorizontal="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:paddingHorizontal="16dp"
android:paddingVertical="8dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/btnSetTarget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/white"
android:text="设置目标" />

<Button
android:layout_marginStart="10dp"
android:id="@+id/btnTrack"
android:layout_width="0dp"
<TextView
android:textColor="@color/purple_700"
android:text="相似度阈值"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:max="100" />

<TextView
android:id="@+id/tv_threshold"
android:text="0.3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/white"
android:text="跟踪" />
android:orientation="horizontal"
android:gravity="center">

<Button
android:id="@+id/btnSetTarget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/white"
android:text="设置目标" />

<Button
android:layout_marginStart="10dp"
android:id="@+id/btnTrack"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/white"
android:text="跟踪" />
</LinearLayout>

</LinearLayout>



</RelativeLayout>
23 changes: 22 additions & 1 deletion lighttrack/src/main/cpp/lighttrack_jni.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <android/asset_manager_jni.h>
#include <android/bitmap.h>
#include <jni.h>
#include <pthread.h>
#include <string>

#include "LightTrack.h"
Expand Down Expand Up @@ -67,6 +68,9 @@ static jfieldID hId;
static LightTrack* siam_tracker;
static cv::Mat init_window;
static cv::Rect last_rect;
static float similarity_threshold = 0.3;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;


JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
LOGI("JNI_OnLoad");
Expand Down Expand Up @@ -226,7 +230,10 @@ Java_com_zbgd_lighttrack_LightTrackNcnn_Track(JNIEnv *env, jobject thiz, jobject
double score = compareHist(init_window, track_window);
LOGI("Similarity score= %f \n", score);
// 相似度大于0.5的情况才进行矩形框标注
if (score > 0.5){
pthread_mutex_lock(&lock);
float threshold = similarity_threshold;
pthread_mutex_unlock(&lock);
if (score > threshold){
// Draw rect.
//cv::rectangle(mat, rect, cv::Scalar(0, 255, 0));
last_rect = rect;
Expand All @@ -244,6 +251,11 @@ Java_com_zbgd_lighttrack_LightTrackNcnn_Track(JNIEnv *env, jobject thiz, jobject
siam_tracker->target_sz.y = float(last_rect.height);
}
return NULL;
} else{
siam_tracker->target_pos.x = last_rect.x;
siam_tracker->target_pos.y = last_rect.y;
siam_tracker->target_sz.x = float(last_rect.width);
siam_tracker->target_sz.y = float(last_rect.height);
}
LOGI("Target not in range");

Expand All @@ -256,5 +268,14 @@ Java_com_zbgd_lighttrack_LightTrackNcnn_GetFPS(JNIEnv *env, jobject thiz){
return siam_tracker->get_fps();
}

JNIEXPORT jboolean JNICALL
Java_com_zbgd_lighttrack_LightTrackNcnn_SetSimilarityThreshold(JNIEnv *env, jobject thiz, jfloat threshold){
LOGI("set similarity_threshold %f \n",threshold);
pthread_mutex_lock(&lock);
similarity_threshold = threshold;
pthread_mutex_unlock(&lock);
return JNI_TRUE;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class LightTrackNcnn {
//设置目标 bitmap为当前帧
external fun SetTemplate(bitmap: Bitmap,obj: Obj):Boolean
external fun GetFPS(): Float
//设置目标匹配相似度阈值
external fun SetSimilarityThreshold(threshold: Float):Boolean
//跟踪 bitmap为当前帧
external fun Track(bitmap: Bitmap):Obj?
val obj = Obj()
Expand All @@ -33,8 +35,6 @@ class LightTrackNcnn {
var w: Float = 0f//
var h: Float = 0f//
}


companion object {
init {
System.loadLibrary("lighttrack")
Expand Down

0 comments on commit 986e491

Please sign in to comment.