树莓派5项目备份

因为动的地方有三点,

1、固定ip为192.168.2.2
2、开机自启动程序放在/home/pi/record.py,设置开机自启动。/etc/rc.local文件为下

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
sudo  python3 /home/pi/record.py &
exit 0

3/recoding程序为以下。注意代码要在电脑编辑好移动到树莓派,要不然可能不符合utf-8报错。

import cv2
import numpy as np
import socket
import pyaudio
import threading

# 视频设置
camera = cv2.VideoCapture(2)  # 使用默认摄像头

# 音频设置
chunk = 1024
format = pyaudio.paInt16
channels = 1
rate = 44100

# 网络设置
target_ip = '192.168.2.1'  # 目标IP地址
target_port_video = 9092
target_port_audio = 9093
client_socket_video = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket_audio = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# 初始化PyAudio
audio = pyaudio.PyAudio()
stream = audio.open(format=format, channels=channels, rate=rate, input=True, frames_per_buffer=chunk)

def send_video():
    while True:
        ret, frame = camera.read()
        if not ret:
            break

        # 调整JPEG压缩质量以减小视频帧的大小
        encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
        _, video_data = cv2.imencode('.jpg', frame, encode_param)
        video_bytes = video_data.tobytes()

        # 确保数据包大小不超过UDP限制
        if len(video_bytes) <= 65507:
            client_socket_video.sendto(video_bytes, (target_ip, target_port_video))
        else:
            print("Frame too large to send over UDP")

def send_audio():
    while True:
        audio_data = stream.read(chunk, exception_on_overflow=False)
        client_socket_audio.sendto(audio_data, (target_ip, target_port_audio))

try:
    video_thread = threading.Thread(target=send_video)
    audio_thread = threading.Thread(target=send_audio)

    video_thread.start()
    audio_thread.start()

    video_thread.join()
    audio_thread.join()

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    if camera.isOpened():
        camera.release()

    stream.stop_stream()
    stream.close()
    audio.terminate()
    client_socket_video.close()
    client_socket_audio.close()

ssh软件:MobaXterm       MobaXterm Xserver with SSH, telnet, RDP, VNC and X11 - Download

链接:https://pan.baidu.com/s/1ArefeyduYORP1N-a9Ck0GQ?pwd=y04g 
提取码:y04g 
--来自百度网盘超级会员V4的分享

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值