You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.1 KiB
40 lines
1.1 KiB
1 week ago
|
import av
|
||
|
import cv2 as cv
|
||
|
|
||
|
class Camera:
|
||
|
def __init__(self, cam_type_state, stream_type, camera_id):
|
||
|
try:
|
||
|
camera_id = int(camera_id)
|
||
|
except ValueError: # allow strings, ex: /dev/video0
|
||
|
pass
|
||
|
self.cam_type_state = cam_type_state
|
||
|
self.stream_type = stream_type
|
||
|
self.cur_frame_id = 0
|
||
|
|
||
|
print(f"Opening {cam_type_state} at {camera_id}")
|
||
|
|
||
|
self.cap = cv.VideoCapture(camera_id)
|
||
|
|
||
|
self.cap.set(cv.CAP_PROP_FRAME_WIDTH, 1280.0)
|
||
|
self.cap.set(cv.CAP_PROP_FRAME_HEIGHT, 720.0)
|
||
|
self.cap.set(cv.CAP_PROP_FPS, 25.0)
|
||
|
|
||
|
self.W = self.cap.get(cv.CAP_PROP_FRAME_WIDTH)
|
||
|
self.H = self.cap.get(cv.CAP_PROP_FRAME_HEIGHT)
|
||
|
|
||
|
@classmethod
|
||
|
def bgr2nv12(self, bgr):
|
||
|
frame = av.VideoFrame.from_ndarray(bgr, format='bgr24')
|
||
|
return frame.reformat(format='nv12').to_ndarray()
|
||
|
|
||
|
def read_frames(self):
|
||
|
while True:
|
||
|
ret, frame = self.cap.read()
|
||
|
if not ret:
|
||
|
break
|
||
|
# Rotate the frame 180 degrees (flip both axes)
|
||
|
frame = cv.flip(frame, -1)
|
||
|
yuv = Camera.bgr2nv12(frame)
|
||
|
yield yuv.data.tobytes()
|
||
|
self.cap.release()
|