就上一代,这一次更新增加了许多功能,
功能说明
该程序是一个基于Python的摄像头应用,使用了OpenCV和PIL库。它提供了以下功能:
显示摄像头画面:实时显示摄像头捕获的画面。
拍照:用户可以通过点击界面中的“拍照”按钮来拍摄照片,并保存到指定文件夹中。
录像:用户可以通过点击“开始/停止录像”按钮来开始或停止录制视频,并保存到指定文件夹中。
放大/缩小:用户可以通过点击“放大”或“缩小”按钮来调整摄像头画面的缩放比例。
切换摄像头:用户可以点击“切换摄像头”按钮来在连接的多个摄像头之间切换。
查看照片:用户可以点击“查看照片”按钮来浏览已经拍摄的照片。
关闭程序:用户可以通过点击“关闭”按钮或按键盘上的 ‘q’ 键来关闭程序。
import cv2
import os
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import datetime
# 检查并创建保存照片和视频的文件夹
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
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: # 查看照片按钮区域
view_photos(photo_folder)
# 保存照片
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 view_photos(folder):
files = os.listdir(folder)
jpg_files = [f for f in files if f.endswith('.jpg')]
if not jpg_files:
print("没有照片可查看")
return
for file in jpg_files:
file_path = os.path.join(folder, file)
image = cv2.imread(file_path)
cv2.imshow('查看照片', image)
key = cv2.waitKey(0) & 0xFF
if key == ord('q'):
break
cv2.destroyWindow('查看照片')
# 主函数
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)
width, height = 900, 1600 # 16:9 比例
roi = [0, 0, width, height]
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_width = int(frame_width * (9 / 25))
roi_height = int(frame_height * (16 / 25))
roi_x = (frame_width - roi_width) // 2
roi_y = (frame_height - roi_height) // 2
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=(0, 0, 0))
draw.rectangle((10, 250, 100, 290), fill=(0, 255, 255))
draw.text((20, 255), "缩小", font=font, fill=(0, 0, 0))
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()