166 lines
4.7 KiB
Python
166 lines
4.7 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import json
|
|
import base64
|
|
from flask import Flask, render_template, request, jsonify, redirect, url_for
|
|
from flask_cors import CORS
|
|
from werkzeug.utils import secure_filename
|
|
from app.api.baidu_image_search import BaiduImageSearch
|
|
from app.api.image_utils import ImageUtils
|
|
|
|
app = Flask(__name__, template_folder='app/templates', static_folder='app/static')
|
|
CORS(app) # 启用CORS支持跨域请求
|
|
|
|
# 配置上传文件夹
|
|
UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'uploads')
|
|
if not os.path.exists(UPLOAD_FOLDER):
|
|
os.makedirs(UPLOAD_FOLDER)
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
|
|
# 允许的文件扩展名
|
|
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
|
|
|
|
# 初始化百度图像搜索API
|
|
image_search_api = BaiduImageSearch()
|
|
|
|
def allowed_file(filename):
|
|
"""检查文件扩展名是否允许"""
|
|
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
@app.route('/')
|
|
def index():
|
|
"""首页"""
|
|
return render_template('index.html')
|
|
|
|
@app.route('/upload', methods=['POST'])
|
|
def upload_image():
|
|
"""上传图片到图库"""
|
|
if 'file' not in request.files:
|
|
return jsonify({'error': '没有文件上传'}), 400
|
|
|
|
file = request.files['file']
|
|
if file.filename == '':
|
|
return jsonify({'error': '没有选择文件'}), 400
|
|
|
|
if not allowed_file(file.filename):
|
|
return jsonify({'error': '不支持的文件类型'}), 400
|
|
|
|
# 获取表单数据
|
|
name = request.form.get('name', '')
|
|
image_id = request.form.get('id', '')
|
|
tags = request.form.get('tags', '')
|
|
|
|
# 创建brief信息
|
|
brief = {
|
|
'name': name,
|
|
'id': image_id
|
|
}
|
|
|
|
# 保存文件
|
|
filename = secure_filename(file.filename)
|
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
|
file.save(file_path)
|
|
|
|
try:
|
|
# 调用API添加图片
|
|
result = image_search_api.add_image(
|
|
image_path=file_path,
|
|
brief=brief,
|
|
tags=tags
|
|
)
|
|
|
|
# 添加本地存储路径到结果
|
|
result['file_path'] = file_path
|
|
|
|
return jsonify(result)
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@app.route('/search', methods=['POST'])
|
|
def search_image():
|
|
"""搜索相似图片"""
|
|
if 'file' not in request.files:
|
|
return jsonify({'error': '没有文件上传'}), 400
|
|
|
|
file = request.files['file']
|
|
if file.filename == '':
|
|
return jsonify({'error': '没有选择文件'}), 400
|
|
|
|
if not allowed_file(file.filename):
|
|
return jsonify({'error': '不支持的文件类型'}), 400
|
|
|
|
# 获取表单数据
|
|
tags = request.form.get('tags', '')
|
|
tag_logic = request.form.get('tag_logic', '0')
|
|
|
|
# 保存文件
|
|
filename = secure_filename(file.filename)
|
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
|
file.save(file_path)
|
|
|
|
try:
|
|
# 调用API搜索图片
|
|
result = image_search_api.search_image(
|
|
image_path=file_path,
|
|
tags=tags,
|
|
tag_logic=tag_logic
|
|
)
|
|
|
|
return jsonify(result)
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@app.route('/delete', methods=['POST'])
|
|
def delete_image():
|
|
"""删除图库中的图片"""
|
|
data = request.get_json()
|
|
|
|
if not data or 'cont_sign' not in data:
|
|
return jsonify({'error': '缺少必要参数'}), 400
|
|
|
|
cont_sign = data['cont_sign']
|
|
|
|
try:
|
|
# 调用API删除图片
|
|
result = image_search_api.delete_image(cont_sign=cont_sign)
|
|
return jsonify(result)
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@app.route('/update', methods=['POST'])
|
|
def update_image():
|
|
"""更新图库中的图片信息"""
|
|
data = request.get_json()
|
|
|
|
if not data or 'cont_sign' not in data:
|
|
return jsonify({'error': '缺少必要参数'}), 400
|
|
|
|
cont_sign = data['cont_sign']
|
|
brief = data.get('brief')
|
|
tags = data.get('tags')
|
|
|
|
try:
|
|
# 调用API更新图片
|
|
result = image_search_api.update_image(
|
|
cont_sign=cont_sign,
|
|
brief=brief,
|
|
tags=tags
|
|
)
|
|
return jsonify(result)
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@app.route('/api/token')
|
|
def get_token():
|
|
"""获取API访问令牌"""
|
|
try:
|
|
token = image_search_api.get_access_token()
|
|
return jsonify({'access_token': token})
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, host='0.0.0.0', port=5000)
|