def upduser(): if request.method == 'POST': nickname = request.values.get('nickname').strip() signature = request.values.get('signature').strip() f = request.files['headurl'] user = User.query.filter_by(id=current_user.id).first() user.nickname = nickname user.signature = signature if f.filename != '': filename = f.filename f = f.read() suffix = filename[filename.find('.') + 1:] url = fdfs_addr + fdfs_client.uploadbyBuffer(f, suffix) user.head_url = url db.session.commit() return redirect('/user/myspace')
def srcnn_process(imgid, albumid, userid, action, times=1): db.session.commit() img = Image.query.filter_by(id=imgid).first() if img == None: return False suffix = img.name[img.name.find('.') + 1:] imgContent = fdfs_client.downloadbyBuffer(img.url[len(fdfs_addr):]) img = cv.imdecode(np.frombuffer(imgContent, np.uint8), cv.IMREAD_COLOR) g1 = tf.Graph() with tf.Session(graph=g1) as sess: srcnn = SRCNN(sess, "\srcnn\checkpoint\srcnn_21") if ImageType(action) == ImageType.SRCNN: print('清晰化处理') img = srcnn.superresolution(img) else: print('放大处理') print(int(times[0:1])) img = srcnn.upscaling(img, int(times[0:1]), True) img = Img.fromarray(img) f = BytesIO() img.save(f, format='PNG') f = f.getvalue() # 保存并上传数据库 url = fdfs_addr + fdfs_client.uploadbyBuffer(f, suffix) imgname = url[url.rfind('/') + 1:] newimg = Image(imgname, url, ImageType(action), imgid, userid, -1) db.session.add(newimg) db.session.flush() db.session.commit() if albumid == None or albumid == '' or albumid == '1': rediskey = 'album:' + str(userid) + ':1' else: rediskey = 'album:' + str(userid) + ':' + albumid conn.zadd(rediskey, {newimg.id: time.time()}) # 系统统计结果 dictstr = conn.rpop('count') dict = eval(dictstr) dict['count'] = int(dict['count']) + 1 conn.rpush('count', json.dumps(dict)) return True
def updatealb(): if request.method == 'POST': albumid = request.values.get('albumid').strip() if int(albumid) == 1: return redirect('/user/useralbum') name = request.values.get('name').strip() introduce = request.values.get('introduce').strip() f = request.files['updatefrontcover'] album = Photoalbum.query.filter_by(id=albumid).first() orginlname = album.name album.name = name album.introduce = introduce if f.filename != '': filename = f.filename f = f.read() suffix = filename[filename.find('.') + 1:] url = fdfs_addr + fdfs_client.uploadbyBuffer(f, suffix) album.front_cover = url db.session.commit() #fireEvent(EventModel(EventType.ALBUM, -1, EntityType.USER, album.id, current_user.id,{'orginlname': orginlname, 'action': '修改'})) return redirect('/user/useralbum')
def compare(): data = json.loads(request.get_data(as_text=True)) imgid = data['imgid'] img = Image.query.filter_by(id=imgid).first() orginimg = Image.query.filter_by(id=img.orig_id).first() # 原图 if ImageType(img.action) == ImageType.SRCNN: return jsonify(code=200, message='和原图进行比较', url=orginimg.url) else: if ImageType(img.action)==ImageType.UPSCALE_2X: times = 2 # Upscale_2x eimg=Image.query.filter_by(orig_id=img.orig_id, action=ImageType.BICUBIC_UPSCALE_2X.value).first() if eimg!=None: return jsonify(code=201,url=eimg.url) elif ImageType(img.action)==ImageType.UPSCALE_3X: times = 3 eimg = Image.query.filter_by(orig_id=img.orig_id, action=ImageType.BICUBIC_UPSCALE_3X.value).first() if eimg != None: return jsonify(code=201, url=eimg.url) imgContent = fdfs_client.downloadbyBuffer(orginimg.url[len(fdfs_addr):]) orgimg = cv.imdecode(np.frombuffer(imgContent, np.uint8), cv.IMREAD_COLOR) with tf.Session() as sess: srcnn = SRCNN(sess, "../srcnn/checkpoint/srcnn_21") img = srcnn.upscaling(orgimg, times, False) img = Img.fromarray(img) f = BytesIO() img.save(f, format='PNG') f = f.getvalue() suffix = orginimg.name[orginimg.name.find('.') + 1:] url = fdfs_addr + fdfs_client.uploadbyBuffer(f, suffix) imgname = url[url.rfind('/') + 1:] if times==3: newpic = Image(imgname, url,ImageType.BICUBIC_UPSCALE_3X , orginimg.id, current_user.id, -1) elif times==2: newpic = Image(imgname, url,ImageType.BICUBIC_UPSCALE_2X , orginimg.id, current_user.id, -1) db.session.add(newpic) db.session.flush() db.session.commit() return jsonify(code=201, message='和传统放大方法进行比较', url=newpic.url)
def index(): if request.method == 'POST': f = request.files['txt_file'] albumid = request.values.get('albumid') filename = f.filename f = f.read() suffix = filename[filename.find('.') + 1:] url = fdfs_addr + fdfs_client.uploadbyBuffer(f, suffix) name = url[url.rfind('/') + 1:] # 上传到数据库 img = Image(name, url, ImageType.ORIGIN, -1, current_user.id, -1) db.session.add(img) db.session.flush() db.session.commit() # 添加到相册中,默认是添加到系统相册中 if albumid == None or albumid == '' or albumid == '1': rediskey = 'album:' + str(current_user.id) + ':1' else:# 指定相册 rediskey = 'album:' + str(current_user.id) + ':' + albumid conn.zadd(rediskey, {img.id: time.time()}) return jsonify(code=200, message="success upload") else: return render_template('uploader.html')
def addalb(): if request.method == 'POST': name = request.values.get('name').strip() introduce = request.values.get('introduce').strip() f = request.files['addfrontcover'] filename = f.filename if filename != '': f = f.read() suffix = filename[filename.find('.') + 1:] url = fdfs_addr + fdfs_client.uploadbyBuffer(f, suffix) else: url = '/static/img/kittens.jpg' # 数据库添加字典的内容 photoalbum = Photoalbum(name, introduce, url) db.session.add(photoalbum) db.session.flush() db.session.commit() # 当前用户添加字典的id rediskey = 'useralbum:' + str(current_user.id) conn.sadd(rediskey, photoalbum.id) # 给用户发私信 # fireEvent(EventModel(EventType.ALBUM, -1, EntityType.USER, photoalbum.id, current_user.id,{'orginlname': name, 'action': '新建'})) return redirect('/user/useralbum')
def index(): if request.method == 'POST': content = request.values.get('dynamiccontent').strip() imgid = request.values.get('imgid') compareimgid = request.values.get('compareimgid') dynamic = Dynamic(content, current_user.id) db.session.add(dynamic) db.session.flush() db.session.commit() if imgid == None: files = request.files.getlist('dynamicimg') for f in files: filename = f.filename if filename == None or filename == '': break f = f.read() suffix = filename[filename.find('.') + 1:] url = fdfs_addr + fdfs_client.uploadbyBuffer(f, suffix) name = url[url.rfind('/') + 1:] img = Image(name, url, ImageType.ORIGIN, -1, current_user.id, dynamic.id) db.session.add(img) db.session.commit() fireEvent( EventModel(EventType.DYNAMIC, current_user.id, EntityType.DYNAMIC, dynamic.id, current_user.id, {'detail': '/dynamic/' + str(dynamic.id)})) else: img = Image.query.filter_by(id=imgid).first() compareimg = Image.query.filter_by(id=compareimgid).first() img.dynamic_id = dynamic.id compareimg.dynamic_id = dynamic.id db.session.commit() fireEvent( EventModel(EventType.SHARE, current_user.id, EntityType.DYNAMIC, dynamic.id, current_user.id, {'detail': '/dynamic/' + str(dynamic.id)})) return redirect('/') else: imgid = request.args.get('imgid') if imgid == None: return render_template('adddynamic.html') else: content = '' img = Image.query.filter_by(id=imgid).first() # 处理之后的图片 if ImageType(img.action) == ImageType.SRCNN: compareimg = Image.query.filter_by( id=img.orig_id).first() # 原图 content = '原图和系统处理过之后的图片比较' elif ImageType(img.action) == ImageType.UPSCALE_2X: compareimg = Image.query.filter_by( orig_id=img.orig_id, action=ImageType.BICUBIC_UPSCALE_2X.value).first() content = '传统的2x处理和系统SRCNN的2x处理的图片比较' elif ImageType(img.action) == ImageType.UPSCALE_3X: compareimg = Image.query.filter_by( orig_id=img.orig_id, action=ImageType.BICUBIC_UPSCALE_3X.value).first() content = '传统的3x处理和系统SRCNN的3x处理的图片比较' return render_template('adddynamic.html', img=img, compareimg=compareimg, content=content)
def superresolution(): if request.method == 'POST': data = json.loads(request.get_data(as_text=True)) imgid = data['imgid'] albumid = data['albumid'] flag = data['flag'] srcnnimg = Image.query.filter_by(id=imgid).first() if ImageType(srcnnimg.action)!=ImageType.ORIGIN: return jsonify(code=400, message='此图片为处理过的图片,请选择其他功能') srcnnimg = Image.query.filter_by(action=ImageType.SRCNN.value, orig_id=imgid).first() if srcnnimg != None: return jsonify(code=400, message='此图片已经优化过,请在该相册中查找') img = Image.query.filter_by(id=imgid).first() suffix = img.name[img.name.find('.') + 1:] imgContent = fdfs_client.downloadbyBuffer(img.url[len(fdfs_addr):]) img = cv.imdecode(np.frombuffer(imgContent, np.uint8), cv.IMREAD_COLOR) if img.shape[0] > 1500 or img.shape[1] > 1500: return jsonify(code=400, message='该图片尺寸较大,执行该操作会导致内存泄漏') if img.shape[0] > 900 or img.shape[1] > 900: # 大型任务,交给线程池处理 fireEvent(EventModel(EventType.TASK, current_user.id, EntityType.IMAGE, imgid, current_user.id, {'task': 'srcnn_process', 'action': ImageType.SRCNN.value, 'suffix': suffix, 'albumid': albumid})) if flag==True: return jsonify(code=203, message='请去图片所在相册查看结果') return jsonify(code=201, message='得到的图片较大,稍后请刷新相册页面') else: # 小图片直接处理 with tf.Session() as sess: srcnn = SRCNN(sess, "\srcnn\checkpoint\srcnn_21") img = srcnn.superresolution(img) img = Img.fromarray(img) f = BytesIO() img.save(f, format='PNG') f = f.getvalue() # 保存并上传数据库 url = fdfs_addr + fdfs_client.uploadbyBuffer(f, suffix) imgname = url[url.rfind('/') + 1:] newimg = Image(imgname, url, ImageType.SRCNN, imgid, current_user.id, -1) db.session.add(newimg) db.session.flush() db.session.commit() if albumid == None or albumid == '' or albumid == '1': rediskey = 'album:' + str(current_user.id) + ':1' else: rediskey = 'album:' + str(current_user.id) + ':' + albumid conn.zadd(rediskey, {newimg.id: time.time()}) # 系统统计 for i in range(0,2): dictstr=conn.rpop('count') dict=eval(dictstr) dict['count']=int(dict['count'])+1 conn.rpush('count',json.dumps(dict)) if flag==True: return jsonify(code=203, message='请去图片所在相册查看结果') return jsonify(code=200, message="success srcnn") else: rediskey = 'useralbum:' + str(current_user.id) albumdictlist = [] for albumid in conn.smembers(rediskey): dict = {} albumid=byte2int(albumid) album = Photoalbum.query.filter_by(id=albumid).first() dict['id'] = album.id dict['name'] = album.name albumdictlist.append(dict) return render_template('superresolution.html', albums=albumdictlist)