def post(self): ret = {'status': 0, 'message': 'OK'} db = Mydb() try: upfilepath = '' ivecfile = '' upload_path = config_get('wavdir', 'sre') if not os.path.exists(upload_path): os.makedirs(upload_path) logger.info('upload path is ' + upload_path) ivec_path = config_get('ivectordir', 'sre') validframes = int(config_get('validframes', 'sre')) if not os.path.exists(ivec_path): os.makedirs(ivec_path) logger.info('ivec path is ' + ivec_path) info = self.request.protocol + '://' + self.request.host + ', method=' + self.request.method + ', access url=' + self.request.uri logger.info(info) body = self.request.body_arguments Args = [ self_argument('UserId', required=True, helpinfo='miss user id'), self_argument('IsSelf', required=True, helpinfo='miss isself symbol') ] s, vals = DataIsValid(Args, body) if s: raise Exception(vals) userid = vals['UserId'] isself = vals['IsSelf'] isself_symbol = False if isself == 'yes': isself_symbol = True elif isself != 'no': logger.warning('isself param maybe wrong:' + isself) logger.info('userid is ' + userid) if not (userid): raise Exception('No user id') res, userinfo = db.get('SELECT * FROM user WHERE userid="%s"' % (userid)) if res != 0: raise Exception('cannot find user: %s information.' % (userid)) if len(userinfo) > 0: latest_info = sorted(userinfo, key=lambda x: int(x['timeid']), reverse=True)[0] else: raise Exception('%s\'s userinfo is empty' % (userid)) logger.info('query user %s info:%s' % (userid, str(latest_info))) file_metas = self.request.files['UploadFile'] wav_uuid = uuid.uuid1() logger.info('generate uuid:' + str(wav_uuid)) wavpath = upload_path + '/' + userid if not os.path.exists(wavpath): os.makedirs(wavpath) for meta in file_metas: filename = meta['filename'] upfilepath = wavpath.rstrip('/') + '/' + str( wav_uuid) + '_' + str(isself_symbol) + '_' + filename with open(upfilepath, 'wb') as up: up.write(meta['body']) up.close() break ivecfile_path = ivec_path.rstrip('/') + '/' + userid + '/' if not os.path.exists(ivecfile_path): os.makedirs(ivecfile_path) ivecfile = ivecfile_path.rstrip('/') + '/' + str( wav_uuid) + '.ivec' ubm = get_ubm() sre_compute = CpuCompute() #use str function is to avoid string encode error if not sre_compute.Compute(ubm, str(upfilepath), str(ivecfile), int(validframes)): raise Exception('compute ivector failed') sre_score = ScorePLDA() mean = get_mean() if not mean: raise Exception('ubm is not inited') #get all user enrolling ivecotr res, users = db.get( 'select a.* from user a where timeid=(select max(timeid) from user where userid=a.userid)' ) if res != 0: raise Exception(users) #use str function is to avoid string encode error scores = {} for enroll_user in users: score = float( sre_score.score_plda(ubm.ubm, str(enroll_user['ivecpath']), str(ivecfile), str(mean))) if not enroll_user['userid'] in scores.keys(): scores[enroll_user['userid']] = score else: raise Exception("duplicate user score") speaker_top3 = sorted(scores.items(), key=lambda x: float(x[1]), reverse=True)[0:3] speaker = speaker_top3[0][0] speaker_top3_str = str(speaker_top3) logger.info("speaker score top3:" + speaker_top3_str) ret['message'] = 'speaker is %s' % (speaker) except Exception, e: logger.error(str(e)) ret = {'status': -1, 'message': str(e)} if os.path.isfile(upfilepath): os.remove(upfilepath) if os.path.isfile(ivecfile): os.remove(ivecfile)
def post(self): ret = {'status': 0, 'message': 'OK'} db = Mydb() try: upfilepath = '' ivecfile = '' upload_path = config_get('wavdir', 'sre') dependent_threshold = float( config_get('dependent-threshold', 'sre')) independent_threshold = float( config_get('independent-threshold', 'sre')) if not os.path.exists(upload_path): os.makedirs(upload_path) logger.info('upload path is ' + upload_path) ivec_path = config_get('ivectordir', 'sre') validframes = int(config_get('validframes', 'sre')) if not os.path.exists(ivec_path): os.makedirs(ivec_path) logger.info('ivec path is ' + ivec_path) info = self.request.protocol + '://' + self.request.host + ', method=' + self.request.method + ', access url=' + self.request.uri logger.info(info) body = self.request.body_arguments Args = [ self_argument('UserId', required=True, helpinfo='miss user id'), self_argument('IsSelf', required=True, helpinfo='miss isself symbol'), self_argument('TextRelevance', required=True, helpinfo='miss text relevance symbol') ] s, vals = DataIsValid(Args, body) if s: raise Exception(vals) userid = vals['UserId'] isself = vals['IsSelf'] text_relevance = vals['TextRelevance'] isself_symbol = False if isself == 'yes': isself_symbol = True elif isself != 'no': logger.warning('isself param maybe wrong:' + isself) text_relevance_symbol = False if text_relevance == 'yes': text_relevance_symbol = True elif text_relevance != 'no': logger.warning('text_relevance param maybe wrong:' + text_relevance) logger.info('userid is ' + userid) if not (userid): raise Exception('No user id') res, userinfo = db.get('SELECT * FROM user WHERE userid="%s"' % (userid)) if res != 0: raise Exception('cannot find user: %s information.' % (userid)) if len(userinfo) > 0: latest_info = sorted(userinfo, key=lambda x: int(x['timeid']), reverse=True)[0] else: raise Exception('%s\'s userinfo is empty' % (userid)) logger.info('query user %s info:%s' % (userid, str(latest_info))) file_metas = self.request.files['UploadFile'] wav_uuid = uuid.uuid1() logger.info('generate uuid:' + str(wav_uuid)) wavpath = upload_path + '/' + userid if not os.path.exists(wavpath): os.makedirs(wavpath) for meta in file_metas: filename = meta['filename'] upfilepath = wavpath.rstrip('/') + '/' + str( wav_uuid) + '_' + str(isself_symbol) + '_' + filename with open(upfilepath, 'wb') as up: up.write(meta['body']) up.close() break ivecfile_path = ivec_path.rstrip('/') + '/' + userid + '/' if not os.path.exists(ivecfile_path): os.makedirs(ivecfile_path) ivecfile = ivecfile_path.rstrip('/') + '/' + str( wav_uuid) + '.ivec' ubm = get_ubm() sre_compute = CpuCompute() #use str function is to avoid string encode error if not sre_compute.Compute(ubm, str(upfilepath), str(ivecfile), int(validframes)): raise Exception('compute ivector failed') sre_score = ScorePLDA() mean = get_mean() if not mean: raise Exception('ubm is not inited') #use str function is to avoid string encode error score = float( sre_score.score_plda(ubm.ubm, str(latest_info['ivecpath']), str(ivecfile), str(mean))) res, _ = db.modify( 'INSERT INTO valid (userid, wavpath, ivecpath, isself, score) values ("%s", "%s", "%s", %s, %f);' % (userid, upfilepath, ivecfile, isself_symbol, score)) if res != 0: raise Exception('insert record into user error') threshold = 0 if text_relevance_symbol: threshold = dependent_threshold ret['message'] = 'Text-dependent valid function, ' else: threshold = independent_threshold ret['message'] = 'Text-independent valid function, ' if score > threshold: ret['message'] += 'you are in person, threshold is ' + str( threshold) else: ret['message'] += 'you are not in person, threshold is ' + str( threshold) ret['message'] += ', score is %f' % (score) except Exception, e: logger.error(str(e)) ret = {'status': -1, 'message': str(e)} if os.path.isfile(upfilepath): os.remove(upfilepath) if os.path.isfile(ivecfile): os.remove(ivecfile)
def post(self): ret = {'status': 0, 'message': 'OK'} db = Mydb() try: upfilepath = '' ivecfile = '' upload_path = config_get('wavdir', 'sre') if not os.path.exists(upload_path): os.makedirs(upload_path) logger.info('upload path is ' + upload_path) ivec_path = config_get('ivectordir', 'sre') validframes = int(config_get('validframes', 'sre')) if not os.path.exists(ivec_path): os.makedirs(ivec_path) logger.info('ivec path is ' + ivec_path) info = self.request.protocol + '://' + self.request.host + ', method=' + self.request.method + ', access url=' + self.request.uri logger.info(info) body = self.request.body_arguments Args = [ self_argument('UserId', required=True, helpinfo='miss user id') ] s, vals = DataIsValid(Args, body) if s: raise Exception(vals) userid = vals['UserId'] logger.info('userid is ' + userid) if not (userid): raise Exception('No user id') file_metas = self.request.files['UploadFile'] wav_uuid = uuid.uuid1() logger.info('generate uuid:' + str(wav_uuid)) wavpath = upload_path + '/' + userid if not os.path.exists(wavpath): os.makedirs(wavpath) for meta in file_metas: filename = meta['filename'] upfilepath = wavpath.rstrip('/') + '/' + str( wav_uuid) + '_' + filename with open(upfilepath, 'wb') as up: up.write(meta['body']) up.close() break ivecfile_path = ivec_path.rstrip('/') + '/' + userid + '/' if not os.path.exists(ivecfile_path): os.makedirs(ivecfile_path) ivecfile = ivecfile_path.rstrip('/') + '/' + str( wav_uuid) + '.ivec' ubm = get_ubm() sre_compute = CpuCompute() #use str function is to avoid string encode error if not sre_compute.Compute(ubm, str(upfilepath), str(ivecfile), int(validframes)): raise Exception('compute ivector failed') res, userinfo = db.get('SELECT * FROM user WHERE userid="%s";' % (userid)) if len(userinfo) > 0: logger.warning( 'the user %s was enrolled, verification just use latest ivector' % (userid)) sql_command = 'INSERT INTO user (userid, wavpath, ivecpath) values ("%s", "%s", "%s");' % ( userid, upfilepath, ivecfile) res, _ = db.modify(sql_command) if res != 0: raise Exception('insert record into user error') except Exception, e: logger.error(str(e)) ret = {'status': -1, 'message': str(e)} if os.path.isfile(upfilepath): os.remove(upfilepath) if os.path.isfile(ivecfile): os.remove(ivecfile)