From 192811c39a256fa27afb92c6cd4b7053dec67baa Mon Sep 17 00:00:00 2001 From: David Vo Date: Mon, 6 Jan 2025 08:40:46 +1100 Subject: [PATCH 1/2] Revert "Remove Deprecated Axis Camera Example (#126)" This reverts commit 961251f1a26097767ca9ae4fa719fcbf2f7f2684. --- AxisCamera/robot.py | 25 +++++++++++++++++ AxisCamera/vision.py | 65 ++++++++++++++++++++++++++++++++++++++++++++ run_tests.sh | 1 + 3 files changed, 91 insertions(+) create mode 100644 AxisCamera/robot.py create mode 100644 AxisCamera/vision.py diff --git a/AxisCamera/robot.py b/AxisCamera/robot.py new file mode 100644 index 00000000..e3bb8376 --- /dev/null +++ b/AxisCamera/robot.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +# +# Copyright (c) FIRST and other WPILib contributors. +# Open Source Software; you can modify and/or share it under the terms of +# the WPILib BSD license file in the root directory of this project. +# + + +import wpilib +import wpilib.cameraserver + + +class MyRobot(wpilib.TimedRobot): + """ + This is a demo program showing the use of OpenCV to do vision processing. The image is acquired + from the Axis camera, then a rectangle is put on the image and sent to the dashboard. OpenCV has + many methods for different types of processing. + """ + + def robotInit(self): + # Your image processing code will be launched via a stub that will set up logging and initialize NetworkTables + # to talk to your robot code. + # https://robotpy.readthedocs.io/en/stable/vision/roborio.html#important-notes + + wpilib.CameraServer.launch("vision.py:main") diff --git a/AxisCamera/vision.py b/AxisCamera/vision.py new file mode 100644 index 00000000..1f4bb5c4 --- /dev/null +++ b/AxisCamera/vision.py @@ -0,0 +1,65 @@ +# +# Copyright (c) FIRST and other WPILib contributors. +# Open Source Software; you can modify and/or share it under the terms of +# the WPILib BSD license file in the root directory of this project. +# + + +import ntcore +import numpy +from cscore import CameraServer +import cv2 + + +# +# This code will work both on a RoboRIO and on other platforms. The exact mechanism +# to run it differs depending on whether you’re on a RoboRIO or a coprocessor +# +# https://robotpy.readthedocs.io/en/stable/vision/code.html + + +def main(): + # Get the Axis camera from CameraServer + camera = CameraServer.addAxisCamera("axis-camera.local") + + # Set the resolution + camera.setResolution(640, 480) + + # Get a CvSink. This will capture Mats from the camera + cvSink = CameraServer.getVideo() + + # Setup a CvSource. This will send images back to the Dashboard + outputStream = CameraServer.putVideo("Rectangle", 640, 480) + + # Mats are very memory expensive. Lets reuse this Mat. + mat = numpy.zeros((480, 640, 3), dtype="uint8") + + # Declare the color of the rectangle + rectColor = (255, 255, 255) + + # The camera code will be killed when the robot.py program exits. If you wish to perform cleanup, + # you should register an atexit handler. The child process will NOT be launched when running the robot code in + # simulation or unit testing mode + + while True: + # Tell the CvSink to grab a frame from the camera and put it in the source mat. If there is an error notify the + # output. + + if cvSink.grabFrame(mat) == 0: + # Send the output the error. + outputStream.notifyError(cvSink.getError()) + + # skip the rest of the current iteration + continue + + # Put a rectangle on the image + mat = cv2.rectangle( + img=mat, + pt1=(100, 100), + pt2=(400, 400), + color=rectColor, + lineType=5, + ) + + # Give the output stream a new image to display + outputStream.putFrame(mat) diff --git a/run_tests.sh b/run_tests.sh index 74526baa..4d5ee432 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -11,6 +11,7 @@ BASE_TESTS=" ArmBot ArmBotOffboard ArmSimulation + AxisCamera CANPDP DifferentialDriveBot DigitalCommunication From 67dc2b13e5c9e343eba1b51338d33c74cca9cf16 Mon Sep 17 00:00:00 2001 From: David Vo Date: Mon, 6 Jan 2025 08:48:46 +1100 Subject: [PATCH 2/2] Convert AxisCamera example to generic HTTP camera --- {AxisCamera => HttpCamera}/robot.py | 6 ------ {AxisCamera => HttpCamera}/vision.py | 14 +++++++++++--- run_tests.sh | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) rename {AxisCamera => HttpCamera}/robot.py (69%) rename {AxisCamera => HttpCamera}/vision.py (77%) diff --git a/AxisCamera/robot.py b/HttpCamera/robot.py similarity index 69% rename from AxisCamera/robot.py rename to HttpCamera/robot.py index e3bb8376..57ce3932 100644 --- a/AxisCamera/robot.py +++ b/HttpCamera/robot.py @@ -11,12 +11,6 @@ class MyRobot(wpilib.TimedRobot): - """ - This is a demo program showing the use of OpenCV to do vision processing. The image is acquired - from the Axis camera, then a rectangle is put on the image and sent to the dashboard. OpenCV has - many methods for different types of processing. - """ - def robotInit(self): # Your image processing code will be launched via a stub that will set up logging and initialize NetworkTables # to talk to your robot code. diff --git a/AxisCamera/vision.py b/HttpCamera/vision.py similarity index 77% rename from AxisCamera/vision.py rename to HttpCamera/vision.py index 1f4bb5c4..5eeff1a6 100644 --- a/AxisCamera/vision.py +++ b/HttpCamera/vision.py @@ -4,9 +4,15 @@ # the WPILib BSD license file in the root directory of this project. # +""" +This is a demo program showing the use of OpenCV to do vision processing. The image is acquired +from an HTTP camera, then a rectangle is put on the image and sent to the dashboard. OpenCV has +many methods for different types of processing. +""" import ntcore import numpy +import cscore from cscore import CameraServer import cv2 @@ -19,9 +25,11 @@ def main(): - # Get the Axis camera from CameraServer - camera = CameraServer.addAxisCamera("axis-camera.local") - + # Create an HTTP camera. The address will need to be modified to have the + # correct team number. The exact path will depend on the source. + camera = cscore.HttpCamera("HTTP Camera", "http://10.x.y.11/video/stream.mjpg") + # Start capturing images + CameraServer.startAutomaticCapture(camera) # Set the resolution camera.setResolution(640, 480) diff --git a/run_tests.sh b/run_tests.sh index 4d5ee432..20c216d8 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -11,7 +11,6 @@ BASE_TESTS=" ArmBot ArmBotOffboard ArmSimulation - AxisCamera CANPDP DifferentialDriveBot DigitalCommunication @@ -32,6 +31,7 @@ BASE_TESTS=" HatchbotInlined HatchbotTraditional HidRumble + HttpCamera I2CCommunication IntermediateVision MagicbotSimple