Esempio n. 1
0
def captioning(request, template_name="dbs.html"):
    socketid = uuid.uuid4()
    if request.method == "POST":
        try:
            image_folder = request.POST.get('image_folder')
            image_folder = urllib2.unquote(image_folder)
            prefix = request.POST.get('prefix', '')
            socketid = request.POST.get('socketid')
            demoType = request.POST.get("demoType")
            B = request.POST.get("B", 6)
            M = request.POST.get("M", 3)
            lamda = request.POST.get("lamda", 0.5)

            if demoType == "demoImage":
                image_path = os.path.join(settings.BASE_DIR, str(image_folder)[1:])
                random_uuid = uuid.uuid1()
                # place the demo image in a new folder as a new job
                output_dir = os.path.join(settings.MEDIA_ROOT, 'images', str(random_uuid))

                if not os.path.exists(output_dir):
                    os.makedirs(output_dir)

                print image_path, output_dir
                shutil.copy(image_path, output_dir)
                image_folder = output_dir

            log_to_terminal(socketid, {"terminal": "Starting Diverse Beam Search Job..."})
            response = dbs_captioning(prefix, image_folder, B, M, lamda, socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
Esempio n. 2
0
def vqa_task(image_path, question, vqa_model, socketid):

    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host=settings.PIKA_HOST))
    channel = connection.channel()
    if vqa_model == "HieCoAtt":
        queue = "vqa_demo_task_queue"
        channel.queue_declare(queue=queue, durable=True)
    if vqa_model == "pythia":
        queue = "vqa_demo_task_queue_with_pythia"
        channel.queue_declare(queue=queue, durable=True)
    message = {
        'image_path': image_path,
        'question': question,
        'socketid': socketid,
    }
    log_to_terminal(socketid, {"terminal": "Publishing job to VQA Queue"})
    channel.basic_publish(
        exchange='',
        routing_key=queue,
        body=json.dumps(message),
        properties=pika.BasicProperties(
            delivery_mode=2,  # make message persistent
        ))

    print(" [x] Sent %r" % message)
    log_to_terminal(socketid, {"terminal": "Job published successfully"})
    connection.close()
Esempio n. 3
0
def captioning(request, template_name="dbs.html"):
    socketid = uuid.uuid4()
    if request.method == "POST":
        try:
            image_folder = request.POST.get('image_folder')
            image_folder = urllib2.unquote(image_folder)
            prefix = request.POST.get('prefix', '')
            socketid = request.POST.get('socketid')
            demoType = request.POST.get("demoType")
            B = request.POST.get("B", 6)
            M = request.POST.get("M", 3)
            lamda = request.POST.get("lamda", 0.5)

            if demoType == "demoImage":
                image_path = os.path.join(settings.BASE_DIR,
                                          str(image_folder)[1:])
                random_uuid = uuid.uuid1()
                # place the demo image in a new folder as a new job
                output_dir = os.path.join(settings.MEDIA_ROOT, 'images',
                                          str(random_uuid))

                if not os.path.exists(output_dir):
                    os.makedirs(output_dir)

                print image_path, output_dir
                shutil.copy(image_path, output_dir)
                image_folder = output_dir

            log_to_terminal(
                socketid, {"terminal": "Starting Diverse Beam Search Job..."})
            response = dbs_captioning(prefix, image_folder, B, M, lamda,
                                      socketid)
        except Exception, err:
            log_to_terminal(socketid, {"terminal": traceback.print_exc()})
Esempio n. 4
0
def dbs_captioning(prefix, image_folder, B, M, lamda, socketid):
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()

    channel.queue_declare(queue='dbs_task_queue', durable=True)
    message = {
        'image_folder': image_folder + "/",
        'prefix': prefix,
        'B': B,
        'M': M,
        'lambda': lamda,
        'socketid': socketid,
    }

    log_to_terminal(socketid, {"terminal": "Publishing job to DBS Queue"})
    channel.basic_publish(exchange='',
                      routing_key='dbs_task_queue',
                      body=json.dumps(message),
                      properties=pika.BasicProperties(
                         delivery_mode = 2, # make message persistent
                      ))

    print(" [x] Sent %r" % message)
    log_to_terminal(socketid, {"terminal": "Job published successfully"})
    connection.close()
Esempio n. 5
0
def ws_message(message):
    print "Message recieved from client side and the content is ", message.content[
        'text']
    # prefix, label = message['path'].strip('/').split('/')
    socketid = message.content['text']

    Group(socketid).add(message.reply_channel)
    log_to_terminal(socketid, {"info": "User added to the Channel Group"})
Esempio n. 6
0
def callback(ch, method, properties, body):
    try:
        print(" [x] Received %r" % body)
        body = yaml.safe_load(body) # using yaml instead of json.loads since that unicodes the string in value
        result = DBSTorchModel.predict(body['image_folder'], body['prefix'], body['B'], body['M'], body['lambda'])
        result = json.loads(result)
        result['img_url'] = result['img_path'].replace(settings.BASE_DIR, "")
        log_to_terminal(body['socketid'], {"terminal": result})
        log_to_terminal(body['socketid'], {"result": result})
        log_to_terminal(body['socketid'], {"terminal": "Completed the Diverse Beam Search task"})

        ch.basic_ack(delivery_tag = method.delivery_tag)
    except Exception, err:
        log_to_terminal(body['socketid'], {"terminal": json.dumps({"Traceback": str(traceback.print_exc())})})
