Skip to main content

Ubuntu System


📸 Camera Usage Method

The camera uses the UVC protocol. Languages that support the UVC protocol, such as C++ or Python, can be used for camera functions. Using Python on Ubuntu to call a UVC camera for video recording can be achieved through the OpenCV library. UVC (USB Video Class) cameras typically support standard video input protocols and are compatible with most operating systems. Using OpenCV, we can easily capture video and save it to a file.

Step 1: Install Dependencies

First, ensure you have OpenCV installed. Execute the following command in the terminal to install it:

sudo apt update
sudo apt install python3-opencv

Step 2: Write Python Code for Video Recording

Using OpenCV makes it easy to capture video and save it to a file. Here is a simple example showing how to call a UVC camera via Python for video recording:

import cv2

# Open the camera (default camera, 0 is the first camera, if multiple exist, change to 1, 2, etc.)
cap = cv2.VideoCapture(0)

if not cap.isOpened():
print("Cannot open camera")
exit()

# Get the camera's resolution (width and height)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Define the video codec format, XVID is a common video encoding method
fourcc = cv2.VideoWriter_fourcc(*'XVID')

# Create a VideoWriter object to save the video to a file
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (frame_width, frame_height))

while True:
# Read a video frame
ret, frame = cap.read()

if not ret:
print("Cannot read video frame")
break

# Write the current frame to the output file
out.write(frame)

# Display the current frame
cv2.imshow('Camera Feed', frame)

# Press 'q' to exit recording
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the camera and close all windows
cap.release()
out.release()
cv2.destroyAllWindows()

Code Explanation

  1. Open the camera: cv2.VideoCapture(0) attempts to open the default camera. If you have multiple cameras connected, use other indices (e.g., 1, 2, etc.) to select different cameras.
  2. Get resolution: Obtain the camera's resolution via cap.get(cv2.CAP_PROP_FRAME_WIDTH) and cap.get(cv2.CAP_PROP_FRAME_HEIGHT).
  3. Video codec format: fourcc = cv2.VideoWriter_fourcc(*'XVID') sets the video codec format to XVID, commonly used for AVI format.
  4. Create VideoWriter object: cv2.VideoWriter('output.avi', fourcc, 20.0, (frame_width, frame_height)) creates a VideoWriter object for saving video frames to a file. Parameters include the filename, encoding method, frame rate, and resolution.
  5. Video capture and display: Capture video data frame by frame via cap.read(), and write each frame to the video file. cv2.imshow('Camera Feed', frame) displays the real-time video.
  6. Key press to exit: Press the 'q' key to exit video recording.
  7. Resource release: cap.release() and out.release() release the camera and video writing resources respectively. cv2.destroyAllWindows() closes all display windows.

Step 3: Run the Code

Save the code as a .py file (e.g., record_video.py), then run it in the terminal:

python3 record_video.py

Step 4: Operation

Key Operations

  1. Press the 'q' key to exit recording.
  2. The recorded video will be saved as an output.avi file.

Advanced Operations

  1. Resolution setting: You can manually set the camera's resolution. For example:
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

This will make the camera capture video at 1280x720.

  1. Other video codec formats: You can use different video codec formats to save the file, for example:

  2. fourcc = cv2.VideoWriter_fourcc(*'MJPG') (suitable for .avi files)

  3. fourcc = cv2.VideoWriter_fourcc(*'H264') (suitable for .mp4 files)

  4. Adjust frame rate: When creating the VideoWriter, you can adjust the frame rate, for example:

out = cv2.VideoWriter('output.avi', fourcc, 30.0, (frame_width, frame_height))

This sets the video frame rate to 30 frames per second.


Summary

Through OpenCV, you can easily use Python on Ubuntu to call a UVC camera for video recording. This method is simple and efficient and allows for further customization as needed. If more efficient video processing or further image processing operations are required, you can also combine it with other computer vision technologies for optimization.

🎙 Microphone Usage Method

Check Devices

View the list of microphone devices to check if the device is properly installed. Check parameter information, such as supported sample rate, number of channels, sound card position. To prevent device permission issues, authorize device access.

