MinerU https://github.com/opendatalab/MinerU/

官网有详细的说明,需要 gpu cuda 支持。

实测笔记本 cpu 模式也能使用,如下。

1. pip 安装 mineru (CPU模式)

1
2
3
4
5
6
# 创建虚拟环境
python -m venv .venv_mnu
.venv_mnu\Scripts\activate

# 安装
pip install -U "mineru[core]" -i https://mirrors.aliyun.com/pypi/simple

2. 预训练模型下载

1
2
3
4
5
6
7
# cmd
mineru-models-download
# [huggingface|modelscope] 选择 modelscope 源
# [pipeline|vlm|all] 选择 pipeline 模型

# 或
mineru-models-download -s modelscope -m pipeline

模型默认会被下载到 ~\.cache\modelscope\hub\models
同时创建一个配置文件 ~\mineru.json

默认下载到当前用户目录下。

运行

帮助:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
mineru --help

Usage: mineru [OPTIONS]

Options:
-v, --version 显示版本并退出
-p, --path PATH 输入文件路径或目录(必填)
-o, --output PATH 输出目录(必填)
-m, --method [auto|txt|ocr] 解析方法:auto(默认)、txt、ocr(仅用于 pipeline 后端)
-b, --backend [pipeline|vlm-transformers|vlm-sglang-engine|vlm-sglang-client]
解析后端(默认为 pipeline)
-l, --lang [ch|ch_server|... ] 指定文档语言(可提升 OCR 准确率,仅用于 pipeline 后端)
-u, --url TEXT 当使用 sglang-client 时,需指定服务地址
-s, --start INTEGER 开始解析的页码(从 0 开始)
-e, --end INTEGER 结束解析的页码(从 0 开始)
-f, --formula BOOLEAN 是否启用公式解析(默认开启,仅 pipeline 后端)
-t, --table BOOLEAN 是否启用表格解析(默认开启,仅 pipeline 后端)
-d, --device TEXT 推理设备(如 cpu/cuda/cuda:0/npu/mps,仅 pipeline 后端)
--vram INTEGER 单进程最大 GPU 显存占用(仅 pipeline 后端)
--source [huggingface|modelscope|local]
模型来源,默认 huggingface
--help 显示帮助信息

例:

1
2
3
mineru -d cpu --source local -o . -p 123.pdf 

mineru -d cpu --source local -o . -l ch_lite -p 123.pdf

耗时

使用 cpu (笔记本 intel ultra 7 155h), 检测一个 10 页的pdf,内容主要是中文文字。

步骤 说明 数量 耗时
Layout Predict pdf 页面解析 10/10 00:18<00:00, 1.82s/it
MFD Predict 数学公式检测 10/10 00:52<00:00, 5.27s/it
MFR Predict 数学公式识别 1/1 00:01<00:00, 1.40s/it
OCR-det ch OCR检测中文 51/51 00:23<00:00, 2.14it/s
Table Predict 表格识别 1/1 00:04<00:00, 4.47s/it
OCR-rec Predict OCR识别 319/319 00:52<00:00, 6.12it/s
Processing pages 处理页面 10/10 00:02<00:00, 3.49it/s

PDFMathTranslate(pdf2zh)支持科学论文翻译和双语对照。

支持全文翻译,并且还能保持原来的 pdf 文档中文字、公式、图表的排版形式。

现在已经发布了 V2.0版。

1. 使用方法

https://pdf2zh-next.com/zh/index.html

2. 使用方法-加入 scoop 和右键菜单。

2.1 scoop 安装

https://github.com/shenbo/portablesoft/blob/master/bucket/pdf2zh.json

阅读全文 »

pdf-renamer 可以对已下载的论文 PDF 文件 进行重命名。

调用 pdf2bib 以及 pdf2doi 查找论文的 DOI、并获得 BibTeX 数据。

1. 安装

1
2
3
pip install pdf-renamer

pip install pdf2bib

2. pdfrenamer 使用方法

1
2
3
4
5
6
7
8
$ pdfrenamer --h
usage: pdfrenamer [-h] [-s] [-ro] [-f FORMAT] [-sf] [-max_length_authors MAX_LENGTH_AUTHORS]
[-max_length_filename MAX_LENGTH_FILENAME] [-max_words_title MAX_WORDS_TITLE]
[-case CASE]
[-add_abbreviation_file PATH_ABBREVIATION_FILE] [-fr] [-sd]
[-install--right--click] [-uninstall--right--click]
[path ...]

阅读全文 »

caj2pdf: 将知网caj文件转换为pdf

https://github.com/shenbo/caj2pdf/

  • 在原来基础上使用 pystand 简单封装,并增加右键菜单快捷命令

