43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
|
|
import base64
|
|
import socketio
|
|
import time
|
|
from picamera2 import Picamera2
|
|
import numpy as np
|
|
|
|
SERVER_URL = 'http://192.168.114.26:5000' # 修改为你的服务端地址和端口
|
|
|
|
sio = socketio.Client()
|
|
|
|
@sio.event
|
|
def connect():
|
|
print('已连接到服务端')
|
|
sio.emit('register_client', {'type': 'pi_camera'})
|
|
|
|
@sio.event
|
|
def disconnect():
|
|
print('与服务端断开连接')
|
|
|
|
def main():
|
|
sio.connect(SERVER_URL)
|
|
picam = Picamera2()
|
|
config = picam.create_video_configuration(main={'size': (640, 480)})
|
|
picam.configure(config)
|
|
picam.start()
|
|
time.sleep(1) # 等待摄像头启动
|
|
|
|
try:
|
|
while True:
|
|
frame = picam.capture_array()
|
|
# 转为JPEG并编码为base64
|
|
import cv2
|
|
_, buffer = cv2.imencode('.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
|
|
jpg_as_text = base64.b64encode(buffer).decode('utf-8')
|
|
sio.emit('video_frame', {'frame': jpg_as_text})
|
|
time.sleep(1/30) # 控制帧率
|
|
finally:
|
|
picam.close()
|
|
sio.disconnect()
|
|
|
|
if __name__ == '__main__':
|
|
main() |