RaCamera.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # -*- coding:UTF-8 -*-
  2. import cv2
  3. import threading
  4. import time
  5. from picamera import PiCamera
  6. from datetime import datetime
  7. import os
  8. class RaCamera:
  9. camera_active = False
  10. __recording_active = False
  11. camera_recording = False
  12. __frame = None
  13. __recordingThread=None
  14. __writer=None
  15. recording_path="/home/admin/Video/"
  16. video_resulution_x = 1920
  17. video_resulution_y = 1080
  18. video_fps = 8.0
  19. video_duration = 30
  20. def __init__(self):
  21. pass
  22. def startCamera(self):
  23. if self.camera_active==True:
  24. return
  25. print("Start Camera")
  26. self.camera_active = True
  27. self.__cameraThread = threading.Thread(target = self.threadCamera)
  28. self.lock = threading.Lock()
  29. print("- Starting Thread")
  30. self.__cameraThread.start()
  31. time.sleep(0.2)
  32. def stopCamera(self):
  33. if self.camera_active==False:
  34. return
  35. print("Stop Camera")
  36. self.camera_active = False
  37. print("- Wait for Thread ending")
  38. self.__cameraThread.join()
  39. print("- Stop recording")
  40. self.stopRecording()
  41. self.__frame = None
  42. def makePhoto(self, filename="test.jpg"):
  43. camera = PiCamera()
  44. camera.start_preview()
  45. sleep(5)
  46. camera.capture(filename)
  47. camera.stop_preview()
  48. def startRecording(self):
  49. if self.camera_recording == True:
  50. print("Recording already started")
  51. return
  52. print("Start Recording")
  53. self.camera_recording = True
  54. self.startCamera()
  55. self.lock = threading.Lock()
  56. self.__recordingThread = threading.Thread(target = self.treadRecording)
  57. self.__recordingThread.start()
  58. def stopRecording(self):
  59. if self.camera_recording==False:
  60. print("Recording already stopped")
  61. return
  62. print("Stop Recording")
  63. self.__recording_active = False
  64. self.camera_recording = False
  65. self.__recordingThread.join()
  66. def getFrame(self):
  67. return self.__frame
  68. def isActive(self):
  69. return self.camera_active
  70. def isRecording(self):
  71. return self.camera_recording
  72. def treadRecording(self):
  73. with self.lock:
  74. while self.camera_recording == True:
  75. startDate = datetime.today()
  76. startTime = time.perf_counter()
  77. #fourcc = cv2.VideoWriter_fourcc(*'X264')
  78. fourcc = cv2.VideoWriter_fourcc(*'mp4v')
  79. #fourcc = cv2.VideoWriter_fourcc(*'xvid')
  80. #fourcc = cv2.VideoWriter_fourcc(*'XVID')
  81. #fourcc = cv2.VideoWriter_fourcc(*'MJPG')
  82. #fourcc = cv2.VideoWriter_fourcc(*'DIVX')
  83. #fourcc = cv2.VideoWriter_fourcc(*'WMV1')
  84. #fourcc = cv2.VideoWriter_fourcc(*'WMV1')
  85. #fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
  86. filename = startDate.strftime("%Y-%m-%d_%H-%M-%S") + ".mp4"
  87. print("- Video File Recording: "+self.recording_path+filename)
  88. print("- FPS: "+str(self.video_fps))
  89. print("- Resolution: "+str(self.video_resulution_x)+"x"+str(self.video_resulution_y))
  90. self.__writer = cv2.VideoWriter(self.recording_path+filename, fourcc, self.video_fps, (self.video_resulution_x, self.video_resulution_y))
  91. self.__recording_active = True
  92. while self.camera_recording == True and self.__recording_active == True and time.perf_counter() - startTime < self.video_duration:
  93. time.sleep(1)
  94. print("- Video File closing")
  95. self.__recording_active = False
  96. time.sleep(0.5)
  97. self.__writer.release()
  98. if os.path.isfile(self.recording_path+filename) == False:
  99. print("NO VIDEO FILE FOUND")
  100. def threadCamera(self):
  101. with self.lock:
  102. print("Video Res: " + str(self.video_resulution_x) + "x"+str(self.video_resulution_y))
  103. cap = cv2.VideoCapture(1)
  104. cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.video_resulution_x)
  105. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.video_resulution_y)
  106. #cap.set(cv2.CAP_PROP_FPS, self.video_fps)
  107. if not cap.isOpened():
  108. print("Cannot open camera")
  109. self.camera_active = False
  110. return
  111. success,image = cap.read()
  112. #camera = PiCamera()
  113. #camera.resolution = (self.video_resulution_x, self.video_resulution_y)
  114. #camera.contrast = 10
  115. #camera.image_effect = "watercolor"
  116. while self.camera_active:
  117. ret, frame = cap.read()
  118. #cv2.imread("Frame", frame)
  119. if ret:
  120. self.__frame = frame
  121. if self.__recording_active==True:
  122. self.__writer.write(frame)
  123. print("- Release Camera")
  124. cap.release()
  125. cv2.destroyAllWindows()