1. 克隆仓库

1
2
3
git clone https://github.com/shenbo/caj2pdf.git
# 或
git clone https://github.com/caj2pdf/caj2pdf.git

2. 创建 python 虚拟环境

  • 只需要安装一个库第三方库:pypdf2
  • pystand 需要 Python embed
  • 原来的 dll 文件路径是相对路径,需要调整一下
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
Write-Host "==== 0. Create folders ===="
New-Item -Path "./build" -ItemType Directory -Force
New-Item -Path "./.cache" -ItemType Directory -Force

Write-Host "==== 1. Setup Python venv ===="
if (-not (Test-Path ".venv_caj")) {
python -m venv .venv_caj
}
.venv_caj/Scripts/activate.ps1
python -V
pip install pypdf2

Write-Host "==== 1.1 Download Python embed version===="
$pythonUrl = "https://www.python.org/ftp/python/3.13.4/python-3.13.4-embed-amd64.zip"
if (-not (Test-Path "./.cache/python.zip")) {
Invoke-WebRequest -Uri $pythonUrl -OutFile "./.cache/python.zip"
}
Expand-Archive -Path "./.cache/python.zip" -DestinationPath "./build/runtime" -Force

Write-Host "==== 2. Copy files to ./build/* ===="
Get-ChildItem -Path "./*.py" | Copy-Item -Destination "./build" -Recurse -Force
Get-ChildItem -Path "./lib" | Copy-Item -Destination "./build/lib" -Recurse -Force
Copy-Item -Path "./caj2pdf" -Destination "./build/caj2pdf.int" -Recurse -Force

Write-Host "==== 2.1 change .dll path ===="
$jbigdec_file = './build/jbigdec.py'
$old_cmd = '"./lib/bin/libjbigdec-w64.dll"'
$new_cmd = 'os.path.join(os.path.dirname(__file__), "lib/libjbigdec-w64.dll")'
(Get-Content $jbigdec_file).replace($old_cmd, $new_cmd) | Set-Content $jbigdec_file

阅读全文 »

0. xlwings 简介

xlwings 提供了四种用法:

  • xlwings (Open Source), 本地 python模块、excel插件,代码在本地,运行在本地
  • xlwings PRO, 商业版,收费
  • xlwings Lite, 代码内置在excel文件中、运行在云端(代码公开)
  • xlwings Server, 商业版,收费

1. xlwings (Open Source) 本地版,在 excel 中使用自定义 python 函数

1.1 excel 启用宏

文件 -> 选项 -> 信任中心 -> 信任中心设置 ->
宏设置 -> 勾选 启用VBA宏信任对VBA工程对象模型的访问

1.2 安装 xlwings (Open Source)

1
2
3
4
5
6

pip install xlwings
xlwings addin install

# xlwings addin remove
# pip uninstall xlwings

1.3 快速使用

1
2
3
4
xlwings config create --force
# .xlwings\xlwings.conf

xlwings quickstart xlwings_test

xlwings quickstart 命令会自动建立一个文件夹,内部有两个文件:

  • xlwings_test.xlsm
  • xlwings_test.py
阅读全文 »

1. pyproject.toml 说明

pyproject.toml 是目前官方推荐的打包方式。

官网写的比较抽象。
这里记录下一个简单的使用方法。

2. 包的文件夹结构

建议的包文件结构如下,假设要创建一个名为snake的包:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
snake_proj
├──src/
└──snake/
├──__init__.py
├──AA.py
├──BB.py
└──snake_data/
├──xx.txt
└──yy.txt
├──tests/
└──snake/
├──.gitignore
├──LICENSE
├──pyproject.toml
└──README.md

3. 编写 pyproject.toml

阅读全文 »

一、WSL 安装 Ubuntu22.04

  1. 启用 WSL

  2. 打开 Microsoft Store,安装 Ubuntu22.04

  3. 设置源,更新
    ref: https://linuxmirrors.cn/use/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 用户权限
sudo -i

