Skip to content

Commit bb06e29

Browse files
v3.0 edits (#3743)
Co-authored-by: Naomi Pentrel <[email protected]>
1 parent 5250fe6 commit bb06e29

27 files changed

+3445
-169
lines changed

assets/services/data/time-series.png

40.7 KB
Loading

docs/data-ai/_index.md

+10
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ no_list: true
88
open_on_desktop: true
99
overview: true
1010
---
11+
12+
<!-- TODO: make this into a proper page, just wanted to save some info -->
13+
14+
Machine learning (ML) provides your machines with the ability to adjust their behavior based on models that recognize patterns or make predictions.
15+
16+
Common use cases include:
17+
18+
- Object detection, which enables machines to detect people, animals, plants, or other objects with bounding boxes, and to perform actions when they are detected.
19+
- Object classification, which enables machines to separate people, animals, plants, or other objects into predefined categories based on their characteristics, and to perform different actions based on the classes of objects.
20+
- Speech recognition, natural language processing, and speech synthesis, which enable machines to verbally communicate with us.

docs/data-ai/ai/act.md

+144-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,148 @@ title: "Act based on inferences"
44
weight: 70
55
layout: "docs"
66
type: "docs"
7-
no_list: true
8-
description: "TODO"
7+
description: "Use the vision service API to act based on inferences."
98
---
9+
10+
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.
11+
12+
## Program a line following robot
13+
14+
For example, you can [program a line following robot](/tutorials/services/color-detection-scuttle/) that uses a vision service to follow a colored object.
15+
16+
You can use the following code to detect and follow the location of a colored object:
17+
18+
{{% expand "click to view code" %}}
19+
20+
```python {class="line-numbers linkable-line-numbers"}
21+
async def connect():
22+
opts = RobotClient.Options.with_api_key(
23+
# Replace "<API-KEY>" (including brackets) with your machine's API key
24+
api_key='<API-KEY>',
25+
# Replace "<API-KEY-ID>" (including brackets) with your machine's
26+
# API key ID
27+
api_key_id='<API-KEY-ID>'
28+
)
29+
return await RobotClient.at_address("ADDRESS FROM THE VIAM APP", opts)
30+
31+
32+
# Get largest detection box and see if it's center is in the left, center, or
33+
# right third
34+
def leftOrRight(detections, midpoint):
35+
largest_area = 0
36+
largest = {"x_max": 0, "x_min": 0, "y_max": 0, "y_min": 0}
37+
if not detections:
38+
print("nothing detected :(")
39+
return -1
40+
for d in detections:
41+
a = (d.x_max - d.x_min) * (d.y_max-d.y_min)
42+
if a > largest_area:
43+
a = largest_area
44+
largest = d
45+
centerX = largest.x_min + largest.x_max/2
46+
if centerX < midpoint-midpoint/6:
47+
return 0 # on the left
48+
if centerX > midpoint+midpoint/6:
49+
return 2 # on the right
50+
else:
51+
return 1 # basically centered
52+
53+
54+
async def main():
55+
spinNum = 10 # when turning, spin the motor this much
56+
straightNum = 300 # when going straight, spin motor this much
57+
numCycles = 200 # run the loop X times
58+
vel = 500 # go this fast when moving motor
59+
60+
# Connect to robot client and set up components
61+
machine = await connect()
62+
base = Base.from_robot(machine, "my_base")
63+
camera_name = "<camera-name>"
64+
camera = Camera.from_robot(machine, camera_name)
65+
frame = await camera.get_image(mime_type="image/jpeg")
66+
67+
# Convert to PIL Image
68+
pil_frame = viam_to_pil_image(frame)
69+
70+
# Grab the vision service for the detector
71+
my_detector = VisionClient.from_robot(machine, "my_color_detector")
72+
73+
# Main loop. Detect the ball, determine if it's on the left or right, and
74+
# head that way. Repeat this for numCycles
75+
for i in range(numCycles):
76+
detections = await my_detector.get_detections_from_camera(camera_name)
77+
78+
answer = leftOrRight(detections, pil_frame.size[0]/2)
79+
if answer == 0:
80+
print("left")
81+
await base.spin(spinNum, vel) # CCW is positive
82+
await base.move_straight(straightNum, vel)
83+
if answer == 1:
84+
print("center")
85+
await base.move_straight(straightNum, vel)
86+
if answer == 2:
87+
print("right")
88+
await base.spin(-spinNum, vel)
89+
# If nothing is detected, nothing moves
90+
91+
await robot.close()
92+
93+
if __name__ == "__main__":
94+
print("Starting up... ")
95+
asyncio.run(main())
96+
print("Done.")
97+
```
98+
99+
{{% /expand%}}
100+
101+
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.
102+
Use something like a red sports ball or book cover as a target to follow to test your rover:
103+
104+
<div class="aligncenter">
105+
{{<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">}}
106+
</div>
107+
108+
## Act in industrial applications
109+
110+
You can also act based on inferences in an industrial context.
111+
For example, you can program a robot arm to halt operations when workers enter dangerous zones, preventing potential accidents.
112+
113+
The code for this would look like:
114+
115+
```python {class="line-numbers linkable-line-numbers"}
116+
detections = await detector.get_detections_from_camera(camera_name)
117+
for d in detections:
118+
if d.confidence > 0.6 and d.class_name == "PERSON":
119+
arm.stop()
120+
```
121+
122+
You can also use inferences of computer vision for quality assurance purposes.
123+
For example, you can program a robot arm doing automated harvesting to use vision to identify ripe produce and pick crops selectively.
124+
125+
The code for this would look like:
126+
127+
```python {class="line-numbers linkable-line-numbers"}
128+
classifications = await detector.get_classifications_from_camera(
129+
camera_name,
130+
4)
131+
for c in classifications:
132+
if d.confidence > 0.6 and d.class_name == "RIPE":
133+
arm.pick()
134+
```
135+
136+
To get inferences programmatically, you will want to use the vision service API:
137+
138+
{{< cards >}}
139+
{{% card link="/dev/reference/apis/services/vision/" customTitle="Vision service API" noimage="True" %}}
140+
{{< /cards >}}
141+
142+
To implement industrial solutions in code, you can also explore the following component APIs:
143+
144+
{{< cards >}}
145+
{{< card link="/dev/reference/apis/components/arm/" customTitle="Arm API" noimage="True" >}}
146+
{{< card link="/dev/reference/apis/components/base/" customTitle="Base API" noimage="True" >}}
147+
{{< card link="/dev/reference/apis/components/camera/" customTitle="Camera API" noimage="True" >}}
148+
{{< card link="/dev/reference/apis/components/gripper/" customTitle="Gripper API" noimage="True" >}}
149+
{{< card link="/dev/reference/apis/components/motor/" customTitle="Motor API" noimage="True" >}}
150+
{{< card link="/dev/reference/apis/components/sensor/" customTitle="Sensor API" noimage="True" >}}
151+
{{< /cards >}}

docs/data-ai/ai/advanced/conditional-sync.md

-9
This file was deleted.

0 commit comments

Comments
 (0)