Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -191,140 +191,147 @@ def process_android_image():
|
|
| 191 |
|
| 192 |
|
| 193 |
@app.route('/iphone', methods=['POST'])
|
| 194 |
-
def process_iphone_image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
try:
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
try:
|
| 204 |
-
extracted_data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
|
| 205 |
-
except Exception as e:
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
group_position = {
|
| 229 |
-
"left": extracted_data['left'][i],
|
| 230 |
-
"top": extracted_data['top'][i],
|
| 231 |
-
"width": extracted_data['width'][i],
|
| 232 |
-
"height": extracted_data['height'][i],
|
| 233 |
-
}
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
"
|
| 238 |
-
"
|
| 239 |
-
"
|
|
|
|
| 240 |
}
|
|
|
|
|
|
|
| 241 |
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
}
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 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,
|
| 284 |
-
)
|
| 285 |
-
|
| 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,
|
| 293 |
-
)
|
| 294 |
-
|
| 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,
|
| 302 |
-
)
|
| 303 |
-
|
| 304 |
-
updated_member_count = {
|
| 305 |
-
'id': f"Grup 路 {anggota} anggota",
|
| 306 |
-
'en': f"Group 路 {anggota} member"
|
| 307 |
-
}.get(lang)
|
| 308 |
-
|
| 309 |
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
-
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
|
| 329 |
if __name__ == '__main__':
|
| 330 |
-
app.run(port=
|
|
|
|
| 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}")
|
| 330 |
+
return jsonify({
|
| 331 |
+
'status': 'failed',
|
| 332 |
+
'message': str(e),
|
| 333 |
+
'traceback': error_traceback
|
| 334 |
+
}), 500
|
| 335 |
|
| 336 |
if __name__ == '__main__':
|
| 337 |
+
app.run(port=7860, debug=True, host='0.0.0.0')
|