Skip to content

Instantly share code, notes, and snippets.

@lumwb
Created June 6, 2020 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lumwb/3801b9e44bc984b5b0cacb5b78d0e64b to your computer and use it in GitHub Desktop.
Save lumwb/3801b9e44bc984b5b0cacb5b78d0e64b to your computer and use it in GitHub Desktop.
Start Recording
def start_recording(self):
self.recording = 1
while self.recording == 1:
# Find haar cascade to draw bounding box around face
frame = self.vs.read()
self.videoFrameStore.append(frame)
# fps stuff
self.out.write(frame)
self.fps.update()
# audio stuff
data = self.stream.read(self.chunk, exception_on_overflow=False)
self.audioFrames.append(data)
self.win.update()
for currentVideoFrame in self.videoFrameStore:
# convert to gray and get face position
gray = self.cv2.cvtColor(currentVideoFrame, cv2.COLOR_BGR2GRAY)
faces = self.facecasc.detectMultiScale(
gray, scaleFactor=1.3, minNeighbors=5)
for (x, y, w, h) in faces:
roi_gray = gray[y:y + h, x:x + w]
cropped_img = np.expand_dims(np.expand_dims(
cv2.resize(roi_gray, (48, 48)), -1), 0)
prediction = self.model.predict(cropped_img)
maxindex = int(np.argmax(prediction))
# increment score
emotion = self.emotion_dict[maxindex]
self.emotion_score_dict[emotion] += 1
# write to audio file
self.stream.stop_stream()
self.stream.close()
self.audio.terminate()
self.fps.stop()
# save the audio frames as .wav file
wavefile = self.wave.open(self.wav_output_filename, 'wb')
wavefile.setnchannels(self.chans)
wavefile.setsampwidth(self.audio.get_sample_size(self.form_1))
wavefile.setframerate(self.samp_rate)
wavefile.writeframes(b''.join(self.audioFrames))
wavefile.close()
# write final emotion score to txt file
with open(self.dateTimeString + '.txt', 'w') as file:
file.write(json.dumps(self.emotion_score_dict))
print("[INFO] elasped time: {:.2f}".format(self.fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(self.fps.fps()))
# cap.release()
self.out.release()
self.cv2.destroyAllWindows()
self.vs.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment