Skip to main content

简单易用的Python摄像头工具包,支持本地显示和网页显示,提供丰富的图像处理效果

Project description

AIToolkit Camera - 简易摄像头工具包

版本 Python 版本 许可证

aitoolkit_cam 是一个针对Python的简单易用的摄像头工具包,让摄像头开发变得轻松简单。无论您是教育工作者还是学生,都可以通过几行代码轻松实现摄像头功能。

核心特点

  • 🌟 简单易用:几行代码即可启动摄像头和网页服务
  • 🌐 网页实时查看:支持通过浏览器远程查看摄像头画面
  • 🔄 迭代器接口:兼容Python迭代器,可在for循环中使用
  • 🖼️ 图像处理:支持基础图像处理功能
  • 🔌 资源管理:自动释放摄像头资源

安装方法

pip install aitoolkit-cam

基础用法

简单示例

from aitoolkit_cam import Camera

# 创建摄像头对象
cam = Camera()
cam.web_enabled = True  # 启用网页服务

# 启动摄像头
cam.start()

# 获取访问地址
url = cam.get_web_url()
print(f"访问地址: {url}")
print("请在浏览器中访问上述地址")

try:
    # 循环获取视频帧并在网页显示
    for frame in cam:
        cam.cv_show(frame, "web")
except KeyboardInterrupt:
    print("正在退出...")
finally:
    # 释放资源
    cam.stop()

Jupyter Notebook中使用

from aitoolkit_cam import Camera
import threading
import time

# 全局变量
cam = None
running = True

def stop_camera():
    """停止摄像头"""
    global cam, running
    running = False
    if cam:
        print("正在停止摄像头...")
        cam.stop()
        time.sleep(0.5)
        print("摄像头已停止")
    return "摄像头已停止,资源已释放"

def camera_loop(camera):
    """摄像头循环"""
    global running
    try:
        for frame in camera:
            if not running:
                break
            camera.cv_show(frame, "web")
    except Exception as e:
        print(f"摄像头循环错误: {e}")
    finally:
        if running:
            running = False
            camera.stop()

def start_camera():
    """启动摄像头"""
    global cam, running
    
    # 如果已有运行实例,先停止
    if cam and running:
        stop_camera()
    
    running = True
    
    # 创建摄像头对象
    cam = Camera()
    cam.web_enabled = True
    
    # 启动摄像头
    print("正在启动摄像头和网页服务...")
    start_time = time.time()
    cam.start()
    
    # 获取地址
    url = cam.get_web_url()
    print(f"启动耗时: {time.time() - start_time:.2f}秒")
    print(f"访问地址: {url}")
    
    # 在后台线程中运行摄像头循环
    thread = threading.Thread(target=camera_loop, args=(cam,), daemon=True)
    thread.start()
    
    print("摄像头已在后台运行")
    print("使用 stop_camera() 函数停止摄像头")
    return url

# 使用方法:
# 1. 运行 start_camera() 启动摄像头
# 2. 使用返回的URL访问摄像头画面
# 3. 完成后运行 stop_camera() 释放资源

高级用法

使用反向代理解决端口变化问题

当需要在前端页面或其他应用中嵌入摄像头画面时,可以使用反向代理保持URL稳定:

1. 安装Nginx

# 在Ubuntu/Debian上
sudo apt install nginx

# 在CentOS/RHEL上
sudo yum install nginx

# 在Windows上可以下载安装包
# http://nginx.org/en/download.html

2. 配置Nginx反向代理

创建或编辑Nginx配置文件(例如/etc/nginx/conf.d/camera.conf):

server {
    listen 80;
    server_name your_server_name;  # 修改为您的服务器名称或IP

    location /camera/ {
        proxy_pass http://localhost:8000/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # WebSocket支持
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        
        # 超时设置
        proxy_read_timeout 86400;
    }
}

3. 启动摄像头服务和Nginx

# 重启Nginx应用配置
sudo systemctl restart nginx

# 启动摄像头服务(端口固定为8000)
python -c "
from aitoolkit_cam import Camera
cam = Camera()
cam.web_enabled = True
cam.port = 8000
cam.start()
print(f'摄像头服务已启动: {cam.get_web_url()}')
input('按Enter键退出...')
cam.stop()
"

现在可以通过 http://your_server_name/camera/ 访问摄像头,无论底层摄像头服务端口如何变化。

在前端页面中嵌入摄像头画面

使用反向代理后,可以在HTML页面中嵌入摄像头画面:

<!DOCTYPE html>
<html>
<head>
    <title>摄像头画面</title>
    <style>
        .camera-container {
            width: 640px;
            height: 480px;
            margin: 0 auto;
            border: 1px solid #ccc;
            overflow: hidden;
        }
        .camera-feed {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }
    </style>
</head>
<body>
    <div class="camera-container">
        <img src="http://your_server_name/camera/video" class="camera-feed" alt="摄像头画面">
    </div>
</body>
</html>

进阶功能

图像处理

from aitoolkit_cam import Camera, apply_effect

# 创建摄像头对象
cam = Camera()
cam.web_enabled = True
cam.start()

# 应用图像效果
for frame in cam:
    # 应用灰度效果
    processed = apply_effect(frame, "grayscale")
    cam.cv_show(processed, "web")

使用上下文管理器

from aitoolkit_cam import Camera

# 使用with语句自动管理资源
with Camera() as cam:
    cam.web_enabled = True
    cam.start()
    url = cam.get_web_url()
    print(f"访问地址: {url}")
    
    # 处理10帧后退出
    count = 0
    for frame in cam:
        cam.cv_show(frame, "web")
        count += 1
        if count >= 10:
            break

# with语句结束后自动释放资源

开发者信息

  • 作者:[您的名字]
  • 版本:0.2.2
  • 许可证:MIT

贡献

欢迎提交问题和贡献代码以改进这个项目!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aitoolkit_cam-0.2.2.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aitoolkit_cam-0.2.2-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file aitoolkit_cam-0.2.2.tar.gz.

File metadata

  • Download URL: aitoolkit_cam-0.2.2.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for aitoolkit_cam-0.2.2.tar.gz
Algorithm Hash digest
SHA256 75f0911eb8546fa91b232f2f9e74d382be88e42eeeb5205a614947ee837792d0
MD5 fb1c8561734b9a09de868c725ea70aab
BLAKE2b-256 53b5924930d7a2264fda221a0c8c1d77b6fae43887649b6f1e784d0360668275

See more details on using hashes here.

File details

Details for the file aitoolkit_cam-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: aitoolkit_cam-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for aitoolkit_cam-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a4250331dd2ac9edd0eee20dd038c6ddad8e1c5e856036bb563b6187f941a555
MD5 b65db36675cf1a8f7449c10708273f68
BLAKE2b-256 195706732ab5eb2795d9b8ec23464d7aa7ef36faf6d7d9392707a0056d28acdb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page