Slide 72
Slide 72 text
async def handle_frame_event(frame_event: VideoFrameEvent, output_source: rtc.VideoSource):
buffer: VideoFrame = frame_event.frame
arr = np.frombuffer(buffer.data, dtype=np.uint8)
arr = arr.reshape((buffer.height, buffer.width, 3))
src_image = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
gray = cv2.cvtColor(src_image, cv2.COLOR_BGR2GRAY)
cv2.imshow(windows[0], gray)
blurred = cv2.GaussianBlur(gray, (7, 7), 0)
cv2.imshow(windows[1], blurred)
_, thresh = cv2.threshold(blurred, 120, 255, cv2.THRESH_BINARY_INV)
cv2.imshow(windows[2], thresh)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
dest_image = src_image
for contour in contours:
cv2.drawContours(dest_image, [contour], -1, (0, 255, 0), 2)
cv2.imshow(windows[4], dest_image)
frame = rtc.VideoFrame(
buffer.width,
buffer.height,
rtc.VideoBufferType.RGB24,
cv2.cvtColor(dest_image, cv2.COLOR_BGR2RGB).data
)
output_source.capture_frame(frame)
cv2.waitKey(1)