Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from datetime import datetime
  4. import gtk
  5. import keyboard
  6.  
  7. import pyaudio
  8. import wave
  9. import sys
  10.  
  11. flagrecord=True
  12. chunk = 1024
  13. FORMAT = pyaudio.paInt16
  14. CHANNELS = 1
  15. RATE = 44100
  16.  
  17.  
  18. def show_webcam(flagrecord):
  19.  
  20. cam = cv2.VideoCapture(0)
  21. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  22. frame_width = int(cam.get(3))
  23. frame_height = int(cam.get(4))
  24. FONT = cv2.FONT_HERSHEY_PLAIN
  25. filename = datetime.now().strftime("%Y-%m-%d_%H.%M.%S") + ".avi"
  26. filenamea = datetime.now().strftime("%Y-%m-%d_%H.%M.%S")
  27.  
  28. p = pyaudio.PyAudio()
  29. stream = p.open(format = FORMAT,
  30. channels = CHANNELS,
  31. rate = RATE,
  32. input = True,
  33. frames_per_buffer = chunk)
  34. all = []
  35.  
  36. out = cv2.VideoWriter(filename,fourcc, 30, (frame_width,frame_height))
  37.  
  38. while True:
  39.  
  40. ret_val, img = cam.read()
  41. title = datetime.now().strftime("%Y-%m-%d*%H:%M:%S")
  42.  
  43. if flagrecord:
  44.  
  45. img = cv2.flip(img,1)
  46. cv2.putText(img, "REC", (40,40), FONT, 3 , (0,0,255), 2)
  47. cv2.circle(img, (20,20), 10 , (0,0,255), -1)
  48. cv2.rectangle(img, (30,430),(600,480),(0,0,0), -1)
  49. cv2.putText(img, title, (40,470), FONT, 3 , (255,255,255), 2)
  50. cv2.imshow('Grabacion de Audiencias', img)
  51. out.write(img)
  52.  
  53. stream.start_stream()
  54. data = stream.read(chunk)
  55. all.append(data)
  56.  
  57. else:
  58.  
  59. img = cv2.flip(img,1)
  60. cv2.putText(img, "PAUSE", (40,40), FONT, 3 , (255,0,0), 2)
  61. cv2.circle(img, (20,20), 10 , (255,0,0), -1)
  62. cv2.rectangle(img, (50,430),(570,480),(0,0,0), -1)
  63. cv2.putText(img, "Audiencias En Pausa", (60,470), FONT, 3 , (255,255,255), 2)
  64. cv2.imshow('Grabacion de Audiencias', img)
  65. stream.stop_stream()
  66.  
  67. if cv2.waitKey(1) == 27:
  68. break
  69. if keyboard.is_pressed('p'):
  70. flagrecord=False
  71. if keyboard.is_pressed('c'):
  72. flagrecord=True
  73. if keyboard.is_pressed('q'):
  74. break
  75.  
  76. cam.release()
  77. out.release()
  78. cv2.destroyAllWindows()
  79.  
  80. data = ''.join(all)
  81. wf = wave.open(filenamea, 'wb')
  82. wf.setnchannels(CHANNELS)
  83. wf.setsampwidth(p.get_sample_size(FORMAT))
  84. wf.setframerate(RATE)
  85. wf.writeframes(data)
  86. wf.close()
  87.  
  88.  
  89. def main():
  90. show_webcam(mirror=True)
  91.  
  92.  
  93. if __name__ == '__main__':
  94. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement