Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed May 28, 2024
2 parents 4a0daec + 55571bb commit 33eb026
Show file tree
Hide file tree
Showing 20 changed files with 998 additions and 16 deletions.
5 changes: 5 additions & 0 deletions components/maix/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config BUILD_WITH_MAIXPY
bool "Used to indicate that maixpy ​​is compiling"
default y
help
application code can use this macro to do special process when compiling MaixPy.
77 changes: 77 additions & 0 deletions docs/doc/en/audio/play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
title: MaixPy Playback Audio
update:
- date: 2024-05-20
author: lxowalle
version: 1.0.0
content: Initial document
---

## Introduction

This document provides instructions on how to play audio


## How to use

### Hardware operation

![image-20240520134637905](../../../static/image/maixcam_hardware_back.png)

The `MaixCAM` does not have a built-in speaker, so you will need to solder a `1W` speaker yourself. The pins for soldering the speaker are shown in the diagram above on the `VOP` and `VON` pins corresponding to the Speaker.

Note: If the `MaixCAM` has copper posts attached to these pins, they can be soldered directly to the posts, or on the other side of the board for aesthetic reasons.

### Code

Method of playing a PCM file

```python
from maix import audio, time, app

p = audio.Player()

with open('/root/output.pcm', 'rb') as f:
ctx = f.read()

p.play(bytes(ctx))

while not app.need_exit():
time.sleep_ms(10)

print("play finish!")
```

Steps:


1. Import the audio, time and app modules:

```python
from maix import audio, time, app
```

2. Initialize the player:

```python
p = audio.Player()
```
- Note that the default sample rate is 48k, the sample format is little-endian format - signed 16-bit, and the sample channel is 1. You can also customise the parameters like this `p = audio.Player(sample_rate=48000, format=audio.Format.FMT_S16_LE, channel = 1)`. So far only tested with sample rate 48000, format `FMT_S16_LE`, and number of sampling channels 1.

3. Open and playback a PCM file

```python
with open('/root/output.pcm', 'rb') as f:
ctx = f.read()

p.play(bytes(ctx))

while not app.need_exit():
time.sleep_ms(10)
```
- `with open(‘xxx’,‘rb’) as f:` open file `xxx` and get file object `f`
- `ctx = f.read()` reads the contents of the file into `ctx`
- `p.play(bytes(ctx))` plays the audio, `p` is the opened player object, `ctx` is the `PCM` data converted to type bytes
- `time.sleep_ms(10)` Here there is a loop to wait for the playback to complete, as the playback operation is performed asynchronously, and if the program exits early, then it may result in the audio not being played completely.

4. Done
61 changes: 61 additions & 0 deletions docs/doc/en/audio/record.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: MaixPy Audio Record
update:
- date: 2024-05-20
author: lxowalle
version: 1.0.0
content: Initial document
---

## Introduction

This document provides methods for recording

### How to use

An example of a recording

```python
from maix import audio, time, app

r = audio.Recorder()
r.volume(12)
print("sample_rate:{} format:{} channel:{}".format(r.sample_rate(), r.format(), r.channel()))

while not app.need_exit():
data = r.record()
print("data size", len(data))

time.sleep_ms(10)

print("record finish!")
```

Steps:

1. Import the audio, time and app modules:

```python
from maix import audio, time, app
```

2. Initialize Recorder

```python
r = audio.Recorder()
r.volume(12)
```

- Note that the default sample rate is 48k, the sample format is little-endian format - signed 16-bit, and the sample channel is 1. You can also customise the parameters like this `r = audio.Recorder(sample_rate=48000, format=audio.Format.FMT_S16_LE, channel = 1)`. So far only tested with sample rate 48000, format `FMT_S16_LE`, and number of sampling channels 1.

- `r.volume(12)` is used to set the volume, the volume range is [0,24]

3. Start recording

```python
data = r.record()
```

- `data` is `bytes` type data in `PCM` format that holds the currently recorded audio. The `PCM` format is set when initialising the `Recorder` object, see step 2.

