Spaces:
Configuration error
Configuration error
File size: 4,076 Bytes
ea9cf0f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | # 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,
) |