Exemple #1
0
def post_(id_item, fp,token):
    classpath= os.path.join(settings.BASE_DIR, 'plugins_script',"speaker_extractor","lium_spkdiarization-8.4.1.jar" )
    ubm= os.path.join(settings.MEDIA_ROOT,"models/audio/globals/ubm.gmm")
    id_persona=None
    result=segfile_compact_name2(fp)
    uniform_tag_ids_arr =[]

    for res in result:
	try:
		name=res[0]
		feature_path = None
		model = None
		p=re.compile('[A-Z]')
		st=int( float(res[1]))#*1000 )
		dur=int (float(res[2]))#*1000)
		feature_path = split4diarization(os.path.join(settings.MEDIA_ROOT, 'items', str(id_item), 'audio.wav'),st,dur,"/tmp/model.wav")
		inst = create_instance('audio', False, token=token) # impostare id modello		
		set_instance_feature(inst['id'], feature_path, token)
		mai=p.findall(name)
		
		f_name="Unknown"
		s_name=name + '_' + str(id_item)
                print "f_name, s_name ", f_name, s_name
                # corregge il problema delle identita' duplicate
                persona = None
		#feature_path = split4diarization(fp,st,dur,"/tmp/model_wav")
                if f_name == "Unknown":
		id_persona=persona["id"]
		tag=create_tag(id_item,id_persona, "speaker", token)
		dtag=create_dtag(tag["id"], st*10, dur*10, token=token)
		uniform_tag = create_tag(id_item, id_persona, "face+speaker", token)
		uniform_tag_ids_arr.append(uniform_tag['id'])

	except Exception, e:
		print e
Exemple #2
0
def __generate_instances(auth_params, func_params):
    """
    Funzione utilizzata per estrarre le istanze da un file audio e
    salvarle nel database senza associarle ad alcuna entita'/modello.
    Le istanze sono ottenute applicando prima una conversione sulla traccia
    audio originale e successivamente splittandola sul parlato delle persone.
    Il parametro generato dalle funzioni contiene i dati associati al nuovo
    """
    try:
        # extract all needed parameters
        item_id   = func_params['id']
        item_path = os.path.join(get_media_root(), func_params['file'])
        temp_root = os.path.join('/tmp', 'speak_recog_' + str(item_id))
        dest_path = os.path.join(temp_root, 'audio.wav')
        properties_path = dest_path.split(".")[-2]+'.properties'
        settings_path   = os.path.join(get_media_root(), 'models/audio/globals/settings.properties')
        token     = auth_params.get('token', '1234')
        #print 'item_id',   item_id
        #print 'item_path', item_path
        #print 'temp_root', temp_root
        #print 'dest_path', dest_path
        #print 'properties_path', properties_path
        #print 'settings_path',   settings_path

        # create a directory for temporary files for diarization phase
        if os.path.exists(temp_root):
            shutil.rmtree(temp_root)
        os.mkdir(temp_root)
        os.chmod(temp_root, 0o777)
        os.mkdir(temp_root + '/out')
        os.chmod(temp_root + '/out', 0o777)

        # extract the item audio and convert it in the wav format
        command  = '/usr/bin/ffmpeg -y -i "' + item_path + '"'
        command += ' -strict -2 -acodec pcm_s16le -ac 1 -ar 16000 '
        command += '"' + dest_path + '"'
        subprocess.call(command, shell=True)
        #print command
        
        # generate the local settings file
        with open(properties_path, "w") as f:
	    f.write("fileName=" + dest_path)
            with open(settings_path) as fp:
                for line in fp:
                    f.write(line)
                    #print line
            f.writelines("outputRoot=" + temp_root + "/out/")

        # applica la fase di diarization e calcola gli spezzoni audio
        diarization(properties_path)            # salva gli spezzoni audio nel file di settings?
        
        # extract the audio portions and save them in a temp directory
        occurrences = segfile_compact_name2(temp_root + '/out/audio')
        for o in occurrences:
            start    = int(o[1])*10
            duration = int(o[2])*10
            # genera lo spezzone a partire dal file audio
            feature_path = temp_root + '/out/segment_' + str(item_id) + '_' + str(o[1]) + '.wav'
            split4diarization(dest_path, start, duration, feature_path)

            # genera il tag e il dtag da associare all'istanza
            persona = create_person('Unknown', o[0] + '_' + str(item_id), token=token)
            tag     = create_tag(item_id, persona['id'], 'speaker', token=token)
            dtag    = create_dtag(tag['id'], start, duration, token=token)

            # crea l'istanza e la carica nel database
            inst = create_instance('audio', False, token=token)
            set_instance_feature(inst['id'], feature_path, token=token)

        # remove all temporary directories and files
        #os.remove(temp_path)
    except Exception as e:
        print e
