import os import gradio as gr from Plan.AiLLM import llm_recognition from Plan.pytesseractOCR import ocr_recognition from Preprocess.preprocessImg import PreprocessImg # 取得所有語言清單 languages = os.popen('tesseract --list-langs').read().split('\n')[1:-1] # 預處理圖片 def preprocess_image(image): if image is None: gr.Warning("尚未上傳圖片!") raise ValueError("尚未上傳圖片!") preprocessed_images = PreprocessImg(image) return ( preprocessed_images, True, preprocessed_images[0], preprocessed_images[1], preprocessed_images[2], preprocessed_images[3], preprocessed_images[4] ) # pytesseract OCR def Basic_ocr(valid_type, language, preprocessed_images, finish_pre_img): if not finish_pre_img: gr.Warning("請先完成圖像預處理!") raise ValueError("請先完成圖像預處理!") # 方案一 ocr_result_001 = ocr_recognition(preprocessed_images[0], valid_type, language) # 方案二 ocr_result_002 = ocr_recognition(preprocessed_images[1], valid_type, language) # 方案三 ocr_result_003 = ocr_recognition(preprocessed_images[2], valid_type, language) # 方案四 ocr_result_004 = ocr_recognition(preprocessed_images[3], valid_type, language) # 方案五 ocr_result_005 = ocr_recognition(preprocessed_images[4], valid_type, language) return ocr_result_001, ocr_result_002, ocr_result_003, ocr_result_004, ocr_result_005 # AI LLM OCR def AiLLM_ocr(valid_type, language, preprocessed_images, finish_pre_img): if not finish_pre_img: gr.Warning("請先完成圖像預處理!") raise ValueError("請先完成圖像預處理!") # 方案一 llm_result_001 = llm_recognition(preprocessed_images[0], valid_type, language) # 方案二 llm_result_002 = llm_recognition(preprocessed_images[1], valid_type, language) # 方案三 llm_result_003 = llm_recognition(preprocessed_images[2], valid_type, language) # 方案四 llm_result_004 = llm_recognition(preprocessed_images[3], valid_type, language) # 方案五 llm_result_005 = llm_recognition(preprocessed_images[4], valid_type, language) return llm_result_001, llm_result_002, llm_result_003, llm_result_004, llm_result_005 # VIEW with gr.Blocks() as demo: with gr.Row(): image_input = gr.Image(type="pil", label="上傳圖片") validation_type = gr.Dropdown(choices=["純文字", "身分證正面", "身分證反面"], label="驗證類別") language_dropdown = gr.Dropdown(choices=languages, value="chi_tra", label="語言") with gr.Row(): preImg_button = gr.Button("圖片預先處理") with gr.Row(): with gr.Column(): ocr_button = gr.Button("使用 Pytesseract OCR 辨識") gr.Markdown( "
Package: Google Pytesseract
") with gr.Column(): llm_button = gr.Button("使用 AI LLM 模型辨識") gr.Markdown( "
Package:Bert-base-chinese
") with gr.Row(): preprocess_output_001 = gr.Image(type="pil", label="預處理後的圖片-方案一") ocr_output_001 = gr.JSON(label="OCR-001-解析結果") llm_output_001 = gr.JSON(label="AiLLM-001-解析結果") with gr.Row(): preprocess_output_002 = gr.Image(type="pil", label="預處理後的圖片-方案二") ocr_output_002 = gr.JSON(label="OCR-002-解析結果") llm_output_002 = gr.JSON(label="AiLLM-002-解析結果") with gr.Row(): preprocess_output_003 = gr.Image(type="pil", label="預處理後的圖片-方案三") ocr_output_003 = gr.JSON(label="OCR-003-解析結果") llm_output_003 = gr.JSON(label="AiLLM-003-解析結果") with gr.Row(): preprocess_output_004 = gr.Image(type="pil", label="預處理後的圖片-方案四") ocr_output_004 = gr.JSON(label="OCR-004-解析結果") llm_output_004 = gr.JSON(label="AiLLM-004-解析結果") with gr.Row(): preprocess_output_005 = gr.Image(type="pil", label="預處理後的圖片-方案五") ocr_output_005 = gr.JSON(label="OCR-005-解析結果") llm_output_005 = gr.JSON(label="AiLLM-005-解析結果") # 定義狀態 finish_pre_img_state = gr.State(False) preprocessed_images_state = gr.State([]) # 預先處理圖片 按鈕 preImg_button.click(preprocess_image, inputs=[image_input], outputs=[preprocessed_images_state, finish_pre_img_state, preprocess_output_001, preprocess_output_002, preprocess_output_003, preprocess_output_004, preprocess_output_005]) # pytesseract 按鈕 ocr_button.click(Basic_ocr, inputs=[validation_type, language_dropdown, preprocessed_images_state, finish_pre_img_state], outputs=[ocr_output_001, ocr_output_002, ocr_output_003, ocr_output_004, ocr_output_005]) # AI LLM 按鈕 llm_button.click(AiLLM_ocr, inputs=[validation_type, language_dropdown, preprocessed_images_state, finish_pre_img_state], outputs=[llm_output_001, llm_output_002, llm_output_003, llm_output_004, llm_output_005]) demo.launch(share=False)