Rzx008 commited on
Commit
ce5f5e3
verified
1 Parent(s): 9639207

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -298
app.py CHANGED
@@ -1,10 +1,8 @@
1
  from flask import Flask, request, jsonify, send_file
2
- import cv2
3
- import pytesseract
4
- from PIL import Image, ImageDraw, ImageFont
5
- import numpy as np
6
  import io
7
  import traceback
 
 
8
 
9
  app = Flask(__name__)
10
 
@@ -23,307 +21,21 @@ def process_android_image():
23
 
24
  try:
25
  file_bytes = image_file.read()
26
- image_array = np.frombuffer(file_bytes, np.uint8)
27
- image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
28
- extracted_data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
29
- text_list = extracted_data['text']
30
-
31
- group_position = None
32
- split_position = None
33
- member_position = None
34
- member_count_position = None
35
- second_member_position = None
36
- second_member_count_position = None
37
- lang = ''
38
-
39
- for i, text in enumerate(text_list):
40
- if "Grup" in text or "Group" in text:
41
- lang = 'id' if "Grup" in text else 'en'
42
- group_position = {
43
- "left": extracted_data['left'][i],
44
- "top": extracted_data['top'][i],
45
- "width": extracted_data['width'][i],
46
- "height": extracted_data['height'][i],
47
- }
48
-
49
- split_position = {
50
- "left": extracted_data['left'][i + 1],
51
- "top": extracted_data['top'][i + 1],
52
- "width": extracted_data['width'][i + 1],
53
- "height": extracted_data['height'][i + 1],
54
- }
55
-
56
- member_position = {
57
- "left": extracted_data['left'][i + 3],
58
- "top": extracted_data['top'][i + 3],
59
- "width": extracted_data['width'][i + 3],
60
- "height": extracted_data['height'][i + 3],
61
- }
62
-
63
- if i + 2 < len(text_list):
64
- member_count_position = {
65
- "left": extracted_data['left'][i + 2],
66
- "top": extracted_data['top'][i + 2],
67
- "width": extracted_data['width'][i + 2],
68
- "height": extracted_data['height'][i + 2],
69
- }
70
-
71
- for j in range(i + 4, len(text_list)):
72
- if "Anggota" in text_list[j]:
73
- second_member_position = {
74
- "left": extracted_data['left'][j],
75
- "top": extracted_data['top'][j],
76
- "width": extracted_data['width'][j],
77
- "height": extracted_data['height'][j],
78
- }
79
- if j + 2 < len(text_list):
80
- second_member_count_position = {
81
- "left": extracted_data['left'][j - 1],
82
- "top": extracted_data['top'][j - 1],
83
- "width": extracted_data['width'][j - 1],
84
- "height": extracted_data['height'][j - 1],
85
- }
86
- break
87
- break
88
-
89
- x = member_position['left'] + member_position['width'] + 100
90
- y = member_position['top'] + member_position['height'] - 100
91
- bg_color = image[y, x]
92
- rgb = (int(bg_color[0]), int(bg_color[1]), int(bg_color[2]))
93
-
94
-
95
- if group_position:
96
- cv2.rectangle(
97
- image,
98
- (group_position['left'] - 5, group_position['top'] - 5),
99
- (group_position['left'] + group_position['width'], group_position['top'] + group_position['height']),
100
- rgb,
101
- -1,
102
- )
103
-
104
- if split_position:
105
- cv2.rectangle(
106
- image,
107
- (split_position['left'] - 5, split_position['top'] - 5),
108
- (split_position['left'] + split_position['width'], split_position['top'] + split_position['height']),
109
- rgb,
110
- -1,
111
- )
112
-
113
- if member_position:
114
- cv2.rectangle(
115
- image,
116
- (member_position['left'] - 5, member_position['top'] - 5),
117
- (member_position['left'] + member_position['width'], member_position['top'] + member_position['height']),
118
- rgb,
119
- -1,
120
- )
121
-
122
- if member_count_position:
123
- cv2.rectangle(
124
- image,
125
- (member_count_position['left'] - 5, member_count_position['top'] - 5),
126
- (member_count_position['left'] + member_count_position['width'], member_count_position['top'] + member_count_position['height']),
127
- rgb,
128
- -1,
129
- )
130
-
131
- updated_member_count = {
132
- 'id': f"Grup 路 {anggota} anggota",
133
- 'en': f"Group 路 {anggota} members"
134
- }.get(lang)
135
-
136
-
137
- font_path = "Roboto-Regular.ttf"
138
- font_size = max(member_count_position['width'], member_count_position['height']) - 2
139
- font = ImageFont.truetype(font_path, font_size)
140
-
141
- image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
142
- draw = ImageDraw.Draw(image_pil)
143
- text_x = group_position['left']
144
- text_y = member_count_position['top'] - 4
145
- draw.text((text_x, text_y), updated_member_count, font=font, fill=(90, 94, 95, 255))
146
- image = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
147
-
148
- if second_member_position:
149
- cv2.rectangle(
150
- image,
151
- (second_member_position['left'] - 5, second_member_position['top'] - 5),
152
- (second_member_position['left'] + second_member_position['width'], second_member_position['top'] + second_member_position['height']),
153
- rgb,
154
- -1,
155
- )
156
-
157
- if second_member_count_position:
158
- cv2.rectangle(
159
- image,
160
- (second_member_count_position['left'] - 5, second_member_count_position['top'] - 5),
161
- (second_member_count_position['left'] + second_member_count_position['width'], second_member_count_position['top'] + second_member_count_position['height']),
162
- rgb,
163
- -1,
164
- )
165
- updated_second_member_count = {
166
- 'id': f"{anggota} Anggota",
167
- 'en': f"{anggota} Members"
168
- }.get(lang)
169
-
170
- font_path = "Roboto-Regular.ttf"
171
- font_size = max(second_member_count_position['width'], second_member_count_position['height']) - 1
172
- font = ImageFont.truetype(font_path, font_size)
173
-
174
- image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
175
- draw = ImageDraw.Draw(image_pil)
176
- text_x = second_member_count_position['left']
177
- text_y = second_member_count_position['top'] - 4
178
- draw.text((text_x, text_y), updated_second_member_count, font=font, fill=(90, 94, 95, 255))
179
- image = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
180
-
181
-
182
- _, img_encoded = cv2.imencode('.jpg', image)
183
- return send_file(io.BytesIO(img_encoded.tobytes()), mimetype='image/jpeg')
184
-
185
  except Exception as e:
