35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
import cv2
|
||
import numpy as np
|
||
|
||
cap = cv2.VideoCapture('/dev/video0', cv2.CAP_V4L2)
|
||
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('B', 'G', 'R', '3'))
|
||
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
|
||
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
|
||
|
||
ret, frame = cap.read()
|
||
if ret:
|
||
print(f"Captured frame shape: {frame.shape}")
|
||
|
||
# 解决形状问题,reshape frames to (720, 1280, 3)
|
||
frame = frame.reshape((720, 1280, 3)) # ensure the correct shape if frame is 1D
|
||
|
||
# 确保数据是有效的8位无符号整型
|
||
if frame.dtype != np.uint8:
|
||
frame = frame.astype(np.uint8)
|
||
|
||
# 输出调试信息
|
||
print(f"Frame dtype: {frame.dtype}, Frame shape after reshape: {frame.shape}")
|
||
|
||
# 尝试保存为JPEG
|
||
cv2.imwrite("hdmi_frame.jpg", frame)
|
||
print("Frame saved as JPEG")
|
||
|
||
# 可选:保存RAW数据
|
||
with open("hdmi_frame.raw", "wb") as f:
|
||
f.write(frame.tobytes())
|
||
print("RAW data saved")
|
||
else:
|
||
print("Capture failed")
|
||
|
||
cap.release()
|