Spaces:
Sleeping
Sleeping
File size: 2,797 Bytes
713d623 bd2dc2e b3f1777 713d623 a8a350d b3f1777 713d623 bd2dc2e 713d623 4c9a049 713d623 a8a350d 713d623 68e3db1 713d623 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import gradio as gr
import vtracer
import os
import base64
from io import BytesIO
from PIL import Image
import json
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/api/predict', methods=['POST'])
def predict():
try:
# Get image file from request
if 'image' not in request.files:
return jsonify({"error": "No image file provided"}), 400
image_file = request.files['image']
image = Image.open(image_file)
# Get parameters from request
colormode = request.form.get('colormode', 'color')
hierarchical = request.form.get('hierarchical', 'stacked')
mode = request.form.get('mode', 'spline')
filter_speckle = int(request.form.get('filter_speckle', 4))
color_precision = int(request.form.get('color_precision', 6))
layer_difference = int(request.form.get('layer_difference', 16))
corner_threshold = int(request.form.get('corner_threshold', 60))
length_threshold = float(request.form.get('length_threshold', 4.0))
max_iterations = int(request.form.get('max_iterations', 10))
splice_threshold = int(request.form.get('splice_threshold', 45))
path_precision = int(request.form.get('path_precision', 3))
# Create temp directory if it doesn't exist
os.makedirs('temp', exist_ok=True)
# Save the input image to a temporary file
input_path = "temp/temp_input.jpg"
output_path = "temp/svg_output.svg"
image.save(input_path)
# Convert the image to SVG using VTracer
vtracer.convert_image_to_svg_py(
input_path,
output_path,
colormode=colormode,
hierarchical=hierarchical,
mode=mode,
filter_speckle=filter_speckle,
color_precision=color_precision,
layer_difference=layer_difference,
corner_threshold=corner_threshold,
length_threshold=length_threshold,
max_iterations=max_iterations,
splice_threshold=splice_threshold,
path_precision=path_precision
)
# Read the SVG output
with open(output_path, "r") as f:
svg_content = f.read()
# Create SVG with viewBox
full_svg = f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>'
# Return SVG content and path
return jsonify({
"data": [full_svg, output_path],
"success": True
})
except Exception as e:
return jsonify({"error": str(e), "success": False}), 500
if __name__ == "__main__":
app.run(host='0.0.0.0', port=7860)
|