一个为中学生和初学者设计的超简单Python摄像头库
Project description
aitoolkit-cam 是一个强大的Python摄像头工具库,它将复杂的摄像头操作封装成极其简洁的接口。无论你是编程新手还是经验丰富的开发者,都可以用最少的代码快速实现本地窗口显示和网页流媒体功能。
✨ 核心特性
- 极简API: 只需几行代码即可启动摄像头并显示视频。
- Jupyter 模式: 默认开启安全模式,在Jupyter环境中自动处理资源释放,避免内核崩溃。
- 双显示模式: 同时支持本地
OpenCV窗口和Web浏览器实时视频流。 - 自动管理: 使用
with语句自动完成摄像头的启动和关闭,无需手动管理。 - 智能健壮: 自动检测可用摄像头,处理各种异常。
- 中文友好: 完美支持在窗口标题中显示中文。
🚀 快速上手
首先,请确保你已经安装了 aitoolkit-cam 和 opencv-python。
pip install aitoolkit-cam opencv-python
本地窗口显示
这是在本地窗口中显示摄像头的最简单方法。代码会在一个名为"实时画面"的窗口中显示视频,直到你按下 'q' 键。
from aitoolkit_cam import Camera
# 使用 'with' 语句自动管理摄像头
try:
with Camera() as cam:
print("📹 摄像头已启动, 按 'q' 键退出。")
# 循环获取并显示每一帧
for frame in cam:
# show() 方法会处理显示和按键检测
if cam.show(frame, window_name="实时画面"):
break
print("✅ 演示完成")
except Exception as e:
print(f"❌ 启动失败: {e}")
Jupyter Notebook / Lab
在Jupyter环境中使用 aitoolkit-cam 同样简单。默认的Jupyter模式会自动显示50帧后停止,防止无限运行和资源泄漏。
from aitoolkit_cam import Camera
# 在Jupyter中,这会自动显示50帧然后停止
with Camera() as cam:
for frame in cam:
if cam.show(frame, window_name="Jupyter演示"):
break
网页流模式
想在浏览器里看视频?只需一个参数即可。在Jupyter中,下面的代码同样会因为 max_frames 的限制而自动停止。
from aitoolkit_cam import Camera
# 启用web模式,在Jupyter中会自动限制帧数
try:
with Camera(web_enabled=True) as cam:
url = cam.start() # start()会返回URL
print(f"🌍 Web服务已启动: {url}")
print(f"👉 请在浏览器中打开此URL。服务将在 {cam.max_frames} 帧后或关闭浏览器页签后自动停止。")
# 这个循环驱动摄像头运行,直到达到帧数限制
for frame in cam:
# 在web模式下,我们不需要在循环里做任何事
# 后台线程会自动将帧推送到网页
pass
print("✅ Web演示结束。")
except Exception as e:
print(f"❌ 启动失败: {e}")
📚 文档
🤝 贡献
我们欢迎任何形式的贡献!无论是提交bug报告、功能请求还是代码PR,请随时通过 GitHub Issues 与我们联系。
📄 许可
本项目采用 MIT 许可。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aitoolkit_cam-0.5.0.tar.gz.
File metadata
- Download URL: aitoolkit_cam-0.5.0.tar.gz
- Upload date:
- Size: 34.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a6d7baba44087cc503e1df39daefa7fe6d9bec2a84498b6822a3801f2c02975
|
|
| MD5 |
28f69293c46ea8b2bf6d1db74feabd84
|
|
| BLAKE2b-256 |
9d7015d75c8bedfb472c3b680f44db624435fd5ccea186e91753308a631c2e65
|
File details
Details for the file aitoolkit_cam-0.5.0-py3-none-any.whl.
File metadata
- Download URL: aitoolkit_cam-0.5.0-py3-none-any.whl
- Upload date:
- Size: 29.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f1c21bf33a8296adf20654a2f21e8da915e95286312ff9789a77939a665781e
|
|
| MD5 |
6595527dde37ad6ff8b91cabf4881068
|
|
| BLAKE2b-256 |
781b303d1d2569f631033838bf0f619bab2b00d4f9787c090ad6e01551a17171
|