Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,43 @@
|
|
| 1 |
import tensorflow as tf
|
| 2 |
import numpy as np
|
| 3 |
from tensorflow.keras.utils import CustomObjectScope
|
| 4 |
-
from transformers import TFAutoModelForSequenceClassification
|
| 5 |
from PIL import Image
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
def absolute_difference(x):
|
| 9 |
return tf.abs(x[0] - x[1])
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def preprocess_image(image):
|
| 17 |
image = image.convert("RGB") # แปลงเป็น RGB
|
|
@@ -27,15 +53,18 @@ def predict_signature(image1, image2):
|
|
| 27 |
if image1 is None or image2 is None:
|
| 28 |
print("ไม่พบรูปภาพ")
|
| 29 |
return "❌ กรุณาอัปโหลดภาพทั้งสองภาพ"
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
try:
|
| 32 |
print("กำลังประมวลผลรูปภาพที่ 1...")
|
| 33 |
-
|
| 34 |
print("กำลังประมวลผลรูปภาพที่ 2...")
|
| 35 |
-
|
| 36 |
|
| 37 |
print("กำลังส่งข้อมูลเข้าโมเดล...")
|
| 38 |
-
prediction = model.predict([
|
| 39 |
|
| 40 |
print(f"ได้รับผลลัพธ์: {prediction}, รูปร่าง: {prediction.shape}")
|
| 41 |
|
|
@@ -53,7 +82,7 @@ def predict_signature(image1, image2):
|
|
| 53 |
result = "✅ ลายเซ็นเป็นของจริง" if prediction_value >= threshold else "❌ ลายเซ็นเป็นของปลอม"
|
| 54 |
|
| 55 |
print(f"ผลลัพธ์การตรวจสอบ: {result}")
|
| 56 |
-
return f"
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
print(f"เกิดข้อผิดพลาด: {str(e)}")
|
|
@@ -61,16 +90,18 @@ def predict_signature(image1, image2):
|
|
| 61 |
traceback.print_exc() # แสดงรายละเอียดของข้อผิดพลาดทั้งหมด
|
| 62 |
return f"❌ เกิดข้อผิดพลาด: {str(e)}"
|
| 63 |
|
|
|
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
-
gr.Markdown("# 🖊
|
| 66 |
-
|
| 67 |
with gr.Row():
|
| 68 |
img1 = gr.Image(type="pil", label="ลายเซ็นที่ 1", show_label=True)
|
| 69 |
img2 = gr.Image(type="pil", label="ลายเซ็นที่ 2", show_label=True)
|
| 70 |
-
|
| 71 |
btn_predict = gr.Button("ตรวจสอบความถูกต้อง")
|
| 72 |
output = gr.Textbox(label="ผลลัพธ์")
|
| 73 |
-
|
| 74 |
btn_predict.click(predict_signature, inputs=[img1, img2], outputs=output)
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
import numpy as np
|
| 3 |
from tensorflow.keras.utils import CustomObjectScope
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import gradio as gr
|
| 6 |
+
import os
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
|
| 9 |
+
# ฟังก์ชันสำหรับคำนวณค่าความแตกต่างแบบสัมบูรณ์
|
| 10 |
def absolute_difference(x):
|
| 11 |
return tf.abs(x[0] - x[1])
|
| 12 |
|
| 13 |
+
# ดาวน์โหลดโมเดลจาก Hugging Face Hub
|
| 14 |
+
def load_model_from_hub():
|
| 15 |
+
try:
|
| 16 |
+
print("กำลังดาวน์โหลดโมเดลจาก Hugging Face Hub...")
|
| 17 |
+
model_path = hf_hub_download(
|
| 18 |
+
repo_id="Nithikorn/Signature_Detection",
|
| 19 |
+
filename="final_signature_model1.keras"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
print(f"✅ ดาวน์โหลดโมเดลสำเร็จที่: {model_path}")
|
| 23 |
+
|
| 24 |
+
# โหลดโมเดลด้วย CustomObjectScope
|
| 25 |
+
with CustomObjectScope({'absolute_difference': absolute_difference}):
|
| 26 |
+
model = tf.keras.models.load_model(model_path)
|
| 27 |
+
|
| 28 |
+
print("✅ โหลดโมเดลสำเร็จ!")
|
| 29 |
+
return model
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"❌ เกิดข้อผิดพลาดในการโหลดโมเดล: {str(e)}")
|
| 32 |
+
raise e
|
| 33 |
+
|
| 34 |
+
# โหลดโมเดล
|
| 35 |
+
try:
|
| 36 |
+
model = load_model_from_hub()
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"❌ ไม่สามารถโหลดโมเดลได้: {str(e)}")
|
| 39 |
+
# สร้างโมเดลว่างไว้ก่อนเพื่อให้โค้ดรันต่อไปได้ แต่จะแสดงข้อความแจ้งเตือนเมื่อมีการใช้งาน
|
| 40 |
+
model = None
|
| 41 |
|
| 42 |
def preprocess_image(image):
|
| 43 |
image = image.convert("RGB") # แปลงเป็น RGB
|
|
|
|
| 53 |
if image1 is None or image2 is None:
|
| 54 |
print("ไม่พบรูปภาพ")
|
| 55 |
return "❌ กรุณาอัปโหลดภาพทั้งสองภาพ"
|
| 56 |
+
|
| 57 |
+
if model is None:
|
| 58 |
+
return "❌ ไม่สามารถโหลดโมเดลได้ กรุณาตรวจสอบว่าได้อัปโหลดโมเดลไปยัง Hugging Face Hub แล้ว"
|
| 59 |
|
| 60 |
try:
|
| 61 |
print("กำลังประมวลผลรูปภาพที่ 1...")
|
| 62 |
+
img1_processed = preprocess_image(image1)
|
| 63 |
print("กำลังประมวลผลรูปภาพที่ 2...")
|
| 64 |
+
img2_processed = preprocess_image(image2)
|
| 65 |
|
| 66 |
print("กำลังส่งข้อมูลเข้าโมเดล...")
|
| 67 |
+
prediction = model.predict([img1_processed, img2_processed])
|
| 68 |
|
| 69 |
print(f"ได้รับผลลัพธ์: {prediction}, รูปร่าง: {prediction.shape}")
|
| 70 |
|
|
|
|
| 82 |
result = "✅ ลายเซ็นเป็นของจริง" if prediction_value >= threshold else "❌ ลายเซ็นเป็นของปลอม"
|
| 83 |
|
| 84 |
print(f"ผลลัพธ์การตรวจสอบ: {result}")
|
| 85 |
+
return f"คะแนนความเหมือน: {prediction_value:.4f}\n{result}"
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
print(f"เกิดข้อผิดพลาด: {str(e)}")
|
|
|
|
| 90 |
traceback.print_exc() # แสดงรายละเอียดของข้อผิดพลาดทั้งหมด
|
| 91 |
return f"❌ เกิดข้อผิดพลาด: {str(e)}"
|
| 92 |
|
| 93 |
+
# สร้าง Gradio UI
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
+
gr.Markdown("# 🖊 ระบบตรวจสอบลายเซ็น")
|
| 96 |
+
|
| 97 |
with gr.Row():
|
| 98 |
img1 = gr.Image(type="pil", label="ลายเซ็นที่ 1", show_label=True)
|
| 99 |
img2 = gr.Image(type="pil", label="ลายเซ็นที่ 2", show_label=True)
|
| 100 |
+
|
| 101 |
btn_predict = gr.Button("ตรวจสอบความถูกต้อง")
|
| 102 |
output = gr.Textbox(label="ผลลัพธ์")
|
| 103 |
+
|
| 104 |
btn_predict.click(predict_signature, inputs=[img1, img2], outputs=output)
|
| 105 |
|
| 106 |
+
# รัน Gradio app
|
| 107 |
+
demo.launch()
|