add proxy
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
import os
|
||||
import requests
|
||||
from flask import Flask, request, jsonify, send_from_directory
|
||||
from flask_cors import CORS
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
STATIC_FOLDER = os.path.join(APP_ROOT, 'static')
|
||||
|
||||
app = Flask(__name__, static_folder=STATIC_FOLDER, static_url_path='/static')
|
||||
CORS(app)
|
||||
|
||||
CHANGE_APP_BASE_URL = "http://127.0.0.1:28888"
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def log_message(msg):
|
||||
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {msg}")
|
||||
logger.info(msg)
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return send_from_directory(STATIC_FOLDER, 'index.html')
|
||||
|
||||
|
||||
@app.route('/health', methods=['GET'])
|
||||
def health():
|
||||
return jsonify({"status": "ok", "service": "change_cloth_proxy"})
|
||||
|
||||
|
||||
@app.route('/change_cloth', methods=['POST'])
|
||||
def change_cloth():
|
||||
log_message("change_cloth called")
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({"ret": -1, "state": -1, "msg": "No JSON data provided"}), 400
|
||||
|
||||
required_fields = ["human_url", "cloth_url", "output_format"]
|
||||
for field in required_fields:
|
||||
if field not in data:
|
||||
return jsonify({"ret": -1, "state": -1, "msg": f"Missing required field: {field}"}), 400
|
||||
|
||||
log_message(f"Forwarding change_cloth request: human_url={data.get('human_url')}, cloth_url={data.get('cloth_url')}")
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{CHANGE_APP_BASE_URL}/change_cloth",
|
||||
json=data,
|
||||
timeout=600
|
||||
)
|
||||
log_message(f"change_cloth response status: {response.status_code}")
|
||||
return jsonify(response.json()), response.status_code
|
||||
except requests.exceptions.Timeout:
|
||||
log_message("change_cloth request timeout")
|
||||
return jsonify({"ret": -1, "state": -1, "msg": "Request timeout"}), 504
|
||||
except requests.exceptions.RequestException as e:
|
||||
log_message(f"change_cloth request error: {str(e)}")
|
||||
return jsonify({"ret": -1, "state": -1, "msg": f"Request failed: {str(e)}"}), 500
|
||||
|
||||
|
||||
@app.route('/change_cloth_base64', methods=['POST'])
|
||||
def change_cloth_base64():
|
||||
log_message("change_cloth_base64 called")
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({"ret": -1, "state": -1, "msg": "No JSON data provided"}), 400
|
||||
|
||||
required_fields = ["human_img", "cloth_img", "output_format"]
|
||||
for field in required_fields:
|
||||
if field not in data:
|
||||
return jsonify({"ret": -1, "state": -1, "msg": f"Missing required field: {field}"}), 400
|
||||
|
||||
log_message("Forwarding change_cloth_base64 request")
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{CHANGE_APP_BASE_URL}/change_cloth_base64",
|
||||
json=data,
|
||||
timeout=600
|
||||
)
|
||||
log_message(f"change_cloth_base64 response status: {response.status_code}")
|
||||
return jsonify(response.json()), response.status_code
|
||||
except requests.exceptions.Timeout:
|
||||
log_message("change_cloth_base64 request timeout")
|
||||
return jsonify({"ret": -1, "state": -1, "msg": "Request timeout"}), 504
|
||||
except requests.exceptions.RequestException as e:
|
||||
log_message(f"change_cloth_base64 request error: {str(e)}")
|
||||
return jsonify({"ret": -1, "state": -1, "msg": f"Request failed: {str(e)}"}), 500
|
||||
|
||||
|
||||
@app.route('/do_change_cloth', methods=['POST'])
|
||||
def do_change_cloth():
|
||||
log_message("do_change_cloth called")
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({"ret": -1, "state": -1, "msg": "No JSON data provided"}), 400
|
||||
|
||||
required_fields = ["human_url", "cloth_url", "output_format"]
|
||||
for field in required_fields:
|
||||
if field not in data:
|
||||
return jsonify({"ret": -1, "state": -1, "msg": f"Missing required field: {field}"}), 400
|
||||
|
||||
log_message(f"Forwarding do_change_cloth request: human_url={data.get('human_url')}, cloth_url={data.get('cloth_url')}")
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{CHANGE_APP_BASE_URL}/do_change_cloth",
|
||||
json=data,
|
||||
timeout=600
|
||||
)
|
||||
log_message(f"do_change_cloth response status: {response.status_code}")
|
||||
return jsonify(response.json()), response.status_code
|
||||
except requests.exceptions.Timeout:
|
||||
log_message("do_change_cloth request timeout")
|
||||
return jsonify({"ret": -1, "state": -1, "msg": "Request timeout"}), 504
|
||||
except requests.exceptions.RequestException as e:
|
||||
log_message(f"do_change_cloth request error: {str(e)}")
|
||||
return jsonify({"ret": -1, "state": -1, "msg": f"Request failed: {str(e)}"}), 500
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
log_message(f"Starting change_cloth_proxy service on port 5000, forwarding to {CHANGE_APP_BASE_URL}")
|
||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
||||
@@ -0,0 +1,313 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>服装更换工具</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
h1 {
|
||||
text-align: center;
|
||||
color: #333;
|
||||
}
|
||||
.image-upload-container {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.image-upload-box {
|
||||
width: 48%;
|
||||
border: 2px dashed #ccc;
|
||||
padding: 15px;
|
||||
text-align: center;
|
||||
border-radius: 5px;
|
||||
}
|
||||
.image-upload-box h3 {
|
||||
margin-top: 0;
|
||||
}
|
||||
.image-preview {
|
||||
max-width: 100%;
|
||||
max-height: 300px;
|
||||
margin-top: 10px;
|
||||
display: none;
|
||||
}
|
||||
button {
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
padding: 10px 15px;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
display: block;
|
||||
margin: 20px auto;
|
||||
}
|
||||
button:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
#result {
|
||||
margin-top: 20px;
|
||||
padding: 15px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
display: none;
|
||||
}
|
||||
#loading {
|
||||
text-align: center;
|
||||
display: none;
|
||||
margin: 20px 0;
|
||||
}
|
||||
.spinner {
|
||||
border: 5px solid #f3f3f3;
|
||||
border-top: 5px solid #3498db;
|
||||
border-radius: 50%;
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
animation: spin 2s linear infinite;
|
||||
margin: 0 auto;
|
||||
}
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
.error {
|
||||
color: red;
|
||||
}
|
||||
.option-box {
|
||||
margin: 15px 0;
|
||||
padding: 15px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 5px;
|
||||
}
|
||||
.option-box label {
|
||||
display: block;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.cloth-len-select {
|
||||
margin: 15px 0;
|
||||
}
|
||||
.cloth-len-select label {
|
||||
display: block;
|
||||
margin-bottom: 5px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.cloth-len-select select {
|
||||
width: 100%;
|
||||
padding: 8px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
font-size: 14px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>服装更换工具</h1>
|
||||
|
||||
<div class="image-upload-container">
|
||||
<div class="image-upload-box">
|
||||
<h3>人体图片</h3>
|
||||
<input type="file" id="humanInput" accept="image/*">
|
||||
<img id="humanPreview" class="image-preview" alt="人体图片预览">
|
||||
</div>
|
||||
|
||||
<div class="image-upload-box">
|
||||
<h3>衣服图片</h3>
|
||||
<input type="file" id="clothInput" accept="image/*">
|
||||
<img id="clothPreview" class="image-preview" alt="衣服图片预览">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="image-upload-container">
|
||||
<div class="image-upload-box">
|
||||
<h3>裤子图片</h3>
|
||||
<input type="file" id="kuziInput" accept="image/*">
|
||||
<img id="kuziPreview" class="image-preview" alt="裤子图片预览">
|
||||
</div>
|
||||
|
||||
<div class="cloth-len-select">
|
||||
<label for="clothLenSelect">选择服装长度:</label>
|
||||
<select id="clothLenSelect">
|
||||
<option value="胸">胸</option>
|
||||
<option value="腰">腰</option>
|
||||
<option value="跨">跨</option>
|
||||
<option value="大腿">大腿</option>
|
||||
<option value="膝盖">膝盖</option>
|
||||
<option value="小腿">小腿</option>
|
||||
<option value="脚踝">脚踝</option>
|
||||
<option value="拖地">拖地</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="option-box">
|
||||
<label>
|
||||
<input type="checkbox" id="no2Checkbox"> 设置 no2 不要两步(一步完成)
|
||||
</label>
|
||||
<br>
|
||||
<label>
|
||||
<input type="checkbox" id="tuodiCheckbox"> 设置 tuodi(拖地)
|
||||
</label>
|
||||
<br>
|
||||
<label>
|
||||
<input type="checkbox" id="suitCheckbox"> 设置 suit(套装)
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<button id="submitBtn">提交处理</button>
|
||||
|
||||
<div id="loading">
|
||||
<div class="spinner"></div>
|
||||
<p>正在处理中,请稍候...</p>
|
||||
</div>
|
||||
|
||||
<div id="result"></div>
|
||||
|
||||
<script>
|
||||
document.getElementById('humanInput').addEventListener('change', function(e) {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(event) {
|
||||
const img = document.getElementById('humanPreview');
|
||||
img.src = event.target.result;
|
||||
img.style.display = 'block';
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('clothInput').addEventListener('change', function(e) {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(event) {
|
||||
const img = document.getElementById('clothPreview');
|
||||
img.src = event.target.result;
|
||||
img.style.display = 'block';
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('kuziInput').addEventListener('change', function(e) {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(event) {
|
||||
const img = document.getElementById('kuziPreview');
|
||||
img.src = event.target.result;
|
||||
img.style.display = 'block';
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
});
|
||||
|
||||
document.getElementById('submitBtn').addEventListener('click', function() {
|
||||
const humanFile = document.getElementById('humanInput').files[0];
|
||||
const clothFile = document.getElementById('clothInput').files[0];
|
||||
const kuziFile = document.getElementById('kuziInput').files[0];
|
||||
const no2 = document.getElementById("no2Checkbox").checked;
|
||||
const tuodi = document.getElementById("tuodiCheckbox").checked;
|
||||
const suit = document.getElementById("suitCheckbox").checked;
|
||||
const clothLen = document.getElementById("clothLenSelect").value;
|
||||
|
||||
|
||||
if (!humanFile || !clothFile) {
|
||||
alert('请同时选择人体图片和衣服图片!');
|
||||
return;
|
||||
}
|
||||
|
||||
document.getElementById('loading').style.display = 'block';
|
||||
document.getElementById('result').style.display = 'none';
|
||||
|
||||
Promise.all([
|
||||
readFileAsDataURL(humanFile),
|
||||
readFileAsDataURL(clothFile),
|
||||
kuziFile ? readFileAsDataURL(kuziFile) : Promise.resolve(null)
|
||||
]).then(([humanDataURL, clothDataURL, kuziDataURL]) => {
|
||||
|
||||
const data = {
|
||||
human_img: humanDataURL,
|
||||
cloth_img: clothDataURL,
|
||||
output_format: "url",
|
||||
no2: no2,
|
||||
tuodi: tuodi,
|
||||
suit: suit,
|
||||
cloth_len: clothLen
|
||||
};
|
||||
|
||||
if(kuziDataURL)
|
||||
{
|
||||
data.kuzi_img = kuziDataURL;
|
||||
}
|
||||
|
||||
console.log("准备发送的数据:", data);
|
||||
|
||||
fetch('/change_cloth_base64', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP错误! 状态: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
document.getElementById('loading').style.display = 'none';
|
||||
|
||||
const resultDiv = document.getElementById('result');
|
||||
if (data.url) {
|
||||
resultDiv.innerHTML = `
|
||||
<h3>处理结果</h3>
|
||||
<p>处理成功!点击查看结果:</p>
|
||||
<a href="${data.url}" target="_blank">${data.url}</a>
|
||||
<p><img src="${data.url}" style="max-width: 100%; margin-top: 10px;"></p>
|
||||
<a href="${data.first_url}" target="_blank">${data.url}</a>
|
||||
<p><img src="${data.first_url}" style="max-width: 100%; margin-top: 10px;"></p>
|
||||
<a href="${data.second_url}" target="_blank">${data.url}</a>
|
||||
<p><img src="${data.second_url}" style="max-width: 100%; margin-top: 10px;"></p>
|
||||
`;
|
||||
} else {
|
||||
resultDiv.innerHTML = `
|
||||
<h3>处理结果</h3>
|
||||
<p class="error">处理完成,但未返回预期的URL</p>
|
||||
<p>返回数据:</p>
|
||||
<pre>${JSON.stringify(data, null, 2)}</pre>
|
||||
`;
|
||||
}
|
||||
resultDiv.style.display = 'block';
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
document.getElementById('loading').style.display = 'none';
|
||||
document.getElementById('result').innerHTML = `
|
||||
<h3 class="error">错误</h3>
|
||||
<p>处理过程中出现错误:${error.message}</p>
|
||||
<p>请检查控制台获取更多信息</p>
|
||||
`;
|
||||
document.getElementById('result').style.display = 'block';
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function readFileAsDataURL(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result);
|
||||
reader.onerror = (error) => reject(error);
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user