# 安装 LinuxMirrors:
bash <(curl -sSL https://linuxmirrors.cn/main.sh)

# 恢复原始设置:
# bash <(curl -sSL https://linuxmirrors.cn/main.sh) --use-official-source true

# Docker 源:
# bash <(curl -sSL https://linuxmirrors.cn/docker.sh)

sudo apt update
sudo apt upgrade

  1. 安装 ZSH、oh-my-zsh

参考 zsh & oh-my-zsh 安装配置

1
2
3
4
5
6
7
8
9
sudo apt install zsh
sh -c "$(wget https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh -O -)"

# sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

source ~/.zshrc

二、编译安装 openmc

ref: https://docs.openmc.org/en/stable/quickinstall.html

  1. 参照 Manually Installing from Source, 安装编译所需的基础库
1
sudo apt install g++ cmake libhdf5-dev libpng-dev
  1. 编译 build
1
2
3
4
5
6
git clone --recurse-submodules https://github.com/openmc-dev/openmc.git
cd openmc
mkdir build && cd build
cmake ..
make
sudo make install
  1. 创建虚拟环境,安装 openmc 库
阅读全文 »

一、openmc 面命令

https://docs.openmc.org/en/stable/pythonapi/generated/openmc.XPlane.html

method mcnp paras 参数说明 geometry
openmc.Plane 任意无限平面 a=1, b=0, c=0, d=0 系数 $Ax+By+Cz=D$
openmc.XPlane X无限平面 x0=0 与原点的距离 $x−x_0=0$
openmc.YPlane Y无限平面 y0=0 与原点的距离 $y−y_0=0$
openmc.ZPlane Z无限平面 z0=0 与原点的距离 $z−z_0​=0$
openmc.XCylinder X无限圆柱 y0=0, z0=0, r=1 圆柱中心y0,z0
圆柱半径r
$(y−y_0​)^2+(z−z_0​)^2=r^2$
openmc.YCylinder Y无限圆柱 x0=0, z0=0, r=1 圆柱中心x0,z0
圆柱半径r
$(x−x_0​)^2+(z−z_0​)^2=r^2$
openmc.ZCylinder Z无限圆柱 x0=0, y0=0, r=1 圆柱中心x0,y0
圆柱半径r
$(x−x_0​)^2+(y−y_0​)^2=r^2$
openmc.Sphere 球面 x0=0, y0=0, z0=0, r=1 球心 x0,y0,z0
球半径r
$(x−x_0)^2+(y−y_0)^2+(z−z_0)^2=r^2$
openmc.Cone 圆锥面
openmc.XCone x方向圆锥面 $(y−y_0)^2+(z−z_0)^2=r^2(x−x_0)^2$
openmc.YCone y方向圆锥面
openmc.ZCone z方向圆锥面
openmc.Quadric 二次曲面
openmc.XTorus x环面
openmc.YTorus y环面
openmc.ZTorus z环面

二、openmc 复合面命令 (mcnp macrobody)

https://docs.openmc.org/en/stable/pythonapi/model.html#composite-surfaces

method 复合面 mcnp paras
ConicalFrustum 圆锥台 TRC center_base 底面中心
axis 轴线方向
r1 底面半径
r2 顶面半径
CruciformPrism 多边形棱柱体 distances 距离
center=(0, 0) 中心
axis=”z” 轴线
CylinderSector 无限扇形柱体 r1 扇内半径 r2 扇外半径
theta1 起始角度 theta2 结束角度
center 中心轴坐标
axis 中心轴
HexagonalPrism 六边形柱体 edge_length=1 边长
orientation 方向
origin 原点
IsogonalOctagon 八边形柱体题 center 中心轴坐标
r1 半宽 r2 半宽
axis=”z” 轴线
OrthogonalBox 任意正交体 BOX v 顶点坐标
a1 边1向量
a2 边2向量
a3 边3向量
Polygon 闭合点路径形成的多边形 points 点集
basis 基
RectangularParallelepiped 平行于轴的长方体 RPP xmin xmax ymin ymax zmin zmax 坐标范围
RectangularPrism 四面围成的无限长方体 width 宽
height 高
axis 轴向
origin 原点
RightCircularCylinder 圆柱体 RCC center_base 底面中心
height 高度
radius 半径
axis 中心轴
Vessel 圆柱容器及椭圆封头 r 半径
p1 坐标
p2 坐标
h1 下封头高度
h2 上封头高度
center 中心轴坐标
axis 中心轴
XConeOneSided x方向圆锥
YConeOneSided y方向圆锥
ZConeOneSided z方向圆锥

numpy的保存

https://numpy.org/doc/stable/reference/generated/numpy.save.html

numpy保存数组比较简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1. 保存单个数组
numpy.save(file, arr, allow_pickle=True)

# 2. 保存多个数组
numpy.savez(file, *args, allow_pickle=True, **kwds)

# 保存时如果不指定数组名字, 默认按照 arr_0, arr_1 编号
numpy.savez(file, a1, a2) # Arrays will be named: arr_0, arr_1 ...
# 保存时指定数组名字
numpy.savez(file, a1=a1, a2=a2) # Arrays will be named: a1, a2 ...

# 读取 npz
npzfile = np.load('file.npz')
print(npzfile.files)

a1, a2 = [npzfile[key] for key in npzfile.files]

# 3. 保存多个数组, 保存时进行数据压缩,用法与 numpy.savez() 一样
# 压缩率一般在 10 倍以上,相应牺牲的是读取、保存时间,时间消耗大概是同样的倍率。
numpy.savez_compressed(file, *args, allow_pickle=True, **kwds)

pandas的保存

https://pandas.pydata.org/docs/user_guide/io.html

pandas保存复杂得多,提供了非常多的格式,常见的:csv,xlsx,h5等。

Type Data Description Reader Writer
text CSV read_csv to_csv
text Fixed-Width Text File read_fwf NA
text JSON read_json to_json
text HTML read_html to_html
text LaTeX Styler.to_latex NA
text XML read_xml to_xml
text Local clipboard read_clipboard to_clipboard
binary MS Excel read_excel to_excel
binary OpenDocument read_excel NA
binary HDF5 Format read_hdf to_hdf
binary Feather Format read_feather to_feather
binary Parquet Format read_parquet to_parquet
binary ORC Format read_orc to_orc
binary Stata read_stata to_stata
binary SAS read_sas NA
binary SPSS read_spss NA
binary Python Pickle Format read_pickle to_pickle
SQL SQL read_sql to_sql
阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
> mklink

创建符号链接。

MKLINK [[/D] | [/H] | [/J]] Link Target

/D 创建目录符号链接。默认为文件符号链接。
/H 创建硬链接而非符号链接。
/J 创建目录联接。
Link 指定新的符号链接名称。
Target 指定新链接引用的路径 (相对或绝对)。

例:创建wechat文件夹目录链接

将微信默认的接收文件夹 设置到 指定位置。

注意:命令执行需要左边文件夹不存在,可先将微信退出、默认文件夹重命名为 file1、然后执行命令,再将 file1 中全部文件移至 file 里。

1
2
3
4
5
# mklink /J [微信默认文件夹路径] [自定义的文件夹路径]
mklink /J xxxx\wechat\xwechat_files\wxid_123456789_1234\msg\file yyyy\weixin_file

# output
# 为 xxxx\wechat\xwechat_files\wxid_123456789_1234\msg\file <<===>> yyyy\weixin_file 创建的联接

烟波金陵 缥缈江湖
车走马急 你追我逐
尽己所能 敬我弗如
梦遥路远 终有归途

二零二五元旦于红杏楼

gif

pillow

1
2
3
4
5
6
7
8
9
from PIL import Image
import glob

pngs = glob.glob('temp_png/*.png')
# print(pngs)

frames = [Image.open(png) for png in pngs]

frames[0].save('z.gif', format='GIF', append_images=frames[1:], save_all=True, duration=100, loop=0)

mp4

opencv-python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import glob
import cv2

pngs = glob.glob('temp_png/*.png')
# print(pngs)

frames = [cv2.imread(png) for png in pngs]

h, w, l = frames[0].shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(f'{pl}.mp4', fourcc, 15, (w, h))
for f in frames:
video.write(f)

cv2.destroyAllWindows()
video.release()

用 pandoc 将 latex 转换为 word 文档

ref: https://www.cnblogs.com/jiankychen/p/pandoc.html

1
2
3
4
5
6
7
8
9
10
11
pandoc paper.tex |
--number-sections |
--filter pandoc-crossref |
-t docx+native_numbering |
-M tableEqns -M autoEqnLabels |
--bibliography ref.bib |
--citeproc --csl ieee-wireless-communications-letters.csl |
-M reference-section-title=Reference |
--reference-doc template.docx |
-o output.docx

阅读全文 »

1. blender 运动基础: 平移、旋转

创建一个立方体,按照动作设定坐标后新建关键帧,就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# give Python access to Blender's functionality
import bpy
# extend Python's math functionality
import math

# add a cube into the scene
bpy.ops.mesh.primitive_cube_add()
# get a reference to the currently active object
cube = bpy.context.active_object

# insert keyframe at frame one
cube.keyframe_insert("rotation_euler", frame=1)
# change the rotation of the cube around z-axis
cube.rotation_euler.z = math.radians(360)

# insert keyframe at the last frame
cube.keyframe_insert("rotation_euler", frame=90)

# change the location of the cube on the z-axis
cube.location.z = 0
cube.keyframe_insert("location", frame=100)
cube.location.z = 5
cube.keyframe_insert("location", frame=150)

阅读全文 »

Blender mesh 创建点线面 uOQ-CPcaqMo AxazJi3x6js

任务:创建一个半径为2圆环,圆环上均布32个点,显示为点或者线。

1. 点

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
# give Python access to Blender's functionality
import bpy
# extend Python's math functionality
import math
# extend Python's print functionality
import pprint

# initialize paramaters
vert_count = 32 # show with 16 and 64
angle_step = math.tau / vert_count
radius = 2

# create a list of vert coordinates
vert_coordinates = list()

# repeat code in a loop
for i in range(vert_count):

# calculate current current_angle
current_angle = angle_step * i

# calculate coordinate
x = radius * math.cos(current_angle)
y = radius * math.sin(current_angle)

# visualize what we are doing
bpy.ops.mesh.primitive_ico_sphere_add(radius=0.05, location=(x, y, 0))

# add current coordinate to list
vert_coordinates.append((x, y, 0))

pprint.pprint(vert_coordinates)
阅读全文 »

Blender Python 配置 (on Windows) YUytEtaVrrc

1. 安装 vscode、 blender

1
2
3
4
5
6
scoop install vscode
scoop install blender

# blender 和内置的 python 路径如下:
# blender path : C:\Users\xxxx\scoop\apps\blender\4.2.3\blender.exe
# blender build-in python path : C:\Users\shenb\scoop\apps\blender\4.2.3\4.2\python\bin\python.exe

在 vscode 中选择 python 解释器,注意选择 blender 内置的 python 路径。

2. 安装自动补全 fake-bpy-module

安装时注意要选用 blender 内置的 python -m pip 安装。

ref: https://github.com/nutti/fake-bpy-module

1
2
3
4
5
C:\Users\shenb\scoop\apps\blender\4.2.3\4.2\python\bin\python.exe -m pip install fake-bpy-module

# 安装常用的其他包:
# C:\Users\shenb\scoop\apps\blender\4.2.3\4.2\python\bin\python.exe -m pip install pandas
# C:\Users\shenb\scoop\apps\blender\4.2.3\4.2\python\bin\python.exe -m pip install matplotlib
阅读全文 »

视频内字幕提取

video-subtitles-ocr

视频字幕提取,基于 opencv 和 paddleocr

视频内字幕提取

这里是针对内封了硬字幕的视频,字幕已经成为了画面的一部分。

思路:简单用 opencv 提取视频内的所有帧,现在可以使用 paddleocr,更加方便一点对图片进行 ocr 识别。

0. 首先需要配置一下

0.1 安装 python 库

  • opencv-python
  • scikit-image
  • paddleocr

ref: https://paddlepaddle.github.io/PaddleOCR/latest/quick_start.html#1-paddlepaddle

阅读全文 »

word 不能直接导入 eps 格式的图片,所以先将 eps 图片转换成 png 格式。

这里使用 python 调用 PIL 读取 eps 图片,直接保存为 png 图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# pip install pillow
# scoop install ghostscript

from PIL import Image

import os

for parent, dirnames, filenames in os.walk(os.getcwd()):
for filename in filenames:
ext = os.path.splitext(filename)[1]
if ext != '.eps':
continue

eps_image = Image.open(filename)
size = eps_image.size
print(filename, size)

eps_image.load(scale=10) # 分辨率增大到10倍
eps_image.save(filename+'.png', quality=100)
  • PIL 默认采用 Ghostscript 处理 eps 图片, 所以先确保安装了 Ghostscript。 scoop install ghostscript
  • eps 为矢量图,默认情况下其尺寸较小,导致保存成图片式分辨率较低,eps_image.load(scale=10) 将分辨率增大到10倍后保存。

下载 bili 视频

1
2
3
4
5
6
7
pip install you-get
# you-get https://www.bilibili.com/video/BV1234567890

pip install yt-dlp
# yt-dlp https://www.bilibili.com/video/BV1234567890

# yt-dlp "https://www.youtube.com/watch?v=GGGGGGGGGGGG" --proxy http://127.0.0.1:7890

视频转音频

1
2
3
4
5
6
scoop install ffmpeg
ffmpeg -i xxx.mp4
# checkout the audio video format
ffmpeg -i xxx.mp4 -acodec copy xxx.aac
ffmpeg -i xxx.mp4 -acodec copy xxx.mp3
ffmpeg -i xxx.mkv -acodec copy xxx.opus

音频转文字 📘

https://github.com/openai/whisper

  1. ** whisper **:
1
2
3
4
pip install -U openai-whisper

whisper audio.flac audio.mp3 audio.wav --model small --device cpu --language zh

  1. ** python **:
    使用 Python 运行 main.py 脚本。
1
2
3
4
5
6
import whisper

model = whisper.load_model("small", device="cpu")
result = model.transcribe(f"123.mp3")

print(result["text"]))