editctc005 / app.py
Rzx008's picture
Update app.py
68313fe verified
from flask import Flask, request, jsonify, send_file
import traceback
from android import AndroidEditor
from iphone import IPhoneEditor
import numpy as np
import cv2
import pytesseract
import os
app = Flask(__name__)
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
"""
Endpoint utama yang menampilkan pesan selamat datang
"""
return "Kelas Kink"
@app.route('/android', methods=['POST'])
def process_android_image():
"""
Endpoint untuk memproses gambar dari Android
Parameters:
- anggota: Nama anggota (form data)
- file: File gambar (form data)
Returns:
- JSON response dengan status, theme, dan gambar yang diproses
"""
app.logger.info('Menerima permintaan POST untuk memproses gambar')
anggota = request.form.get('anggota')
image_file = request.files.get('file')
if not anggota or not image_file:
return jsonify({'status': 'failed', 'message': 'Parameter anggota dan file diperlukan'}), 400
try:
file_bytes = image_file.read()
editor = AndroidEditor()
img_b64, theme = editor.process_image_bytes(file_bytes, anggota)
if img_b64 is None:
return jsonify({'status': 'failed', 'message': 'Gagal memproses gambar'}), 500
return jsonify({'status': 'success', 'theme': theme, 'image': img_b64})
except Exception as e:
error_message = str(e)
error_traceback = traceback.format_exc()
print("Error occurred:", error_traceback)
return jsonify({'status': 'failed', 'message': error_message, 'traceback': error_traceback}), 500
@app.route('/iphone', methods=['POST'])
def process_iphone_image():
"""
Endpoint untuk memproses gambar dari iPhone
Parameters:
- anggota: Nama anggota (form data)
- file: File gambar (form data)
Returns:
- JSON response dengan status, theme, dan gambar yang diproses
"""
app.logger.info('Menerima permintaan POST untuk memproses gambar')
anggota = request.form.get('anggota')
image_file = request.files.get('file')
if not anggota or not image_file:
return jsonify({'status': 'failed', 'message': 'Parameter anggota dan file diperlukan'}), 400
try:
file_bytes = image_file.read()
editor = IPhoneEditor()
img_b64, theme = editor.process_image_bytes(file_bytes, anggota)
if img_b64 is None:
return jsonify({'status': 'failed', 'message': 'Gagal memproses gambar'}), 500
return jsonify({'status': 'success', 'theme': theme, 'image': img_b64})
except Exception as e:
error_message = str(e)
error_traceback = traceback.format_exc()
print("Error occurred:", error_traceback)
return jsonify({'status': 'failed', 'message': error_message, 'traceback': error_traceback}), 500
if __name__ == '__main__':
print(1)
app.run(port=7860, debug=True, host='0.0.0.0')