Esempio n. 7
0
def callback(ch, method, properties, body):

    print(" [x] Received %r" % body)
    body = yaml.safe_load(
        body
    )  # using yaml instead of json.loads since that unicodes the string in value

    result = VqaTorchModel.predict(body['image_path'], body['question'])
    result['image_path'] = str(result['image_path']).replace(
        settings.BASE_DIR, '')

    log_to_terminal(body['socketid'], {"terminal": json.dumps(result)})
    log_to_terminal(body['socketid'], {"result": json.dumps(result)})
    log_to_terminal(body['socketid'], {"terminal": "Completed VQA task"})

    ch.basic_ack(delivery_tag=method.delivery_tag)
Esempio n. 8
0
def callback(ch, method, properties, body):

    print(" [x] Received %r" % body)
    body = yaml.safe_load(
        body
    )  # using yaml instead of json.loads since that unicodes the string in value

    try:
        result = VqaTorchModel.predict(body['image_path'], body['question'])
        top5_answer = result['top5_answer']
        top5_softmax = result['softmax_score']
        top5_list = []
        for i in xrange(5):
            temp = {}
            temp['answer'] = top5_answer[i + 1]
            temp['confidence'] = top5_softmax[i + 1] * 100
            top5_list.append(temp)

        top5_list = sorted(top5_list,
                           key=lambda k: k['confidence'],
                           reverse=True)
        result['top5_list'] = top5_list
        result['top5_answer'] = None
        result['softmax_score'] = None
        log_to_terminal(body['socketid'], {"terminal": json.dumps(result)})
        log_to_terminal(body['socketid'], {"result": json.dumps(result)})
        log_to_terminal(body['socketid'], {"terminal": "Completed VQA task"})
        try:
            QuestionAnswer.objects.create(question=body['question'],
                                          image=body['image_path'].replace(
                                              settings.BASE_DIR, ""),
                                          top5_answer=result['top5_list'],
                                          socketid=body['socketid'])
        except:
            print str(traceback.print_exc())

        django.db.close_old_connections()

    except Exception as e:
        print str(e)

    ch.basic_ack(delivery_tag=method.delivery_tag)
Esempio n. 9
0
def callback(ch, method, properties, body):

    print(" [x] Received %r" % body)
    body = yaml.safe_load(
        body
    )  # using yaml instead of json.loads since that unicodes the string in value
    image_name = os.path.basename(body["image_path"])

    if image_name.startswith(constants.COCO_PARTIAL_IMAGE_NAME) and image_name[-5].isdigit():
        image_folder = os.path.join(BASE_VQA_DIR_PATH, "media/test2014")
    elif image_name in [
        "img1.jpg",
        "img2.jpg",
        "img3.jpg",
        "img4.jpg",
        "img5.jpg",
        "img6.jpg",
        "vqa.png",
    ]:
        image_folder = os.path.join(BASE_VQA_DIR_PATH, "static/images")
    else:
        IMAGES_BASE_URL = constants.IMAGES_BASE_URL
        image_dir_name = body["image_path"].split("/")[-2]
        if not os.path.exists(
            os.path.join(BASE_VQA_DIR_PATH, "media/demo", image_dir_name)
        ):
            os.mkdir(os.path.join(BASE_VQA_DIR_PATH, "media/demo", image_dir_name))
        image_url = os.path.join(IMAGES_BASE_URL, image_dir_name, image_name)
        stored_image_path = os.path.join(
            BASE_VQA_DIR_PATH, "media/demo", image_dir_name, image_name
        )
        urllib.request.urlretrieve(image_url, stored_image_path)
        image_folder = os.path.join(BASE_VQA_DIR_PATH, "media/demo", image_dir_name)

    path = os.path.join(image_folder, image_name)
    question = body["question"]
    image_path = demo.get_actual_image(path)
    scores, predictions = demo.predict(path, question)
    scores = [score * 100 for score in scores]
    data = dict(zip(predictions, scores))
    temp_list = []
    for key, value in data.items():
        temp = {"answer": str(key), "confidence": value}
        temp_list.append(temp)
    result = {}
    result["top5_list"] = temp_list
    # result['image_path'] = str(result['image_path']).replace(settings.BASE_DIR, '')
    log_to_terminal(body["socketid"], {"terminal": json.dumps(result)})
    log_to_terminal(body["socketid"], {"result": json.dumps(result)})
    log_to_terminal(body["socketid"], {"terminal": "Completed VQA task"})
    print("[*] Message processed successfully. To exit press CTRL+C")
    # try:
    #     QuestionAnswer.objects.create(question=body['question'],
    #         image=body['image_path'].replace(settings.BASE_DIR, ""),
    #         top5_answer=result['top5_list'],
    #         socketid=body['socketid'],
    #         vqa_model="pythia")
    # except:
    #     print(str(traceback.print_exc()))

    # django.db.close_old_connections()
    ch.basic_ack(delivery_tag=method.delivery_tag)
    print("[*] Message successfully deleted. To exit press CTRL+C")
    print("[*] Waiting for new messages. To exit press CTRL+C")
