-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
46 lines (37 loc) · 1.14 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import cv2
import numpy as np
import wave
import pyaudio
def getBrightness(cam):
ret, frame = cam.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
avg = np.sum(frame)/(frame.shape[0]*frame.shape[1])
avg=avg/255
if(avg > 0.6):
return ("Very bright", avg)
if(avg > 0.4):
return ("Bright", avg)
if(avg>0.2):
return ("Dim", avg)
else:
return ("Dark",avg)
def play_file(fname):
# create an audio object
wf = wave.open(fname, 'rb')
p = pyaudio.PyAudio()
chunk = 1024
# open stream based on the wave object which has been input.
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# read data (based on the chunk size)
data = wf.readframes(chunk)
# play stream (looping from beginning of file to the end)
while data != '':
# writing to the stream is what *actually* plays the sound.
stream.write(data)
data = wf.readframes(chunk)
# cleanup stuff.
stream.close()
p.terminate()