Spaces:
Configuration error
Configuration error
Download app.py from phamha/engineering-drawing-analyzer: direct link, hf CLI and curl.
- Browser
- Download file 4.08 kB
-
https://huggingface.co/spaces/phamha/engineering-drawing-analyzer/resolve/main/app.py
- Command line
-
hf download hf://spaces/phamha/engineering-drawing-analyzer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/phamha/engineering-drawing-analyzer/resolve/main/app.py
4.08 kB
| # app.py β ΔαΊ·t α» root project | |
| import gradio as gr | |
| import json, shutil, tempfile | |
| from pathlib import Path | |
| from PIL import Image | |
| import cv2, numpy as np | |
| from src.inference import run_pipelineas | |
| # Auto-download weights nαΊΏu chΖ°a cΓ³ | |
| import os | |
| if not os.path.exists("best.pt"): | |
| from huggingface_hub import hf_hub_download | |
| hf_hub_download( | |
| repo_id="phamha/drawing-model-weights", | |
| filename="best.pt", | |
| local_dir="." | |
| ) | |
| CHECKPOINT = "best.pt" | |
| # ββ HΓ m xα» lΓ½ cho Gradio βββββββββββββββββββββββββββββββββ | |
| def process(image: Image.Image): | |
| if image is None: | |
| return None, "{}", "ChΖ°a cΓ³ αΊ£nh." | |
| # LΖ°u αΊ£nh PIL tαΊ‘m | |
| tmp_dir = tempfile.mkdtemp() | |
| tmp_path = f"{tmp_dir}/input.jpg" | |
| image.save(tmp_path, quality=95) | |
| try: | |
| result, vis_path = run_pipeline( | |
| image_path=tmp_path, | |
| output_dir=tmp_dir, | |
| checkpoint=CHECKPOINT, | |
| conf=0.3, | |
| ) | |
| except Exception as e: | |
| return None, "{}", f"Lα»i: {e}" | |
| # ββ αΊ’nh visualize βββββββββββββββββββββββββββββββββββββ | |
| vis_bgr = cv2.imread(vis_path) | |
| vis_rgb = cv2.cvtColor(vis_bgr, cv2.COLOR_BGR2RGB) | |
| # ββ JSON (bα» crop_path Δα» gα»n) βββββββββββββββββββββββ | |
| clean = {"image": result["image"], "objects": []} | |
| for obj in result["objects"]: | |
| clean["objects"].append({ | |
| "id": obj["id"], | |
| "class": obj["class"], | |
| "confidence": obj["confidence"], | |
| "bbox": obj["bbox"], | |
| "ocr_content": obj["ocr_content"], | |
| }) | |
| json_str = json.dumps(clean, ensure_ascii=False, indent=2) | |
| # ββ OCR text ΔαΊΉp ββββββββββββββββββββββββββββββββββββββ | |
| ocr_parts = [] | |
| for obj in result["objects"]: | |
| if not obj["ocr_content"]: | |
| continue | |
| content = obj["ocr_content"] | |
| if isinstance(content, dict): # Table | |
| content = content.get("text", "") | |
| sep = "β" * 44 | |
| ocr_parts.append( | |
| f"{sep}\n" | |
| f"[{obj['class']} #{obj['id']}] " | |
| f"conf={obj['confidence']}\n" | |
| f"{sep}\n{content}" | |
| ) | |
| ocr_text = "\n\n".join(ocr_parts) if ocr_parts else "KhΓ΄ng cΓ³ vΓΉng Note/Table." | |
| return vis_rgb, json_str, ocr_text | |
| # ββ Giao diα»n Gradio ββββββββββββββββββββββββββββββββββββββ | |
| with gr.Blocks(title="Engineering Drawing Analyzer") as demo: | |
| gr.Markdown(""" | |
| # π§ Engineering Drawing Analyzer | |
| Tα»± Δα»ng phΓ‘t hiα»n vΓ trΓch xuαΊ₯t **PartDrawing Β· Note Β· Table** tα»« bαΊ£n vαΊ½ kα»Ή thuαΊt. | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| inp_image = gr.Image( | |
| type="pil", | |
| label="Upload bαΊ£n vαΊ½ kα»Ή thuαΊt", | |
| ) | |
| btn = gr.Button("π Detect & OCR", variant="primary") | |
| with gr.Column(scale=1): | |
| out_image = gr.Image(label="KαΊΏt quαΊ£ detection") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| out_json = gr.Code( | |
| language="json", | |
| label="JSON output", | |
| lines=20, | |
| ) | |
| with gr.Column(scale=1): | |
| out_ocr = gr.Textbox( | |
| label="OCR content (Note & Table)", | |
| lines=20, | |
| max_lines=40, | |
| ) | |
| btn.click( | |
| fn=process, | |
| inputs=[inp_image], | |
| outputs=[out_image, out_json, out_ocr], | |
| ) | |
| gr.Examples( | |
| examples=[["test.jpg"]], # thΓͺm αΊ£nh mαΊ«u nαΊΏu cΓ³ | |
| inputs=[inp_image], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| share=True, # tαΊ‘o link public tαΊ‘m thα»i | |
| server_port=7860, | |
| ) |