Exemple #3
0
def post_di_esempio(id_item, fp,token):
    print "***** PLUGIN SPEAKER RECOGNITION: POST DI ESEMPIO ---> Start"
    classpath= os.path.join(get_base_dir(), 'plugins_script',"speaker_extractor","lium_spkdiarization-8.4.1.jar" )
    ubm= os.path.join(get_media_root(),"models/audio/globals/ubm.gmm")
    id_persona=None
    #id_item=3601
    #name_p=open(name_file, "r")
    #name_p_list=name_p.readlines()
    #result=make_name_compact(fp) #result simile a [[nome,start,stop][nome,start,stop]]
    result=segfile_compact_name2(fp)
    print "result=",result
    uniform_tag_ids_arr =[]

    for res in result:
	try:
		name=res[0]
		feature_path = None
		model = None
		p=re.compile('[A-Z]')
		print "find name ", name

		st=int( float(res[1]))#*1000 )
		print "start ", st
		dur=int (float(res[2]))#*1000)
		print "dur ",dur
        	
		
                #feature_path = split4diarization(fp,st,dur,fp+"_"+str(st)+"_"+str(dur))
		feature_path = split4diarization(os.path.join(get_media_root(), 'items', str(id_item), 'audio.wav'),st,dur,"/tmp/model.wav")
		print "feature_path ", feature_path
		inst = create_instance('audio', False, token=token) # impostare id modello		
		set_instance_feature(inst['id'], feature_path, token)


		#if name.find("GiacomoMameli")>-1:
		#    print "trovato giacomino"
		#    id_persona=create_person("Giacomo","Mameli", token)["id"]
                #    #createTagKeyword(id_item, 'Giacomo', 'Mameli', token)
		#    print "id persona ",id_persona
		#else:
		if True:
		    mai=p.findall(name)
		    print "mai ",mai
		    #if len(mai)==2:
		    if True:
			#f_name=name.split(mai[1])[0]
			#s_name=mai[1]+name.split(mai[1])[1]
		        f_name="Unknown"
			s_name=name + '_' + str(id_item)
                        print "f_name, s_name ", f_name, s_name
                        # corregge il problema delle identita' duplicate
                        persona = None
		        #feature_path = split4diarization(fp,st,dur,"/tmp/model_wav")
                        if f_name == "Unknown":
		            print "name Unknown"
			    persona=create_person(f_name,s_name, token)
			    #feature_path = split4diarization(fp,0,None,"/tmp/model_wav")
		            #model_path=_build_model("/tmp/model.wav", None, [f_name, s_name],dur, token) # crea il modello associato alla persona sconosciuta
		            #model_path= create_new_model (classpath, ubm,feature_path, 0, dur,None) #f_name+"_"+ s_name)
		            #print "model_path ",model_path
		            #model = create_model(persona['id'], 'audio', f_name + ' ' + s_name, last_update=None, token=token)
		            ##set_model_file(model['id'], model_path, token=token)
                        #else:
		        #    print " persona nota ", f_name, s_name
                        #    persona=create_person(f_name, s_name, token)
		        #    print "persona ", persona
		        #    # list out of bound
                        #    model = get_models_by_entity(persona['id'], token=token)[0]
		        #    print "model ", model
                        # create a tag for person name
                        #createTagKeyword(id_item, persona['first_name'], persona['last_name'], token)
		        print "calcolo id persona"
			id_persona=persona["id"]
			print "id_persona ",id_persona
		    #else:	
		    #	persona=create_person("Il","Manutentore", token)
		    #	id_persona=persona["id"]
		print "create_tag id_item,id_persona ", id_item, " ",id_persona
		tag=create_tag(id_item,id_persona, "speaker", token)
		print "tag ",tag
		dtag=create_dtag(tag["id"], st*10, dur*10, token=token)
		print "dtag ",dtag

		uniform_tag = create_tag(id_item, id_persona, "face+speaker", token)
		print 'uniform tag', uniform_tag
		uniform_tag_ids_arr.append(uniform_tag['id'])

		# update the instance with the model id
		print 'instance, model',  inst, model
		#edit_instance(inst['id'], model_id=model['id'], token=token)
		print 'ascallo'	

	except Exception, e:
		print e
