Skip to content

Commit

Permalink
Tampilkan kode belok di output
Browse files Browse the repository at this point in the history
  • Loading branch information
yafithekid committed Sep 30, 2015
1 parent 1ef1771 commit cfaafeb
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 89 deletions.
13 changes: 9 additions & 4 deletions app/src/main/java/com/ganesus/numbervision/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void prosesBitmap(String path){



String hasil = detectAll(bmp, KNOWLEDGE_PATH)[0];
String hasil[] = detectAll(bmp, KNOWLEDGE_PATH);

Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap canvas = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), conf);
Expand All @@ -79,9 +79,9 @@ private void prosesBitmap(String path){
ImageView iv_merek = (ImageView) findViewById(R.id.imageView2);
iv_merek.setImageBitmap(hmerek);

String merekmobil = detectMerek(merek)[0];
String merekmobil[] = detectMerek(merek);
TextView tv = (TextView) findViewById(R.id.txtPerhitungan);
tv.setText(merekmobil);
tv.setText(merekmobil[0] + "(" + merekmobil[1] + ")");

}else{
ImageView iv_merek = (ImageView) findViewById(R.id.imageView2);
Expand All @@ -92,7 +92,12 @@ private void prosesBitmap(String path){
}

TextView tv = (TextView) findViewById(R.id.txtInterpretasi);
tv.setText(hasil);
tv.setText(hasil[0]);

TextView tv2 = (TextView) findViewById(R.id.txtKodeBelok);
tv2.setText(hasil[1]);



ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(hh);
Expand Down
57 changes: 51 additions & 6 deletions app/src/main/jni/numbervision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define LEFT 'L'

//subchain code data yang diambil
#define LENGTH_TAKE 30
#define LENGTH_TAKE 20
//derajat minimal agar dia dikatakan belok
#define MIN_D 50.0
//derajat maximal agar dia dikatakan belok
Expand Down Expand Up @@ -563,7 +563,28 @@ bool is_left_turn(double s,double d){
return fabs(s - d) <= 0.0001;
}

bool is_u_turn_cw(double s,double d){
double diff = fabs(s - d);
if (diff > 180.0) diff = 360.0 - diff;
if (diff < 150.0) return false;

s = s + diff;
if (s > 360.0) s -= 360.0;
return fabs(s - d) <= 0.0001;
}

bool is_u_turn_ccw(double s,double d){
double diff = fabs(s - d);
if (diff > 180.0) diff = 360.0 - diff;
if (diff < 150.0) return false;

d = d + diff;
if (d > 360.0) d -= 360.0;
return fabs(s - d) <= 0.0001;
}

string generate_turn(const string &code){
LOGD("generate turn");
string ret;
for(int i = 0; i + LENGTH_TAKE < (int) code.size(); ){
string sleft = code.substr(i,LENGTH_TAKE/2);
Expand All @@ -580,16 +601,24 @@ string generate_turn(const string &code){
ret += RIGHT;
i += LENGTH_TAKE/2;
printf("got right turn at %d, %s %s , deg = %.3lf %.3lf %.3lf\n",i,sleft.c_str(),sright.c_str(),dl,dr,diff);
} else if (is_u_turn_cw(dl,dr)){
ret += RIGHT; ret += RIGHT;
i += LENGTH_TAKE /2;
} else if (is_u_turn_ccw(dl,dr)) {
ret += LEFT; ret += LEFT;
i += LENGTH_TAKE /2;
} else {
i++;
}
}
LOGD("generate turn end");
return ret;
}


int edit_distance(const string& a,const string& b){
int dp[MAX_TURN_CODE][MAX_TURN_CODE];
LOGD("edit distance start");
int dp[1000][1000];

for(int ia = 1; ia <= (int) a.size(); ia++){
dp[ia][0] = ia;
Expand All @@ -609,16 +638,21 @@ int edit_distance(const string& a,const string& b){
}
}
}
LOGD("edit distance end");
return dp[a.size()][b.size()];
}

vector<char> predict(const string& chain_code,const vector<Train>& trains){
LOGD("predict start");
vector<char> probabilities;
probabilities.clear();

int cost = edit_distance(chain_code,trains[0].path);
LOGD("predict prob before");
probabilities.push_back(trains[0].label);
LOGD("predict prob after");
for(int i = 1; i < (int) trains.size(); i++){
LOGD("%d",i);
int cnow = edit_distance(chain_code,trains[i].path);
if (cnow < cost){
cost = cnow;
Expand All @@ -628,6 +662,7 @@ vector<char> predict(const string& chain_code,const vector<Train>& trains){
probabilities.push_back(trains[i].label);
}
}
LOGD("predict end");
return probabilities;
}

Expand Down Expand Up @@ -886,6 +921,8 @@ JNIEXPORT jobjectArray JNICALL Java_com_ganesus_numbervision_MainActivity_detect
vector<BorderInfo> border_infos = get_border_infos(image,nativeBitmap->bitmapInfo.width,nativeBitmap->bitmapInfo.height);
delete nativeBitmap;

stringstream list_kode_belok;

//FILE* file = fopen("/sdcard/textTest.txt","w+");

for (int i=0;i<border_infos.size();i++) {
Expand All @@ -897,7 +934,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_ganesus_numbervision_MainActivity_detect
}

if (border_info.chain_codes.size() > TRESHOLD_ERROR) {
string turns = generate_turn(sskode.str());


//vector<char> predictions = predict(turns, train);

Expand Down Expand Up @@ -937,13 +974,18 @@ JNIEXPORT jobjectArray JNICALL Java_com_ganesus_numbervision_MainActivity_detect
}
fputs(interpretation[currentMin].chain.c_str(), file);
ss << interpretation[currentMin].value;

string turns = generate_turn(interpretation[currentMin].chain);
LOGD("CHAIN CODE: %s", interpretation[currentMin].chain.c_str());
LOGD("%s", turns.c_str());
list_kode_belok << turns << " -> " << interpretation[currentMin].value << endl;
interpretation.erase(interpretation.begin() + currentMin);

}
fclose(file);

