def recognize(): image_path = Config.UPLOAD_IMAGE_PATH document_path = Config.SAVE_DOCUMENT_PATH if request.method == 'POST': str1 = request.form['current_frame'] img_np = base64_to_png(str1) cv2.imwrite(image_path + "origin.png", img_np) red_png = pic_to_red(image_path + "origin.png") picture_sub = PictureSub() src1 = cv2.imread(image_path + red_png) locate = picture_sub.left_up(src1) locate_remote = picture_sub.right_down(src1) move_x = locate_remote['list_x'] - locate['list_x'] move_y = locate_remote['list_y'] - locate['list_y'] print(locate['list_x'], locate['list_y'], move_x, move_y) with open(document_path + "site_0.txt", 'w') as f: f.write(str(locate['list_x']) + '\n') f.write(str(locate['list_y']) + '\n') f.write(str(move_x) + '\n') f.write(str(move_y) + '\n') return { "locate_x": locate['list_x'], "locate_y": locate['list_y'], "move_x": move_x, "move_y": move_y }
def background(): image_path = Config.UPLOAD_IMAGE_PATH document_path = Config.SAVE_DOCUMENT_PATH if request.method == 'POST': str = request.form['back_frame'] id = request.form['id'] print(id) img_np = base64_to_png(str) cv2.imwrite(image_path + "back_" + id + ".png", img_np) return 'done'
def save_image(): image_path = Config.UPLOAD_IMAGE_PATH document_path = Config.SAVE_DOCUMENT_PATH if request.method == 'POST': str = request.form['current_frame'] id = request.form['id'] img_np = base64_to_png(str) # cv2.imwrite(image_path + "info.png", img_np) 对中文路径不友好 cv2.imencode('.png', img_np)[1].tofile(image_path + "chart_" + id + ".png") return "done"
def picture(): # TODO 2019/10/1 12:02 liliangbin 当前帧解码 ,并调用图像处理函数 返回一个字符串 # 输入的base64编码字符串必须符合base64的padding规则。“当原数据长度不是3的整数倍时, 如果最后剩下两个输入数据,在编码结果后加1个“=”; # 如果最后剩下一个输入数据,编码结果后加2个“=”;如果没有剩下任何数据,就什么都不要加,这样才可以保证资料还原的正确性。” # image_path = Config.UPLOAD_IMAGE_PATH document_path = Config.SAVE_DOCUMENT_PATH if request.method == 'POST': str = request.form['current_frame'] id = request.form['id'] img_np = base64_to_png(str) cv2.imwrite(image_path + "current_" + id + ".png", img_np) res = {} sub = PictureSub() # 背景图 background = cv2.imread(image_path + "back_" + id + ".png") currentFrame = img_np q = sub.subtract_demo(background, currentFrame) s = sub.inverse(q) t = sub.iblack(s, 220) cv2.medianBlur(t, 3) cv2.GaussianBlur(t, (5, 5), 1) cv2.GaussianBlur(t, (15, 15), 0) cv2.bilateralFilter(t, 0, 100, 5) cv2.pyrMeanShiftFiltering(t, 10, 50) cv2.imwrite(image_path + "iblack_" + id + ".png", t) cv2.imwrite(image_path + "ipaint_" + id + ".png", s) with open(document_path + "site_" + id + ".txt", "r+") as f: a = f.readlines() print(a) frame_location = Site(int(a[0]), int(a[1]), int(a[2]), int(a[3])) res = sub.ipaint(s, 50, id, frame_location.locate_x, frame_location.move_x, frame_location.locate_y, frame_location.move_y) res['max'] = frame_location.locate_y + frame_location.move_y # 变化得y轴 list_y = np.array(res['list_y']) data_total = res['max'] - list_y max_index = max(data_total.tolist()) res['list_y'] = data_total.tolist() res['max'] = max_index + 20 res['id'] = id # 以前使用的是jsonify===> 前端使用 data["list_y"]==>有什么区别 return res
def draw_frame(): image_path = Config.UPLOAD_IMAGE_PATH # 我们把id存放再前端,draw_frame每次都是唯一的 if request.method == 'POST': str = request.form['current_frame'] img_np = base64_to_png(str) cv2.imwrite(image_path + "draw_frame.png", img_np) print(img_np.shape) else: img_np = cv2.imread(image_path + "draw_frame.png") return render_template("draw_frame.html", currentframe="draw_frame.png", width=img_np.shape[1], height=img_np.shape[0])
def picture(): # TODO 2019/10/1 12:02 liliangbin 当前帧解码 ,并调用图像处理函数 返回一个字符串 # 输入的base64编码字符串必须符合base64的padding规则。“当原数据长度不是3的整数倍时, 如果最后剩下两个输入数据,在编码结果后加1个“=”; # 如果最后剩下一个输入数据,编码结果后加2个“=”;如果没有剩下任何数据,就什么都不要加,这样才可以保证资料还原的正确性。” # import time start = time.clock() # 中间写上代码块 image_path = Config.UPLOAD_IMAGE_PATH document_path = Config.SAVE_DOCUMENT_PATH if request.method == 'POST': str = request.form['current_frame'] id = request.form['id'] imageStart = time.clock() img_np = base64_to_png(str) cv2.imwrite(image_path + "current_" + id + ".png", img_np) imageEnd = time.clock() print('base64topng: %s Seconds' % (imageEnd - imageStart)) res = {} sub = PictureSub() # 背景图 writeImgeStart = time.clock() background = cv2.imread(image_path + "back_" + id + ".png") print(background.shape) writeImgeEnd = time.clock() print('WriteImge: %s Seconds' % (writeImgeEnd - writeImgeStart)) currentFrame = img_np print(currentFrame.shape) q = sub.subtract_demo(background, currentFrame) substractIMRun = time.clock() print('substract : %s Seconds' % (substractIMRun - writeImgeEnd)) s = sub.inverse(q) InvserseSTOP = time.clock() print('inverse : %s Seconds' % (InvserseSTOP - substractIMRun)) # t = sub.iblack(s, 220) imageRun = time.clock() print('iblack: %s Seconds' % (imageRun - InvserseSTOP)) print('sub starct ALL : %s Seconds' % (imageRun - writeImgeEnd)) # cv2.imwrite(image_path + "iblack_" + id + ".png", t) cv2.imwrite(image_path + "ipaint_" + id + ".png", s) imageStop = time.clock() print('write twoImge: %s Seconds' % (imageStop - imageRun)) with open(document_path + "site_" + id + ".txt", "r+") as f: a = f.readlines() print(a) frame_location = Site(int(a[0]), int(a[1]), int(a[2]), int(a[3])) res = sub.ipaint(s, 220, id, frame_location.locate_x, frame_location.move_x, frame_location.locate_y, frame_location.move_y) res['max'] = frame_location.locate_y + frame_location.move_y # 变化得y轴 list_y = np.array(res['list_y']) data_total = res['max'] - list_y max_index = max(data_total.tolist()) res['list_y'] = data_total.tolist() res['max'] = max_index + 20 res['id'] = id # 以前使用的是jsonify===> 前端使用 data["list_y"]==>有什么区别 end = time.clock() print('Running time: %s Seconds' % (end - start)) return res