Esempio n. 1
0
def getTaskOutputFile(taskId):
    task = db.tasks.find_one({"_id": ObjectId(taskId)})
    fileobj = fs.get(task["output_file"])
    data = wrap_file(request.environ, fileobj, buffer_size=1024 * 255)
    response = app.response_class(
        data,
        mimetype=fileobj.content_type,
        direct_passthrough=True,
    )
    response.content_length = fileobj.length
    response.last_modified = fileobj.upload_date
    response.set_etag(fileobj.md5)
    response.cache_control.max_age = 31536000
    response.cache_control.public = True
    response.make_conditional(request)
    return response
Esempio n. 2
0
def task2(taskId):
    try:
        task = db.tasks.find_one_and_update({"_id": taskId},
                                            {"$set": {
                                                "state": "PROCESSING"
                                            }})

        inpzip = fs.get(task["input_file"])
        with zipfile.ZipFile(inpzip) as zip_ref:
            tdir = "/tmp/" + str(taskId) + "/"
            zip_ref.extractall(tdir)
            os.chdir(tdir)
            dirlist = os.listdir()
        imlist = []
        for i in dirlist:
            imlist.append(tdir + i)

        img = fun2(imlist)[0][0]
        success, encoded_image = cv2.imencode('.png', img)
        imgdata = encoded_image.tobytes()
        content_type, _ = guess_type("test.png")
        fsfileid = fs.put(imgdata, content_type=content_type)
        task = db.tasks.find_one_and_update(
            {"_id": ObjectId(taskId)}, {"$set": {
                "output_file": fsfileid
            }})

        task = db.tasks.find_one_and_update({"_id": ObjectId(taskId)},
                                            {"$set": {
                                                "state": "COMPLETED"
                                            }})

    except Exception:
        traceback.print_exc()
        task = db.tasks.find_one_and_update({"_id": ObjectId(taskId)},
                                            {"$set": {
                                                "state": "ERROR"
                                            }})
Esempio n. 3
0
def task3(taskId):
    print("POINT 1")
    try:
        print("POINT 2")
        task = db.tasks.find_one_and_update({"_id": taskId},
                                            {"$set": {
                                                "state": "PROCESSING"
                                            }})
        print("POINT 3")
        inpzip = fs.get(task["input_file"])
        print("POINT 4")
        img = Merge2.run(
            Merge2(taskId, inpzip, task['etc']['alpha'], task['etc']['beta'],
                   task['etc']['zoom']))
        print("POINT 5")
        success, encoded_image = cv2.imencode('.png', img)
        imgdata = encoded_image.tobytes()
        content_type, _ = guess_type("test.png")
        fsfileid = fs.put(imgdata, content_type=content_type)
        print("POINT 6")
        task = db.tasks.find_one_and_update(
            {"_id": ObjectId(taskId)}, {"$set": {
                "output_file": fsfileid
            }})

        task = db.tasks.find_one_and_update({"_id": ObjectId(taskId)},
                                            {"$set": {
                                                "state": "COMPLETED"
                                            }})

    except Exception:
        traceback.print_exc()
        task = db.tasks.find_one_and_update({"_id": ObjectId(taskId)},
                                            {"$set": {
                                                "state": "ERROR"
                                            }})
