| 
 | 1 | +#  | 
 | 2 | +# Copyright (c) FIRST and other WPILib contributors.  | 
 | 3 | +# Open Source Software; you can modify and/or share it under the terms of  | 
 | 4 | +# the WPILib BSD license file in the root directory of this project.  | 
 | 5 | +#  | 
 | 6 | + | 
 | 7 | + | 
 | 8 | +import ntcore  | 
 | 9 | +import numpy  | 
 | 10 | +from cscore import CameraServer  | 
 | 11 | +import cv2  | 
 | 12 | + | 
 | 13 | + | 
 | 14 | +#  | 
 | 15 | +# This code will work both on a RoboRIO and on other platforms. The exact mechanism  | 
 | 16 | +# to run it differs depending on whether you’re on a RoboRIO or a coprocessor  | 
 | 17 | +#  | 
 | 18 | +# https://robotpy.readthedocs.io/en/stable/vision/code.html  | 
 | 19 | + | 
 | 20 | + | 
 | 21 | +def main():  | 
 | 22 | +    # Get the Axis camera from CameraServer  | 
 | 23 | +    camera = CameraServer.addAxisCamera("axis-camera.local")  | 
 | 24 | + | 
 | 25 | +    # Set the resolution  | 
 | 26 | +    camera.setResolution(640, 480)  | 
 | 27 | + | 
 | 28 | +    # Get a CvSink. This will capture Mats from the camera  | 
 | 29 | +    cvSink = CameraServer.getVideo()  | 
 | 30 | + | 
 | 31 | +    # Setup a CvSource. This will send images back to the Dashboard  | 
 | 32 | +    outputStream = CameraServer.putVideo("Rectangle", 640, 480)  | 
 | 33 | + | 
 | 34 | +    # Mats are very memory expensive. Lets reuse this Mat.  | 
 | 35 | +    mat = numpy.zeros((480, 640, 3), dtype="uint8")  | 
 | 36 | + | 
 | 37 | +    # Declare the color of the rectangle  | 
 | 38 | +    rectColor = (255, 255, 255)  | 
 | 39 | + | 
 | 40 | +    # The camera code will be killed when the robot.py program exits. If you wish to perform cleanup,  | 
 | 41 | +    # you should register an atexit handler. The child process will NOT be launched when running the robot code in  | 
 | 42 | +    # simulation or unit testing mode  | 
 | 43 | + | 
 | 44 | +    while True:  | 
 | 45 | +        # Tell the CvSink to grab a frame from the camera and put it in the source mat.  If there is an error notify the  | 
 | 46 | +        # output.  | 
 | 47 | + | 
 | 48 | +        if cvSink.grabFrame(mat) == 0:  | 
 | 49 | +            # Send the output the error.  | 
 | 50 | +            outputStream.notifyError(cvSink.getError())  | 
 | 51 | + | 
 | 52 | +            # skip the rest of the current iteration  | 
 | 53 | +            continue  | 
 | 54 | + | 
 | 55 | +        # Put a rectangle on the image  | 
 | 56 | +        mat = cv2.rectangle(  | 
 | 57 | +            img=mat,  | 
 | 58 | +            pt1=(100, 100),  | 
 | 59 | +            pt2=(400, 400),  | 
 | 60 | +            color=rectColor,  | 
 | 61 | +            lineType=5,  | 
 | 62 | +        )  | 
 | 63 | + | 
 | 64 | +        # Give the output stream a new image to display  | 
 | 65 | +        outputStream.putFrame(mat)  | 
0 commit comments