Last active
February 3, 2018 19:45
-
-
Save isnullnull/4f56cda7df5b4f60f0fb4fb59169b7f5 to your computer and use it in GitHub Desktop.
万年アドベントカレンダーを実装した(OpenCV+Deep Neural Network+RoBoHoN) ref: https://qiita.com/n_u/items/4129564b137c37c40b0b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<hvml version="2.0"> | |
<head> | |
<producer>com.dev.zdev.opencv</producer> | |
<description>advent calendar オープンコール</description> | |
<scene value="com.dev.zdev.opencv.scn.call" /> | |
<version value="1.0" /> | |
<accost priority="75" topic_id="call" word="com.dev.zdev.opencv.acst.call" /> | |
</head> | |
<body> | |
<topic id="call" listen="false"> | |
<action index="1"> | |
<speech>カレンダーを開けるよ…</speech> | |
<behavior id="0x060030" type="normal"><wait ms="300"/></behavior> | |
</action> | |
<action index="2"> | |
<control function="adv_call" target="com.dev.zdev.opencv"/> | |
</action> | |
</topic> | |
</body> | |
</hvml> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compileSdkVersion 14 | |
buildToolsVersion "25.0.0" | |
minSdkVersion 8 | |
↓ | |
compileSdkVersion 21 | |
buildToolsVersion "25.0.2" | |
minSdkVersion 21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<hvml version="2.0"> | |
<head> | |
<producer>com.dev.zdev.opencv</producer> | |
<description>アドベントカレンダーのホーム起動シナリオ</description> | |
<scene value="home" /> | |
<version value="1.0" /> | |
<situation priority="78" topic_id="start" trigger="user-word">${Local:WORD_APPLICATION} eq | |
アドベントカレンダー | |
</situation> | |
<situation priority="78" topic_id="start" trigger="user-word"> | |
${Local:WORD_APPLICATION_FREEWORD} eq アドベントカレンダーあけて | |
</situation> | |
</head> | |
<body> | |
<topic id="start" listen="false"> | |
<action index="1"> | |
<speech>${resolver:speech_ok(${resolver:ok_id})}</speech> | |
<behavior id="${resolver:motion_ok(${resolver:ok_id})}" type="normal" /> | |
<control function="start_activity" target="home"> | |
<data key="package_name" value="com.dev.zdev.opencv" /> | |
<data key="class_name" value="com.dev.zdev.opencv.MainActivity" /> | |
</control> | |
</action> | |
</topic> | |
</body> | |
</hvml> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ResImage { | |
String label; | |
Bitmap bitmapImg; | |
} | |
private ResImage createContent(Bitmap original_img) { | |
final int IN_WIDTH = 300; | |
final int IN_HEIGHT = 300; | |
final float WH_RATIO = (float)IN_WIDTH / IN_HEIGHT; | |
final double IN_SCALE_FACTOR = 0.007843; | |
final double MEAN_VAL = 127.5; | |
final double THRESHOLD = 0.2; | |
try { | |
Loadnetwork(getApplicationContext()); | |
}catch (Exception ex) { | |
Log.e("load model", ex.getMessage()); | |
} | |
Mat img = new Mat(); | |
Utils.bitmapToMat(original_img.copy(Bitmap.Config.ARGB_8888, true), img); | |
Imgproc.cvtColor(img, img, Imgproc.COLOR_RGBA2RGB); | |
// Forward image through network. | |
Size sz = new Size(IN_WIDTH, IN_HEIGHT); | |
Scalar sc = new Scalar(MEAN_VAL, MEAN_VAL, MEAN_VAL); | |
Mat blob = Dnn.blobFromImage(img, IN_SCALE_FACTOR, sz, sc, false, false); | |
net.setInput(blob); | |
Mat detections = net.forward(); | |
int cols = img.cols(); | |
int rows = img.rows(); | |
Size cropSize; | |
if ((float)cols / rows > WH_RATIO) { | |
cropSize = new Size(rows * WH_RATIO, rows); | |
} else { | |
cropSize = new Size(cols, cols / WH_RATIO); | |
} | |
int y1 = (int)(rows - cropSize.height) / 2; | |
int y2 = (int)(y1 + cropSize.height); | |
int x1 = (int)(cols - cropSize.width) / 2; | |
int x2 = (int)(x1 + cropSize.width); | |
Mat subFrame = img.submat(y1, y2, x1, x2); | |
cols = subFrame.cols(); | |
rows = subFrame.rows(); | |
detections = detections.reshape(1, (int)detections.total() / 7); | |
ResImage res_img = new ResImage(); | |
for (int i = 0; i < detections.rows(); ++i) { | |
double confidence = detections.get(i, 2)[0]; | |
if (confidence > THRESHOLD) { | |
// 認識確度が設定閾値以上のものをひとつだけ処理 | |
int classId = (int)detections.get(i, 1)[0]; | |
int xLeftBottom = (int)(detections.get(i, 3)[0] * cols); | |
int yLeftBottom = (int)(detections.get(i, 4)[0] * rows); | |
int xRightTop = (int)(detections.get(i, 5)[0] * cols); | |
int yRightTop = (int)(detections.get(i, 6)[0] * rows); | |
Mat dstimg = subFrame.submat(yLeftBottom, yRightTop, xLeftBottom, xRightTop); | |
res_img.bitmapImg = Bitmap.createBitmap(dstimg.width(), dstimg.height(), Bitmap.Config.ARGB_8888); | |
Utils.matToBitmap(dstimg, res_img.bitmapImg); | |
res_img.label = classNames[classId]; | |
break; | |
} | |
} | |
return res_img; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<hvml version="2.0"> | |
<head> | |
<producer>com.dev.zdev.opencv</producer> | |
<description>advent calendar 結果通知</description> | |
<scene value="com.dev.zdev.opencv.scn.res" /> | |
<version value="1.0" /> | |
<accost priority="75" topic_id="res" word="com.dev.zdev.opencv.acst.res" /> | |
</head> | |
<body> | |
<topic id="res" listen="false"> | |
<action index="1"> | |
<speech>${memory_p:com.dev.zdev.opencv.res}が入ってたね!</speech> | |
<behavior id="assign" type="normal" /> | |
</action> | |
</topic> | |
</body> | |
</hvml> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* advent calendar シーン、accost、通知設定 | |
*/ | |
public static final String SCN_CALL = PACKAGE + ".scn.call"; | |
public static final String SCN_RES = PACKAGE + ".scn.res"; | |
public static final String ACC_CALL = ScenarioDefinitions.PACKAGE + ".acst.call"; | |
public static final String ACC_RES = ScenarioDefinitions.PACKAGE + ".acst.res"; | |
public static final String FUNC_CALL = "adv_call"; | |
/** | |
* memory_pを指定するタグ | |
*/ | |
public static final String MEM_P_RES = ScenarioDefinitions.TAG_MEMORY_PERMANENT + ScenarioDefinitions.PACKAGE + ".res"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment