Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,50 +5,200 @@ from PIL import Image, ImageDraw, ImageFont
|
|
| 5 |
import numpy as np
|
| 6 |
import io
|
| 7 |
import traceback
|
| 8 |
-
import speedtest
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
@app.route('/')
|
| 13 |
def index():
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# Mengambil server terbaik untuk speed test
|
| 18 |
-
st.get_best_server()
|
| 19 |
-
|
| 20 |
-
# Melakukan tes kecepatan unduh dan unggah
|
| 21 |
-
download_speed = st.download() / 1_000_000 # Mengonversi dari bps ke Mbps
|
| 22 |
-
upload_speed = st.upload() / 1_000_000 # Mengonversi dari bps ke Mbps
|
| 23 |
-
|
| 24 |
-
# Mengambil ping
|
| 25 |
-
ping = st.results.ping
|
| 26 |
-
|
| 27 |
-
# Menampilkan informasi
|
| 28 |
-
return jsonify({
|
| 29 |
-
'status': 'success',
|
| 30 |
-
'download_speed_mbps': round(download_speed, 2),
|
| 31 |
-
'upload_speed_mbps': round(upload_speed, 2),
|
| 32 |
-
'ping_ms': ping
|
| 33 |
-
})
|
| 34 |
-
|
| 35 |
-
@app.route('/process_image', methods=['POST'])
|
| 36 |
def process_image():
|
| 37 |
app.logger.info('Menerima permintaan POST untuk memproses gambar')
|
| 38 |
-
anggota = request.form.get('anggota')
|
| 39 |
-
image_file = request.files.get('file')
|
| 40 |
|
| 41 |
if not anggota or not image_file:
|
| 42 |
return jsonify({'status': 'failed', 'message': 'Parameter anggota dan file diperlukan'}), 400
|
| 43 |
|
| 44 |
try:
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
except Exception as e:
|
| 51 |
-
raise ValueError("Gagal membaca atau meng-decode gambar
|
| 52 |
|
| 53 |
try:
|
| 54 |
extracted_data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
|
|
@@ -56,69 +206,25 @@ def process_image():
|
|
| 56 |
raise ValueError("Gagal melakukan ekstraksi teks dengan pytesseract.") from e
|
| 57 |
|
| 58 |
text_list = extracted_data['text']
|
| 59 |
-
|
| 60 |
group_position = None
|
| 61 |
split_position = None
|
| 62 |
member_position = None
|
| 63 |
member_count_position = None
|
| 64 |
-
second_member_position = None
|
| 65 |
-
second_member_count_position = None
|
| 66 |
lang = ''
|
| 67 |
|
| 68 |
-
# Proses pencarian posisi teks pada gambar
|
| 69 |
try:
|
| 70 |
-
for i, text in enumerate(text_list):
|
| 71 |
-
if "Grup" in text:
|
| 72 |
-
lang = 'id'
|
| 73 |
-
group_position = {
|
| 74 |
-
"left": extracted_data['left'][i],
|
| 75 |
-
"top": extracted_data['top'][i],
|
| 76 |
-
"width": extracted_data['width'][i],
|
| 77 |
-
"height": extracted_data['height'][i],
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
split_position = {
|
| 81 |
-
"left": extracted_data['left'][i + 1],
|
| 82 |
-
"top": extracted_data['top'][i + 1],
|
| 83 |
-
"width": extracted_data['width'][i + 1],
|
| 84 |
-
"height": extracted_data['height'][i + 1],
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
member_position = {
|
| 88 |
-
"left": extracted_data['left'][i + 3],
|
| 89 |
-
"top": extracted_data['top'][i + 3],
|
| 90 |
-
"width": extracted_data['width'][i + 3],
|
| 91 |
-
"height": extracted_data['height'][i + 3],
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
-
if i + 2 < len(text_list):
|
| 95 |
-
member_count_position = {
|
| 96 |
-
"left": extracted_data['left'][i + 2],
|
| 97 |
-
"top": extracted_data['top'][i + 2],
|
| 98 |
-
"width": extracted_data['width'][i + 2],
|
| 99 |
-
"height": extracted_data['height'][i + 2],
|
| 100 |
-
}
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
"top": extracted_data['top'][j - 1],
|
| 114 |
-
"width": extracted_data['width'][j - 1],
|
| 115 |
-
"height": extracted_data['height'][j - 1],
|
| 116 |
-
}
|
| 117 |
-
break
|
| 118 |
-
break
|
| 119 |
-
|
| 120 |
-
elif "Group" in text:
|
| 121 |
-
lang = 'en'
|
| 122 |
group_position = {
|
| 123 |
"left": extracted_data['left'][i],
|
| 124 |
"top": extracted_data['top'][i],
|
|
@@ -147,26 +253,11 @@ def process_image():
|
|
| 147 |
"width": extracted_data['width'][i + 2],
|
| 148 |
"height": extracted_data['height'][i + 2],
|
| 149 |
}
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
for j in range(i + 4, len(text_list)):
|
| 152 |
-
if "Anggota" in text_list[j]:
|
| 153 |
-
second_member_position = {
|
| 154 |
-
"left": extracted_data['left'][j],
|
| 155 |
-
"top": extracted_data['top'][j],
|
| 156 |
-
"width": extracted_data['width'][j],
|
| 157 |
-
"height": extracted_data['height'][j],
|
| 158 |
-
}
|
| 159 |
-
if j + 2 < len(text_list):
|
| 160 |
-
second_member_count_position = {
|
| 161 |
-
"left": extracted_data['left'][j - 1],
|
| 162 |
-
"top": extracted_data['top'][j - 1],
|
| 163 |
-
"width": extracted_data['width'][j - 1],
|
| 164 |
-
"height": extracted_data['height'][j - 1],
|
| 165 |
-
}
|
| 166 |
-
break
|
| 167 |
-
break
|
| 168 |
except Exception as e:
|
| 169 |
-
|
| 170 |
|
| 171 |
try:
|
| 172 |
x = member_position['left'] + member_position['width'] + 100
|
|
@@ -174,11 +265,10 @@ def process_image():
|
|
| 174 |
bg_color = image[y, x]
|
| 175 |
rgb = (int(bg_color[0]), int(bg_color[1]), int(bg_color[2]))
|
| 176 |
|
| 177 |
-
# Draw rectangles and add text
|
| 178 |
if group_position:
|
| 179 |
cv2.rectangle(
|
| 180 |
image,
|
| 181 |
-
(group_position['left']
|
| 182 |
(group_position['left'] + group_position['width'], group_position['top'] + group_position['height']),
|
| 183 |
rgb,
|
| 184 |
-1,
|
|
@@ -187,7 +277,7 @@ def process_image():
|
|
| 187 |
if split_position:
|
| 188 |
cv2.rectangle(
|
| 189 |
image,
|
| 190 |
-
(split_position['left']
|
| 191 |
(split_position['left'] + split_position['width'], split_position['top'] + split_position['height']),
|
| 192 |
rgb,
|
| 193 |
-1,
|
|
@@ -196,7 +286,7 @@ def process_image():
|
|
| 196 |
if member_position:
|
| 197 |
cv2.rectangle(
|
| 198 |
image,
|
| 199 |
-
(member_position['left']
|
| 200 |
(member_position['left'] + member_position['width'], member_position['top'] + member_position['height']),
|
| 201 |
rgb,
|
| 202 |
-1,
|
|
@@ -205,7 +295,7 @@ def process_image():
|
|
| 205 |
if member_count_position:
|
| 206 |
cv2.rectangle(
|
| 207 |
image,
|
| 208 |
-
(member_count_position['left']
|
| 209 |
(member_count_position['left'] + member_count_position['width'], member_count_position['top'] + member_count_position['height']),
|
| 210 |
rgb,
|
| 211 |
-1,
|
|
@@ -213,12 +303,12 @@ def process_image():
|
|
| 213 |
|
| 214 |
updated_member_count = {
|
| 215 |
'id': f"Grup 路 {anggota} anggota",
|
| 216 |
-
'en': f"Group 路 {anggota}
|
| 217 |
}.get(lang)
|
| 218 |
|
| 219 |
|
| 220 |
-
font_path = "
|
| 221 |
-
font_size = max(member_count_position['width'], member_count_position['height'])
|
| 222 |
font = ImageFont.truetype(font_path, font_size)
|
| 223 |
|
| 224 |
image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
|
@@ -228,40 +318,13 @@ def process_image():
|
|
| 228 |
draw.text((text_x, text_y), updated_member_count, font=font, fill=(90, 94, 95, 255))
|
| 229 |
image = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
|
| 230 |
|
| 231 |
-
if second_member_position:
|
| 232 |
-
cv2.rectangle(
|
| 233 |
-
image,
|
| 234 |
-
(second_member_position['left'] - 5, second_member_position['top'] - 5),
|
| 235 |
-
(second_member_position['left'] + second_member_position['width'], second_member_position['top'] + second_member_position['height']),
|
| 236 |
-
rgb,
|
| 237 |
-
-1,
|
| 238 |
-
)
|
| 239 |
-
|
| 240 |
-
if second_member_count_position:
|
| 241 |
-
cv2.rectangle(
|
| 242 |
-
image,
|
| 243 |
-
(second_member_count_position['left'] - 5, second_member_count_position['top'] - 5),
|
| 244 |
-
(second_member_count_position['left'] + second_member_count_position['width'], second_member_count_position['top'] + second_member_count_position['height']),
|
| 245 |
-
rgb,
|
| 246 |
-
-1,
|
| 247 |
-
)
|
| 248 |
except Exception as e:
|
| 249 |
-
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 254 |
-
img_io = io.BytesIO()
|
| 255 |
-
image_pil.save(img_io, 'JPEG' if is_jpg else 'PNG', quality=90)
|
| 256 |
-
img_io.seek(0)
|
| 257 |
-
|
| 258 |
-
return send_file(img_io, mimetype='image/jpeg' if is_jpg else 'image/png')
|
| 259 |
-
|
| 260 |
-
except Exception as e:
|
| 261 |
-
return jsonify({'status': 'failed', 'message': 'Gagal mengonversi gambar untuk diunduh.', 'error': str(e)}), 500
|
| 262 |
except Exception as e:
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
if __name__ == '__main__':
|
| 267 |
-
app.run(port=
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import io
|
| 7 |
import traceback
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
@app.route('/')
|
| 12 |
def index():
|
| 13 |
+
return "Kelas Kink"
|
| 14 |
+
|
| 15 |
+
@app.route('/android', methods=['POST'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def process_image():
|
| 17 |
app.logger.info('Menerima permintaan POST untuk memproses gambar')
|
| 18 |
+
anggota = request.form.get('anggota')
|
| 19 |
+
image_file = request.files.get('file')
|
| 20 |
|
| 21 |
if not anggota or not image_file:
|
| 22 |
return jsonify({'status': 'failed', 'message': 'Parameter anggota dan file diperlukan'}), 400
|
| 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_image(image_path, anggota):
|
| 195 |
+
try:
|
| 196 |
try:
|
| 197 |
+
image = cv2.imread(image_path)
|
| 198 |
+
if image is None:
|
| 199 |
+
raise ValueError("Gagal membaca gambar. Pastikan path file benar.")
|
| 200 |
except Exception as e:
|
| 201 |
+
raise ValueError(f"Gagal membaca atau meng-decode gambar: {str(e)}") from e
|
| 202 |
|
| 203 |
try:
|
| 204 |
extracted_data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
|
|
|
|
| 206 |
raise ValueError("Gagal melakukan ekstraksi teks dengan pytesseract.") from e
|
| 207 |
|
| 208 |
text_list = extracted_data['text']
|
|
|
|
| 209 |
group_position = None
|
| 210 |
split_position = None
|
| 211 |
member_position = None
|
| 212 |
member_count_position = None
|
|
|
|
|
|
|
| 213 |
lang = ''
|
| 214 |
|
|
|
|
| 215 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
+
group_indices = []
|
| 218 |
+
for i, text in enumerate(text_list):
|
| 219 |
+
if text in ["Grup", "Group"]:
|
| 220 |
+
group_indices.append(i)
|
| 221 |
+
|
| 222 |
+
if len(group_indices) >= 2:
|
| 223 |
+
i = group_indices[1]
|
| 224 |
+
|
| 225 |
+
if (i + 3 < len(text_list) and (text_list[i+1] == "-") and text_list[i+3] in ["anggota", "member"]):
|
| 226 |
+
lang = 'id' if text_list[i] == "Grup" else 'en'
|
| 227 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
group_position = {
|
| 229 |
"left": extracted_data['left'][i],
|
| 230 |
"top": extracted_data['top'][i],
|
|
|
|
| 253 |
"width": extracted_data['width'][i + 2],
|
| 254 |
"height": extracted_data['height'][i + 2],
|
| 255 |
}
|
| 256 |
+
else:
|
| 257 |
+
print("Pola grup tidak ditemukan atau tidak cukup")
|
| 258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
except Exception as e:
|
| 260 |
+
raise ValueError(f"Gagal dalam memproses posisi teks: {str(e)}")
|
| 261 |
|
| 262 |
try:
|
| 263 |
x = member_position['left'] + member_position['width'] + 100
|
|
|
|
| 265 |
bg_color = image[y, x]
|
| 266 |
rgb = (int(bg_color[0]), int(bg_color[1]), int(bg_color[2]))
|
| 267 |
|
|
|
|
| 268 |
if group_position:
|
| 269 |
cv2.rectangle(
|
| 270 |
image,
|
| 271 |
+
(group_position['left'] , group_position['top'] ),
|
| 272 |
(group_position['left'] + group_position['width'], group_position['top'] + group_position['height']),
|
| 273 |
rgb,
|
| 274 |
-1,
|
|
|
|
| 277 |
if split_position:
|
| 278 |
cv2.rectangle(
|
| 279 |
image,
|
| 280 |
+
(split_position['left'] , split_position['top'] ),
|
| 281 |
(split_position['left'] + split_position['width'], split_position['top'] + split_position['height']),
|
| 282 |
rgb,
|
| 283 |
-1,
|
|
|
|
| 286 |
if member_position:
|
| 287 |
cv2.rectangle(
|
| 288 |
image,
|
| 289 |
+
(member_position['left'] , member_position['top'] ),
|
| 290 |
(member_position['left'] + member_position['width'], member_position['top'] + member_position['height']),
|
| 291 |
rgb,
|
| 292 |
-1,
|
|
|
|
| 295 |
if member_count_position:
|
| 296 |
cv2.rectangle(
|
| 297 |
image,
|
| 298 |
+
(member_count_position['left'] , member_count_position['top'] ),
|
| 299 |
(member_count_position['left'] + member_count_position['width'], member_count_position['top'] + member_count_position['height']),
|
| 300 |
rgb,
|
| 301 |
-1,
|
|
|
|
| 303 |
|
| 304 |
updated_member_count = {
|
| 305 |
'id': f"Grup 路 {anggota} anggota",
|
| 306 |
+
'en': f"Group 路 {anggota} member"
|
| 307 |
}.get(lang)
|
| 308 |
|
| 309 |
|
| 310 |
+
font_path = "SFNSText-Regular.otf"
|
| 311 |
+
font_size = max(member_count_position['width'], member_count_position['height']) + 5.5
|
| 312 |
font = ImageFont.truetype(font_path, font_size)
|
| 313 |
|
| 314 |
image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
|
|
|
| 318 |
draw.text((text_x, text_y), updated_member_count, font=font, fill=(90, 94, 95, 255))
|
| 319 |
image = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
|
| 320 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
except Exception as e:
|
| 322 |
+
raise ValueError(f"Gagal dalam memproses gambar: {str(e)}")
|
| 323 |
|
| 324 |
+
_, img_encoded = cv2.imencode('.jpg', image)
|
| 325 |
+
return send_file(io.BytesIO(img_encoded.tobytes()), mimetype='image/jpeg')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
except Exception as e:
|
| 327 |
+
print(f"Error: {str(e)}")
|
|
|
|
| 328 |
|
| 329 |
if __name__ == '__main__':
|
| 330 |
+
app.run(port=5000, debug=True)
|