4. Done, you can do voice processing on the `PCM` data returned by `r.record()` when doing your own applications.
2 changes: 1 addition & 1 deletion docs/doc/en/sidebar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ items:
label: Play video
- file: video/jpeg_streaming.md
label: JPEG stream
- file: video/rtsp.md
- file: video/rtsp_streaming.md
label: RTSP stream


Expand Down
105 changes: 101 additions & 4 deletions docs/doc/en/video/jpeg_streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ update:
author: neucrack
version: 1.0.0
content: Initial document

- date: 2024-05-20
author: lxowalle
version: 1.0.1
content: update JPEG-HTTP usage
---

## Introduction

Sometimes it is necessary to send images to a server or push video from a camera to a server. Here, we provide the simplest method, which is to compress images into `JPEG` format and send them one by one to the server.
Sometimes it is necessary to send images to a server, or to push video from a webcam to a server, so here are two ways to do it.

- One of the simplest methods is to compress images into `JPEG` format and send them one by one to the server. Note, this is a very basic method and not a formal way to stream video. It is also not suitable for high-resolution, high-frame-rate video streams, as it involves sending images one by one. For more efficient video streaming, please use the `RTSP` or `RTMP` modules discussed later.

Note, this is a very basic method and not a formal way to stream video. It is also not suitable for high-resolution, high-frame-rate video streams, as it involves sending images one by one. For more efficient video streaming, please use the `RTSP` or `RTMP` modules discussed later.
- Set up an HTTP server, so that the PC side can be accessed directly through the browser.

## How to Use
## Methods for pushing streams as a client

```python
from maix import image
Expand Down Expand Up @@ -42,3 +47,95 @@ print(res.text)
```

As you can see, the image is first converted into `JPEG` format, and then the binary data of the `JPEG` image is sent to the server via `TCP`.

## Methods for pushing streams as a server

```python
from maix import camera, time, app, http

html = """<!DOCTYPE html>
<html>
<head>
<title>JPG Stream</title>
</head>
<body>
<h1>MaixPy JPG Stream</h1>
<img src="/stream" alt="Stream">
</body>
</html>"""

cam = camera.Camera(320, 240)
stream = http.JpegStreamer()
stream.set_html(html)
stream.start()

print("http://{}:{}".format(stream.host(), stream.port()))
while not app.need_exit():
t = time.time_ms()
img = cam.read()
jpg = img.to_jpeg()
stream.write(jpg)
print(f"time: {time.time_ms() - t}ms, fps: {1000 / (time.time_ms() - t)}")
```

Steps:

1. Import the image, camera and http modules:

```python
from maix import image, camera, http
```

2. Initialize the camera:

```python
cam = camera.Camera(320, 240)
```


3. Initialize Stream Object

```python
stream = http.JpegStreamer()
stream.start()
```

- `http.JpegStreamer()` is used to create a `JpegStreamer` object, which will start an `http server` that will be used to publish `jpeg` image streams to clients.
- `stream.start()` is used to start the `http server`.

4. Custom html styles (optional)

```python
html = """<!DOCTYPE html>
<html>
<head>
<title>JPG Stream</title>
</head>
<body>
<h1>MaixPy JPG Stream</h1>
<img src="/stream" alt="Stream">
</body>
</html>"""

stream.set_html(html)
```

- `html = xxx` is the `html` code that can be used to customise the style of your web page. Note that the core code is `<img src=‘/stream’ alt=‘Stream’>`, be sure not to miss this line of code.
- `stream.set_html(html)` is used to set the custom `html` code, this step is optional. The default browsing address is `http://device_ip:8000`.

5. Getting images from the camera and pushing streams

```python
while 1:
img = cam.read()
jpg = img.to_jpeg()
stream.write(jpg)
```

- `img = cam.read()` gets an image from the camera, when initialised as `cam = camera.Camera(320, 240)` the `img` object is an RGB image with a resolution of 320x240.
- `jpg = img.to_jpeg()` converts the image to `jpeg` format
- `stream.write(jpg)` writes the image format to the server and the `http` server will send this image to the `http` client.

6. 6. Done, after running the code above, you can see the video stream directly through your browser, the default address is `http://device_ip:8000`. Open your browser and take a look!


Loading

0 comments on commit 33eb026

Please sign in to comment.