Skip to main content

Prerequisites

Before running the application, ensure you have the following installed on your system:
  • Python (version 3.x)
  • OpenCV library (cv2)

Example Code

You can use the following main.py script to connect to the camera and view the video feed. A critical step when configuring the camera is that the MJPG pixel format must be set before setting the resolution.
import cv2
import time

# -- Camera Parameters --
# Camera Index
CAM_IDX = 0
# resolution
WIDTH = 1600
HEIGHT = 1200

# Hardware supported framerate of the camera. Highest for best results.
FRAMERATE = 60

# used to set the pixel format to MJPEG/MJPG mode.
MJPG = cv2.VideoWriter_fourcc(*'MJPG')

# -- DEVICE SETUP --
stellarHD = cv2.VideoCapture(CAM_IDX)

# set to MJPEG mode, by default idx 0 is YUYV
# MJPG needs to be set, before resolution. Pixel format is always selected first
stellarHD.set(cv2.CAP_PROP_FOURCC, MJPG)

stellarHD.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
stellarHD.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)

# Set framerate
stellarHD.set(cv2.CAP_PROP_FPS, FRAMERATE)

# (Optional) Disable auto exposure
stellarHD.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
stellarHD.set(cv2.CAP_PROP_EXPOSURE, 90)

# Error Check
if ((stellarHD == None) or (not stellarHD.isOpened())):
    print('\nError - could not open video device.\n')
    exit(0)

while(True):
    success, frame = stellarHD.read()
    if (success):
        cv2.imshow('stellarHD', frame)
    
    # required for frames to buffer and show properly.
    k = cv2.waitKey(1)

    # press 'q' to quit
    if k == ord('q'):
        break

stellarHD.release()
cv2.destroyAllWindows()

Further Information

For additional details, examples in other languages like C++, or troubleshooting, please refer to our GitHub repository.

OpenCV Quickstart Repository

Basic Video Capture, Software Frame Sampling, C++ Code, etc.