Skip to content

Commit 019de4c

Browse files
Suave101virtuald
andauthored
Add AxisCamera sample (#103)
Addresses issue #49 [Axis Camera Sample](https://github.com/wpilibsuite/allwpilib/tree/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/axiscamera) necessity! Could someone with a rio test the Axis Camera code because, I do not have a rio or an axis camera currently. --------- Co-authored-by: Dustin Spicuzza <[email protected]>
1 parent e4aced8 commit 019de4c

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

AxisCamera/robot.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) FIRST and other WPILib contributors.
4+
# Open Source Software; you can modify and/or share it under the terms of
5+
# the WPILib BSD license file in the root directory of this project.
6+
#
7+
8+
9+
import wpilib
10+
import wpilib.cameraserver
11+
12+
13+
class MyRobot(wpilib.TimedRobot):
14+
"""
15+
This is a demo program showing the use of OpenCV to do vision processing. The image is acquired
16+
from the Axis camera, then a rectangle is put on the image and sent to the dashboard. OpenCV has
17+
many methods for different types of processing.
18+
"""
19+
20+
def robotInit(self):
21+
# Your image processing code will be launched via a stub that will set up logging and initialize NetworkTables
22+
# to talk to your robot code.
23+
# https://robotpy.readthedocs.io/en/stable/vision/roborio.html#important-notes
24+
25+
wpilib.CameraServer.launch("vision.py:main")

AxisCamera/vision.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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)

run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ BASE_TESTS="
99
ArcadeDrive
1010
ArcadeDriveXboxController
1111
ArmSimulation
12+
AxisCamera
1213
CANPDP
1314
DifferentialDriveBot
1415
DigitalCommunication

0 commit comments

Comments
 (0)