Exemple #4
0
def __generate_instances(auth_params, func_params):
    """
    @param auth_params: Input parameters of the function
                        that generate this function call
    @param func_params: Output parameters of the function
                        that generate this function call
    """
    
    file_path = os.path.join(get_media_root(), func_params['file'])
    item_id = func_params['id']

    # remove existing tags (and dynamic tags) for the item
    tags = get_tags_by_item(func_params['id'], auth_params['token'])
    for tag in tags:
        if tag['type'] == 'face':
            remove_tag(tag['id'], auth_params['token'])


    # extract faces from video and save metadata on filesystem

    # Get available models
    model_type = 'video'
    models = tsm.get_models(model_type, auth_params['token'])

    # Create dictionary with models
    models_list = []
    for model in models:
        model_id = model['id']
        model_file = os.path.join(
            get_media_root(), model['model_file'])
        entity_id = model['entity']
        person = get_person(entity_id, auth_params['token'])
        name = person['first_name']
        surname = person['last_name']
        tag = surname + c.TAG_SEP + name

        model_dict = {c.MODEL_ID_KEY: model_id,
                      c.MODEL_FILE_KEY: model_file,
                      c.TAG_KEY: tag
                      }
        models_list.append(model_dict)

    fe = VideoFaceExtractor(file_path, str(item_id), models_list)

    fe.analyze_video()

    set_status(item_id, "FACE_RECOG", auth_params['token'])

    people = fe.get_people()
    
    uniform_tag_ids_arr = []
    # retrieve dynamic tags and save them on ACTIVE core
    for person_dict in people:

        #print "Tag assegnato al cluster", person_dict['assigned_tag']

        #~ # update the image for the person
        #~ image_path = os.path.join(get_media_root(),'items',
                                  #~ str(item_id), 'Face extraction',
                                  #~ 'Face recognition', 'Key frames',
                                  #~ person_dict[c.KEYFRAME_NAME_KEY])
        #~ set_image(person_id, image_path, 'image/png')

        # check if the person has been recognized
        model_id = person_dict[c.ASSIGNED_LABEL_KEY]
        trusted = False
        instance_id = None
        if model_id == c.UNDEFINED_LABEL:
            print "Creata una nuova persona"
            person = create_person(
                "Unknown", str(func_params['id']) + '_' +
                str(person_dict['person_counter']),
                auth_params['token'])
            person_id = person['id']
            # Create a model for the unknown instance
            model = tsm.create_model(
                person_id, 'video',
                person['first_name'] + ' ' + person['last_name'],
                token=auth_params['token'])
            instance = tsm.create_instance(
                model_type, False, model_id=model['id'],
                token=auth_params['token'])
        else:
            # Create model instance
            instance = tsm.create_instance(
                model_type, trusted, model_id=model_id,
                token=auth_params['token'])
            model = tsm.get_model(model_id)
            person_id = model['entity']

        # update the image for the person
        image_path = os.path.join(fe.rec_path,
                                  c.FACE_RECOGNITION_KEY_FRAMES_DIR,
                                  person_dict[c.KEYFRAME_NAME_KEY])
        set_image(person_id, image_path, 'image/png', auth_params['token'])
        tsm.set_instance_thumbnail(
            instance['id'], image_path, token=auth_params['token'])

        # Get aligned face and set it as instance feature
        print person_dict.keys()
        aligned_face_path = os.path.join(fe.align_path, person_dict[c.MEDOID_ALIGNED_FACE_KEY])
        tsm.set_instance_feature(instance['id'], aligned_face_path, token=auth_params['token'])

        # TODO DELETE?
        # else:
        #     # Find id person by name and surname
        #     tag_parts = person_id.split(c.TAG_SEP)
        #     surname = tag_parts[0]
        #     name = tag_parts[1]
        #     person = create_person(name, surname, auth_params['token'])

        #person_id = person['id']

        #~ if person['image'] == "unknown_user.png":
            #~ set_image(person_id, image_path, 'image/png')


        # create a tag for user name
        #createTagKeyword(item_id, person['first_name'], person['last_name'])

        # create a tag (occurrence of a person in a digital item)
        tag = create_tag(item_id, person_id, "face", auth_params['token'])
        #create audio+video tag
        #uniform_tag = create_tag(item_id, person_id, "face+speaker", auth_params['token'])
        #uniform_tag_ids_arr.append[uniform_tag['id']]

        for segment in person_dict[c.SEGMENTS_KEY]:
            start = segment[c.SEGMENT_START_KEY]
            duration = segment[c.SEGMENT_DURATION_KEY]
            bbox_x, bbox_y, width, height = segment[c.FRAMES_KEY][0][c.DETECTION_BBOX_KEY]

            create_dtag(tag['id'], int(start), int(duration), bbox_x, bbox_y,
                        width, height, auth_params['token'])
    
    """    
    item_status = get_status(item_id, token)
    if "SPEAKER_RECOG" in item_status['status']:       
        #create dtags for audio+video tag
        for u_tag_id in uniform_tag_ids_arr:
            create_uniform_dtags(item_id, u_tag_id, auth_params['token'])
    """
    create_uniform_dtags(item_id, auth_params['token'])