Skip to content

Commit

Permalink
Merge pull request #18 from tink-expo/id-pw-pick
Browse files Browse the repository at this point in the history
Code edits and delay tuning
  • Loading branch information
zealotzealot authored Dec 1, 2019
2 parents b3cb17d + b4d77fe commit b1748ed
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 105 deletions.
1 change: 1 addition & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
/.project
/.settings
/app/sampledata
/gradle.properties
3 changes: 3 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ android {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
buildTypes.each {
it.buildConfigField 'String', 'API_KEY', API_KEY
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class FailActivity extends AppCompatActivity {
private String currentId;
private String currentPw;

private Timer timer;
private Timer timer = null;
private BroadcastReceiver wifiConnectReceiver = null;
private WifiManager wifiManager;

Expand Down Expand Up @@ -83,13 +83,13 @@ public void onClick(DialogInterface dialog, int which) {
}
});

timer = new Timer();
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

Button retryButton = (Button) findViewById(R.id.retryButton);
retryButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
timer = new Timer();
wifiConnectReceiver = WifiScanConnect.connectAndRegisterReceiver(FailActivity.this, wifiManager, new IntentFilter(), new SsidPw(currentId, currentPw), timer);
}
});
Expand All @@ -100,7 +100,9 @@ public void onClick(View view) {
protected void onPause() {
super.onPause();

timer.cancel();
if (timer != null) {
timer.cancel();
}

try {
unregisterReceiver(wifiConnectReceiver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
import static com.example.hsh0908y.auto_wifi.utils.TextBlockGeometry.isOrderY;
import static com.example.hsh0908y.auto_wifi.utils.TextBlockGeometry.rotationNormalize;

// This class was initially designed to return untried SSID and Password whenever the extract method
// is called. However, we changed the design of the application to try extraction and connection only
// once. So our evaluation metrics is only for the first result that the extract method of this class
// returns.

public class SsidPwPick {

private final String[] SSID_TAGS = {"아이디", "id"};
private final String[] PW_TAGS = {"pw", "password", "비밀번호", "비번", "ps", "패스워드", "p/w", "p.w", "pu"};
private final String[] PW_TAGS = {"pw", "password", "비밀번호", "비번", "ps", "패스워드", "p/w", "p.w"};
private final String[] WIFI_TAGS = {"wifi", "wi-fi", "와이파이", "와이-파이"};
private final int NUM_TRY_PW_PER_COMPONENT = 2;

Expand All @@ -38,6 +43,9 @@ public class SsidPwPick {
private final TextBlockGraphComponent ssidTagComponent;
private final List<String> pwCandidateListFromTag;

// Single SsidPwPick object can deal only with single text detection result (textBlockList).

// Constructor of SsidPw for extractSsidPw
public SsidPwPick(List<TextBlock> textBlockList, List<WifiData> wifiDataList) {
this.textBlockList = textBlockList;
rotationNormalize(this.textBlockList);
Expand All @@ -51,6 +59,7 @@ public SsidPwPick(List<TextBlock> textBlockList, List<WifiData> wifiDataList) {
this.wifiDataList = wifiDataList;
}

// Constructor of SsidPw for extractPw
public SsidPwPick(List<TextBlock> textBlockList) {
this.textBlockList = textBlockList;
rotationNormalize(this.textBlockList);
Expand All @@ -59,11 +68,13 @@ public SsidPwPick(List<TextBlock> textBlockList) {
ssidTriedList = new ArrayList<>();
pwCandidateQueue = new LinkedList<>();

ssidTagComponent = getSsidTagComponent();
ssidTagComponent = null;
pwCandidateListFromTag = getPwCandidateListFromTag(true);
this.wifiDataList = null;
}

// Can be called multiple times and returns untried pair of SSID, Password each time it is called.
// When all of the candidates are returned,
public SsidPw extractSsidPw() {
if (wifiDataList == null ||
wifiDataList.isEmpty() || textBlockGraphComponentList.isEmpty()) {
Expand All @@ -80,6 +91,9 @@ public SsidPw extractSsidPw() {
int maximumScore = 0;
for (WifiData wifiData : wifiDataList) {
for (TextBlockGraphComponent component : textBlockGraphComponentList) {
// Not reflected in current implementation, but ComputeScoreSsid() can penaltize the
// component if it exist in ssidTriedList, in order to avoid same SSID to be chosen
// whenever this method is called.
int score = ComputeScoreSsid(wifiData, component);
if (score > maximumScore) {
maximumScore = score;
Expand Down Expand Up @@ -181,6 +195,10 @@ private List<String> getPwCandidateListFromTag(boolean regardWifiTag) {
return new ArrayList<>();
}

// There could be cases where PW Tag component is not found - Example : Word PW is detected
// as other letters.
// For this case, we try to infer from the position information of SSID Tag componet, if it
// exists.
int selectedIndex = -1;
if (ssidTagComponent != null) {
float minDistance = Float.MAX_VALUE;
Expand Down Expand Up @@ -260,10 +278,6 @@ private List<TextBlockGraphComponent> buildTextBlockGraphComponentList() {
textBlockGraphComponentList.add(new TextBlockGraphComponent(textBlockGraphComponent));
}

// for (TextBlockGraphComponent c : textBlockGraphComponentList) {
//// System.out.println(c.getConcatenatedDescription());
//// }

return textBlockGraphComponentList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
import java.util.ArrayList;
import java.util.List;

// Reference code : https://github.com/GoogleCloudPlatform/cloud-vision/tree/master/android/CloudVision

public class TextDetect {

private static final String CLOUD_VISION_API_KEY = "AIzaSyBeqND-h_JplQ_uV3_OyNTgORZLfRqMZOs";
private static final String CLOUD_VISION_API_KEY = BuildConfig.API_KEY;
private static final int MAX_TEXT_RESULTS = 20;
private static final int MAX_DIMENSION = 1200;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ public static BroadcastReceiver connectAndRegisterReceiver(
if (ssidPw.pw != null) {
config.preSharedKey = "\"" + ssidPw.pw + "\"";
}

// config.SSID = "\"" + "SPARCS-AP" + "\"";
// config.preSharedKey = "\"" + "tnfqkrtmtnfqkrtm" + "\"";
Log.d(TAG, config.SSID + " " + config.preSharedKey);

intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
Expand All @@ -72,6 +69,7 @@ public void onReceive(Context context, Intent intent) {
NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
networkInfo.isConnected() && wifiManager.getConnectionInfo().getSSID().equals(config.SSID)) {
Log.d(TAG, "Success");
Intent successIntent = new Intent(activity, SuccessActivity.class);
successIntent.putExtra("id", wifiManager.getConnectionInfo().getSSID());
activity.startActivity(successIntent);
Expand All @@ -95,7 +93,7 @@ public void run() {
activity.startActivity(failIntent);
}
}
}, 3500);
}, 4000);

return connectReceiver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static int getLengthOfLongestCommonSubstring(String s1, String s2) {
return maximum;
}

public static int getEditDistance(String s1, String s2) {
public static int getCustomizedEditDistance(String s1, String s2) {
int l1 = s1.length();
int l2 = s2.length();
int[][] dp = new int[l1 + 1][l2 + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,7 @@ public boolean LoadGroundTruth(String csvPath) {
}

public float evaluateAll(String saveDir, int wifiListSize, int numConfusingWifis) {
File dir = new File(saveDir);
File[] files = dir.listFiles();
List<File> fileList = new ArrayList<>();
for (File file : dir.listFiles()) {
String fileName = file.getName();
if (fileName.endsWith(".save")) {
fileList.add(file);
}
}
fileList.sort(new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
return getIntPrefix(file1.getName()) - getIntPrefix(file2.getName());
}
});
List<File> fileList = getSortedSavedFiles(saveDir);

float scoreSum = 0f;
int count = 0;
Expand All @@ -88,8 +74,8 @@ public int compare(File file1, File file2) {
String groundTruthSsid = groundTruthSsidMap.get(prefix);
String groundTruthPw = groundTruthPwMap.get(prefix);
if (groundTruthSsid != null || groundTruthPw != null) {
System.out.printf("[ %s ]\n", file.getName());
List<TextBlock> textBlockList = TextDetect.getTextBlockListFromSaved(file.getPath());
// System.out.printf("[ %s ]\n", file.getName());
List<TextBlock> textBlockList = TextDetectExperimental.getTextBlockListFromSaved(file.getPath());
scoreSum += evaluate(
textBlockList, groundTruthSsid, groundTruthPw,
wifiListSize, numConfusingWifis);
Expand All @@ -99,14 +85,42 @@ public int compare(File file1, File file2) {
return scoreSum / count;
}

static void printJsonAll(String saveDir) {
List<File> fileList = getSortedSavedFiles(saveDir);
for (File file : fileList) {
System.out.printf("[ %s ]\n", file.getName());
System.out.println(TextDetectExperimental.getJsonFromSaved(file.getPath()));
}
}

private static List<File> getSortedSavedFiles(String saveDir) {
File dir = new File(saveDir);
File[] files = dir.listFiles();
List<File> fileList = new ArrayList<>();
for (File file : dir.listFiles()) {
String fileName = file.getName();
if (fileName.endsWith(".save")) {
fileList.add(file);
}
}
fileList.sort(new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
return getIntPrefix(file1.getName()) - getIntPrefix(file2.getName());
}
});
return fileList;
}

public float evaluate(String savePath,
int wifiListSize, int numConfusingWifis) {
int prefix = getIntPrefix(Paths.get(savePath).getFileName().toString());
String groundTruthSsid = groundTruthSsidMap.get(prefix);
String groundTruthPw = groundTruthPwMap.get(prefix);

if (groundTruthSsid != null || groundTruthPw != null) {
List<TextBlock> textBlockList = TextDetect.getTextBlockListFromSaved(savePath);
List<TextBlock> textBlockList = TextDetectExperimental.getTextBlockListFromSaved(savePath);

return evaluate(textBlockList, groundTruthSsid, groundTruthPw,
wifiListSize, numConfusingWifis);
}
Expand All @@ -116,6 +130,10 @@ public float evaluate(String savePath,
private float evaluate(List<TextBlock> textBlockList,
String groundTruthSsid, String groundTruthPw,
int wifiListSize, int numConfusingWifis) {
// for (TextBlock b : textBlockList) {
// System.out.print(" {" + b.getDescription() + "} ");
// }

if (groundTruthSsid != null) {
return evaluateSsidPw(wifiListSize, numConfusingWifis,
groundTruthSsid, groundTruthPw, textBlockList);
Expand Down Expand Up @@ -161,8 +179,8 @@ private float evaluateSsidPw(
score = ssidScore * 0.5f + getStringScore(ssidPw.pw, groundTruthPw) * 0.5f;
}

System.out.printf("%s / %s\n%s / %s\n\n",
groundTruthSsid, ssidPw.ssid, groundTruthPw, ssidPw.pw);
// System.out.printf("%s / %s\n%s / %s\n\n",
// groundTruthSsid, ssidPw.ssid, groundTruthPw, ssidPw.pw);

return Math.min(1f, Math.max(0f, score));
}
Expand All @@ -172,7 +190,7 @@ private float evaluatePw(String groundTruthPw, List<TextBlock> textBlockList) {
String pw = pickTask.extractPw();
float score = getStringScore(pw, groundTruthPw);

System.out.printf("%s / %s\n\n", groundTruthPw, pw);
// System.out.printf("%s / %s\n\n", groundTruthPw, pw);

return Math.min(1f, Math.max(0f, score));
}
Expand All @@ -185,7 +203,7 @@ private static float getStringScore(String answer, String groundTruth) {
return 1.0f;
}
return 0.5f * (1 -
(float) Algorithm.getEditDistance(groundTruth, answer) / groundTruth.length());
(float) Algorithm.getCustomizedEditDistance(groundTruth, answer) / groundTruth.length());
}

private static String generateRandomString(int length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
*/
public class SsidPwPickEvaluateTest {

// NOTE: Since we didn't upload the sampledata to the Version control system,
// this test can't be run if you have cloned the repository from github.

static final String testDataDir = Paths.get(
System.getProperty("user.dir"), "sampledata").toString();

@Test
public void printEvaluation() {
String testDataDir =
Paths.get(System.getProperty("user.dir"), "sampledata").toString();
String saveDir = Paths.get(testDataDir, "output-test").toString();
Evaluation evaluation = new Evaluation();
evaluation.LoadGroundTruth(Paths.get(testDataDir, "ground_truth.csv").toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.ArrayList;
import java.util.List;

class TextDetect {
class TextDetectExperimental {

static List<TextBlock> getTextBlockListFromSaved(String savePath) {
try {
Expand Down
17 changes: 0 additions & 17 deletions android/gradle.properties

This file was deleted.

0 comments on commit b1748ed

Please sign in to comment.