I'm relative new with Raspberry and I'm trying to stream a video to a PC using OpenCV libraries. I already made connection using the camera documentation but I don't know send the frames using OpenCV.
The code to stream to a network is:
import socket import time import picamera # Connect a client socket to my_server:8000 (change my_server to the # hostname of your server) client_socket = socket.socket() client_socket.connect(('my_server', 8000)) # Make a file-like object out of the connection connection = client_socket.makefile('wb') try: camera = picamera.PiCamera() camera.resolution = (640, 480) camera.framerate = 24 # Start a preview and let the camera warm up for 2 seconds camera.start_preview() time.sleep(2) # Start recording, sending the output to the connection for 60 # seconds, then stop camera.start_recording(connection, format='h264') camera.wait_recording(60) camera.stop_recording() finally: connection.close() client_socket.close()
And the code to stream to OpenCV object is:
# import the necessary packages from picamera.array import PiRGBArray from picamera import PiCamera import time import cv2 # initialize the camera and grab a reference to the raw camera capture camera = PiCamera() camera.resolution = (640, 480) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(640, 480)) # allow the camera to warmup time.sleep(0.1) # capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # grab the raw NumPy array representing the image, then initialize the timestamp # and occupied/unoccupied text image = frame.array # show the frame cv2.imshow("Frame", image) key = cv2.waitKey(1) & 0xFF # clear the stream in preparation for the next frame rawCapture.truncate(0) # if the `q` key was pressed, break from the loop if key == ord("q"): break
This code was not made by me, I need to thank Adrian for his tutorial from here.