Skip to content

Commit

Permalink
added picamera streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
elblogbruno committed Aug 3, 2021
1 parent 04cd93c commit 154e7ef
Showing 1 changed file with 45 additions and 9 deletions.
54 changes: 45 additions & 9 deletions controller/utils/camera.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import cv2
import threading
import uuid
# import the necessary packages
from picamera.array import PiRGBArray # Generates a 3D RGB array
from picamera import PiCamera # Provides a Python interface for the RPi Camera Module
import time # Provides time-related functions

class RecordingThread(threading.Thread):
def __init__(self, name, camera):
Expand Down Expand Up @@ -35,8 +39,12 @@ def __del__(self):

class VideoCamera(object):
def __init__(self, camera_id=0):
# 打开摄像头, 0代表笔记本内置摄像头
self.cap = cv2.VideoCapture(camera_id, cv2.CAP_DSHOW)
self.is_picamera = False
if 'picamera' in camera_id:
self.cap, self.raw_capture = self.init_picamera()
self.is_picamera = True
else:
self.cap = cv2.VideoCapture(camera_id, cv2.CAP_DSHOW)

# 初始化视频录制环境
self.is_record = False
Expand All @@ -45,21 +53,49 @@ def __init__(self, camera_id=0):
# 视频录制线程
self.recordingThread = None

def init_picamera (self):
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 20
raw_capture = PiRGBArray(camera, size=(640, 480))
time.sleep(0.1)
return camera, raw_capture


# 退出程序释放摄像头
def __del__(self):
self.cap.release()

def get_frame(self):
ret, frame = self.cap.read()
if self.is_picamera:
for frame in self.cap.capture_continuous(self.raw_capture, format="bgr", use_video_port=True):

# Grab the raw NumPy array representing the image
image = frame.array

if image:
ret1, jpeg = cv2.imencode('.jpg', frame)

if ret1:
return jpeg.tobytes()


# Clear the stream in preparation for the next frame
self.raw_capture.truncate(0)

else:
return None
else:
ret, frame = self.cap.read()

if ret:
ret1, jpeg = cv2.imencode('.jpg', frame)
if ret:
ret1, jpeg = cv2.imencode('.jpg', frame)

if ret1:
return jpeg.tobytes()
if ret1:
return jpeg.tobytes()

else:
return None
else:
return None

def start_record(self):
self.is_record = True
Expand Down

0 comments on commit 154e7ef

Please sign in to comment.