一个简单的摄像头应用程序3

这是一个简单的摄像头的比较完善的代码了,这里我们完善了框选/查看/等等功能,优化了拍摄范围及出现的一些问题的解决。涵盖了从摄像头捕获图像、处理图像、保存照片、录制视频到界面控制等多个方面。以下是这个app.py的第一个正式版app01.py,以下是它的代码

app01.py

python 复制代码
import cv2
import os
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import datetime
import webbrowser

# 检查并创建保存照片和视频的文件夹
def create_folder(folder_name):
    if not os.path.exists(folder_name):
        os.makedirs(folder_name)
    return folder_name

# 获取文件夹中的最大编号
def get_next_file_number(folder_name, file_extension):
    files = os.listdir(folder_name)
    files = [f for f in files if f.endswith(file_extension)]
    if files:
        numbers = [int(f.split('.')[0]) for f in files]
        return max(numbers) + 1
    else:
        return 1

# 将PIL图像转换为OpenCV图像
def pil_to_cv(image):
    return cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)

# 鼠标回调函数
def mouse_callback(event, x, y, flags, param):
    global next_photo_number, next_video_number, running, recording, out, frame, scale_factor, cam_index, roi, in_view_photos
    if event == cv2.EVENT_LBUTTONDOWN:
        if 10 <= x <= 100 and 10 <= y <= 50:  # 关闭按钮区域
            running = False
        elif 10 <= x <= 100 and 70 <= y <= 110:  # 拍照按钮区域
            save_photo(frame, next_photo_number)
            next_photo_number += 1
        elif 10 <= x <= 100 and 130 <= y <= 170:  # 开始/停止录像按钮区域
            if not recording:
                start_recording()
            else:
                stop_recording()
        elif 10 <= x <= 100 and 190 <= y <= 230:  # 放大按钮区域
            scale_factor = min(3.0, scale_factor * 2)
        elif 10 <= x <= 100 and 250 <= y <= 290:  # 缩小按钮区域
            scale_factor = max(1.0, scale_factor / 2)
        elif 10 <= x <= 100 and 310 <= y <= 350:  # 切换摄像头按钮区域
            switch_camera()
        elif 10 <= x <= 100 and 370 <= y <= 410:  # 查看照片按钮区域
            open_photo_folder()
    elif event == cv2.EVENT_RBUTTONDOWN:
        roi[0], roi[1] = x, y
    elif event == cv2.EVENT_RBUTTONUP:
        roi[2], roi[3] = x - roi[0], y - roi[1]
        if roi[2] < 0:
            roi[0] += roi[2]
            roi[2] = -roi[2]
        if roi[3] < 0:
            roi[1] += roi[3]
            roi[3] = -roi[3]

# 保存照片
def save_photo(frame, photo_number):
    file_path = os.path.join(photo_folder, f"{photo_number}.jpg")
    # 去除界面上的按钮
    clean_frame = remove_buttons(frame)
    # 裁剪区域
    clean_frame = clean_frame[roi[1]:roi[1] + roi[3], roi[0]:roi[0] + roi[2]]
    cv2.imwrite(file_path, clean_frame)
    print(f"照片已保存为 {file_path}")

# 去除界面上的按钮
def remove_buttons(frame):
    pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
    draw = ImageDraw.Draw(pil_image)
    draw.rectangle((0, 0, 110, 420), fill=(0, 0, 0, 0))  # 透明填充
    return pil_to_cv(pil_image)

# 开始录像
def start_recording():
    global out, recording, frame
    file_path = os.path.join(video_folder, f"{next_video_number}.mp4")
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(file_path, fourcc, 20.0, (roi[2], roi[3]))
    recording = True
    print(f"开始录像: {file_path}")

# 停止录像
def stop_recording():
    global out, recording
    out.release()
    recording = False
    print("录像已保存")

# 切换摄像头
def switch_camera():
    global cap, cam_index
    cap.release()
    cam_index = (cam_index + 1) % 2  # 切换到下一个摄像头
    cap = cv2.VideoCapture(cam_index)
    if not cap.isOpened():
        print("无法打开摄像头")
        running = False

# 打开照片文件夹
def open_photo_folder():
    folder_path = os.path.abspath(photo_folder)
    webbrowser.open(folder_path)

