Rzx008 commited on
Commit
45a3fc9
·
verified ·
1 Parent(s): 685b61f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -154
app.py CHANGED
@@ -1,24 +1,45 @@
1
  from flask import Flask, request, jsonify, send_file
2
- import io
3
  import traceback
4
  from android import AndroidEditor
5
- import base64
 
 
 
 
6
 
7
  app = Flask(__name__)
8
 
 
 
 
 
 
9
  @app.route('/')
10
  def index():
 
 
 
11
  return "Kelas Kink"
12
 
13
  @app.route('/android', methods=['POST'])
14
  def process_android_image():
 
 
 
 
 
 
 
 
 
 
15
  app.logger.info('Menerima permintaan POST untuk memproses gambar')
16
  anggota = request.form.get('anggota')
17
  image_file = request.files.get('file')
18
 
19
  if not anggota or not image_file:
20
  return jsonify({'status': 'failed', 'message': 'Parameter anggota dan file diperlukan'}), 400
21
-
22
  try:
23
  file_bytes = image_file.read()
24
  editor = AndroidEditor()
@@ -34,159 +55,35 @@ def process_android_image():
34
 
35
  @app.route('/iphone', methods=['POST'])
36
  def process_iphone_image():
37
- try :
38
- return jsonify({'status': 'failed', 'message': 'Fitur ini sedang dikembangkan'}), 500
39
- except Exception as e:
40
- error_traceback = traceback.format_exc()
41
- print(f"Error occurred: {error_traceback}")
42
- return jsonify({
43
- 'status': 'failed',
44
- 'message': str(e),
45
- 'traceback': error_traceback
46
- }), 500
47
- # app.logger.info('Menerima permintaan POST untuk memproses gambar iPhone')
48
- # anggota = request.form.get('anggota')
49
- # image_file = request.files.get('file')
50
 
51
- # if not anggota or not image_file:
52
- # return jsonify({'status': 'failed', 'message': 'Parameter anggota dan file diperlukan'}), 400
53
-
54
- # try:
55
- # # Read and process the uploaded image
56
- # file_bytes = image_file.read()
57
- # image_array = np.frombuffer(file_bytes, np.uint8)
58
- # image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
59
-
60
- # if image is None:
61
- # raise ValueError("Gagal membaca gambar. File mungkin rusak atau format tidak didukung.")
62
-
63
- # extracted_data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
64
- # text_list = extracted_data['text']
65
 
66
- # group_position = None
67
- # split_position = None
68
- # member_position = None
69
- # member_count_position = None
70
- # lang = ''
71
-
72
- # # Find the second occurrence of "Grup" or "Group"
73
- # group_indices = []
74
- # for i, text in enumerate(text_list):
75
- # if text in ["Grup", "Group"]:
76
- # group_indices.append(i)
77
-
78
- # if len(group_indices) >= 2:
79
- # i = group_indices[1]
80
-
81
- # if (i + 3 < len(text_list) and (text_list[i+1] == "-") and text_list[i+3] in ["anggota", "member"]):
82
- # lang = 'id' if text_list[i] == "Grup" else 'en'
83
-
84
- # group_position = {
85
- # "left": extracted_data['left'][i],
86
- # "top": extracted_data['top'][i],
87
- # "width": extracted_data['width'][i],
88
- # "height": extracted_data['height'][i],
89
- # }
90
-
91
- # split_position = {
92
- # "left": extracted_data['left'][i + 1],
93
- # "top": extracted_data['top'][i + 1],
94
- # "width": extracted_data['width'][i + 1],
95
- # "height": extracted_data['height'][i + 1],
96
- # }
97
-
98
- # member_position = {
99
- # "left": extracted_data['left'][i + 3],
100
- # "top": extracted_data['top'][i + 3],
101
- # "width": extracted_data['width'][i + 3],
102
- # "height": extracted_data['height'][i + 3],
103
- # }
104
-
105
- # if i + 2 < len(text_list):
106
- # member_count_position = {
107
- # "left": extracted_data['left'][i + 2],
108
- # "top": extracted_data['top'][i + 2],
109
- # "width": extracted_data['width'][i + 2],
110
- # "height": extracted_data['height'][i + 2],
111
- # }
112
- # else:
113
- # return jsonify({'status': 'failed', 'message': 'Pola grup tidak ditemukan pada gambar'}), 400
114
-
115
- # if member_position is None:
116
- # return jsonify({'status': 'failed', 'message': 'Pola anggota tidak ditemukan pada gambar'}), 400
117
-
118
- # # Get background color for masking
119
- # x = member_position['left'] + member_position['width'] + 100
120
- # y = member_position['top'] + member_position['height'] - 100
121
- # bg_color = image[y, x]
122
- # rgb = (int(bg_color[0]), int(bg_color[1]), int(bg_color[2]))
123
-
124
- # # Mask the original text areas
125
- # if group_position:
126
- # cv2.rectangle(
127
- # image,
128
- # (group_position['left'], group_position['top']),
129
- # (group_position['left'] + group_position['width'], group_position['top'] + group_position['height']),
130
- # rgb,
131
- # -1,
132
- # )
133
-
134
- # if split_position:
135
- # cv2.rectangle(
136
- # image,
137
- # (split_position['left'], split_position['top']),
138
- # (split_position['left'] + split_position['width'], split_position['top'] + split_position['height']),
139
- # rgb,
140
- # -1,
141
- # )
142
-
143
- # if member_position:
144
- # cv2.rectangle(
145
- # image,
146
- # (member_position['left'], member_position['top']),
147
- # (member_position['left'] + member_position['width'], member_position['top'] + member_position['height']),
148
- # rgb,
149
- # -1,
150
- # )
151
-
152
- # if member_count_position:
153
- # cv2.rectangle(
154
- # image,
155
- # (member_count_position['left'], member_count_position['top']),
156
- # (member_count_position['left'] + member_count_position['width'], member_count_position['top'] + member_count_position['height']),
157
- # rgb,
158
- # -1,
159
- # )
160
-
161
- # # Add new member count text
162
- # updated_member_count = {
163
- # 'id': f"Grup · {anggota} anggota",
164
- # 'en': f"Group · {anggota} member"
165
- # }.get(lang)
166
-
167
- # font_path = "SFNSText-Regular.otf"
168
- # font_size = max(member_count_position['width'], member_count_position['height']) + 5.5
169
- # font = ImageFont.truetype(font_path, int(font_size))
170
-
171
- # image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
172
- # draw = ImageDraw.Draw(image_pil)
173
- # text_x = group_position['left']
174
- # text_y = member_count_position['top'] - 4
175
- # draw.text((text_x, text_y), updated_member_count, font=font, fill=(90, 94, 95, 255))
176
- # image = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
177
-
178
- # Encode and return the processed image
179
- # _, img_encoded = cv2.imencode('.jpg', image)
180
- # return jsonify(io.BytesIO(img_encoded.tobytes()), mimetype='image/jpeg')
181
-
182
- # except Exception as e:
183
- # error_traceback = traceback.format_exc()
184
- # print(f"Error occurred: {error_traceback}")
185
- # return jsonify({
186
- # 'status': 'failed',
187
- # 'message': str(e),
188
- # 'traceback': error_traceback
189
- # }), 500
190
 