// [1] adalah ekspresi input, [2] adalah hasil perhitungan*/
std::string tes[] = { ss.str().c_str(), "44" };
std::string tes[] = { ss.str().c_str(), list_kode_belok.str().c_str() };
//std::string tes[] = { ss.str().c_str(), "44" };
jobjectArray hasil2 = createJavaArray(env, 2, tes);

Expand All @@ -956,6 +998,9 @@ JNIEXPORT jobjectArray JNICALL Java_com_ganesus_numbervision_MainActivity_detect
//vector<Train> train = createKnowledge(path_knowledge);

vector<Train> trains;

string turns;

trains.push_back(Train('0',"RRRR"));
trains.push_back(Train('1',"RLRLLLLRRLLLRRRLLLLRLLRRLRLLLRLLR"));

Expand All @@ -974,7 +1019,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_ganesus_numbervision_MainActivity_detect
}

if (border_info.chain_codes.size() > TRESHOLD_ERROR) {
string turns = generate_turn(sskode.str());
turns = generate_turn(sskode.str());
vector<char> predictions = predict(turns, trains);

DetectedChar detectedChar;
Expand Down Expand Up @@ -1024,7 +1069,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_ganesus_numbervision_MainActivity_detect
}else if (ss.str() == "1"){
ss.str("TOYOTA");
}
std::string tes[] = { ss.str().c_str(), "44" };
std::string tes[] = { ss.str().c_str(), turns };
//std::string tes[] = { ss.str().c_str(), "44" };
jobjectArray hasil2 = createJavaArray(env, 2, tes);

Expand Down
195 changes: 116 additions & 79 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,123 @@
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="Silahkan pilih sumber gambar:" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:onClick="klikGallery" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Plat Nomor:"
android:id="@+id/textView2"
android:layout_marginTop="24dp"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Merek:"
android:id="@+id/textView3"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="21dp"
android:visibility="visible" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/txtInterpretasi"
android:layout_alignTop="@+id/textView2"
android:layout_toRightOf="@+id/textView3"
android:layout_toEndOf="@+id/textView3"
android:layout_marginLeft="41dp"
android:layout_marginStart="41dp" />

<TextView


<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/txtPerhitungan"
android:layout_alignTop="@+id/textView3"
android:layout_alignLeft="@+id/txtInterpretasi"
android:layout_alignStart="@+id/txtInterpretasi"
android:textIsSelectable="false" />

<ImageView
android:layout_width="fill_parent"
android:layout_height="150dp"
android:id="@+id/imageView"
android:layout_below="@+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:adjustViewBounds="true" />

<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView2"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="true" />
android:id="@+id/scrollView" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:text="Silahkan pilih sumber gambar:" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:onClick="klikGallery" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Plat Nomor:"
android:id="@+id/textView2"
android:layout_marginTop="24dp"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Merek:"
android:id="@+id/textView3"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="21dp"
android:visibility="visible" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Kode Belok"
android:id="@+id/textView4"
android:layout_below="@+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/txtInterpretasi"
android:layout_alignTop="@+id/textView2"
android:layout_toRightOf="@+id/textView3"
android:layout_toEndOf="@+id/textView3"
android:layout_marginLeft="41dp"
android:layout_marginStart="41dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/txtPerhitungan"
android:layout_alignTop="@+id/textView3"
android:layout_alignLeft="@+id/txtInterpretasi"
android:layout_alignStart="@+id/txtInterpretasi"
android:textIsSelectable="false" />


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/txtKodeBelok"
android:layout_alignTop="@+id/textView4"
android:layout_alignLeft="@+id/txtPerhitungan"
android:layout_alignStart="@+id/txtPerhitungan"/>

<ImageView
android:layout_width="fill_parent"
android:layout_height="150dp"
android:id="@+id/imageView"
android:layout_below="@+id/txtKodeBelok"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:adjustViewBounds="true" />

<ImageView
android:layout_width="fill_parent"
android:layout_height="150dp"
android:id="@+id/imageView2"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="true" />



</RelativeLayout>
</ScrollView>

</RelativeLayout>

0 comments on commit cfaafeb

Please sign in to comment.