-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v3.0 edits #3743
Merged
Merged
v3.0 edits #3743
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ad45107
moving files around
sguequierre 167e640
make a pr
sguequierre 3584c3a
More edits
sguequierre 4c982f1
fixup
sguequierre f851066
Make changes
sguequierre 0721913
Apply code review feedback:
sguequierre 61cc149
Delete duplicate page
sguequierre 775c488
Merge branch 'v3.0' into v3.0-edits
sguequierre 9725bac
fixup transitions
sguequierre d473919
fixup checks
sguequierre a39e097
test fixups
sguequierre 41a8adc
update table
sguequierre d56cd06
fixup
sguequierre 6b05632
Apply suggestions from code review
sguequierre f0decce
Update docs/data-ai/get-started/capture-sync.md
sguequierre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,148 @@ title: "Act based on inferences" | |
weight: 70 | ||
layout: "docs" | ||
type: "docs" | ||
no_list: true | ||
description: "TODO" | ||
description: "Use the vision service API to act based on inferences." | ||
--- | ||
|
||
You can use the [vision service API](/dev/reference/apis/services/vision/) to get information about your machine's inferences and program behavior based on that. | ||
|
||
## Program a line following robot | ||
|
||
For example, you can [program a line following robot](/tutorials/services/color-detection-scuttle/) that uses a vision service to follow a colored object. | ||
|
||
You can use the following code to detect and follow the location of a colored object: | ||
|
||
{{% expand "click to view code" %}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. capitalization |
||
|
||
```python {class="line-numbers linkable-line-numbers"} | ||
async def connect(): | ||
opts = RobotClient.Options.with_api_key( | ||
# Replace "<API-KEY>" (including brackets) with your machine's API key | ||
api_key='<API-KEY>', | ||
# Replace "<API-KEY-ID>" (including brackets) with your machine's | ||
# API key ID | ||
api_key_id='<API-KEY-ID>' | ||
) | ||
return await RobotClient.at_address("ADDRESS FROM THE VIAM APP", opts) | ||
|
||
|
||
# Get largest detection box and see if it's center is in the left, center, or | ||
# right third | ||
def leftOrRight(detections, midpoint): | ||
largest_area = 0 | ||
largest = {"x_max": 0, "x_min": 0, "y_max": 0, "y_min": 0} | ||
if not detections: | ||
print("nothing detected :(") | ||
return -1 | ||
for d in detections: | ||
a = (d.x_max - d.x_min) * (d.y_max-d.y_min) | ||
if a > largest_area: | ||
a = largest_area | ||
largest = d | ||
centerX = largest.x_min + largest.x_max/2 | ||
if centerX < midpoint-midpoint/6: | ||
return 0 # on the left | ||
if centerX > midpoint+midpoint/6: | ||
return 2 # on the right | ||
else: | ||
return 1 # basically centered | ||
|
||
|
||
async def main(): | ||
spinNum = 10 # when turning, spin the motor this much | ||
straightNum = 300 # when going straight, spin motor this much | ||
numCycles = 200 # run the loop X times | ||
vel = 500 # go this fast when moving motor | ||
|
||
# Connect to robot client and set up components | ||
machine = await connect() | ||
base = Base.from_robot(machine, "my_base") | ||
camera_name = "<camera-name>" | ||
camera = Camera.from_robot(machine, camera_name) | ||
frame = await camera.get_image(mime_type="image/jpeg") | ||
|
||
# Convert to PIL Image | ||
pil_frame = viam_to_pil_image(frame) | ||
|
||
# Grab the vision service for the detector | ||
my_detector = VisionClient.from_robot(machine, "my_color_detector") | ||
|
||
# Main loop. Detect the ball, determine if it's on the left or right, and | ||
# head that way. Repeat this for numCycles | ||
for i in range(numCycles): | ||
detections = await my_detector.get_detections_from_camera(camera_name) | ||
|
||
answer = leftOrRight(detections, pil_frame.size[0]/2) | ||
if answer == 0: | ||
print("left") | ||
await base.spin(spinNum, vel) # CCW is positive | ||
await base.move_straight(straightNum, vel) | ||
if answer == 1: | ||
print("center") | ||
await base.move_straight(straightNum, vel) | ||
if answer == 2: | ||
print("right") | ||
await base.spin(-spinNum, vel) | ||
# If nothing is detected, nothing moves | ||
|
||
await robot.close() | ||
|
||
if __name__ == "__main__": | ||
print("Starting up... ") | ||
asyncio.run(main()) | ||
print("Done.") | ||
``` | ||
|
||
{{% /expand%}} | ||
|
||
If you configured the color detector to detect red in the Viam app, your rover should detect and navigate towards any red objects that come into view of its camera. | ||
Use something like a red sports ball or book cover as a target to follow to test your rover: | ||
|
||
<div class="aligncenter"> | ||
{{<video webm_src="https://storage.googleapis.com/docs-blog/tutorials/videos/scuttledemos_colordetection.webm" mp4_src="https://storage.googleapis.com/docs-blog/tutorials/videos/scuttledemos_colordetection.mp4" poster="/tutorials/scuttlebot/scuttledemos_colordetection.jpg" alt="Detecting color with a Scuttle Robot">}} | ||
</div> | ||
|
||
## Act in industrial applications | ||
|
||
You can also act based on inferences in an industrial context. | ||
For example, you can program a robot arm to halt operations when workers enter dangerous zones, preventing potential accidents. | ||
|
||
The code for this would look like: | ||
|
||
```python {class="line-numbers linkable-line-numbers"} | ||
detections = await detector.get_detections_from_camera(camera_name) | ||
for d in detections: | ||
if d.confidence > 0.6 and d.class_name == "PERSON": | ||
arm.stop() | ||
``` | ||
|
||
You can also use inferences of computer vision for quality assurance purposes. | ||
For example, you can program a robot arm doing automated harvesting to use vision to identify ripe produce and pick crops selectively. | ||
|
||
The code for this would look like: | ||
|
||
```python {class="line-numbers linkable-line-numbers"} | ||
classifications = await detector.get_classifications_from_camera( | ||
camera_name, | ||
4) | ||
for c in classifications: | ||
if d.confidence > 0.6 and d.class_name == "RIPE": | ||
arm.pick() | ||
``` | ||
|
||
To get inferences programmatically, you will want to use the vision service API: | ||
|
||
{{< cards >}} | ||
{{% card link="/dev/reference/apis/services/vision/" customTitle="Vision service API" noimage="True" %}} | ||
{{< /cards >}} | ||
|
||
To implement industrial solutions in code, you can also explore the following component APIs: | ||
|
||
{{< cards >}} | ||
{{< card link="/dev/reference/apis/components/arm/" customTitle="Arm API" noimage="True" >}} | ||
{{< card link="/dev/reference/apis/components/base/" customTitle="Base API" noimage="True" >}} | ||
{{< card link="/dev/reference/apis/components/camera/" customTitle="Camera API" noimage="True" >}} | ||
{{< card link="/dev/reference/apis/components/gripper/" customTitle="Gripper API" noimage="True" >}} | ||
{{< card link="/dev/reference/apis/components/motor/" customTitle="Motor API" noimage="True" >}} | ||
{{< card link="/dev/reference/apis/components/sensor/" customTitle="Sensor API" noimage="True" >}} | ||
{{< /cards >}} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think worth you chatting with Jack and Tahiya about whether we want to introduce the vision service here as one of many possible services you could use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also needs more work in intro to explain that we'll show a few examples here only