Skip to content

Commit e224275

Browse files
authored
DOCS-3284: Add a basic python example to get_image (#3992)
1 parent 2239b1b commit e224275

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

.github/workflows/update_sdk_methods.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,11 +1042,18 @@ def parse(type, names):
10421042
## Determine parameter type:
10431043
param_type = parameter_tag.find_all('span', class_='n')[1].text
10441044

1045+
param_default = parameter_tag.select_one('span.default_value')
1046+
if param_default:
1047+
param_default = param_default.text
1048+
else:
1049+
param_default = None
1050+
1051+
if param_default == "''": # Check for empty string default
1052+
this_method_parameters_dict["optional"] = True
10451053
## Determine if this parameter is optional, and strip off ' | None' syntax if so:
1046-
if param_type.endswith(' | None'):
1054+
elif param_type.endswith(' | None'):
10471055
this_method_parameters_dict["optional"] = True
10481056
param_type = param_type.replace(' | None', "")
1049-
10501057
else:
10511058
this_method_parameters_dict["optional"] = False
10521059

static/include/components/apis/generated/camera.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ If the server does not know how to return the specified MIME type, the server re
99

1010
**Parameters:**
1111

12-
- `mime_type` ([str](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str)) (required): The desired mime type of the image. This does not guarantee output type.
12+
- `mime_type` ([str](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str)) (optional): The desired mime type of the image. This does not guarantee output type.
1313
- `extra` (Mapping[[str](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str), Any]) (optional): Extra options to pass to the underlying RPC call.
1414
- `timeout` ([float](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.
1515

@@ -19,6 +19,16 @@ If the server does not know how to return the specified MIME type, the server re
1919

2020
**Example:**
2121

22+
```python {class="line-numbers linkable-line-numbers"}
23+
my_camera = Camera.from_robot(machine, "my_camera")
24+
frame = await my_camera.get_image()
25+
print(f"Frame: {frame}")
26+
```
27+
28+
If the `mime_type` of your image is `image/vnd.viam.dep`, pass the returned image data to the Viam Python SDK's [`ViamImage.bytes_to_depth_array()`](https://python.viam.dev/autoapi/viam/media/video/index.html#viam.media.video.ViamImage.bytes_to_depth_array) method to decode the raw image data to a standard 2D image representation.
29+
30+
For example:
31+
2232
```python {class="line-numbers linkable-line-numbers"}
2333
from viam.media.video import CameraMimeType
2434

@@ -32,8 +42,6 @@ frame = await my_camera.get_image(mime_type = CameraMimeType.VIAM_RAW_DEPTH)
3242
standard_frame = frame.bytes_to_depth_array()
3343
```
3444

35-
If the `mime_type` of your image is `image/vnd.viam.dep`, pass the returned image data to the Viam Python SDK's [`ViamImage.bytes_to_depth_array()`](https://python.viam.dev/autoapi/viam/media/video/index.html#viam.media.video.ViamImage.bytes_to_depth_array) method to decode the raw image data to a standard 2D image representation.
36-
3745
In addition, the Python SDK provides the helper functions `viam_to_pil_image` and `pil_to_viam_image` to decode the `ViamImage` into a [`PIL Image`](https://omz-software.com/pythonista/docs/ios/Image.html) and vice versa.
3846

3947
For example:
@@ -56,6 +64,9 @@ cropped_pil_frame = pil_frame.crop((0, 0, x / 2.5, y))
5664
cropped_frame = pil_to_viam_image(cropped_pil_frame, frame.mime_type)
5765
```
5866

67+
For documentation on available MIME types, see [`CameraMimeType`](https://python.viam.dev/autoapi/viam/media/video/index.html#viam.media.video.CameraMimeType).
68+
For more information on working with `ViamImage`, see [`ViamImage`](https://python.viam.dev/autoapi/viam/media/video/index.html#viam.media.video.ViamImage).
69+
5970
{{% alert title="Tip" color="tip" %}}
6071

6172
Be sure to close the image when finished.

static/include/components/apis/overrides/methods/python.camera.get_image.after.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
If the `mime_type` of your image is `image/vnd.viam.dep`, pass the returned image data to the Viam Python SDK's [`ViamImage.bytes_to_depth_array()`](https://python.viam.dev/autoapi/viam/media/video/index.html#viam.media.video.ViamImage.bytes_to_depth_array) method to decode the raw image data to a standard 2D image representation.
22

3+
For example:
4+
5+
```python {class="line-numbers linkable-line-numbers"}
6+
from viam.media.video import CameraMimeType
7+
8+
my_camera = Camera.from_robot(robot=machine, name="my_camera")
9+
10+
# Assume "frame" has a mime_type of "image/vnd.viam.dep"
11+
frame = await my_camera.get_image(mime_type = CameraMimeType.VIAM_RAW_DEPTH)
12+
13+
# Convert "frame" to a standard 2D image representation.
14+
# Remove the 1st 3x8 bytes and reshape the raw bytes to List[List[Int]].
15+
standard_frame = frame.bytes_to_depth_array()
16+
```
17+
318
In addition, the Python SDK provides the helper functions `viam_to_pil_image` and `pil_to_viam_image` to decode the `ViamImage` into a [`PIL Image`](https://omz-software.com/pythonista/docs/ios/Image.html) and vice versa.
419

520
For example:
@@ -22,6 +37,9 @@ cropped_pil_frame = pil_frame.crop((0, 0, x / 2.5, y))
2237
cropped_frame = pil_to_viam_image(cropped_pil_frame, frame.mime_type)
2338
```
2439

40+
For documentation on available MIME types, see [`CameraMimeType`](https://python.viam.dev/autoapi/viam/media/video/index.html#viam.media.video.CameraMimeType).
41+
For more information on working with `ViamImage`, see [`ViamImage`](https://python.viam.dev/autoapi/viam/media/video/index.html#viam.media.video.ViamImage).
42+
2543
{{% alert title="Tip" color="tip" %}}
2644

2745
Be sure to close the image when finished.

0 commit comments

Comments
 (0)