arecord -l
sudo chmod 777 /dev/snd/*

Sound Recording

Using a USB protocol microphone for sound recording under Ubuntu can usually be achieved by calling the pyaudio or sounddevice library via Python. Below are example codes using these two libraries to help you record audio via a USB microphone.

Method 1: Using the pyaudio library

  1. Install Dependencies

First, install the pyaudio library. It can be installed via pip:

sudo apt update
sudo apt install portaudio19-dev
pip3 install pyaudio
  1. Recording with pyaudio
import pyaudio
import wave

# Set parameters
FORMAT = pyaudio.paInt16 # Sample format
CHANNELS = 1 # Mono channel
RATE = 44100 # Sample rate, check based on the specific supported sample rate of the device
CHUNK = 1024 # Size of each audio chunk read
RECORD_SECONDS = 5 # Recording duration (seconds)
OUTPUT_FILENAME = "output.wav" # Output filename

# Initialize PyAudio
p = pyaudio.PyAudio()

# Get the list of available microphone devices
device_count = p.get_device_count()
print("Available device count:", device_count)

# Print all device information to find the USB microphone
for i in range(device_count):
info = p.get_device_info_by_index(i)
print(f"Device {i}: {info['name']}")

# Select the appropriate microphone device, assume the USB microphone index is 1
device_index = 1

# Open the microphone device
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index=device_index,
frames_per_buffer=CHUNK)

print("Recording started...")

frames = []

# Record audio
for _ in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)

print("Recording ended...")

# Stop the recording stream and close it
stream.stop_stream()
stream.close()
p.terminate()

# Save the recording as a WAV file
with wave.open(OUTPUT_FILENAME, 'wb') as wf:
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))

print(f"Recording saved as {OUTPUT_FILENAME}")

Code Explanation

  1. Initialize PyAudio: pyaudio.PyAudio() initializes the audio stream.
  2. Get device list: Use get_device_count() to get the number of devices, and get_device_info_by_index() to get detailed information about each device. Find the index of the USB microphone from the output information.
  3. Select device: Select the USB microphone based on the device index. If you have multiple microphones, specify the device by its index.
  4. Record audio: Read audio data from the microphone via stream.read(), reading CHUNK-sized data each time, for a duration of RECORD_SECONDS.
  5. Save as WAV file: Save the recorded audio data as a .wav file via wave.open().

Method 2: Using the sounddevice library

  1. Install Dependencies
pip3 install sounddevice numpy
  1. Recording with sounddevice
import sounddevice as sd
import numpy as np
import wave

# Set parameters
RATE = 44100 # Sample rate
CHANNELS = 1 # Mono channel
RECORD_SECONDS = 5 # Recording duration (seconds)
OUTPUT_FILENAME = "output.wav" # Output filename

# Get available devices on the system
print("Available microphone device list:")
print(sd.query_devices())

# Select the device index for the USB microphone
device_index = 1 # Assume the USB microphone index is 1

# Start recording
print("Recording started...")
audio_data = sd.rec(int(RATE * RECORD_SECONDS), samplerate=RATE, channels=CHANNELS, dtype='int16', device=device_index)
sd.wait() # Wait for the recording to finish
print("Recording ended...")

# Save the recording as a WAV file
with wave.open(OUTPUT_FILENAME, 'wb') as wf:
wf.setnchannels(CHANNELS)
wf.setsampwidth(2) # 16-bit sample width
wf.setframerate(RATE)
wf.writeframes(audio_data.tobytes())

print(f"Recording saved as {OUTPUT_FILENAME}")

Code Explanation

  1. Get device list: sd.query_devices() lists all audio devices on the system, allowing you to find the device index of the USB microphone.
  2. Select device: Choose the appropriate microphone device via device_index.
  3. Recording: Use sd.rec() to record audio for a duration of RECORD_SECONDS seconds.
  4. Save as WAV file: Save the recorded audio data in WAV format.

Advanced Operations

  1. Set sample rate and channels: You can adjust the sample rate (RATE) and number of channels (CHANNELS) as needed, for example, use 2 channels for stereo recording.
  2. Adjust microphone device index: If your computer has multiple microphones, you can select the correct device index via the output of sd.query_devices() or pyaudio's device list.

Summary

Through pyaudio or sounddevice, you can easily call a USB protocol microphone for sound recording under Ubuntu. These libraries provide simple interfaces to access system audio devices and can save the recorded audio in common formats (like WAV). If further audio processing is needed, you can combine them with other libraries (like numpy or scipy) for more complex analysis and processing.

📢 Speaker Usage Method

  • Check if the speaker is properly installed. If the status shows off, turn on the external sound.
alsamixer -c 1
  • Set the speaker to unmute and set the volume
amixer -c 1 sset 'Headphone' 100% unmute

🖥️ Screen Display Usage Method

Same as Android, implement the UI application within the Ubuntu system. Also, pay attention to the actual effective display area issue.