| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- # -*- coding:UTF-8 -*-
- import cv2
- import threading
- import time
- from picamera import PiCamera
- from datetime import datetime
- import os
- class RaCamera:
-
- camera_active = False
- __recording_active = False
- camera_recording = False
- __frame = None
- __recordingThread=None
- __writer=None
- recording_path="/home/admin/Video/"
- video_resulution_x = 1920
- video_resulution_y = 1080
- video_fps = 8.0
- video_duration = 30
- def __init__(self):
- pass
- def startCamera(self):
- if self.camera_active==True:
- return
- print("Start Camera")
- self.camera_active = True
- self.__cameraThread = threading.Thread(target = self.threadCamera)
- self.lock = threading.Lock()
- print("- Starting Thread")
- self.__cameraThread.start()
- time.sleep(0.2)
- def stopCamera(self):
- if self.camera_active==False:
- return
- print("Stop Camera")
- self.camera_active = False
- print("- Wait for Thread ending")
- self.__cameraThread.join()
- print("- Stop recording")
- self.stopRecording()
- self.__frame = None
- def makePhoto(self, filename="test.jpg"):
- camera = PiCamera()
- camera.start_preview()
- sleep(5)
- camera.capture(filename)
- camera.stop_preview()
- def startRecording(self):
- if self.camera_recording == True:
- print("Recording already started")
- return
- print("Start Recording")
- self.camera_recording = True
- self.startCamera()
- self.lock = threading.Lock()
- self.__recordingThread = threading.Thread(target = self.treadRecording)
- self.__recordingThread.start()
-
- def stopRecording(self):
- if self.camera_recording==False:
- print("Recording already stopped")
- return
- print("Stop Recording")
- self.__recording_active = False
- self.camera_recording = False
- self.__recordingThread.join()
- def getFrame(self):
- return self.__frame
- def isActive(self):
- return self.camera_active
- def isRecording(self):
- return self.camera_recording
- def treadRecording(self):
- with self.lock:
- while self.camera_recording == True:
- startDate = datetime.today()
- startTime = time.perf_counter()
- #fourcc = cv2.VideoWriter_fourcc(*'X264')
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
- #fourcc = cv2.VideoWriter_fourcc(*'xvid')
- #fourcc = cv2.VideoWriter_fourcc(*'XVID')
- #fourcc = cv2.VideoWriter_fourcc(*'MJPG')
- #fourcc = cv2.VideoWriter_fourcc(*'DIVX')
- #fourcc = cv2.VideoWriter_fourcc(*'WMV1')
- #fourcc = cv2.VideoWriter_fourcc(*'WMV1')
- #fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
- filename = startDate.strftime("%Y-%m-%d_%H-%M-%S") + ".mp4"
- print("- Video File Recording: "+self.recording_path+filename)
- print("- FPS: "+str(self.video_fps))
- print("- Resolution: "+str(self.video_resulution_x)+"x"+str(self.video_resulution_y))
- self.__writer = cv2.VideoWriter(self.recording_path+filename, fourcc, self.video_fps, (self.video_resulution_x, self.video_resulution_y))
- self.__recording_active = True
-
- while self.camera_recording == True and self.__recording_active == True and time.perf_counter() - startTime < self.video_duration:
- time.sleep(1)
- print("- Video File closing")
- self.__recording_active = False
- time.sleep(0.5)
- self.__writer.release()
- if os.path.isfile(self.recording_path+filename) == False:
- print("NO VIDEO FILE FOUND")
- def threadCamera(self):
- with self.lock:
- print("Video Res: " + str(self.video_resulution_x) + "x"+str(self.video_resulution_y))
- cap = cv2.VideoCapture(1)
- cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.video_resulution_x)
- cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.video_resulution_y)
- #cap.set(cv2.CAP_PROP_FPS, self.video_fps)
- if not cap.isOpened():
- print("Cannot open camera")
- self.camera_active = False
- return
- success,image = cap.read()
- #camera = PiCamera()
- #camera.resolution = (self.video_resulution_x, self.video_resulution_y)
- #camera.contrast = 10
- #camera.image_effect = "watercolor"
-
- while self.camera_active:
- ret, frame = cap.read()
- #cv2.imread("Frame", frame)
- if ret:
- self.__frame = frame
- if self.__recording_active==True:
- self.__writer.write(frame)
- print("- Release Camera")
- cap.release()
- cv2.destroyAllWindows()
|