Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vo class 0.11 #55

Open
ras120 opened this issue May 20, 2018 · 0 comments
Open

Vo class 0.11 #55

ras120 opened this issue May 20, 2018 · 0 comments
Assignees
Labels

Comments

@ras120
Copy link
Collaborator

ras120 commented May 20, 2018

image
파일다운
Vo.zip

temp,py 실행 전 check.py를 실행하여 녹음장치 index, rate 확인하여 본 코드에 적용할것

실행 시 루프 진입
-큰 소리가 났을 때 녹음 시작
-녹음 후 분석 시작, 분석 결과 푸시
반복


import wave
import threading
from matplotlib import pyplot as plt
import pylab
import time
import pyaudio
from six.moves import queue

class Vo():
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5 # 녹음 길이(초)
DEVICE_INDEX = 1
WAVE_OUTPUT_FILENAME = "file.wav"
Recode_Value = 1000 # 큰 소리 값
# 여기까지 recode 관련 녹음 환경 설정

Cry_Value = 1000
Cut_Value = 100
Sick_Value = 50000
Hungry_Value = 20000
#Sleepy_Value = 0

# 여기까지 detection 관련

def __init__(self,Push):
    self.mPush =Push
    self.run()

def run(self):
    while(True):
        detection_flag = self.recode()
        if detection_flag:
            self.detectino()

def recode(self):
    audio = pyaudio.PyAudio()
    stream = audio.open(format=self.FORMAT, channels=self.CHANNELS, rate=self.RATE, input=True,
                        input_device_index=self.DEVICE_INDEX, frames_per_buffer=self.CHUNK)
    frames = []
    print("do")

    # 첫번째 프레임 확인
    data = stream.read(self.CHUNK)
    check = pylab.fromstring(data, 'int16')
    if (sum(abs(check)) / len(check)) < self.Recode_Value:
        stream.stop_stream()
        stream.close()
        audio.terminate()
        return False
    print("recode...")
    for i in range(1, int(self.RATE / self.CHUNK * self.RECORD_SECONDS)):
        data = stream.read(self.CHUNK)
        frames.append(data)

    stream.stop_stream()
    stream.close()
    audio.terminate()

    waveFile = wave.open(self.WAVE_OUTPUT_FILENAME, 'wb')
    waveFile.setnchannels(self.CHANNELS)
    waveFile.setsampwidth(audio.get_sample_size(self.FORMAT))
    waveFile.setframerate(self.RATE)
    waveFile.writeframes(b''.join(frames))
    waveFile.close()
    return True

def detectino(self):
    spec_flag = 0
    wav = wave.open(self.WAVE_OUTPUT_FILENAME, 'r')
    frames = wav.readframes(-1)
    sound_info = pylab.fromstring(frames, 'int16')
    frame_rate = wav.getframerate()
    wav.close()
    pylab.figure(num=None, figsize=(19, 12))
    pylab.subplot(111)
    pylab.specgram(sound_info, Fs=frame_rate)
    ax1 = plt.subplot(211)
    Pxx, freqs, bins, im = plt.specgram(sound_info, Fs=frame_rate)

    # --------여기부터-------------
    tmp = []  # 추출된 특징값들
    for j in range(len(bins)):
        k = 0
        for i in range(0, 20):
            if k < Pxx[i][j]:
                k = Pxx[i][j]
        tmp.append(k)

    count = 0
    len_count = 0
    sum_a = 0
    for i in range(len(tmp)):
        if tmp[i] > self.Cry_Value:  # 아이 울음소리 got it
            count += 1
            sum_a += tmp[i]
        if tmp[i] > self.Cut_Value:  # 작은소리를 제외한 len을 구하기 위함
            len_count += 1

    bindo = count / len_count  # 아이 울음소리의 빈도
    aver = sum_a / count  # 아이 울음소리의 평균 값

    result = "-"
    if bindo > 0.6:  # 울음소리의 빈도가 일정 이상일 경우
        result = "울어요"

        # 추출된 값의 범위에 따라 울음의 타입별 동작(범위 수정필요)
    if aver > self.Sick_Value:
        result += ", 아파요"
    elif aver > self.Hungry_Value:
        result += ", 배고파요"
    elif aver <= self.Hungry_Value:
        result += ", 졸려요"

    self.mPush.insertMSG(result)

class Push(threading.Thread):
def init(self):
self.sem_using = threading.Semaphore(1) # 1명만 쓸것
self.sem_emp = threading.Semaphore(0)
self.Q=[]
#self.mvi = Vi(self)
self.mvo = Vo(self)
#self.VIth =threading.Thread(target=self.mvi.run)
self.VOth = threading.Thread(target=self.mvo.run)

def insertMSG(self,str):
    self.sem_using.acquire()
    self.Q.append(str)
    print("--------------------in 관측자[%s]삽입완료"%str)
    self.sem_using.release()
    self.sem_emp.release() # 세마포어 값 증가

def getMSG(self):
    self.sem_emp.acquire()
    self.sem_using.acquire()
    msg =self.Q[0]
    del self.Q[0]
    self.sem_using.release()
    return msg

def run(self):
    #self.VIth.start()
    self.VOth.start()
    while(True):
        print("in 관찰자[%s]"%( self.getMSG()) )

p = Push()
p.run()

==============================================================

파일 수정본.

Vo.zip

  • 위 파일에서 detection부분(detectino 라고 잘못 표기되어있음.)을 detection2 로 함수 교체.
@ras120 ras120 self-assigned this May 20, 2018
@ras120 ras120 added the info label May 20, 2018
@Kownby Kownby self-assigned this May 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants