mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[robotpy][examples] Split examples and snippets (#8944)
This also updates the bazel scripts to behave more like the C++ and Java examples, and updates the copybara scripts to be able to sync up `mostrobotpy`
This commit is contained in:
53
robotpyExamples/snippets/IntermediateVision/vision.py
Normal file
53
robotpyExamples/snippets/IntermediateVision/vision.py
Normal file
@@ -0,0 +1,53 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
#
|
||||
# This is a demo program showing the use of OpenCV to do vision processing. The image is acquired
|
||||
# from the USB camera, then a rectangle is put on the image and sent to the dashboard. OpenCV has
|
||||
# many methods for different types of processing.
|
||||
#
|
||||
# NOTE: This code runs in its own process, so we cannot access the robot here,
|
||||
# nor can we create/use/see wpilib objects
|
||||
#
|
||||
# To try this code out locally (if you have robotpy-cscore installed), you
|
||||
# can execute `python3 -m cscore vision.py:main`
|
||||
#
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from cscore import CameraServer as CS
|
||||
|
||||
|
||||
def main():
|
||||
# Get the UsbCamera from CameraServer
|
||||
camera = CS.startAutomaticCapture()
|
||||
# Set the resolution
|
||||
camera.setResolution(640, 480)
|
||||
|
||||
# Get a CvSink. This will capture images from the camera
|
||||
cvSink = CS.getVideo()
|
||||
# Setup a CvSource. This will send images back to the Dashboard
|
||||
outputStream = CS.putVideo("Rectangle", 640, 480)
|
||||
|
||||
# Mats are very memory expensive. Lets reuse this Mat.
|
||||
mat = np.zeros(shape=(480, 640, 3), dtype=np.uint8)
|
||||
|
||||
while True:
|
||||
# Tell the CvSink to grab a frame from the camera and put it
|
||||
# in the source image. If there is an error notify the output.
|
||||
time, mat = cvSink.grabFrame(mat)
|
||||
if time == 0:
|
||||
# Send the output the error.
|
||||
outputStream.notifyError(cvSink.getError())
|
||||
# skip the rest of the current iteration
|
||||
continue
|
||||
|
||||
# Put a rectangle on the image
|
||||
cv2.rectangle(mat, (100, 100), (400, 400), (255, 255, 255), 5)
|
||||
|
||||
# Give the output stream a new image to display
|
||||
outputStream.putFrame(mat)
|
||||
Reference in New Issue
Block a user