Skip to content

Commit 4e7b525

Browse files
authored
Merge pull request #128 from robotpy/revert-126-RemoveAxisCamera
Revert "Remove Deprecated Axis Camera Example"
2 parents 961251f + 67dc2b1 commit 4e7b525

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

HttpCamera/robot.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
def robotInit(self):
15+
# Your image processing code will be launched via a stub that will set up logging and initialize NetworkTables
16+
# to talk to your robot code.
17+
# https://robotpy.readthedocs.io/en/stable/vision/roborio.html#important-notes
18+
19+
wpilib.CameraServer.launch("vision.py:main")

HttpCamera/vision.py

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

run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ BASE_TESTS="
3131
HatchbotInlined
3232
HatchbotTraditional
3333
HidRumble
34+
HttpCamera
3435
I2CCommunication
3536
IntermediateVision
3637
MagicbotSimple

0 commit comments

Comments
 (0)