|
| 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 | +This is a demo program showing the use of OpenCV to do vision processing. The image is acquired |
| 9 | +from an HTTP camera, then a rectangle is put on the image and sent to the dashboard. OpenCV has |
| 10 | +many methods for different types of processing. |
| 11 | +""" |
| 12 | + |
| 13 | +import ntcore |
| 14 | +import numpy |
| 15 | +import cscore |
| 16 | +from cscore import CameraServer |
| 17 | +import cv2 |
| 18 | + |
| 19 | + |
| 20 | +# |
| 21 | +# This code will work both on a RoboRIO and on other platforms. The exact mechanism |
| 22 | +# to run it differs depending on whether you’re on a RoboRIO or a coprocessor |
| 23 | +# |
| 24 | +# https://robotpy.readthedocs.io/en/stable/vision/code.html |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + # Create an HTTP camera. The address will need to be modified to have the |
| 29 | + # correct team number. The exact path will depend on the source. |
| 30 | + camera = cscore.HttpCamera("HTTP Camera", "http://10.x.y.11/video/stream.mjpg") |
| 31 | + # Start capturing images |
| 32 | + CameraServer.startAutomaticCapture(camera) |
| 33 | + # Set the resolution |
| 34 | + camera.setResolution(640, 480) |
| 35 | + |
| 36 | + # Get a CvSink. This will capture Mats from the camera |
| 37 | + cvSink = CameraServer.getVideo() |
| 38 | + |
| 39 | + # Setup a CvSource. This will send images back to the Dashboard |
| 40 | + outputStream = CameraServer.putVideo("Rectangle", 640, 480) |
| 41 | + |
| 42 | + # Mats are very memory expensive. Lets reuse this Mat. |
| 43 | + mat = numpy.zeros((480, 640, 3), dtype="uint8") |
| 44 | + |
| 45 | + # Declare the color of the rectangle |
| 46 | + rectColor = (255, 255, 255) |
| 47 | + |
| 48 | + # The camera code will be killed when the robot.py program exits. If you wish to perform cleanup, |
| 49 | + # you should register an atexit handler. The child process will NOT be launched when running the robot code in |
| 50 | + # simulation or unit testing mode |
| 51 | + |
| 52 | + while True: |
| 53 | + # Tell the CvSink to grab a frame from the camera and put it in the source mat. If there is an error notify the |
| 54 | + # output. |
| 55 | + |
| 56 | + if cvSink.grabFrame(mat) == 0: |
| 57 | + # Send the output the error. |
| 58 | + outputStream.notifyError(cvSink.getError()) |
| 59 | + |
| 60 | + # skip the rest of the current iteration |
| 61 | + continue |
| 62 | + |
| 63 | + # Put a rectangle on the image |
| 64 | + mat = cv2.rectangle( |
| 65 | + img=mat, |
| 66 | + pt1=(100, 100), |
| 67 | + pt2=(400, 400), |
| 68 | + color=rectColor, |
| 69 | + lineType=5, |
| 70 | + ) |
| 71 | + |
| 72 | + # Give the output stream a new image to display |
| 73 | + outputStream.putFrame(mat) |
0 commit comments