jibsn commited on
Commit
7099d4e
·
verified ·
1 Parent(s): 1df5a00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -17
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
- # YOLO 会保存预测结果到 runs/predict/xxx 目录
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="上传图片或 PDF(单页)", file_types=[".png", ".jpg", ".jpeg", ".pdf"]),
49
- # outputs=gr.Image(type="filepath", label="检测结果"),
50
- outputs=gr.Image(type="pil", label="检测结果"),
51
- title="PDF / 图片目标检测",
52
  description=(
53
- "上传一张图片或单页 PDF,系统会自动选择对应的 YOLO 模型进行检测并输出结果。\n"
54
- "PDF 使用 moldet_yolo11l_960_doc.pt,图片使用 moldet_yolo11l_640_general.pt"
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