Commit b697f429 authored by licaiwang99's avatar licaiwang99
Browse files

feat: 講退出會關掉程式,刪掉了所有部會用到的功能,現在說話會跟著字數的不同而增長或縮短

"#1"
parent 191717dd
package com.nuwarobotics.sdk.sample.tutorial
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
class APMICFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_apmic, container, false)
view.findViewById<Button>(R.id.asr).setOnClickListener {
(activity as MainActivity).getRobot().stopTTSandRecognize()
(activity as MainActivity).getRobot().startSpeech2Text(false)
val text2 = view.findViewById<Button>(R.id.asr).text.toString()
(activity as MainActivity).mainProcess(text2)
}
return view
}
companion object {
fun newInstance(): APMICFragment = APMICFragment()
}
}
\ No newline at end of file
......@@ -5,15 +5,12 @@ import android.os.Build
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.Fragment
import android.support.v7.app.ActionBar
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import com.nuwarobotics.sdk.sample.tutorial.api.apmic.ApplicationInterface
import com.nuwarobotics.service.IClientId
import com.nuwarobotics.service.agent.NuwaRobotAPI
import com.nuwarobotics.service.agent.RobotEventListener
......@@ -22,14 +19,11 @@ import com.nuwarobotics.service.camera.common.Constants
import com.nuwarobotics.service.camera.common.CsDebug
import com.nuwarobotics.service.camera.sdk.CameraSDK
import com.nuwarobotics.service.camera.sdk.OutputData
import io.ktor.client.HttpClient
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.time.delay
class MainActivity : AppCompatActivity() {
//Private lateinit var
private lateinit var mRobot: NuwaRobotAPI
private lateinit var mCameraSDK_facedect: CameraSDK
private lateinit var mCameraSDK_facerec: CameraSDK
......@@ -37,32 +31,7 @@ class MainActivity : AppCompatActivity() {
private lateinit var mToast: Toast
private lateinit var toolbar: ActionBar
private lateinit var toastTimer: CountDownTimer
private lateinit var api: ApplicationInterface
//Private
private var tts_status = TTS_STATUS.UN_LOCK
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_apmic -> {
mRobot.startTTS("啟動聊天模式")
val apmicFragment = APMICFragment.newInstance()
openFragment(apmicFragment)
mRobot.startWakeUp(true)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_other -> {
if(myFamily[myFamilyId] != "non")
{
face_recog = true
}
mRobot.startTTS("啟動人臉辨識模式")
val otherFragment = OtherFragment.newInstance()
openFragment(otherFragment)
return@OnNavigationItemSelectedListener true
}
}
false
}
//人臉辨識
private val mCameraSDKCallback: CameraSDK.CameraSDKCallback = object: CameraSDK.CameraSDKCallback
{
override fun onConnected(p0: Boolean) {
......@@ -74,7 +43,6 @@ class MainActivity : AppCompatActivity() {
@SuppressLint("MissingPermission")
override fun onOutput(p0: MutableMap<Int, OutputData>?) {
val sb = StringBuilder()
var temp: String = "0"
for (Int in p0?.keys!!)
{
var outputData = p0?.get(Int)
......@@ -85,7 +53,6 @@ class MainActivity : AppCompatActivity() {
//判斷有沒有偵測到人臉來設定狀態
if( outputData?.data?.indexOf("bottom") == -1)
{
//showToast("錯誤"+ outputData?.data?.indexOf("bottom"), 1)
face_status = false
}
if( outputData?.data?.indexOf("bottom") != -1)
......@@ -95,30 +62,6 @@ class MainActivity : AppCompatActivity() {
if (outputData != null) {
if(face_status) {
//TODO: 思考更精準的人臉辨識方法
//存人臉的四個部分來判斷,不知道會不會更準?
bottom = outputData.data.indexOf("bottom")
left = outputData.data.indexOf("left")
right = outputData.data.indexOf("right")
top = outputData.data.indexOf("top")
endstring = outputData.data.lastIndexOf("]")
temp = outputData.data.substring(bottom, left).replace("bottom", "").replace(",", "").replace(":", "")
if (temp.substring(1, temp.length - 1) == "") {
bottom = 1
}
bottom = temp.substring(1, temp.length - 1).toInt()
temp = outputData.data.substring(left, right).replace("left", "").replace(",", "").replace(":", "")
if (temp.substring(1, temp.length - 1) == "") {
left = 1
}
left = temp.substring(1, temp.length - 1).toInt()
temp = outputData.data.substring(right, top).replace("right", "").replace(",", "").replace(":", "")
if (temp.substring(1, temp.length - 1) == "") {
right = 1
}
right = temp.substring(1, temp.length - 1).toInt()
if ((outputData?.data?.indexOf("conf") != -1)) {
var idx = outputData.data.lastIndexOf("idx")
match = outputData.data.indexOf("conf")
......@@ -135,103 +78,54 @@ class MainActivity : AppCompatActivity() {
}
welcomeMes()
}
//用內建的參數conf來判斷
/* if (outputData?.data?.indexOf("conf") != -1)
{
var idx = outputData.data.lastIndexOf("idx")
match = outputData.data.indexOf("conf")
temp_conf = outputData.data.substring(match, idx).replace("conf", "").replace(",", "").replace(":", "")
temp_conf = temp_conf.substring(2,8)
clock+=temp_conf.toFloat()
//有存到人臉之後才能做判斷
//faceRecognize()
}*/
}
}
}
}
}
internal fun faceRecognize()
{
if (temp_conf.toFloat() > 0.75 && face_recog && catch==false)
{
getRobot().startTTS("抓到" + myFamily[myFamilyId] + "了")
catch = true
}
}
internal var mBuildGrammar: Boolean = false
internal var match: Int=0
internal var face_status = false
internal var catch = true
internal var clock = 0.0
internal var processTime = 0.0
internal var temp_conf:String="0"
internal var replyLength: Long = 4
enum class TTS_STATUS { LOCK, UN_LOCK }
internal fun welcomeMes()
{
if(processTime <30 ) {
if (clock > 0.5) {
getRobot().startTTS("需要凱比幫忙嗎?")
talkwithKebbi()
// 一個字345
replyLength = 2415
speakImage()
backImage()
replyLength = 0
if(catch)
{
getRobot().startSpeech2Text(false)
}
clock = 0.0
processTime = 65.0
}
}
}
internal fun talkwithKebbi()
{
if(catch)
{
getRobot().startSpeech2Text(false)
}
}
internal fun Exit()
{
mCameraSDK_facedect.unregister()
mCameraSDK_facerec.unregister()
bottom = 0
left = 0
right = 0
top = 0
endstring = 0
//人臉資料備份參數
}
//Internal var
internal var mBuildGrammar: Boolean = false
// TODO: 把人名跟人臉的資料儲存改成動態陣列
//可以拿來存人臉資料,方便以後的操作
internal var bottom: Int = 0
internal var left: Int = 0
internal var right:Int = 0
internal var top: Int = 0
//人臉資料備份參數
internal var bottom2 = arrayListOf<Int>()
internal var left2 = arrayListOf<Int>()
internal var right2 = arrayListOf<Int>()
internal var top2 = arrayListOf<Int>()
internal var endstring: Int = 0
internal var match: Int=0
//internal var user = ""
internal var myFamilyId = 0
internal var myFamily = arrayListOf<String>()
internal var face_status = false
internal var face_recog = false
internal var catch = true
internal var clock = 0.0
internal var processTime = 0.0
internal var temp_conf:String="0"
//Other
enum class TTS_STATUS { LOCK, UN_LOCK }
//工程師測試用---強制拖出人臉辨識
//OpentalkApi
internal fun mainProcess(text: String) {
val Oapi = OpentalkApi()
if (!text.isBlank()) {
showToast("語音轉文字:$text")
if(text == "退出")
{
this.finish()
}
Oapi.getAnalysisMatch({
GlobalScope.apply {
launch(Dispatchers.Main) {
val fromAnalysisMatch = JSON.parse(FromAnalysisMatch.serializer(), it)
val reply = fromAnalysisMatch.result[0].send.text
replyLength = (reply.length)*345.toLong()
getRobot().startTTS(reply)
speakImage()
backImage()
......@@ -242,7 +136,6 @@ class MainActivity : AppCompatActivity() {
processTime = 70.00
}
}
//Toast處理
internal fun showToast(msg: String) {
showToast(msg, 0)
}
......@@ -253,7 +146,6 @@ class MainActivity : AppCompatActivity() {
} catch (e: UninitializedPropertyAccessException) {
Log.d(TAG, "第一次執行")
}
toastTimer = object : CountDownTimer(duration.toLong(), 1000) {
override fun onTick(millisUntilFinished: Long) {
Log.d(TAG, "TICK")
......@@ -273,12 +165,6 @@ class MainActivity : AppCompatActivity() {
mToast.show()
}
}
//換臉
internal fun setImage() {
mUI.post {
imageView1.setImageResource(R.drawable.face)
}
}
internal fun speakImage() {
try {
imageView1.visibility = View.GONE
......@@ -295,17 +181,14 @@ class MainActivity : AppCompatActivity() {
imageView2.visibility = View.GONE
imageView1.visibility = View.VISIBLE
imageView1.setImageResource(R.drawable.face)
}, 3000)
}, replyLength)
}
//反正不太會用到的部分
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
hideStatusBar()
supportActionBar!!.hide()
setContentView(R.layout.activity_main)
toolbar = supportActionBar!!
navigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
api = ApplicationInterface(resources.getString(R.string.api_url), resources.getString(R.string.email), resources.getString(R.string.token), resources.getString(R.string.session))
mUI = Handler()
mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT)
mRobot = NuwaRobotAPI(this, IClientId(packageName))
......@@ -313,14 +196,7 @@ class MainActivity : AppCompatActivity() {
mCameraSDK_facerec = CameraSDK(this@MainActivity)
mCameraSDK_facedect.register(mCameraSDKCallback, Constants.FACE_DETECTION)
mCameraSDK_facerec.register(mCameraSDKCallback, Constants.FACE_RECOGNITION)
myFamily.add("non")
getRobot().startTTS("你好我是凱比")
registerNuwaRobotListener()
/* 留著看以後有沒有要做物件辨識
private lateinit var mCameraSDK_objrec: CameraSDK (J個要放前面)
mCameraSDK_objrec.register(mCameraSDKCallback, Constants.OBJ_RECOGNITION)
mCameraSDK_objrec= CameraSDK(this@MainActivity)
*/
}
override fun onResume() {
super.onResume()
......@@ -339,12 +215,6 @@ class MainActivity : AppCompatActivity() {
actionBar?.hide()
}
}
private fun openFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
internal fun getRobot(): NuwaRobotAPI {
return mRobot
}
......@@ -543,6 +413,21 @@ class MainActivity : AppCompatActivity() {
internal val TAG = "xxx_nuwa_sdk"
}
}
......
package com.nuwarobotics.sdk.sample.tutorial
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import kotlinx.serialization.UnstableDefault
class OtherFragment : Fragment() {
@UnstableDefault
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_other, container, false)
view.findViewById<Button>(R.id.speak).setOnClickListener {
//(activity as MainActivity).user = view.findViewById<EditText>(R.id.tts_text_input).text.toString()
// (activity as MainActivity).myFamily.add(view.findViewById<EditText>(R.id.tts_text_input).text.toString())
// (activity as MainActivity).myFamilyId++
// (activity as MainActivity).speakImage()
//(activity as MainActivity).getRobot().startTTS("好的"+(activity as MainActivity).user+"按下人臉辨識讓我看看")
// (activity as MainActivity).getRobot().startTTS("好的"+ (activity as MainActivity).myFamily[(activity as MainActivity).myFamilyId]+"按下人臉辨識讓我看看")
// (activity as MainActivity).backImage()
}
view.findViewById<Button>(R.id.camera).setOnClickListener {
// (activity as MainActivity).getRobot().startTTS("你是" + (activity as MainActivity).myFamily[(activity as MainActivity).myFamilyId] + "嗎")
// (activity as MainActivity).getRobot().startTTS("你是家族裡的第" + (activity as MainActivity).myFamilyId + "位成員")
// (activity as MainActivity).getRobot().startTTS("現在開始記錄" + (activity as MainActivity).myFamily[(activity as MainActivity).myFamilyId] + "的臉部特徵")
// (activity as MainActivity).bottom2.add((activity as MainActivity).bottom)
// (activity as MainActivity).right2.add((activity as MainActivity).right)
// (activity as MainActivity).left2.add((activity as MainActivity).left)
// (activity as MainActivity).top2.add((activity as MainActivity).top)
// (activity as MainActivity).getRobot().startTTS("完成")
// (activity as MainActivity).face_recog = true
}
view.findViewById<Button>(R.id.myFamily).setOnClickListener {
}
view.findViewById<Button>(R.id.EXIT).setOnClickListener {
(activity as MainActivity).getRobot().startTTS("緊急拖出")
(activity as MainActivity).Exit()
}
return view
}
companion object {
fun newInstance(): OtherFragment = OtherFragment()
}
}
package com.nuwarobotics.sdk.sample.tutorial.api.apmic
import khttp.post
import khttp.responses.Response
import org.json.JSONException
var optionsHashMap: HashMap<String, String> = hashMapOf()
data class ApplicationInterface(private val url: String, private val email: String, private val token: String, private val session: String) {
private lateinit var response: Response
fun call(text: String): ApplicationInterface {
response = post(
url = url,
json = mapOf(
"email" to email,
"token" to token,
"tag" to text,
"tsession" to session
)
)
return this
}
fun getResponse(): ExtendedResponse {
return ExtendedResponse(this.response)
}
enum class STATUS { ERROR, NO_PERMISSION, OK }
data class ExtendedResponse(private val response: Response) {
fun getStatus(): STATUS {
return try {
when (response.jsonObject.getString("status")) {
"succeed" -> STATUS.OK
"error" -> STATUS.NO_PERMISSION
else -> STATUS.ERROR
}
} catch (e: JSONException) {
STATUS.ERROR
}
}
fun getOptions(): ArrayList<String> {
val tempArrayList = ArrayList<String>()
val optionJSONArray = response.jsonObject.getJSONArray("option")
for (i in 0..(optionJSONArray.length() - 1)) {
val option = optionJSONArray.getJSONObject(i).getString("name")
tempArrayList.add(option)
}
return tempArrayList
}
fun getOptionsHashMap(): HashMap<String, String> {
val tempHashMap = HashMap<String, String>()
val optionJSONArray = response.jsonObject.getJSONArray("option")
for (i in 0..(optionJSONArray.length() - 1)) {
val key = optionJSONArray.getJSONObject(i).getString("key")
val option = optionJSONArray.getJSONObject(i).getString("name")
tempHashMap[option] = key
}
return tempHashMap
}
fun setOptionsHashMap(options: HashMap<String, String>) {
optionsHashMap = options
}
fun getAnswer(): String {
return response.jsonObject.getJSONArray("reply").getJSONObject(0).getString("answer")
}
fun getReply(): String {
return response.jsonObject.getString("reply")
}
/* New */
fun getAction(): String {
return response.jsonObject.getJSONArray("reply").getJSONObject(0).getString("action")
}
fun getMedia(): String {
return response.jsonObject.getJSONArray("reply").getJSONObject(0).getString("media")
}
/* Boolean Status */
fun isMultipleChoice(): Boolean {
if (response.jsonObject.getInt("type") == -1) return true
return false
}
fun isFinalStep(): Boolean {
if (response.jsonObject.getJSONArray("reply").getJSONObject(0).getInt("step") != 0) return false
return true
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<vector
android:height="108dp"
android:width="108dp"
android:viewportHeight="108"
android:viewportWidth="108"
xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#008577"
android:pathData="M0,0h108v108h-108z"/>
<path android:fillColor="#00000000" android:pathData="M9,0L9,108"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M19,0L19,108"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M29,0L29,108"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M39,0L39,108"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M49,0L49,108"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M59,0L59,108"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M69,0L69,108"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M79,0L79,108"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M89,0L89,108"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M99,0L99,108"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M0,9L108,9"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M0,19L108,19"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M0,29L108,29"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M0,39L108,39"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M0,49L108,49"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M0,59L108,59"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M0,69L108,69"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M0,79L108,79"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M0,89L108,89"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M0,99L108,99"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M19,29L89,29"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M19,39L89,39"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M19,49L89,49"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M19,59L89,59"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M19,69L89,69"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M19,79L89,79"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
<path android:fillColor="#00000000" android:pathData="M29,19L29,89"
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>