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" }})
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" }})
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" }})
def newTask(taskType): if taskType == "task1": return render_template("task1.html") elif taskType == "task2": form = Task2Form() elif taskType == "task3": form = Task3Form() elif taskType == "task4": form = Task4Form() elif taskType == "task5": form = Task5Form() elif taskType == "task6": form = Task6Form() if form.validate_on_submit(): if taskType == "task4": f = form.infile.data filename = secure_filename(f.filename) content_type, _ = guess_type(filename) f_id = fs.put(f, filename=filename, content_type=content_type) task = { "input_file": f_id, "created_at": datetime.datetime.utcnow(), "task_type": taskType, "state": "IDLE", "etc": { 'alpha': form.alpha.data, 'beta': form.beta.data, 'zoom': form.zoom.data } } taskId = db.tasks.insert_one(task).inserted_id print("INFO: QUEUED TASK " + str(taskId)) rq_task = queue.enqueue('app.tasks.' + taskType, taskId) task["redis_task"] = rq_task elif taskType == "task6": f = form.infile.data filename = secure_filename(f.filename) content_type, _ = guess_type(filename) f_id = fs.put(f, filename=filename, content_type=content_type) task = { "input_file": f_id, "created_at": datetime.datetime.utcnow(), "task_type": taskType, "state": "IDLE", } taskId = db.tasks.insert_one(task).inserted_id print("INFO: QUEUED TASK " + str(taskId)) rq_task = queue.enqueue('app.tasks.' + taskType, taskId) task["redis_task"] = rq_task elif taskType == "task5": f = form.infile.data filename = secure_filename(f.filename) content_type, _ = guess_type(filename) f_id = fs.put(f, filename=filename, content_type=content_type) task = { "input_file": f_id, "created_at": datetime.datetime.utcnow(), "task_type": taskType, "state": "IDLE", "etc": { 'isField': form.isField.data } } taskId = db.tasks.insert_one(task).inserted_id print("INFO: QUEUED TASK " + str(taskId)) rq_task = queue.enqueue('app.tasks.' + taskType, taskId) task["redis_task"] = rq_task elif taskType == "task2": f = form.infile.data filename = secure_filename(f.filename) content_type, _ = guess_type(filename) f_id = fs.put(f, filename=filename, content_type=content_type) task = { "input_file": f_id, "created_at": datetime.datetime.utcnow(), "task_type": taskType, "state": "IDLE", } taskId = db.tasks.insert_one(task).inserted_id print("INFO: QUEUED TASK " + str(taskId)) rq_task = queue.enqueue('app.tasks.' + taskType, taskId) task["redis_task"] = rq_task elif taskType == "task3": f = form.infile.data filename = secure_filename(f.filename) content_type, _ = guess_type(filename) f_id = fs.put(f, filename=filename, content_type=content_type) task = { "input_file": f_id, "created_at": datetime.datetime.utcnow(), "task_type": taskType, "state": "IDLE", "etc": { 'alpha': form.alpha.data, 'beta': form.beta.data, 'zoom': form.zoom.data } } taskId = db.tasks.insert_one(task).inserted_id print("INFO: QUEUED TASK " + str(taskId)) rq_task = queue.enqueue('app.tasks.' + taskType, taskId) task["redis_task"] = rq_task return redirect('/viewDetails/' + str(taskId)) return render_template("newTask.html", title=taskType, taskType=taskType, form=form)