2020-09-21 20:44:29 +00:00
|
|
|
#Modified by smartbuilds.io
|
|
|
|
#Date: 27.09.20
|
|
|
|
#Desc: This scrtipt script..
|
|
|
|
|
2022-05-05 11:08:12 +00:00
|
|
|
import cv2 as cv
|
2020-09-21 20:44:29 +00:00
|
|
|
from imutils.video.pivideostream import PiVideoStream
|
|
|
|
import imutils
|
|
|
|
import time
|
2022-05-05 11:08:12 +00:00
|
|
|
from datetime import datetime
|
2020-09-21 20:44:29 +00:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
class VideoCamera(object):
|
2022-05-05 11:08:12 +00:00
|
|
|
def __init__(self, flip = False, file_type = ".jpg", photo_string= "stream_photo"):
|
|
|
|
self.vs = PiVideoStream(resolution=(1920, 1080), framerate=30).start()
|
|
|
|
self.flip = flip # Flip frame vertically
|
|
|
|
self.file_type = file_type # image type i.e. .jpg
|
|
|
|
self.photo_string = photo_string # Name to save the photo
|
2020-09-21 20:44:29 +00:00
|
|
|
time.sleep(2.0)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.vs.stop()
|
|
|
|
|
|
|
|
def flip_if_needed(self, frame):
|
|
|
|
if self.flip:
|
|
|
|
return np.flip(frame, 0)
|
|
|
|
return frame
|
|
|
|
|
|
|
|
def get_frame(self):
|
|
|
|
frame = self.flip_if_needed(self.vs.read())
|
2022-05-05 11:08:12 +00:00
|
|
|
ret, jpeg = cv.imencode(self.file_type, frame)
|
|
|
|
self.previous_frame = jpeg
|
|
|
|
return jpeg.tobytes()
|
|
|
|
|
|
|
|
# Take a photo, called by camera button
|
|
|
|
def take_picture(self):
|
|
|
|
frame = self.flip_if_needed(self.vs.read())
|
|
|
|
ret, image = cv.imencode(self.file_type, frame)
|
|
|
|
today_date = datetime.now().strftime("%m%d%Y-%H%M%S") # get current time
|
|
|
|
cv.imwrite(str(self.photo_string + "_" + today_date + self.file_type), frame)
|