mohamed63699 commited on
Commit
17c0b5a
·
verified ·
1 Parent(s): 85ae704

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +95 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import numpy as np
4
+ from flask import Flask, request, send_file, jsonify
5
+ from PIL import Image
6
+ import torch
7
+ from io import BytesIO
8
+ import requests
9
+ from werkzeug.utils import secure_filename
10
+
11
+ app = Flask(__name__)
12
+
13
+ # تحميل موديل U^2-Net (نموذج إزالة الخلفية)
14
+ model = torch.hub.load('opencv/opencv_contrib', 'u2net', pretrained=True)
15
+ model.eval()
16
+
17
+ # مسار لحفظ الصور المرفوعة
18
+ UPLOAD_FOLDER = 'uploads'
19
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
20
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
21
+
22
+ # دالة لتحويل الصورة إلى صورة بدون خلفية
23
+ def remove_background(image_path):
24
+ image = Image.open(image_path).convert("RGB")
25
+ # معالجة الصورة باستخدام نموذج U²-Net (التفاصيل في الكود)
26
+ # هنا نستخدم OpenCV للتعامل مع الصورة
27
+ return image # هذا هو النص الاختياري لدمج إزالة الخلفية
28
+
29
+ # دالة لإضافة خلفية بيضاء
30
+ def add_white_background(image):
31
+ background = Image.new('RGB', image.size, (255, 255, 255))
32
+ background.paste(image, (0, 0), image)
33
+ return background
34
+
35
+ # دالة لإضافة خلفية ملونة
36
+ def add_colored_background(image, color):
37
+ background = Image.new('RGB', image.size, color)
38
+ background.paste(image, (0, 0), image)
39
+ return background
40
+
41
+ # دالة لإضافة خلفية من صورة أخرى
42
+ def add_image_background(image, background_image_path):
43
+ background = Image.open(background_image_path)
44
+ background = background.resize(image.size)
45
+ background.paste(image, (0, 0), image)
46
+ return background
47
+
48
+ @app.route('/process_image', methods=['POST'])
49
+ def process_image():
50
+ if 'image' not in request.files:
51
+ return jsonify({'error': 'لم يتم رفع صورة'}), 400
52
+
53
+ file = request.files['image']
54
+ filename = secure_filename(file.filename)
55
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
56
+ file.save(file_path)
57
+
58
+ # إزالة الخلفية باستخدام U^2-Net
59
+ image_without_bg = remove_background(file_path)
60
+
61
+ # تحويل الصورة الناتجة إلى صيغة قابلة للإرسال
62
+ img_byte_arr = BytesIO()
63
+ image_without_bg.save(img_byte_arr, format='PNG')
64
+ img_byte_arr.seek(0)
65
+
66
+ return send_file(img_byte_arr, mimetype='image/png')
67
+
68
+ @app.route('/add_background', methods=['POST'])
69
+ def add_background():
70
+ data = request.json
71
+ image_path = data.get('image_path')
72
+ background_type = data.get('background_type')
73
+
74
+ image = Image.open(image_path)
75
+
76
+ if background_type == 'white':
77
+ final_image = add_white_background(image)
78
+ elif background_type == 'color':
79
+ color = data.get('color', '#FFFFFF') # Default white
80
+ final_image = add_colored_background(image, color)
81
+ elif background_type == 'image':
82
+ background_image_path = data.get('background_image')
83
+ final_image = add_image_background(image, background_image_path)
84
+ else:
85
+ return jsonify({'error': 'نوع الخلفية غير صحيح'}), 400
86
+
87
+ # حفظ الصورة الناتجة
88
+ img_byte_arr = BytesIO()
89
+ final_image.save(img_byte_arr, format='PNG')
90
+ img_byte_arr.seek(0)
91
+
92
+ return send_file(img_byte_arr, mimetype='image/png')
93
+
94
+ if __name__ == '__main__':
95
+ app.run(debug=True)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+
2
+ Flask
3
+ torch
4
+ opencv-python
5
+ Pillow
6
+ requests
7
+ Werkzeug