Exemple #1
0
def extract_audio():

    if request.method == 'POST':
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and (file.filename.endswith('.mp4') or file.filename.endswith('.avi')):
            connection = pika.BlockingConnection(
                pika.ConnectionParameters(host='rabbitmq'))
            channel = connection.channel()
            channel.queue_declare(queue='audio_extractor', durable=True)

            print("XAXA:                ", os.getenv('FILES_URL', 'XORUMELOS'))
            file_url = upload(file.read(), buffer=True, mime=file.mimetype)['name']

            # DB
            db_conn = Connection()
            file_oid = db_conn.insert_doc_mongo(file.read())
            oid, project_id = db_conn.insert_jobs(
                type='audio_extractor', status='new', file=file_oid, file_name=file.filename)
            message = {'type': 'audio_extractor', 'status': 'new',
                       'oid': file_oid, 'project_id': project_id, 'file': file_url}
            channel.basic_publish(
                exchange='', routing_key='audio_extractor', body=json.dumps(message))
            connection.close()
            return {'project_id': project_id}
        else:
            flash('Invalid file')
            return redirect(request.url)
Exemple #2
0
def asr():
    if request.method == 'POST':
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and (file.filename.endswith('.mp3') or file.filename.endswith('.wav')):
            connection = pika.BlockingConnection(
                pika.ConnectionParameters(host='rabbitmq'))
            channel = connection.channel()
            channel.queue_declare(queue='asr')

            file_url = upload(file.read(), buffer=True, mime=file.mimetype)['name']

            # DB
            db_conn = Connection()
            oid = db_conn.insert_jobs(
                type='asr', status='new', file=file.read())
            message = {'type': 'asr', 'status': 'new',
                       'oid': oid, 'file': file_url}
            channel.basic_publish(
                exchange='', routing_key='asr', body=str(message))
            connection.close()
            return 'Done'
        else:
            flash('Invalid file')
            return redirect(request.url)
def do_work(connection, channel, delivery_tag, body):
    try:
        print(" [x] Received %r" % body, flush=True)
        args = json.loads(body)
        oid = args['oid']
        project_id = args['project_id']
        # conn = Connection()
        # file = conn.get_file(oid)
        # file = conn.get_doc_mongo(file_oid=oid)
        file = download(args['file'], buffer=True)

        result = ast.literal_eval(file.decode('utf-8'))

        count = 0
        dict_result = {}
        previous_duration = 0
        for key, value in result.items():
            result = main(value['bytes'])
            dict_result[count] = result
            count += 1
            #time.sleep(1)

        payload = bytes(str(dict_result), encoding='utf-8')
        uploaded = upload(payload, buffer=True, mime='text/plain')
        conn = Connection()
        #  inserts the result of processing in database
        file_oid = conn.insert_doc_mongo(payload)
        conn.insert_jobs(type='asr',
                         status='done',
                         file=file_oid,
                         project_id=project_id)

        message = {
            'type': 'aggregator',
            'status': 'new',
            'oid': file_oid,
            'project_id': project_id,
            'file': uploaded['name'],
            'queue': 'asr'
        }

        #  post a message on topic_segmentation queue
        connection_out = pika.BlockingConnection(
            pika.ConnectionParameters(host=os.environ['QUEUE_SERVER']))
        channel2 = connection_out.channel()

        channel2.queue_declare(queue='aggregator', durable=True)
        channel2.basic_publish(exchange='',
                               routing_key='aggregator',
                               body=json.dumps(message))

    except Exception as e:
        print('Connection Error %s' % e, flush=True)
    print(" [x] Done", flush=True)
    cb = functools.partial(ack_message, channel, delivery_tag)
    connection.add_callback_threadsafe(cb)