# 主函数
def main():
    global running, frame, photo_folder, video_folder, next_photo_number, next_video_number, recording, out, scale_factor, cam_index, roi

    photo_folder = "photos"
    video_folder = "videos"
    create_folder(photo_folder)
    create_folder(video_folder)

    next_photo_number = get_next_file_number(photo_folder, '.jpg')
    next_video_number = get_next_file_number(video_folder, '.mp4')

    running = True
    recording = False
    out = None
    scale_factor = 1.0
    cam_index = 0

    cap = cv2.VideoCapture(cam_index)
    if not cap.isOpened():
        print("无法打开摄像头")
        return

    cv2.namedWindow('摄像头')
    cv2.setMouseCallback('摄像头', mouse_callback)

    # 使用支持中文的字体文件
    font_path = "simhei.ttf"  # 确保这个路径指向你的 simhei.ttf 文件
    font = ImageFont.truetype(font_path, 20)

    # 初始化ROI(Region of Interest)
    roi = [0, 0, 1920, 1080]

    while running:
        ret, frame = cap.read()
        if not ret:
            print("无法获取帧")
            break

        # 缩放图像
        frame = cv2.resize(frame, None, fx=scale_factor, fy=scale_factor)

        # 计算ROI的坐标
        frame_height, frame_width, _ = frame.shape
        roi_x = max(0, min(roi[0], frame_width - 1))
        roi_y = max(0, min(roi[1], frame_height - 1))
        roi_width = max(0, min(roi[2], frame_width - roi_x))
        roi_height = max(0, min(roi[3], frame_height - roi_y))
        roi = [roi_x, roi_y, roi_width, roi_height]

        # 将OpenCV图像转换为PIL图像
        pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        draw = ImageDraw.Draw(pil_image)

        # 绘制按钮
        draw.rectangle((10, 10, 100, 50), fill=(0, 0, 255))
        draw.text((20, 15), "关闭", font=font, fill=(255, 255, 255))

        draw.rectangle((10, 70, 100, 110), fill=(255, 0, 0))
        draw.text((20, 75), "拍照", font=font, fill=(255, 255, 255))

        draw.rectangle((10, 130, 100, 170), fill=(0, 255, 0))
        draw.text((20, 135), "录像", font=font, fill=(255, 255, 255))

        draw.rectangle((10, 190, 100, 230), fill=(0, 255, 255))
        draw.text((20, 195), "放大", font=font, fill=(255, 255, 255))

        draw.rectangle((10, 250, 100, 290), fill=(0, 255, 255))
        draw.text((20, 255), "缩小", font=font, fill=(255, 255, 255))

        draw.rectangle((10, 310, 100, 350), fill=(255, 255, 0))
        draw.text((20, 315), "切换摄像头", font=font, fill=(0, 0, 0))

        draw.rectangle((10, 370, 100, 410), fill=(255, 165, 0))
        draw.text((20, 375), "查看照片", font=font, fill=(0, 0, 0))

        # 绘制ROI区域
        draw.rectangle((roi[0], roi[1], roi[0] + roi[2], roi[1] + roi[3]), outline=(0, 255, 0), width=2)

        # 显示当前照片编号和缩放比例
        draw.text((10, 430), f"当前照片编号: {next_photo_number}", font=font, fill=(0, 255, 0))
        draw.text((10, 460), f"当前缩放比例: {scale_factor:.1f}x", font=font, fill=(0, 255, 0))

        # 将PIL图像转换回OpenCV图像
        frame = pil_to_cv(pil_image)

        cv2.imshow('摄像头', frame)

        key = cv2.waitKey(1) & 0xFF
        if key == ord('q'):  # 按 'q' 键退出
            running = False

        # 检查窗口是否被关闭
        if cv2.getWindowProperty('摄像头', cv2.WND_PROP_VISIBLE) < 1:
            running = False

        if recording:
            out.write(frame[roi[1]:roi[1] + roi[3], roi[0]:roi[0] + roi[2]])

    if recording:
        stop_recording()

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()
相关推荐
凤枭香3 分钟前
Python OpenCV 傅里叶变换
开发语言·图像处理·python·opencv
CSDN云计算3 分钟前
如何以开源加速AI企业落地,红帽带来新解法
人工智能·开源·openshift·红帽·instructlab
艾派森14 分钟前
大数据分析案例-基于随机森林算法的智能手机价格预测模型
人工智能·python·随机森林·机器学习·数据挖掘
hairenjing112316 分钟前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机
小蜗子20 分钟前
Multi‐modal knowledge graph inference via media convergenceand logic rule
人工智能·知识图谱
SpikeKing33 分钟前
LLM - 使用 LLaMA-Factory 微调大模型 环境配置与训练推理 教程 (1)
人工智能·llm·大语言模型·llama·环境配置·llamafactory·训练框架
黄焖鸡能干四碗1 小时前
信息化运维方案,实施方案,开发方案,信息中心安全运维资料(软件资料word)
大数据·人工智能·软件需求·设计规范·规格说明书
1 小时前
开源竞争-数据驱动成长-11/05-大专生的思考
人工智能·笔记·学习·算法·机器学习
ctrey_1 小时前
2024-11-4 学习人工智能的Day21 openCV(3)
人工智能·opencv·学习
攻城狮_Dream1 小时前
“探索未来医疗:生成式人工智能在医疗领域的革命性应用“
人工智能·设计·医疗·毕业