FlaskCameraLiveStream/camera.py

45 lines
1.3 KiB
Python

import cv2
from datetime import datetime
import numpy as np
class VideoCamera(object):
def __init__(self, flip=False, file_type=".jpg", photo_string="stream_photo"):
self.vs = cv2.VideoCapture(0)
self.flip = flip
self.file_type = file_type
self.photo_string = photo_string
self.exposure_value = self.vs.get(cv2.CAP_PROP_EXPOSURE)
def __del__(self):
self.vs.release()
def flip_if_needed(self, frame):
if self.flip:
return np.flip(frame, 0)
return frame
def get_frame(self):
ret, frame = self.vs.read()
if not ret:
return None
frame = self.flip_if_needed(frame)
ret, jpeg = cv2.imencode(self.file_type, frame)
return jpeg.tobytes()
def take_picture(self):
ret, frame = self.vs.read()
if not ret:
return
frame = self.flip_if_needed(frame)
today_date = datetime.now().strftime("%m%d%Y-%H%M%S")
cv2.imwrite(str("static/images/" + self.photo_string + "_" + today_date + self.file_type), frame)
def set_exposure(self, exposure_value):
self.vs.set(cv2.CAP_PROP_EXPOSURE, exposure_value)
self.exposure_value = exposure_value
def get_exposure(self):
return self.exposure_value