def do_work(connection, channel, delivery_tag, body):
    try:
        print(" [x] Received %r" % body, flush=True)
        args = json.loads(body)
        oid = args['oid']
        project_id = args['project_id']
        print('data: ', args)
        print(str(oid) + '!!!???', flush=True)
        print(str(project_id) + '!!!???', flush=True)

        flows = new_aggregate_flow(args, args['queue'])
        if flows:
            data = {}
            for ch in flows.values():
                data[ch['queue']] = download(ch['file'], buffer=True)

            print(data.keys(), flush=True)
            # calls the audio extract algorithm
            # print(data,  flush=True)

            conn = Connection()
            try:
                payload = bytes(str(data), encoding='utf-8')
                file_oid = conn.insert_doc_mongo(payload)
                conn.insert_jobs('aggregator', 'done', file_oid, project_id)
                
                uploaded = upload(payload, buffer=True, mime='text/plain')
                
                message = {'type': 'segmentation', 'status': 'new',
                           'oid': file_oid, 'project_id': project_id, 'file': uploaded['name']}
                connection_out = pika.BlockingConnection(
                    pika.ConnectionParameters(host=os.environ['QUEUE_SERVER']))
                channel2 = connection_out.channel()

                channel2.queue_declare(queue='segmentation', durable=True)
                channel2.basic_publish(
                    exchange='', routing_key='segmentation', body=json.dumps(message))

            except Exception as e:
                print(e, flush=True)

    except Exception as e:
        print(e, flush=True)
    print(" [x] Done", flush=True)

    cb = functools.partial(ack_message, channel, delivery_tag)
    connection.add_callback_threadsafe(cb)
def callback(ch, method, properties, body):

    try:
        print(" [x] Received %r" % body, flush=True)
        args = json.loads(body)
        oid = args['oid']
        project_id = args['project_id']
        print(str(oid) + '!!!???', flush=True)
        print(str(project_id) + '!!!???', flush=True)

        # conn = Connection()
        # file = conn.get_doc_mongo(file_oid=oid)
        file = download(args['file'], buffer=True)

        data = extract(file)  # calls the audio extract algorithm
        # print(data,  flush=True)

        conn = Connection()
        try:
            uploaded = upload(data, buffer=True, mime='audio/wav')

            file_oid = conn.insert_doc_mongo(data)

            conn.insert_jobs('audio_extractor', 'done', file_oid, project_id)
            message = {'type': 'vad', 'status': 'new',
                       'oid': file_oid, 'project_id': project_id, 'file': uploaded['name']}
            connection = pika.BlockingConnection(
                pika.ConnectionParameters(host=os.environ['QUEUE_SERVER']))
            channel = connection.channel()

            channel.queue_declare(queue='vad', durable=True)
            channel.basic_publish(
                exchange='', routing_key='vad', body=json.dumps(message))

        except Exception as e:
            print(e, flush=True)

    except Exception as e:
        print(e, flush=True)
    print(" [x] Done", flush=True)
    ch.basic_ack(delivery_tag=method.delivery_tag)
Exemple #6
0
import sys, os.path
modules_dir = (os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) +
               '/src/')
sys.path.append(modules_dir)

from os import getenv
url = getenv('FILES_URL', 'http://localhost:3000')

# Usage example
from files_ms_client import download, upload, delete
url = 'http://localhost:3000'
r = upload('../temp/test.jpg', url=url)
print(r)
download(r['name'], '../temp/result.jpg', url=url)
print(delete(r['name'], url=url))
Exemple #7
0
def callback(ch, method, properties, body):
    try:
        print(" [x] Received %r" % body, flush=True)
        args = json.loads(body)
        oid = args['oid']
        project_id = args['project_id']
        # conn = Connection()
        # file = conn.get_doc_mongo(file_oid=oid)

        file = download(args['file'], buffer=True)

        try:

            data = main(file)  # calls the VAD algorithm
            # print(data(,  flush=True)

        except Exception as e:
            # print('aaaaaaaaaaa', flush=True)
            logging.debug('Connection Error %s' % e)

        conn = Connection()
        try:

            uploaded = upload(data, buffer=True, mime='text/plain')

            file_oid = conn.insert_doc_mongo(data)
            conn.insert_jobs('vad', 'done', file_oid, project_id)

            # Posts low level features jobs
            message = {
                'type': 'low_level_features',
                'status': 'new',
                'oid': file_oid,
                'project_id': project_id,
                'file': uploaded['name']
            }
            connection = pika.BlockingConnection(
                pika.ConnectionParameters(host=os.environ['QUEUE_SERVER']))
            channel = connection.channel()

            channel.queue_declare(queue='low_level_features', durable=True)
            channel.basic_publish(exchange='',
                                  routing_key='low_level_features',
                                  body=json.dumps(message))

            # Posts asr jobs

            channel_asr = connection.channel()
            message_asr = {
                'type': 'asr',
                'status': 'new',
                'oid': file_oid,
                'project_id': project_id,
                'file': uploaded['name']
            }

            channel_asr.queue_declare(queue='asr', durable=True)
            channel_asr.basic_publish(exchange='',
                                      routing_key='asr',
                                      body=json.dumps(message_asr))

        except Exception as e:
            print(e, flush=True)

            LOGGER.info('Error Inserting % ' % e)

    except Exception as e:
        print(e, flush=True)

        logging.info('error')

    print(" [x] Done", flush=True)
    ch.basic_ack(delivery_tag=method.delivery_tag)
