Iām new to programming.
I want to see the RAW format of my laptop camera.
So I am trying to use the python script below to print out the format, but I cannot get result with CAP_MSMF as backend.
I think the code 22
(which converts to \x16\x00\x00\x00
) I got is weird. Can somebody explain about it?
See below:
import cv2
def get_camera_fourcc(camera_index=0):
# Force using MSMF backend (recommended for Windows 10+)
cap = cv2.VideoCapture(camera_index, cv2.CAP_MSMF)
# Get FourCC code (may return integer like 22)
fourcc_code = int(cap.get(cv2.CAP_PROP_FOURCC))
# Convert to FourCC string (e.g., 'MJPG')
fourcc_str = (
chr((fourcc_code >> 0) & 0xFF) +
chr((fourcc_code >> 8) & 0xFF) +
chr((fourcc_code >> 16) & 0xFF) +
chr((fourcc_code >> 24) & 0xFF)
)
print(f"š· Camera index {camera_index} information:")
print(f"š§ Backend: {cap.getBackendName()}")
print(f"šļø FourCC code: {fourcc_str} (numeric: {fourcc_code})")
# Try to read a frame to check actual data format
ret, frame = cap.read()
if ret:
print(f"š¼ļø Image shape: {frame.shape} (H, W, C)")
print(f"šØ Data type: {frame.dtype}")
else:
print("ā ļø Failed to read frame data")
cap.release()
return fourcc_str
if __name__ == "__main__":
camera_index = 1 # This is the HD Webcam on my Windows laptop
fourcc = get_camera_fourcc(camera_index)
if fourcc == "MJPG":
print("ā
Camera uses MJPG (Motion-JPEG) encoding")
elif fourcc == "YUY2":
print("ā
Camera uses YUY2 (uncompressed YUV 4:2:2)")
elif fourcc == "NV12":
print("ā
Camera uses NV12 (YUV 4:2:0)")
else:
print(f"ā Unknown FourCC format: {fourcc}")
Result:
š· Camera index 1 information:
š§ Backend: MSMF
šļø FourCC code: (numeric: 22)
š¼ļø Image shape: (480, 640, 3) (H, W, C)
šØ Data type: uint8
ā Unknown FourCC format: