Update app.py
Browse files
app.py
CHANGED
|
@@ -5,34 +5,29 @@ import gradio as gr
|
|
| 5 |
from ultralytics import YOLO
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
model_doc = YOLO("moldet_yolo11l_960_doc.pt") # PDF
|
| 10 |
-
model_img = YOLO("moldet_yolo11l_640_general.pt") #
|
| 11 |
|
| 12 |
def process_file(file):
|
| 13 |
-
# 创建临时目录
|
| 14 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 15 |
input_path = os.path.join(tmpdir, "input.png")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
if file.name.lower().endswith(".pdf"):
|
| 19 |
-
# PDF -> 转第一页为图片
|
| 20 |
pdf = fitz.open(file.name)
|
| 21 |
page = pdf[0]
|
| 22 |
-
pix = page.get_pixmap(dpi=300)
|
| 23 |
pix.save(input_path)
|
| 24 |
|
| 25 |
-
# 运行 PDF 模型
|
| 26 |
results = model_doc.predict(input_path, save=True, imgsz=960, conf=0.5)
|
| 27 |
|
| 28 |
else:
|
| 29 |
-
# 图片直接保存
|
| 30 |
Image.open(file.name).save(input_path)
|
| 31 |
|
| 32 |
-
# 运行图片模型
|
| 33 |
results = model_img.predict(input_path, save=True, imgsz=640, conf=0.5)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
# save_dir = results[0].save_dir
|
| 37 |
# pred_path = os.path.join(save_dir, os.path.basename(input_path))
|
| 38 |
result_img = results[0].plot()
|
|
@@ -45,13 +40,12 @@ def process_file(file):
|
|
| 45 |
# Gradio UI
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=process_file,
|
| 48 |
-
inputs=gr.File(label="
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
title="PDF / 图片目标检测",
|
| 52 |
description=(
|
| 53 |
-
|
| 54 |
-
|
| 55 |
)
|
| 56 |
)
|
| 57 |
|
|
|
|
| 5 |
from ultralytics import YOLO
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
+
# preload detection models
|
| 9 |
+
model_doc = YOLO("moldet_yolo11l_960_doc.pt") # for PDF
|
| 10 |
+
model_img = YOLO("moldet_yolo11l_640_general.pt") # general
|
| 11 |
|
| 12 |
def process_file(file):
|
|
|
|
| 13 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 14 |
input_path = os.path.join(tmpdir, "input.png")
|
| 15 |
|
| 16 |
+
# pdf or image
|
| 17 |
if file.name.lower().endswith(".pdf"):
|
|
|
|
| 18 |
pdf = fitz.open(file.name)
|
| 19 |
page = pdf[0]
|
| 20 |
+
pix = page.get_pixmap(dpi=300)
|
| 21 |
pix.save(input_path)
|
| 22 |
|
|
|
|
| 23 |
results = model_doc.predict(input_path, save=True, imgsz=960, conf=0.5)
|
| 24 |
|
| 25 |
else:
|
|
|
|
| 26 |
Image.open(file.name).save(input_path)
|
| 27 |
|
|
|
|
| 28 |
results = model_img.predict(input_path, save=True, imgsz=640, conf=0.5)
|
| 29 |
|
| 30 |
+
# results is a list, our results is in results[0]
|
| 31 |
# save_dir = results[0].save_dir
|
| 32 |
# pred_path = os.path.join(save_dir, os.path.basename(input_path))
|
| 33 |
result_img = results[0].plot()
|
|
|
|
| 40 |
# Gradio UI
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=process_file,
|
| 43 |
+
inputs=gr.File(label="Upload an image or a single PDF file", file_types=[".png", ".jpg", ".jpeg", ".pdf"]),
|
| 44 |
+
outputs=gr.Image(type="pil", label="Detection results"),
|
| 45 |
+
title="Molecule detection",
|
|
|
|
| 46 |
description=(
|
| 47 |
+
"Upload an image or a single PDF file to detect molecules using a YOLO-based deep learning model.\n
|
| 48 |
+
The system processes the input and returns an annotated image with bounding boxes and labels around the detected molecular structures."
|
| 49 |
)
|
| 50 |
)
|
| 51 |
|