def add_gallery(): form = GalleryForm() if form.validate_on_submit(): if request.files.getlist('photos'): ps = [] for file in request.files.getlist('photos'): if file and allowed_file(file.filename): filename = secure_filename(file.filename) extension = filename.rsplit('.', 1)[1].lower() photo = Photo(extension=extension) photo.save() ps.append(photo) unified_filename = str(photo.id) + '.' + extension f = file try: picture_handler(f, unified_filename) except: print("picture file may not be updated") flash('New picture uploaded!') gallery = Gallery(name=form.name.data, description=form.description.data, photos=ps) gallery.save() return redirect(url_for('main.galleries')) return render_template('admin/add_gallery.html', title='Create Gallery', form=form)
def photos(): if request.method == "POST": filename = str(request.data.get('filename', '')) if filename: photo = Photo(filename=filename) photo.save() response = jsonify({ 'id': photo.id, 'filename': photo.filename, 'date_created': photo.date_created, 'date_modified': photo.date_modified }) response.status_code = 201 return response else: # GET photos = Photo.get_all() results = [] for photo in photos: obj = { 'id': photo.id, 'filename': photo.filename, 'date_created': photo.date_created, 'date_modified': photo.date_modified } results.append(obj) response = jsonify(results) response.status_code = 200 return response
def take(self, request): filename = '%s.jpg' % datetime.now().strftime('%Y%m%d%H%M%S%f') Camera.initialize() Camera.camera.capture(os.path.join( settings.MEDIA_ROOT, filename), quality=100 ) photo = Photo() photo.name = filename photo.save() return Response(PhotoSerializer(photo).data)
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)