This commit is contained in:
2025-02-19 19:57:21 +08:00
parent 9eb3879aab
commit 994058a339
4 changed files with 80 additions and 29 deletions

View File

@@ -11,7 +11,7 @@
#include <pthread.h> // 引入 pthread 库
// 宏定义
#define USE_FEC
// #define USE_FEC
#ifdef USE_FEC
#define FEC_SIZE 32 // 前向纠错冗余数据大小
#else
@@ -202,6 +202,8 @@ static PyObject *serial_send(PyObject *self, PyObject *args) {
// 拷贝该帧对应数据段
memcpy(send_buffer + HEADER_SIZE, data + offset, data_len);
printf("Received frame: seq=%d, len=%d\r\n", frame_counter, data_len);
// 计算 CRC32 校验和
uint32_t crc = calculate_crc32(send_buffer, HEADER_SIZE + DATA_SIZE + FEC_SIZE);
memcpy(send_buffer + HEADER_SIZE + DATA_SIZE + FEC_SIZE, &crc, CHECKSUM_SIZE);

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.8 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -1,43 +1,92 @@
import serial_module
import cv2
import time
import threading
# 全局变量
frame = None
frame_lock = threading.Lock()
thread_started = False
# 摄像头读取线程
def cap_thread():
global frame, thread_started
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open camera.")
return
# 标记线程已启动
thread_started = True
while True:
time_now = time.time()
ret, temp_frame = cap.read()
if ret:
with frame_lock:
# 在图像上打印时间
# print(f"Time: {time_now:.2f}")
cv2.putText(temp_frame, f"{time_now:.2f}", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 4)
frame = temp_frame
else:
print("Error: Failed to read frame from camera.")
time.sleep(0.1) # 短暂休眠,避免频繁尝试读取
# 初始化串口
serial_module.init("/dev/ttyUSB0")
cap = cv2.VideoCapture(0)
# 启动摄像头读取线程
threading.Thread(target=cap_thread, daemon=True).start()
# while True:
for _ in range(100):
# 等待子线程初始化完成
while not thread_started:
time.sleep(0.1)
# 主线程处理逻辑
# for _ in range(100):
while True:
time_via = time.time()
ret, frame = cap.read()
frame = cv2.resize(frame, (224,224))
cv2.imshow('frame',frame)
frame_jpg = cv2.imencode('.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 50])[1].tobytes()
print(len(frame_jpg))
# 检查帧是否可用
with frame_lock:
if frame is None:
# print('frame is None')
time.sleep(0.1) # 短暂休眠,避免频繁检测
continue
# 调整帧大小
try:
frame_resized = cv2.resize(frame, (224, 224))
except Exception as e:
print(f"Error resizing frame: {e}")
continue
# 编码帧为 JPEG 格式
try:
frame_jpg = cv2.imencode('.jpg', frame_resized, [int(cv2.IMWRITE_JPEG_QUALITY), 50])[1].tobytes()
except Exception as e:
print(f"Error encoding frame: {e}")
continue
# 打印帧大小并发送数据
print(f"Frame size: {len(frame_jpg)} bytes")
serial_module.send(frame_jpg)
cv2.imwrite('test_send.jpg', frame)
# # 创建一个大小为 3.5K 的二进制数据
# file_size = int(3.5 * 1024)
# empty_binary_data = bytearray(file_size)
# empty_binary_data = bytes(empty_binary_data)
# # 发送数据
# serial_module.send(empty_binary_data)
# 保存帧到文件(用于调试)
try:
cv2.imwrite('test_send.jpg', frame_resized)
except Exception as e:
print(f"Error saving frame: {e}")
print(time.time() - time_via)
time.sleep(4)
# 打印时间消耗
print(f"Time taken: {time.time() - time_via:.3f} seconds")
# 控制循环频率
# time.sleep(4)
# 按 'q' 键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# # 发送数据
# data = b"Your binary data here" # 替换为你的二进制数据
# serial_module.send(data)
# # 接收数据
# result = serial_module.receive()
# if result:
# frame_number, pdu, rssi = result
# print(f"Frame Number: {frame_number}")
# print(f"PDU: {pdu}")
# print(f"RSSI: {rssi}")
# 释放资源
cv2.destroyAllWindows()