function main()
{
    $ret = new TomoeReturn();
    //返回客户端的信息
    $type = isset($_POST['type']) ? $_POST['type'] : null;
    //$type = TYPE_RECOGNIZE; //debug
    if (!isset($type)) {
        $ret->msgno = MSG_ERR;
        //提示没有输入
        $ret->msg = MSG_ERR_NOINPUT;
    } else {
        switch ($type) {
            case TYPE_RECOGNIZE:
                //识别
                $writing = isset($_POST['c']) ? json_decode($_POST['c']) : null;
                //获取客户端传过来的笔画
                $rank = isset($_POST['rank']) ? $_POST['rank'] : 100;
                //$rank = 10;   //debug
                //$writing = debug_getInput();  //debug
                recognize($ret, $writing, $rank);
                break;
            case TYPE_GET_CHARS:
                //获取汉字(用于学习)
                $trainType = isset($_POST['trainType']) ? $_POST['trainType'] : null;
                $char = isset($_POST['c']) ? $_POST['c'] : null;
                $unicodeFrom = isset($_POST['unicodeFrom']) ? $_POST['unicodeFrom'] : null;
                $unicodeTo = isset($_POST['unicodeTo']) ? $_POST['unicodeTo'] : null;
                get_chars($ret, $trainType, $char, $unicodeFrom, $unicodeTo);
                break;
            case TYPE_LEARN:
                //学习
                $writing = isset($_POST['c']) ? json_decode($_POST['c']) : null;
                //获取客户端传过来的笔画
                $id = isset($_POST['id']) ? $_POST['id'] : null;
                $user_id = isset($_POST['user_id']) ? $_POST['user_id'] : null;
                //$id = 1;    //debug;
                //$writing = debug_getInput();//debug;
                learn($ret, $writing, $id, $user_id);
                //debug_train($writing, $ret);
                break;
            case TYPE_GET_WRITINGS:
                //获取笔迹(用于管理)
                $char = isset($_POST['c']) ? $_POST['c'] : null;
                getWritings($ret, $char);
                break;
            case TYPE_DEL_WRITING:
                //删除某笔画
                //delWriting($writing, $ret);
                break;
            case TYPE_UNKNOWN:
                //未知请求
            //未知请求
            default:
                $ret->msgno = MSG_ERR;
                $ret->msg = MSG_ERR_UNKNOWN_REQUEST;
                break;
        }
    }
    echo json_encode($ret);
}
Exemple #2
0
// the list of person_name to train and recognize for
$person_names = array("denny", "beauty");
// store the face_ids obtained by detection/detect API
$face_ids = array();
// register new people, detect faces
foreach ($person_names as $person_name) {
    detect($api, $person_name, $face_ids);
}
// the name of group for testing
$group = "sample_group";
// generate a new group, add people into group
create_group($api, $group, $person_names);
// generate training model for group
train($api, $group);
// finally, search people in the group
recognize($api, $person_names[0], $group);
/* 
 *	create new person, detect faces from person's image_url
 */
