不得不感叹啊,咱们工作效率就是快啊!你瞧,咱们这才多少天呢?半天都不到,咱们的课表已经安排出来了。可是啊,当我们看到这个跳绳的时候?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()
由于时间仓促,本视频只能录制视频而不能录制声音。不过也够用了