191
  if __name__ == '__main__':
192
  app.run(port=7860, debug=True, host='0.0.0.0')
 
1
  from flask import Flask, request, jsonify, send_file
 
2
  import traceback
3
  from android import AndroidEditor
4
+ from iphone import IPhoneEditor
5
+ import numpy as np
6
+ import cv2
7
+ import pytesseract
8
+ import os
9
 
10
  app = Flask(__name__)
11
 
12
+ ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
13
+
14
+ def allowed_file(filename):
15
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
16
+
17
  @app.route('/')
18
  def index():
19
+ """
20
+ Endpoint utama yang menampilkan pesan selamat datang
21
+ """
22
  return "Kelas Kink"
23
 
24
  @app.route('/android', methods=['POST'])
25
  def process_android_image():
26
+ """
27
+ Endpoint untuk memproses gambar dari Android
28
+
29
+ Parameters:
30
+ - anggota: Nama anggota (form data)
31
+ - file: File gambar (form data)
32
+
33
+ Returns:
34
+ - JSON response dengan status, theme, dan gambar yang diproses
35
+ """
36
  app.logger.info('Menerima permintaan POST untuk memproses gambar')
37
  anggota = request.form.get('anggota')
38
  image_file = request.files.get('file')
39
 
40
  if not anggota or not image_file:
41
  return jsonify({'status': 'failed', 'message': 'Parameter anggota dan file diperlukan'}), 400
42
+
43
  try:
44
  file_bytes = image_file.read()
45
  editor = AndroidEditor()
 
55
 
56
  @app.route('/iphone', methods=['POST'])
57
  def process_iphone_image():
58
+ """
59
+ Endpoint untuk memproses gambar dari iPhone
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ Parameters:
62
+ - anggota: Nama anggota (form data)
63
+ - file: File gambar (form data)
64
+
65
+ Returns:
66
+ - JSON response dengan status, theme, dan gambar yang diproses
67
+ """
68
+ app.logger.info('Menerima permintaan POST untuk memproses gambar')
69
+ anggota = request.form.get('anggota')
70
+ image_file = request.files.get('file')
71
+
72
+ if not anggota or not image_file:
73
+ return jsonify({'status': 'failed', 'message': 'Parameter anggota dan file diperlukan'}), 400
 
74
 
75
+ try:
76
+ file_bytes = image_file.read()
77
+ editor = IPhoneEditor()
78
+ img_b64, theme = editor.process_image_bytes(file_bytes, anggota)
79
+ if img_b64 is None:
80
+ return jsonify({'status': 'failed', 'message': 'Gagal memproses gambar'}), 500
81
+ return jsonify({'status': 'success', 'theme': theme, 'image': img_b64})
82
+ except Exception as e:
83
+ error_message = str(e)
84
+ error_traceback = traceback.format_exc()
85
+ print("Error occurred:", error_traceback)
86
+ return jsonify({'status': 'failed', 'message': error_message, 'traceback': error_traceback}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  if __name__ == '__main__':
89
  app.run(port=7860, debug=True, host='0.0.0.0')