【有趣的Python小程序】如何用OpenCv与Tkinter做一个自动录制跳绳视频的程序

不得不感叹啊,咱们工作效率就是快啊!你瞧,咱们这才多少天呢?半天都不到,咱们的课表已经安排出来了。可是啊,当我们看到这个跳绳的时候?WHAT?这是什么鬼?于是为了我们的视频更加简单的创作。我特地编写了一个这样的程序!在这里插入图片描述

思路介绍

首先咱们创建一个GUI界面。然后当我们准备好的时候,我们将点击界面上的开始跳绳。然后播放老师发给我们的音乐。同时调用我们系统的摄像头。进行录像。整一个过程还是特别简单的!

GUI界面

首先咱们先来一段基础代码。这段代码实际上就是在将我们整一个界面放置在界面中间。

import tkinter

skip = tkinter.Tk()
skip.title("SkipRope 智能跳绳") # 设置标题

# 设置居中显示,代码来自 https://blog.csdn.net/weixin_48690683/article/details/122773297
width = 300
height = 300
screen_height = skip.winfo_screenheight()
screen_width = skip.winfo_screenwidth()
x = int(screen_width / 2 - width / 2)
y = int(screen_height / 2 - height / 2)
size = '{}x{}+{}+{}'.format(width, height, x, y)
skip.geometry(size)

skip.mainloop()

在这里插入图片描述
接下来咱们将创建一个BUTTON 来接受用户给我们发送的信息。同时创建一个函数处理这个信息
在这里插入图片描述接收老师的音频文件,并且重命名
在这里插入图片描述
存放在同一目录下
在这里插入图片描述在这里插入图片描述

编写代码,这样咱们就将最简单的部分完成了
在这里插入图片描述

import tkinter
from audioplayer import AudioPlayer

skip_player = AudioPlayer("./SkipMusic.mp3")

skip = tkinter.Tk()
skip.title("SkipRope 智能跳绳") # 设置标题

# 设置居中显示,代码来自 https://blog.csdn.net/weixin_48690683/article/details/122773297
width = 300
height = 300
screen_height = skip.winfo_screenheight()
screen_width = skip.winfo_screenwidth()
x = int(screen_width / 2 - width / 2)
y = int(screen_height / 2 - height / 2)
size = '{}x{}+{}+{}'.format(width, height, x, y)
skip.geometry(size)

def start_click():
	skip_player.play()

def pause_click():
	skip_player.pause()

def resume_click():
	skip_player.resume()


start_button = tkinter.Button(skip, text = "START 开始", bg = "green", fg = "black", command = start_click).pack()
pause_button = tkinter.Button(skip, text = "PAUSE 暂停", bg = "green", fg = "black", command = pause_click).pack()
resume_button = tkinter.Button(skip, text = "RESUME 继续", bg = "green", fg = "black", command = resume_click).pack()

skip.mainloop()

在这里插入图片描述

录像功能

在这里我们使用OpenCV来进行图像处理 pip install opencv-python
在这里插入图片描述
编写代码
在这里插入图片描述

最终效果

在这里插入图片描述
在这里插入图片描述

完整代码

import tkinter
from audioplayer import AudioPlayer
import cv2

skip_player = AudioPlayer("./SkipMusic.mp3")

skip = tkinter.Tk()
skip.title("SkipRope 智能跳绳") # 设置标题

# 设置居中显示,代码来自 https://blog.csdn.net/weixin_48690683/article/details/122773297
width = 300
height = 300
screen_height = skip.winfo_screenheight()
screen_width = skip.winfo_screenwidth()
x = int(screen_width / 2 - width / 2)
y = int(screen_height / 2 - height / 2)
size = '{}x{}+{}+{}'.format(width, height, x, y)
skip.geometry(size)

def captureVideoFromCamera():
	cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
	fourcc = cv2.VideoWriter_fourcc(*'XVID')
	out = cv2.VideoWriter('output.avi',fourcc, 30.0, (640,480))

	while(cap.isOpened()):
	    ret, frame = cap.read()
	    if ret==True:
	        frame = cv2.flip(frame,1)
	        out.write(frame) # 保存视频
	        cv2.imshow('frame', frame)
	        if cv2.waitKey(1) & 0xFF == ord('q'):
	            break
	    else:
	        break

	cap.release()
	out.release()
	cv2.destroyAllWindows()

def start_click():
	skip_player.play()
	captureVideoFromCamera()

def pause_click():
	skip_player.pause()

def resume_click():
	skip_player.resume()


start_button = tkinter.Button(skip, text = "START 开始", bg = "green", fg = "black", command = start_click).pack()
pause_button = tkinter.Button(skip, text = "PAUSE 暂停", bg = "green", fg = "black", command = pause_click).pack()
resume_button = tkinter.Button(skip, text = "RESUME 继续", bg = "green", fg = "black", command = resume_click).pack()

skip.mainloop()

由于时间仓促,本视频只能录制视频而不能录制声音。不过也够用了

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地摊主老袁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值