一、课件
二、课后习题
1、准备开发环境
安装opencv
Python2:apt install python-opencv -y
Python3:pip3 install opencv-python
安装PyAduio
Python2:
apt-get install portaudio19-dev python-all-dev apt-get install python-pyaudio
Python3:
apt-get install portaudio19-dev python3-all-dev
apt-get install python3-pyaudio
Windows: PyAudio‑0.2.11‑cp27‑cp27m‑win32.whl https://download.lfd.uci.edu/pythonlibs/w3jqiv8s/cp27/PyAudio-0.2.11-cp27-cp27m-win32.whl
PyAudio‑0.2.11‑cp37‑cp37m‑win32.whl https://download.lfd.uci.edu/pythonlibs/w3jqiv8s/PyAudio-0.2.11-cp37-cp37m-win32.whl
2、学习Python摄像头、Python音频录放音、Python视频播放例程 学习三个例程代码,运行一下,把过程录屏,保存到视频文件中
3、修改Python摄像头或者视频播放例程的代码,实现三个函数,分别把彩色图像转为灰度图、二值化和调整图片尺寸,提交Python脚本源文件
三、答案
音频
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wave
import pyaudio
CHUNK = 1024
RECORD_RATE = 44100
RECORD_FORMAT = pyaudio.paInt16
RECORD_SECONDS = 10
RECORD_CHANNELS = 1
class Audio:
def __init__(self, wave_file):
self.wave = None
self.audio_params = dict()
self.audio_file = wave_file
self.audio = pyaudio.PyAudio()
def get_wav_file(self):
if not self.wave:
self.wave = wave.open(self.audio_file, 'rb')
return self.wave
def get_audio_params(self):
if 0 == len(self.audio_params):
self.get_wav_file()
self.audio_params['rate'] = self.wave.getframerate()
self.audio_params['channels'] = self.wave.getnchannels()
self.audio_params['format'] = self.audio.get_format_from_width(self.wave.getsampwidth())
print(self.audio_params)
return self.audio_params
def play(self):
params = self.get_audio_params()
stream = self.audio.open(format=params['format'], channels=params['channels'], rate=params['rate'], output=True)
data = self.wave.readframes(CHUNK)
while data != b'':
stream.write(data)
data = self.wave.readframes(CHUNK)
stream.stop_stream()
stream.close()
self.audio.terminate()
def record(self):
stream = self.audio.open(format=RECORD_FORMAT, channels=RECORD_CHANNELS,
rate=RECORD_RATE, input=True, frames_per_buffer=CHUNK)
print("* start recording")
frames = []
for i in range(0, int(RECORD_RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
self.audio.terminate()
self.save_wav_file(frames)
def save_wav_file(self, frames):
# 新建wav文件
wf = wave.open('record.wav', 'wb')
wf.setnchannels(RECORD_CHANNELS)
wf.setsampwidth(self.audio.get_sample_size(RECORD_FORMAT))
wf.setframerate(RECORD_RATE)
# 音频数据写入到wav文件中
wf.writeframes(b''.join(frames))
wf.close()
def aikit_paly_audio():
audio = Audio('test.wav')
audio.play()
def aikit_record_audio():
audio = Audio('record.wav')
audio.record()
audio = Audio('record.wav')
audio.play()
if __name__ == '__main__':
# aikit_paly_audio()
aikit_record_audio()
pass
视频
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cv2
def to_grey(frame):
video_gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
cv2.imshow('video', video_gray)
def rgb_color(frame):
cv2.imshow('video', frame) # 显示
def resize(frame):
# 获取图片的宽和高
width, height = frame.shape[:2][::-1]
# 将图片缩小便于显示观看
video_resize = cv2.resize(frame,(int(width * 0.5), int(height * 0.5)), interpolation=cv2.INTER_CUBIC)
cv2.imshow("video", video_resize)
def two_value(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)
cv2.imshow("video", binary)
def play_video():
# 获得视频的格式
video_capture = cv2.VideoCapture('喝水智能助手2.mp4')
# 获得码率及尺寸
fps = video_capture.get(cv2.CAP_PROP_FPS)
size = (int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
nums = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
# 读帧
success, frame = video_capture.read()
while success:
# to_grey(frame)
# resize(frame)
rgb_color(frame)
# two_value(frame)
cv2.waitKey(int(1000 / int(fps))) # 延迟
success, frame = video_capture.read() # 获取下一帧
video_capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
play_video()
pass
摄像头
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cv2
def aikit_take_pictures():
camera = cv2.VideoCapture(0)
while True:
ret, frame = camera.read() # 一帧一帧读取视频
cv2.imshow('frame', frame) # 显示结果
# 根据用户的键盘操作,保存照片或者停止程序
key_code = cv2.waitKey(1)
if key_code & 0xFF == ord('p'): # 按p保存照片
cv2.imwrite("photo.jpg", frame)
print('Successful photo')
if key_code & 0xFF == ord('q'): # 按q停止
break
camera.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
aikit_take_pictures()
pass
四、Opencv和pyAudio安装
注意事项:
1.Pycharm在创建工程的时候要勾选
inherit global site-packages
Make available to all projectes
2.使用摄像头和麦克风的时候一定要开启系统调用权限
Comments | NOTHING