function detect(&$api, $person_name, &$face_ids)
{
    // obtain photo_url to train
    $url = getTrainingUrl($person_name);
    // detect faces in this photo
    $result = $api->face_detect($url);
    // skip errors
    if (empty($result->face)) {
        return false;
    }
    // skip photo with multiple faces (we are not sure which face to train)
    if (count($result->face) > 1) {
Exemple #3
0
 // Если есть в базе отсылаем сообщение //
 $reading = $vk->request('messages.markAsRead', array('peer_id' => $uid));
 sleep(1);
 $typing = $vk->request('messages.setActivity', array('type' => 'typing', 'user_id' => $uid));
 sleep(1);
 $repquotes = array("\"", "\\'");
 // фильтруем сторонние символы
 $filtered = addslashes(str_replace($repquotes, '', $value['body']));
 $mes = file_get_contents($config['url'] . '/sp.php?session=' . $row['chatid'] . '&text=' . urlencode($filtered));
 $send = $vk->request('messages.send', array('message' => strip_tags($mes), 'uid' => $value['uid']));
 if ($send['error']['error_code'] == '14' and $config['antigate'] !== null) {
     // Загружаем капчу на сервер //
     file_put_contents("captcha/captcha.jpg", file_get_contents($send['error']['captcha_img']));
     // Уникальный ID капчи //
     $captcha['id'] = $send['error']['captcha_sid'];
     $captcha['key'] = recognize("captcha/captcha.jpg", $config['antigate'], false, "antigate.com");
     // Повторяем отправку вместе с разгаданной капчей //
     $send = $vk->request('messages.send', array('message' => strip_tags($mes), 'uid' => $value['uid'], 'captcha_sid' => $captcha['id'], 'captcha_key' => $captcha['key']));
 } elseif ($send['error']['error_code'] == '14' and $config['antigate'] == null) {
     // Загружаем капчу на сервер //
     file_put_contents("captcha/captcha.jpg", file_get_contents($send['error']['captcha_img']));
     // Уникальный ID капчи //
     if (isset($_GET['key'])) {
         $captcha['id'] = $send['error']['captcha_sid'];
         $captcha['key'] = $_GET['key'];
         // Повторяем отправку вместе с разгаданной капчей //
         $send = $vk->request('messages.send', array('message' => strip_tags($mes), 'uid' => $value['uid'], 'captcha_sid' => $captcha['id'], 'captcha_key' => $captcha['key']));
         ?>
             <script>history.go(-1);</script>
             <?php 
     }
Exemple #4
0
 public function responseMsg()
 {
     //get post data, May be due to the different environments
     $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
     //extract post data
     if (!empty($postStr)) {
         $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
         $fromUsername = $postObj->FromUserName;
         $toUsername = $postObj->ToUserName;
         $msgType = $postObj->MsgType;
         if ($msgType == "text") {
             $keyword = trim($postObj->Content);
             $time = time();
             $textTpl = "<xml>\n\t\t\t\t\t\t\t\t<ToUserName><![CDATA[%s]]></ToUserName>\n\t\t\t\t\t\t\t\t<FromUserName><![CDATA[%s]]></FromUserName>\n\t\t\t\t\t\t\t\t<CreateTime>%s</CreateTime>\n\t\t\t\t\t\t\t\t<MsgType><![CDATA[%s]]></MsgType>\n\t\t\t\t\t\t\t\t<Content><![CDATA[%s]]></Content>\n\t\t\t\t\t\t\t\t<FuncFlag>0</FuncFlag>\n\t\t\t\t\t\t\t\t</xml>";
             if (!empty($keyword)) {
                 $api_key = "SEwcXuDQE7ZcGM0Fxz2B02zb";
                 // initialize client object
                 $api = new BaiduTranslateClient($api_key);
                 $result = $api->translate($keyword, "auto", "auto");
                 $contentStr = "Translation Failed!";
                 if (!empty($result->trans_result)) {
                     if (count($result->trans_result) > 0) {
                         $contentStr = $result->trans_result[0]->dst;
                     }
                 }
                 $msgType = "text";
                 //$contentStr = "Hi, I'm Mars Robot! 有何贵干? 你可以发送位置,试试看。";
                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                 echo $resultStr;
             } else {
                 echo "Input something...";
             }
         } else {
             if ($msgType == "location") {
                 $location_x = $postObj->Location_X;
                 $location_y = $postObj->Location_Y;
                 $scale = $postObj->Scale;
                 $label = $postObj->Label;
                 $time = time();
                 $textTpl = "<xml>\n\t\t\t\t\t\t\t\t<ToUserName><![CDATA[%s]]></ToUserName>\n\t\t\t\t\t\t\t\t<FromUserName><![CDATA[%s]]></FromUserName>\n\t\t\t\t\t\t\t\t<CreateTime>%s</CreateTime>\n\t\t\t\t\t\t\t\t<MsgType><![CDATA[%s]]></MsgType>\n\t\t\t\t\t\t\t\t<Content><![CDATA[%s]]></Content>\n\t\t\t\t\t\t\t\t<FuncFlag>0</FuncFlag>\n\t\t\t\t\t\t\t\t</xml>";
                 $api_key = "0e4fde2b4acbc043abdb68df511359ae";
                 // initialize client object
                 $api = new BaiduMapClient($api_key);
                 $result = $api->geocoder_location($location_x . "," . $location_y);
                 if (!empty($result)) {
                     $msgType = "text";
                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $label . $result);
                     echo $resultStr;
                 }
             } else {
                 if ($msgType == "image") {
                     $url = $postObj->PicUrl;
                     $time = time();
                     $api_key = "e336859e1099669c6662a3ca76c590b8";
                     $api_secret = "HcMo_lQFQ4RZ1qrijxjjnzoHeNjxMc18";
                     // initialize client object
                     $api = new FacePPClient($api_key, $api_secret);
                     $person_name = md5(time() . rand());
                     //$file_name = $this->getFileByWget($url, $person_name . ".jpg");
                     $ret = $this->downloadImage($url, "/home/clasix/sites/weixin/images/" . $person_name);
                     if (!$ret) {
                         $textTpl = "<xml>\n\t\t\t\t\t\t\t\t<ToUserName><![CDATA[%s]]></ToUserName>\n\t\t\t\t\t\t\t\t<FromUserName><![CDATA[%s]]></FromUserName>\n\t\t\t\t\t\t\t\t<CreateTime>%s</CreateTime>\n\t\t\t\t\t\t\t\t<MsgType><![CDATA[%s]]></MsgType>\n\t\t\t\t\t\t\t\t<Content><![CDATA[%s]]></Content>\n\t\t\t\t\t\t\t\t<FuncFlag>0</FuncFlag>\n\t\t\t\t\t\t\t\t</xml>";
                         $msgType = "text";
                         $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, "File Download Failed!");
                         echo $resultStr;
                     } else {
                         //
                         // do search by url_img
                         $group = "sample_group";
                         $face_ids = array();
                         detect($api, $person_name, $face_ids);
                         $result = recognize($api, $person_name, $group);
                         if (!empty($result)) {
                             $textTpl = "<xml>\n\t\t\t\t\t\t\t\t\t <ToUserName><![CDATA[%s]]></ToUserName>\n\t\t\t\t\t\t\t\t\t <FromUserName><![CDATA[%s]]></FromUserName>\n\t\t\t\t\t\t\t\t\t <CreateTime>%s</CreateTime>\n\t\t\t\t\t\t\t\t\t <MsgType><![CDATA[%s]]></MsgType>\n\t\t\t\t\t\t\t\t\t <Content><![CDATA[]]></Content>\n\t\t\t\t\t\t\t\t\t <ArticleCount>1</ArticleCount>\n\t\t\t\t\t\t\t\t\t <Articles>\n\t\t\t\t\t\t\t\t\t <item>\n\t\t\t\t\t\t\t\t\t <Title><![CDATA[Similar Pic]]></Title>\n\t\t\t\t\t\t\t\t\t <Description><![CDATA[nothing]]></Description>\n\t\t\t\t\t\t\t\t\t <PicUrl><![CDATA[%s]]></PicUrl>\n\t\t\t\t\t\t\t\t\t <Url><![CDATA[%s]]></Url>\n\t\t\t\t\t\t\t\t\t </item>\n\t\t\t\t\t\t\t\t\t </Articles>\n\t\t\t\t\t\t\t\t\t <FuncFlag>0</FuncFlag>\n\t\t\t\t\t\t\t\t\t </xml>";
                             $msgType = "news";
                             $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, getWeiXinUrl($result), getWeiXinUrl($result));
                             echo $resultStr;
                         } else {
                             $textTpl = "<xml>\n\t\t\t\t\t\t\t\t\t<ToUserName><![CDATA[%s]]></ToUserName>\n\t\t\t\t\t\t\t\t\t<FromUserName><![CDATA[%s]]></FromUserName>\n\t\t\t\t\t\t\t\t\t<CreateTime>%s</CreateTime>\n\t\t\t\t\t\t\t\t\t<MsgType><![CDATA[%s]]></MsgType>\n\t\t\t\t\t\t\t\t\t<Content><![CDATA[%s]]></Content>\n\t\t\t\t\t\t\t\t\t<FuncFlag>0</FuncFlag>\n\t\t\t\t\t\t\t\t\t</xml>";
                             $msgType = "text";
                             $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, "No Similar Pictures");
                             echo $resultStr;
                         }
                         // do train job
                         $api->group_add_person($person_name, $group);
                         train($api, $group);
                     }
                 } else {
                     echo "";
                     exit;
                 }
             }
         }
     } else {
         echo "";
         exit;
     }
 }
Exemple #5
0
    return $result;
}
function recognize($file, $key)
{
    $uuid = generateRandomSelection(0, 30, 64);
    $uuid = implode($uuid);
    $uuid = substr($uuid, 1, 32);
    $curl = curl_init();
    $url = 'https://asr.yandex.net/asr_xml?' . http_build_query(array('key' => $key, 'uuid' => $uuid, 'topic' => 'notes', 'lang' => 'ru-RU'));
    curl_setopt($curl, CURLOPT_URL, $url);
    $data = file_get_contents(realpath($file));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: audio/x-wav'));
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    $response = curl_exec($curl);
    $err = curl_errno($curl);
    curl_close($curl);
    if ($err) {
        throw new exception("curl err {$err}");
    }
    echo $response;
}
print_r($argv);
$filename = $argv[1];
$key = $argv[2];
recognize($filename, $key);
print "done\n";