一个简单的摄像头应用程序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()
相关推荐
xiandong201 小时前
240925-GAN生成对抗网络
图像处理·人工智能·深度学习·神经网络·生成对抗网络·php·gan
Q8343158192 小时前
华为 海思22AP10(SS524)H.265 编解码处理器用户指南
arm开发·人工智能·嵌入式硬件·音视频·硬件工程·h.265·视频编解码
Einstein·Jun2 小时前
深度学习----------------------------编码器、解码器架构
人工智能·深度学习
墨染辉2 小时前
微调方法概述
人工智能·python·机器学习
浪子L3 小时前
YOLOv8改进 - 注意力篇 - 引入SEAttention注意力机制
人工智能·深度学习·计算机视觉
Bosenya123 小时前
【PyTorch】图像分割
人工智能·pytorch·python
计算机科研之友(Friend)3 小时前
物联网(一)——CMC特刊推荐
开发语言·人工智能·深度学习·物联网·计算机视觉·网络安全
凡人的AI工具箱3 小时前
15分钟学 Python 第35天 :Python 爬虫入门(一)
开发语言·数据结构·人工智能·后端·爬虫·python
江鸟19984 小时前
持续更新:当前最好用的AI 编程工具,Cursor 编程指南
人工智能