Exemple #8
0
def do_work(connection, channel, delivery_tag, body):
    try:
        print(" [x] Received %r" % body, flush=True)
        args = json.loads(body) 
        oid = args['oid']
        project_id = args['project_id']
        conn = Connection()
        # file = conn.get_file(oid)
        # file = conn.get_doc_mongo(file_oid=oid)
        file = download(args['file'], buffer=True)

        result = ast.literal_eval(file.decode('utf-8'))
        #print(result.keys(), flush=True)
        chunks = []
        low_features_dict = ast.literal_eval(result['low_level_features'].decode('utf-8'))
        asr_dict = ast.literal_eval(result['asr'].decode('utf-8'))
        print(low_features_dict, flush=True)
        print(asr_dict, flush=True)
        for k, v in low_features_dict.items():
            s = Shot(k, low_features_dict[k]['pitch'], low_features_dict[k]['volume'],
                     low_features_dict[k]['pause'], [], init_time=low_features_dict[k]['init_time'], end_time=0)
            s.extractTranscriptAndConcepts(asr_dict[k], False, docSim=docSim)
            chunks.append(s)
        # print(result['low_level_features'], flush=True)
        # print(result['asr'], flush=True)
        chunks = [s for s in chunks if s.valid_vector]
        if len(chunks) < 2:
            boundaries = [0]
        else:
            '''calls the genetic algorithm'''
            ga = GA.GeneticAlgorithm(population_size=100, constructiveHeuristic_percent=0.3, mutation_rate=0.05,
                                     cross_over_rate=0.4, docSim=docSim, shots=chunks,
                                     n_chunks=len(chunks), generations=500, local_search_percent=0.3,
                                     video_length=100, stopwords=stopwords, ocr_on=False)
            boundaries = ga.run()
        #print(chunks, flush=True)
        print(boundaries, flush=True)
        topics = {}
        topics["topics"] = boundaries
        payload = bytes(str(topics), encoding='utf-8')

        uploaded = upload(payload, buffer=True, mime='text/json')

        file_oid = conn.insert_doc_mongo(payload)
        conn = Connection()
        conn.insert_jobs(type='segmentation', status='done', file=file_oid, project_id=project_id)
        #
        # #print(result, flush=True)
        # count = 0
        # dict_result = {}
        # previous_duration = 0
        # for key, value in result.items():
        #     result = main(value['bytes'])
        #     dict_result[count] = result
        #     count += 1
        #     #time.sleep(1)
        #
        # payload = bytes(str(dict_result), encoding='utf-8')
        # conn = Connection()
        #
        # #  inserts the result of processing in database
        # file_oid = conn.insert_doc_mongo(payload)
        # conn.insert_jobs(type='asr', status='done', file=file_oid, project_id=project_id)
        #
        # message = {'type': 'aggregator', 'status': 'new', 'oid': file_oid, 'project_id': project_id}
        #
        # #  post a message on topic_segmentation queue
        # connection_out = pika.BlockingConnection(pika.ConnectionParameters(host=os.environ['QUEUE_SERVER']))
        # channel2 = connection_out.channel()
        #
        # channel2.queue_declare(queue='aggregator', durable=True)
        # channel2.basic_publish(exchange='', routing_key='aggregator', body=json.dumps(message))

    except Exception as e:
        # print(e, flush=True)
        print('Connection Error %s' % e, flush=True)
    print(" [x] Done", flush=True)
    cb = functools.partial(ack_message, channel, delivery_tag)
    connection.add_callback_threadsafe(cb)