"""Independent offline OpenCV install verification lab: version, build info, and codec roundtrip."""
import json
import platform
import sys
from pathlib import Path
import cv2
import numpy as np
def run_cases():
cases = []
# 1. Python 3 & cv2.__version__ verification
py_major, py_minor = sys.version_info[:2]
cv_ver = cv2.__version__
cv_file = Path(cv2.__file__)
ok1 = py_major == 3 and py_minor >= 8 and bool(cv_ver) and cv_file.exists()
cases.append(dict(
name="verify_python_and_cv2_version",
inputs=dict(min_python=[3, 8]),
prerequisites="隔离 Python 3 环境与已安装的 OpenCV Python 绑定",
action="读取 sys.version_info、cv2.__version__ 与模块安装路径,确认可正常导入运行。",
expected=dict(python3=True, version_nonempty=True, module_file_exists=True),
actual=dict(
python3=(py_major == 3 and py_minor >= 8),
version_nonempty=bool(cv_ver),
module_file_exists=cv_file.exists(),
),
measurements=dict(
python_version=platform.python_version(),
cv2_version=cv_ver,
numpy_version=np.__version__,
),
passed=ok1,
))
# 2. Core vs headless build inspection
build_info = cv2.getBuildInformation()
has_python3_section = "Python 3:" in build_info
gui_line = ""
for line in build_info.splitlines():
if "GUI:" in line or "Win32 UI:" in line or "GTK" in line:
gui_line = line.strip()
break
core_symbols = ["imread", "imwrite", "cvtColor", "GaussianBlur", "SIFT_create", "dnn"]
present = {s: hasattr(cv2, s) for s in core_symbols}
ok2 = has_python3_section and all(present.values())
cases.append(dict(
name="verify_core_vs_headless_build",
inputs=dict(checked_symbols=core_symbols),
prerequisites="调用 cv2.getBuildInformation() 解析当前二进制构建特性",
action="确认核心图像处理、SIFT 与 DNN 接口可用,并读取当前无头构建的 GUI 配置状态。",
expected=dict(python3_binding=True, all_core_symbols_present=True),
actual=dict(python3_binding=has_python3_section, all_core_symbols_present=all(present.values())),
measurements=dict(
gui_build_line=gui_line,
has_contrib_surf=hasattr(cv2, "SURF_create"),
symbol_map=present,
),
passed=ok2,
))
# 3. In-memory PNG encode/decode lossless roundtrip verifying codec libraries work
arr = np.arange(0, 48 * 64 * 3, dtype=np.uint32).reshape(48, 64, 3)
img = (arr % 256).astype(np.uint8)
ok_enc, buf = cv2.imencode(".png", img)
decoded = cv2.imdecode(buf, cv2.IMREAD_COLOR)
lossless = bool(ok_enc and decoded is not None and np.array_equal(img, decoded))
ok3 = lossless and int(buf.size) > 0
cases.append(dict(
name="verify_install_commands_and_roundtrip",
inputs=dict(shape=[48, 64, 3], format=".png"),
prerequisites="合成 48x64 BGR 测试矩阵",
action="执行 cv2.imencode('.png') 与 cv2.imdecode 无损编解码回环,核对图像编解码依赖正常。",
expected=dict(encoded=True, lossless_decoded=True),
actual=dict(encoded=bool(ok_enc), lossless_decoded=lossless),
measurements=dict(png_bytes=int(buf.size), decoded_shape=list(decoded.shape)),
passed=ok3,
))
print(json.dumps(dict(version=platform.python_version(), system=platform.system(), cases=cases), ensure_ascii=False))
if __name__ == "__main__":
run_cases()
本例需在 Windows x64 CPU、CPython 3.12.14、OpenCV (opencv-python-headless)、NumPy 2.5.4、Matplotlib 3.11.2。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64 CPU、CPython 3.12.14、OpenCV (opencv-python-headless)、NumPy 2.5.4、Matplotlib 3.11.2。
入口文件:projects/opencv-install-lab/explore-install.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-install.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64 CPU、CPython 3.12.14、OpenCV (opencv-python-headless)、NumPy 2.5.4、Matplotlib 3.11.2。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联31章) · 含当前章节或实例 · mbskycom202612a566.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a590.zip