Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,44 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
import zipfile
|
| 4 |
-
import shutil
|
| 5 |
-
import subprocess
|
| 6 |
-
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 10 |
-
# This zip was uploaded to your space
|
| 11 |
ZIP_PATH = os.path.join(BASE_DIR, "engineering_drawing_code.zip")
|
| 12 |
-
# Where the repo will live once extracted
|
| 13 |
REPO_DIR = os.path.join(BASE_DIR, "engineering_drawing_repo")
|
| 14 |
|
| 15 |
def extract_two_outputs(uploaded_image_path):
|
| 16 |
-
# 1)
|
| 17 |
if not os.path.isdir(REPO_DIR):
|
| 18 |
with zipfile.ZipFile(ZIP_PATH, "r") as z:
|
| 19 |
z.extractall(REPO_DIR)
|
| 20 |
-
# 2) Clean
|
| 21 |
out_excel = os.path.join(BASE_DIR, "drawingInfo.xlsx")
|
| 22 |
out_png = os.path.join(BASE_DIR, "extracted", "drawing01.png")
|
| 23 |
if os.path.exists(out_excel):
|
| 24 |
os.remove(out_excel)
|
| 25 |
if os.path.isdir(os.path.join(BASE_DIR, "extracted")):
|
| 26 |
shutil.rmtree(os.path.join(BASE_DIR, "extracted"))
|
| 27 |
-
# 3) Prepare images
|
| 28 |
images_folder = os.path.join(REPO_DIR, "images")
|
| 29 |
if os.path.isdir(images_folder):
|
| 30 |
shutil.rmtree(images_folder)
|
| 31 |
os.makedirs(images_folder, exist_ok=True)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# 4) Run the extraction script
|
| 35 |
subprocess.run(
|
| 36 |
[sys.executable, "mainExtractionOCR.py"],
|
| 37 |
cwd=REPO_DIR,
|
| 38 |
check=True,
|
| 39 |
)
|
| 40 |
-
# 5) Return the
|
| 41 |
return out_png, out_excel
|
| 42 |
|
| 43 |
-
# Build Gradio
|
| 44 |
demo = gr.Blocks()
|
| 45 |
-
|
| 46 |
with demo:
|
| 47 |
-
gr.Markdown("## Engineering-Drawing Extractor")
|
| 48 |
with gr.Row():
|
| 49 |
-
inp = gr.Image(type="filepath", label="Upload
|
| 50 |
with gr.Row():
|
| 51 |
balloon = gr.Image(type="filepath", label="Ballooned Drawing")
|
| 52 |
excel = gr.File(label="Download Extracted Data (Excel)")
|
|
|
|
| 1 |
+
import os, sys, zipfile, shutil, subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
| 5 |
ZIP_PATH = os.path.join(BASE_DIR, "engineering_drawing_code.zip")
|
|
|
|
| 6 |
REPO_DIR = os.path.join(BASE_DIR, "engineering_drawing_repo")
|
| 7 |
|
| 8 |
def extract_two_outputs(uploaded_image_path):
|
| 9 |
+
# 1) Unzip the extractor repo if needed
|
| 10 |
if not os.path.isdir(REPO_DIR):
|
| 11 |
with zipfile.ZipFile(ZIP_PATH, "r") as z:
|
| 12 |
z.extractall(REPO_DIR)
|
| 13 |
+
# 2) Clean previous outputs
|
| 14 |
out_excel = os.path.join(BASE_DIR, "drawingInfo.xlsx")
|
| 15 |
out_png = os.path.join(BASE_DIR, "extracted", "drawing01.png")
|
| 16 |
if os.path.exists(out_excel):
|
| 17 |
os.remove(out_excel)
|
| 18 |
if os.path.isdir(os.path.join(BASE_DIR, "extracted")):
|
| 19 |
shutil.rmtree(os.path.join(BASE_DIR, "extracted"))
|
| 20 |
+
# 3) Prepare images folder
|
| 21 |
images_folder = os.path.join(REPO_DIR, "images")
|
| 22 |
if os.path.isdir(images_folder):
|
| 23 |
shutil.rmtree(images_folder)
|
| 24 |
os.makedirs(images_folder, exist_ok=True)
|
| 25 |
+
shutil.copyfile(uploaded_image_path,
|
| 26 |
+
os.path.join(images_folder, "01.png"))
|
| 27 |
+
# 4) Run the OCR/extraction script
|
| 28 |
subprocess.run(
|
| 29 |
[sys.executable, "mainExtractionOCR.py"],
|
| 30 |
cwd=REPO_DIR,
|
| 31 |
check=True,
|
| 32 |
)
|
| 33 |
+
# 5) Return the ballooned drawing + Excel
|
| 34 |
return out_png, out_excel
|
| 35 |
|
| 36 |
+
# Build Gradio UI
|
| 37 |
demo = gr.Blocks()
|
|
|
|
| 38 |
with demo:
|
| 39 |
+
gr.Markdown("## Engineering-Drawing Information Extractor")
|
| 40 |
with gr.Row():
|
| 41 |
+
inp = gr.Image(type="filepath", label="Upload drawing")
|
| 42 |
with gr.Row():
|
| 43 |
balloon = gr.Image(type="filepath", label="Ballooned Drawing")
|
| 44 |
excel = gr.File(label="Download Extracted Data (Excel)")
|