def Photo(isGET=False, data=dict()): if isGET: ph = models.Photo.query.get(data['id']) with open(ph.bit_file, 'r') as f: content = f.read() result = { "type": 6, "serialisedData": { "width": ph.width, "height": ph.height, "format": ph.texture_format, "mipmapCount": ph.mipmap_count, "data": content, "id": ph.id } } result['serialisedData'] = str(result['serialisedData']) return result else: ph = models.Photo(width=data['width'], height=data['height'], texture_format=data['format'], mipmap_count=data['mipmapCount'], bit_file=None) db.session.add(ph) db.session.commit() ph.bit_file = "../db/" + "ph" + str(ph.id) with open(ph.bit_file, 'w') as f: f.write(data['data']) db.session.commit() return str(ph.id)
def edit_by_id(id): user = session['user'] logged_in = True the_user = models.User.query.filter_by(username=user).first() the_settings = models.Settings.query.filter_by(user=the_user.id).first() display_name = the_settings.displayName post = models.Content.query.filter_by(id=id).first() if request.method == 'POST' and 'photo' in request.files: form = request.form pic = request.files['photo'] filename = photos.save(pic) user = session['user'] url = photos.url(filename) rec = models.Photo(filename=filename, user=user, url=url) db.session.add(rec) db.session.commit() if form is not None: res = posts.update_post_in_db(id, form["content"], url) print res else: print "nothing to update in db" return redirect(url_for('read')) return render_template('editPost.html', post=post, user=user, logged_in=logged_in, displayName=display_name)
def import_photoset(id): # Do some stuff account = models.FlickrAccount.query.get(1) print account #Set the api keys flickr_api.set_keys(api_key=account.key, api_secret=str(account.secret)) auth_handler = flickr_api.auth.AuthHandler( access_token_key=account.oauth_token, access_token_secret=str(account.oauth_secret)) # Set the authentication handler flickr_api.set_auth_handler(auth_handler) user = flickr_api.test.login() photosets = user.getPhotosets(id=id) print photosets photoset = [i for i in user.getPhotosets() if i.id == id][0] # Create category category = models.PhotoCategory() category.name = "Trips" category.slug = "Vacations and Trips" db.session.add(category) # Add the photoset to the db photoset_db = models.PhotoAlbum(category) photoset_db.name = photoset.title photoset_db.description = "First visit to San Francisco" category.albums.append(photoset_db) #db.session.add(photoset_db) photos = photoset.getPhotos() print photoset for photo in photos: print photo photo_db = models.Photo(photoset_db) sizes = photo.getSizes() photo_db.thumbnail_path = sizes['Medium 640']['source'] photo_db.path = sizes['Large 1600']['source'] photo_db.height = sizes['Large 1600']['height'] photo_db.width = sizes['Large 1600']['width'] print sizes['Medium 640']['source'] photoset_db.photos.append(photo_db) #db.session.add(photo_db) #print flickr_api.Photo.getInfo(photo=photo) #db.session db.session.add(category) db.session.commit()
def upload(): """ Upload a post Use Flask-Uploads for photo uploads :return: a redirect to write """ form = request.form print request.files if request.method == 'POST' and 'photo' in request.files: pic = request.files['photo'] filename = photos.save(pic) user = session['user'] url = photos.url(filename) rec = models.Photo(filename=filename, user=user, url=url) db.session.add(rec) db.session.commit() if form is not None: res = add_post_to_db(form['content'], url) print res else: print "nothing to add to db" return redirect(url_for('write'))
def recognition(self, request): BASE64 = request.data.get("image") imageType = "BASE64" account = request.data.get("account") imageName = uuid.uuid1() # # # 根据不同的imageType得到BASE64 # if imageType.equals("FILE"): # # # 写入本地文件夹 # imageAbsPath = "/static/files/images/local" + os.sep + str(imageName) # try: # with open(imageAbsPath, "wb+") as fp: # for chunk in image.chunk(): # fp.write(chunk) # except: # return HttpResponse("人脸识别功能模块失败") # # # 转成base64 # with open(imageAbsPath, 'rb') as f: # base64_data = base64.b64encode(f.read()) # BASE64 = base64_data.decode() # else: # BASE64 = image # 将BASE64写入本地文件夹 base64LocalAbsPath = "app/static/files/base64TXT/local" + os.sep + str(imageName) + ".txt" file = open(base64LocalAbsPath, 'w') file.write(BASE64) file.close() # 配置识别要求信息 options = {} options["face_field"] = "age,beauty,expression,gender,face_shape,glasses,eye_status,emotion,race" options["max_face_num"] = 1 options["face_type"] = "LIVE" options["liveness_control"] = "LOW" # 进行图像识别 dict = client.detect(BASE64, "BASE64", options)['result'] # 提取信息 result = {} result["age"] = dict["face_list"][0]["age"] result["beauty"] = dict["face_list"][0]["beauty"] * 0.4 + 60 result["expression"] = dict["face_list"][0]["expression"]["type"] result["gender"] = dict["face_list"][0]["gender"]["type"] result["face_shape"] = dict["face_list"][0]["face_shape"]["type"] if dict["face_list"][0]["glasses"]["type"]=='none': result["glasses"] = False else: result["glasses"] = True result["emotion"] = dict["face_list"][0]["emotion"]["type"] result["race"] = dict["face_list"][0]["race"]["type"] result["face_width"] = dict["face_list"][0]["location"]["width"] result["face_height"] = dict["face_list"][0]["location"]["height"] result["image_name"] = imageName # 打印测试 for key in result.keys(): print(key, ":", result[key]) user = User.objects.get(account=account) # 将识别信息写入数据库 # Photo = models.Photo() # Photo.age = result["age"] # Photo.beauty = result["beauty"] # Photo.expression = result["expression"] # Photo.gender = result["gender"] # Photo.face_shape = result["face_shape"] # Photo.glasses = result["glasses"] # Photo.emotion = result["emotion"] # Photo.race = result["race"] # Photo.face_height = result["face_height"] # Photo.face_width = result["face_width"] # Photo.image_name = imageName Photo = models.Photo() Photo.age = result["age"] Photo.beauty = result["beauty"] Photo.expression = result["expression"] Photo.gender = result["gender"] Photo.face_shape = result["face_shape"] Photo.glasses = result["glasses"] Photo.emotion = result["emotion"] Photo.race = result["race"] Photo.face_height = result["face_height"] Photo.face_width = result["face_width"] Photo.image_name = imageName Photo.public = True # Photo.save() # 反过来将数据库中生成的photo_id提取出来写进result # p = Photo.objects.get() # 将其他信息写入数据库 Photo.base64 = base64LocalAbsPath Photo.date = str(datetime.now()) # user = User.objects.get(account=account) Photo.account = user # Photo.account_id = user.user_id Photo.save() photo_id = Photo.photo_id result["photo_id"] = photo_id return JsonResponse(result)