186
  error_message = str(e)
187
-
188
  error_traceback = traceback.format_exc()
189
  print("Error occurred:", error_traceback)
190
  return jsonify({'status': 'failed', 'message': error_message, 'traceback': error_traceback}), 500
191
 
192
-
193
  @app.route('/iphone', methods=['POST'])
194
  def process_iphone_image():
195
- app.logger.info('Menerima permintaan POST untuk memproses gambar iPhone')
196
- anggota = request.form.get('anggota')
197
- image_file = request.files.get('file')
198
-
199
- if not anggota or not image_file:
200
- return jsonify({'status': 'failed', 'message': 'Parameter anggota dan file diperlukan'}), 400
201
-
202
- try:
203
- # Read and process the uploaded image
204
- file_bytes = image_file.read()
205
- image_array = np.frombuffer(file_bytes, np.uint8)
206
- image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
207
-
208
- if image is None:
209
- raise ValueError("Gagal membaca gambar. File mungkin rusak atau format tidak didukung.")
210
-
211
- extracted_data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
212
- text_list = extracted_data['text']
213
-
214
- group_position = None
215
- split_position = None
216
- member_position = None
217
- member_count_position = None
218
- lang = ''
219
-
220
- # Find the second occurrence of "Grup" or "Group"
221
- group_indices = []
222
- for i, text in enumerate(text_list):
223
- if text in ["Grup", "Group"]:
224
- group_indices.append(i)
225
-
226
- if len(group_indices) >= 2:
227
- i = group_indices[1]
228
-
229
- if (i + 3 < len(text_list) and (text_list[i+1] == "-") and text_list[i+3] in ["anggota", "member"]):
230
- lang = 'id' if text_list[i] == "Grup" else 'en'
231
-
232
- group_position = {
233
- "left": extracted_data['left'][i],
234
- "top": extracted_data['top'][i],
235
- "width": extracted_data['width'][i],
236
- "height": extracted_data['height'][i],
237
- }
238
-
239
- split_position = {
240
- "left": extracted_data['left'][i + 1],
241
- "top": extracted_data['top'][i + 1],
242
- "width": extracted_data['width'][i + 1],
243
- "height": extracted_data['height'][i + 1],
244
- }
245
-
246
- member_position = {
247
- "left": extracted_data['left'][i + 3],
248
- "top": extracted_data['top'][i + 3],
249
- "width": extracted_data['width'][i + 3],
250
- "height": extracted_data['height'][i + 3],
251
- }
252
-
253
- if i + 2 < len(text_list):
254
- member_count_position = {
255
- "left": extracted_data['left'][i + 2],
256
- "top": extracted_data['top'][i + 2],
257
- "width": extracted_data['width'][i + 2],
258
- "height": extracted_data['height'][i + 2],
259
- }
260
- else:
261
- return jsonify({'status': 'failed', 'message': 'Pola grup tidak ditemukan pada gambar'}), 400
262
-
263
- # Get background color for masking
264
- x = member_position['left'] + member_position['width'] + 100
265
- y = member_position['top'] + member_position['height'] - 100
266
- bg_color = image[y, x]
267
- rgb = (int(bg_color[0]), int(bg_color[1]), int(bg_color[2]))
268
-
269
- # Mask the original text areas
270
- if group_position:
271
- cv2.rectangle(
272
- image,
273
- (group_position['left'], group_position['top']),
274
- (group_position['left'] + group_position['width'], group_position['top'] + group_position['height']),
275
- rgb,
276
- -1,
277
- )
278
-
279
- if split_position:
280
- cv2.rectangle(
281
- image,
282
- (split_position['left'], split_position['top']),
283
- (split_position['left'] + split_position['width'], split_position['top'] + split_position['height']),
284
- rgb,
285
- -1,
286
- )
287
-
288
- if member_position:
289
- cv2.rectangle(
290
- image,
291
- (member_position['left'], member_position['top']),
292
- (member_position['left'] + member_position['width'], member_position['top'] + member_position['height']),
293
- rgb,
294
- -1,
295
- )
296
-
297
- if member_count_position:
298
- cv2.rectangle(
299
- image,
300
- (member_count_position['left'], member_count_position['top']),
301
- (member_count_position['left'] + member_count_position['width'], member_count_position['top'] + member_count_position['height']),
302
- rgb,
303
- -1,
304
- )
305
-
306
- # Add new member count text
307
- updated_member_count = {
308
- 'id': f"Grup 路 {anggota} anggota",
309
- 'en': f"Group 路 {anggota} member"
310
- }.get(lang)
311
-
312
- font_path = "SFNSText-Regular.otf"
313
- font_size = max(member_count_position['width'], member_count_position['height']) + 5.5
314
- font = ImageFont.truetype(font_path, int(font_size))
315
-
316
- image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
317
- draw = ImageDraw.Draw(image_pil)
318
- text_x = group_position['left']
319
- text_y = member_count_position['top'] - 4
320
- draw.text((text_x, text_y), updated_member_count, font=font, fill=(90, 94, 95, 255))
321
- image = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
322
-
323
- # Encode and return the processed image
324
- _, img_encoded = cv2.imencode('.jpg', image)
325
- return send_file(io.BytesIO(img_encoded.tobytes()), mimetype='image/jpeg')
326
-
327
  except Exception as e:
328
  error_traceback = traceback.format_exc()
329
  print(f"Error occurred: {error_traceback}")
@@ -332,6 +44,149 @@ def process_iphone_image():
332
  'message': str(e),
333
  'traceback': error_traceback
334
  }), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
 
336
  if __name__ == '__main__':
337
- app.run(port=7860, host='0.0.0.0')
 
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
 
 
21
 
22
  try:
23
  file_bytes = image_file.read()
24
+ editor = AndroidEditor()
25
+ img_b64, theme = editor.process_image_bytes(file_bytes, anggota)
26
+ if img_b64 is None:
27
+ return jsonify({'status': 'failed', 'message': 'Gagal memproses gambar'}), 500
28
+ return jsonify({'status': 'success', 'theme': theme, 'image': img_b64})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  except Exception as e:
30
  error_message = str(e)
 
31
  error_traceback = traceback.format_exc()
32
  print("Error occurred:", error_traceback)
33
  return jsonify({'status': 'failed', 'message': error_message, 'traceback': error_traceback}), 500
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}")
 
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')