update
This commit is contained in:
105
test_send.py
105
test_send.py
@@ -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()
|
||||
Reference in New Issue
Block a user