Esempio n. 4
0
def task6(taskId):
    try:
        task = db.tasks.find_one_and_update({"_id": ObjectId(taskId)},
                                            {"$set": {
                                                "state": "PROCESSING"
                                            }})

        # Reading
        img_str = fs.get(task["input_file"]).read()
        nparr = np.fromstring(img_str, np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

        print('slicing colors...')
        # convert to hsv
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        row, column, channels = img.shape
        new_val = 210

        #####################BLUR########################################
        beps = 35
        hsv = cv2.GaussianBlur(hsv, (beps, beps), 0)
        #####################BLUR########################################

        print(hsv.shape[0], 'x', hsv.shape[1])
        used = []
        for i in range(0, hsv.shape[0]):
            used.append([])
            for j in range(0, hsv.shape[1]):
                used[i].append(False)

        def bfs(y, x, val, eps):
            q = [(y, x)]
            while len(q) > 0:
                x = q[0][1]
                y = q[0][0]
                used[y][x] = True
                hsv[y][x][0] = val
                if x + 1 < hsv.shape[1] and used[y][x + 1] == False and abs(
                        float(hsv[y][x + 1][0]) - val) <= eps:
                    q.append((y, x + 1))
                    used[y][x + 1] = True
                if x - 1 >= 0 and used[y][x - 1] == False and abs(
                        float(hsv[y][x - 1][0]) - val) <= eps:
                    q.append((y, x - 1))
                    used[y][x - 1] = True

                if y + 1 < hsv.shape[0] and used[y + 1][x] == False and abs(
                        float(hsv[y + 1][x][0]) - val) <= eps:
                    q.append((y + 1, x))
                    used[y + 1][x] = True
                if y - 1 >= 0 and used[y - 1][x] == False and abs(
                        float(hsv[y - 1][x][0]) - val) <= eps:
                    q.append((y - 1, x))
                    used[y - 1][x] = True
                del q[0]

        print('\nworking with hsv...')
        for i in range(0, hsv.shape[0]):
            for j in range(0, hsv.shape[1]):
                for g in range(1, 11):
                    if i == hsv.shape[0] // 100 * g * 10 and j == 0:
                        print(g * 10, f'%')
                if used[i][j] == False:
                    bfs(i, j, hsv[i][j][0], 2.25)

        mask = cv2.inRange(hsv, (0, 75, 0), (33, 255, 255))

        # slice the red
        imask = mask > 0
        red = np.zeros_like(img, np.uint8)
        red[imask] = img[imask]

        # slice the yellow
        mask = cv2.inRange(hsv, (30.00001, 75, 0), (35, 255, 255))

        imask = mask > 0
        yel = np.zeros_like(img, np.uint8)
        yel[imask] = img[imask]

        for i in range(0, red.shape[0]):
            for j in range(0, red.shape[1]):
                for g in range(1, 11):
                    if i == hsv.shape[0] // 100 * g * 10 and j == 0:
                        print(g * 10, f'%')
                if (i - 1 >= 0) and (yel[i][j].all() != 0 or red[i][j].all() !=
                                     0) and (yel[i - 1][j].all() == 0
                                             and red[i - 1][j].all() == 0):
                    img.itemset((i, j, 0), 255)
                    img.itemset((i, j, 1), 255)
                    img.itemset((i, j, 2), 255)
                if (i + 1 < red.shape[0]) and (yel[i][j].all() != 0
                                               or red[i][j].all() != 0) and (
                                                   yel[i - 1][j].all() == 0 and
                                                   red[i - 1][j].all() == 0):
                    img.itemset((i, j, 0), 255)
                    img.itemset((i, j, 1), 255)
                    img.itemset((i, j, 2), 255)

                if (j - 1 >= 0) and (yel[i][j].all() != 0 or red[i][j].all() !=
                                     0) and (yel[i][j - 1].all() == 0
                                             and red[i][j - 1].all() == 0):
                    img.itemset((i, j, 0), 255)
                    img.itemset((i, j, 1), 255)
                    img.itemset((i, j, 2), 255)
                if (j + 1 < red.shape[1]) and (yel[i][j].all() != 0
                                               or red[i][j].all() != 0) and (
                                                   yel[i][j + 1].all() == 0 and
                                                   red[i][j + 1].all() == 0):
                    img.itemset((i, j, 0), 255)
                    img.itemset((i, j, 1), 255)
                    img.itemset((i, j, 2), 255)

                if yel[i][j].all() != 0:
                    img.itemset((i, j, 1), new_val)
                if red[i][j].all() != 0:
                    img.itemset((i, j, 2), new_val)

        # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        print("algo ended")

        # write output image into fs
        success, encoded_image = cv2.imencode('.png', img)
        imgdata = encoded_image.tobytes()
        content_type, _ = guess_type("test.png")
        fsfileid = fs.put(imgdata, content_type=content_type)

        # link file id with task document
        task = db.tasks.find_one_and_update(
            {"_id": ObjectId(taskId)}, {"$set": {
                "output_file": fsfileid
            }})

        task = db.tasks.find_one_and_update({"_id": ObjectId(taskId)},
                                            {"$set": {
                                                "state": "COMPLETED"
                                            }})
    except Exception:
        traceback.print_exc()
        task = db.tasks.find_one_and_update({"_id": ObjectId(taskId)},
                                            {"$set": {
                                                "state": "ERROR"
                                            }})