BodyBalanceEvaluation/backend/testcamera.py

127 lines
3.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cv2
class CameraViewer:
def __init__(self, device_index=0):
self.device_index = device_index
self.window_name = "Camera Viewer"
def start_stream(self):
cap = cv2.VideoCapture(self.device_index)
if not cap.isOpened():
print(f"无法打开摄像头设备 {self.device_index}")
return
cv2.namedWindow(self.window_name, cv2.WINDOW_NORMAL)
while True:
ret, frame = cap.read()
if not ret:
print("无法获取视频帧")
break
cv2.imshow(self.window_name, frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
# 修改这里的数字可以切换不同摄像头设备
viewer = CameraViewer(device_index=3)
viewer.start_stream()
# import ctypes
# from ctypes import c_int, c_uint16, c_uint8, c_char, c_char_p, Structure, POINTER, byref
# # 设备结构体对应wrapper中FPMS_DEVICE_C
# class FPMS_DEVICE_C(Structure):
# _pack_ = 1
# _fields_ = [
# ("mn", c_uint16),
# ("sn", c_char * 64),
# ("fwVersion", c_uint16),
# ("protoVer", c_uint8),
# ("pid", c_uint16),
# ("vid", c_uint16),
# ("rows", c_uint16),
# ("cols", c_uint16),
# ]
# # 加载DLL
# dll_path = r"D:\BodyBalanceEvaluation\backend\SMiTSenseUsbWrapper.dll"
# dll = ctypes.windll.LoadLibrary(dll_path)
# # 函数原型声明
# # int fpms_usb_init_c(int debugFlag);
# dll.fpms_usb_init_c.argtypes = [c_int]
# dll.fpms_usb_init_c.restype = c_int
# dll.fpms_usb_get_device_list_c.argtypes = [POINTER(FPMS_DEVICE_C), c_int]
# dll.fpms_usb_get_device_list_c.restype = c_int
# dll.fpms_usb_open_c.argtypes = [POINTER(FPMS_DEVICE_C), POINTER(ctypes.c_void_p)]
# dll.fpms_usb_open_c.restype = c_int
# # int fpms_usb_read_frame_c(void* handle, uint16_t* frame);
# dll.fpms_usb_read_frame_c.argtypes = [ctypes.c_void_p, POINTER(c_uint16)]
# dll.fpms_usb_read_frame_c.restype = c_int
# # int fpms_usb_close_c(void* handle);
# dll.fpms_usb_close_c.argtypes = [ctypes.c_void_p]
# dll.fpms_usb_close_c.restype = c_int
# # 其他函数如果需要可以类似声明
# def main():
# # 初始化
# ret = dll.fpms_usb_init_c(0)
# print(f"fpms_usb_init_c 返回值: {ret}")
# if ret != 0:
# print("初始化失败")
# return
# MAX_DEVICES = 8
# devices = (FPMS_DEVICE_C * MAX_DEVICES)() # 创建数组
# count = dll.fpms_usb_get_device_list_c(devices, MAX_DEVICES)
# print(f"设备数量: {count}")
# if count <= 0:
# print("未找到设备或错误")
# return
# for i in range(count):
# dev = devices[i]
# print(f"设备{i}: mn={dev.mn}, sn={dev.sn.decode(errors='ignore').rstrip(chr(0))}, fwVersion={dev.fwVersion}")
# # 打开第一个设备
# handle = ctypes.c_void_p()
# ret = dll.fpms_usb_open_c(byref(devices[0]), byref(handle))
# print(f"fpms_usb_open_c 返回值: {ret}")
# if ret != 0:
# print("打开设备失败")
# return
# # 假设帧大小是 rows * cols
# rows = devices[0].rows
# cols = devices[0].cols
# frame_size = rows * cols
# frame_buffer = (c_uint16 * frame_size)()
# ret = dll.fpms_usb_read_frame_c(handle, frame_buffer)
# print(f"fpms_usb_read_frame_c 返回值: {ret}")
# if ret == 0:
# # 打印前10个数据看看
# print("帧数据前10个点:", list(frame_buffer[:10]))
# else:
# print("读取帧失败")
# # 关闭设备
# ret = dll.fpms_usb_close_c(handle)
# print(f"fpms_usb_close_c 返回值: {ret}")
# if __name__ == "__main__":
# main()