def ws_message(message):
    print "Message recieved from client side and the content is ", message.content[
        'text']
    socketid = message.content['text']
    Group(socketid).add(message.reply_channel)
    log_to_terminal(socketid, {"info": "User added to the Channel Group"})
Esempio n. 11
0
def callback(ch, method, properties, body):
    print("I'm callback")
    start = time.time()
    body = yaml.safe_load(
        body
    )  # using yaml instead of json.loads since that unicodes the string in value
    print(" [x] Received %r" % body)
    try:
        task = Tasks.objects.get(unique_id=int(body["task_id"]))
        question_obj = QuestionAnswer.objects.create(
            task=task,
            input_text=body['question'],
            input_images=body['image_path'],
            socket_id=body['socket_id'])
        print("created question answer object")
    except:
        print(str(traceback.print_exc()))
    try:
        image_path = body["image_path"]
        features, infos = feature_extractor.extract_features(image_path)
        query = body["question"]
        socket_id = body["socket_id"]
        task_id = body["task_id"]
        task = [eval(task_id)]
        answer = custom_prediction(query, task, features, infos, task_id)
        if (task_id == "1" or task_id == "15" or task_id == "2"
                or task_id == "13"):
            top3_answer = answer["top3_answer"]
            top3_confidence = answer["top3_confidence"]
            top3_list = []
            for i in range(3):
                temp = {}
                temp["answer"] = top3_answer[i]
                temp["confidence"] = round(top3_confidence[i] * 100, 2)
                top3_list.append(temp)

            result = {"task_id": task_id, "result": top3_list}
            print("The task result is", result)
            question_obj.answer_text = result
            question_obj.save()

        if (task_id == "4" or task_id == "16" or task_id == "11"):
            print("The answer is", answer)
            image_name_with_bounding_boxes = uuid.uuid4()

            image = image_path[0].split("/")
            abs_path = ""
            for i in range(len(image) - 3):
                abs_path += image[i]
                abs_path += "/"
            color_list = [(0, 0, 255), (0, 255, 0), (255, 0, 0)]
            image_name_list = []
            confidence_list = []
            for i, j in zip(answer, color_list):
                image_obj = cv2.imread(image_path[0])
                image_name = uuid.uuid4()
                image_with_bounding_boxes = cv2.rectangle(
                    image_obj, (i["x1"], i["y1"]), (i["x2"], i["y2"]), j, 4)
                image_name_list.append(str(image_name))
                confidence_list.append(round(i["confidence"], 2))
                cv2.imwrite(
                    os.path.join(abs_path, "media", "refer_expressions_task",
                                 str(image_name) + ".jpg"),
                    image_with_bounding_boxes)
            result = {
                "task_id": task_id,
                "image_name_list": image_name_list,
                "confidence_list": confidence_list
            }
            question_obj.answer_images = result
            question_obj.save()

        if (task_id == "12"):
            print(answer)
            top3_answer = answer["top3_answer"]
            top3_confidence = answer["top3_confidence"]
            top3_list = []
            for i in range(2):
                temp = {}
                temp["answer"] = top3_answer[i]
                temp["confidence"] = round(top3_confidence[i] * 100, 2)
                top3_list.append(temp)
            result = {"task_id": task_id, "result": top3_list}
            question_obj.answer_text = result
            question_obj.save()

        if (task_id == "7"):
            top3_answer = answer["top3_answer"]
            top3_confidence = answer["top3_confidence"]
            image_name_list = []
            confidence_list = []
            for i in range(len(top3_answer)):
                print(image_path[top3_answer[i]])
                if "demo" in image_path[0].split("/"):
                    image_name_list.append(
                        "demo/" + os.path.split(image_path[
                            top3_answer[i]])[1].split(".")[0] + "." +
                        str(image_path[0].split("/")[-1].split(".")[1]))
                else:
                    image_name_list.append(
                        "test2014/" + os.path.split(image_path[
                            top3_answer[i]])[1].split(".")[0] + "." +
                        str(image_path[0].split("/")[-1].split(".")[1]))
                confidence_list.append(round(top3_confidence[i] * 100, 2))
            result = {
                "task_id": task_id,
                "image_name_list": image_name_list,
                "confidence_list": confidence_list
            }
            print("The result is", result)
            question_obj.answer_images = result
            question_obj.save()

        log_to_terminal(body['socket_id'], {"terminal": json.dumps(result)})
        log_to_terminal(body['socket_id'], {"result": json.dumps(result)})
        log_to_terminal(body['socket_id'], {"terminal": "Completed Task"})
        ch.basic_ack(delivery_tag=method.delivery_tag)
        print("Message Deleted")
        django.db.close_old_connections()
    except Exception as e:
        print(traceback.print_exc())
        print(str(e))

    end = time.time()
    print("Time taken is", end - start)