Thik ache! Ami apnar jonno complete face enhancement website baniye dichi API chhada:
python Copy code
from flask import Flask, render_template, request, send_file, jsonify
import cv2
import numpy as np
from PIL import Image, ImageEnhance, ImageFilter
import os
import io
import base64
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ENHANCED_FOLDER'] = 'enhanced'
# Create directories if they don't exist
os.makedirs('uploads', exist_ok=True)
os.makedirs('enhanced', exist_ok=True)
class FaceEnhancer:
def __init__(self):
# Load face cascade
self.face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
def enhance_image(self, image_path):
# Read image
img = cv2.imread(image_path)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
pil_img = Image.fromarray(img_rgb)
# Face detection
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray, 1.1, 4)
# Basic enhancement
enhanced_img = self.apply_enhancements(pil_img)
# Face-specific enhancement
if len(faces) > 0:
enhanced_img = self.enhance_face_region(enhanced_img, faces, img_rgb.shape)
return enhanced_img
def apply_enhancements(self, img):
# Brightness enhancement
brightness_enhancer = ImageEnhance.Brightness(img)
img = brightness_enhancer.enhance(1.2)
# Contrast enhancement
contrast_enhancer = ImageEnhance.Contrast(img)
img = contrast_enhancer.enhance(1.1)
# Color enhancement
color_enhancer = ImageEnhance.Color(img)
img = color_enhancer.enhance(1.1)
# Sharpening
img = img.filter(ImageFilter.UnsharpMask(radius=1, percent=150, threshold=3))
return img
def enhance_face_region(self, img, faces, img_shape):
img_array = np.array(img)
for (x, y, w, h) in faces:
# Extract face region
face_region = img_array[y:y+h, x:x+w]
# Apply face-specific enhancements
face_pil = Image.fromarray(face_region)
# Skin smoothing effect
face_smooth = face_pil.filter(ImageFilter.GaussianBlur(radius=0.5))
face_enhanced = Image.blend(face_pil, face_smooth, 0.3)
# Extra brightness for face
face_bright = ImageEnhance.Brightness(face_enhanced).enhance(1.1)
# Put enhanced face back
img_array[y:y+h, x:x+w] = np.array(face_bright)
return Image.fromarray(img_array)
def upscale_image(self, img, scale_factor=2):
"""Simple upscaling using LANCZOS"""
width, height = img.size
new_size = (int(width * scale_factor), int(height * scale_factor))
return img.resize(new_size, Image.LANCZOS)
enhancer = FaceEnhancer()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/enhance', methods=['POST'])
def enhance_image():
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'})
if file:
# Save uploaded file
filename = file.filename
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
try:
# Enhance image
enhanced_img = enhancer.enhance_image(filepath)
# Get enhancement options
brightness = float(request.form.get('brightness', 1.0))
contrast = float(request.form.get('contrast', 1.0))
sharpness = float(request.form.get('sharpness', 1.0))
upscale = request.form.get('upscale', 'false') == 'true'
# Apply custom settings
if brightness != 1.0:
enhanced_img = ImageEnhance.Brightness(enhanced_img).enhance(brightness)
if contrast != 1.0:
enhanced_img = ImageEnhance.Contrast(enhanced_img).enhance(contrast)
if sharpness != 1.0:
enhanced_img = ImageEnhance.Sharpness(enhanced_img).enhance(sharpness)
# Upscale if requested
if upscale:
enhanced_img = enhancer.upscale_image(enhanced_img, 2)
# Save enhanced image
enhanced_filename = f"enhanced_{filename}"
enhanced_filepath = os.path.join(app.config['ENHANCED_FOLDER'], enhanced_filename)
enhanced_img.save(enhanced_filepath, quality=95)
# Convert to base64 for preview
img_buffer = io.BytesIO()
enhanced_img.save(img_buffer, format='JPEG', quality=85)
img_base64 = base64.b64encode(img_buffer.getvalue()).decode()
return jsonify({
'success': True,
'enhanced_image': f"data:image/jpeg;base64,{img_base64}",
'download_url': f"/download/{enhanced_filename}"
})
except Exception as e:
return jsonify({'error': f'Enhancement failed: {str(e)}'})
@app.route('/download/<filename>')
def download_file(filename):
return send_file(os.path.join(app.config['ENHANCED_FOLDER'], filename), as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Face Enhancement Tool</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>🎨 Face Enhancement Tool</h1>
<p>Upload your photo for professional enhancement</p>
<div class="upload-section">
<input type="file" id="imageInput" accept="image/*" style="display: none;">
<button id="uploadBtn" class="upload-btn">📸 Choose Photo</button>
<div id="fileName" class="file-name"></div>
</div>
<div class="controls" id="controls" style="display: none;">
<h3>Enhancement Settings</h3>
<div class="control-group">
<label>Brightness: <span id="brightnessValue">1.0</span></label>
<input type="range" id="brightness" min="0.5" max="2.0" step="0.1" value="1.0">
</div>
<div class="control-group">
<label>Contrast: <span id="contrastValue">1.0</span></label>
<input type="range" id="contrast" min="0.5" max="2.0" step="0.1" value="1.0">
</div>
<div class="control-group">
<label>Sharpness: <span id="sharpnessValue">1.0</span></label>
<input type="range" id="sharpness" min="0.5" max="2.0" step="0.1" value="1.0">
</div>
<div class="control-group">
<label>
<input type="checkbox" id="upscale"> 2x Upscale (Higher Resolution)
</label>
</div>
<button id="enhanceBtn" class="enhance-btn">✨ Enhance Photo</button>
</div>
<div class="preview-section" id="previewSection" style="display: none;">
<h3>Preview</h3>
<div class="image-comparison">
<div class="image-container">
<h4>Original</h4>
<img id="originalImage" alt="Original">
</div>
<div class="image-container">
<h4>Enhanced</h4>
<img id="enhancedImage" alt="Enhanced">
</div>
</div>
<button id="downloadBtn" class="download-btn">💾 Download Enhanced Photo</button>
</div>
<div id="loading" class="loading" style="display: none;">
<div class="spinner"></div>
<p>Enhancing your photo...</p>
</div>
</div>
<script src="{{ url_for('static', filename='script.js') }}"></script>
</body>
</html>
css Copy code
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 15px;
padding: 30px;
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
color: #333;
margin-bottom: 10px;
font-size: 2.5em;
}
p {
text-align: center;
color: #666;
margin-bottom: 30px;
}
.upload-section {
text-align: center;
margin-bottom: 30px;
}
.upload-btn {
background: linear-gradient(45deg, #667eea, #764ba2);
color: white;
border: none;
padding: 15px 30px;
border-radius: 25px;
font-size: 18px;
cursor: pointer;
transition: transform 0.3s;
}
.upload-btn:hover {
transform: translateY(-2px);
}
.file-name {
margin-top: 10px;
color: #666;
font-style: italic;
}
.controls {
background: #f8f9fa;
padding: 20px;
border-radius: 10px;
margin-bottom: 30px;
}
.control-group {
margin-bottom: 20px;
}
.control-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #333;
}
.control-group input[type="range"] {
width: 100%;
height: 6px;
border-radius: 3px;
background: #ddd;
outline: none;
}
.control-group input[type="checkbox"] {
margin-right: 10px;
}
.enhance-btn {
background: linear-gradient(45deg, #28a745, #20c997);
color: white;
border: none;
padding: 12px 25px;
border-radius: 20px;
font-size: 16px;
cursor: pointer;
transition: transform 0.3s;
}
.enhance-btn:hover {
transform: translateY(-2px);
}
.preview-section {
margin-top: 30px;
}
.image-comparison {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 20px;
}
.image-container {
text-align: center;
}
.image-container h4 {
margin-bottom: 10px;
color: #333;
}
.image-container img {
max-width: 100%;
height: auto;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
.download-btn {
background: linear-gradient(45deg, #dc3545, #fd7e14);
color: white;
border: none;
padding: 12px 25px;
border-radius: 20px;
font-size: 16px;
cursor: pointer;
transition: transform 0.3s;
display: block;
margin: 0 auto;
}
.download-btn:hover {
transform: translateY(-2px);
}
.loading {
text-align: center;
padding: 40px;
}
.spinner {
border: 4px solid #f3f3f3;
border-top: 4px solid #667eea;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 0 auto 20px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@media (max-width: 768px) {
.image-comparison {
grid-template-columns: 1fr;
}
.container {
padding: 20px;
}
h1 {
font-size: 2em;
}
}
Ei complete solution diye apni professional-level face enhancement website paben API chhada!