medical_balloon/Defect_Detection/Class/Camera.py

107 lines
3.6 KiB
Python
Raw Normal View History

2024-07-30 16:18:26 +08:00
from PyQt5.QtWidgets import QWidget, QFileDialog,QMainWindow, QLabel, QSizePolicy, QApplication, QAction, QHBoxLayout
from PyQt5.QtCore import *
from PyQt5 import QtCore, QtGui, QtWidgets
import numpy as np
from pyueye import ueye
import time
import sys
# IDS相機
class Camera_class(QtCore.QThread): # 攝像頭
rawdata = QtCore.pyqtSignal(np.ndarray)
def __init__(self, parent=None):
super().__init__(parent)
print('cam_start')
def run(self): # 運作攝像頭
# init camera
hcam = ueye.HIDS(2)
ret = ueye.is_InitCamera(hcam, None)
print(f"initCamera returns {ret}")
# set color mode
ret = ueye.is_SetColorMode(hcam, ueye.IS_CM_BGR8_PACKED)
print(f"SetColorMode IS_CM_BGR8_PACKED returns {ret}")
# set region of interest
width = 2048
# height = 1088
height = 2048
rect_aoi = ueye.IS_RECT()
rect_aoi.s32X = ueye.int(0)
rect_aoi.s32Y = ueye.int(0)
rect_aoi.s32Width = ueye.int(width)
rect_aoi.s32Height = ueye.int(height)
ueye.is_AOI(hcam, ueye.IS_AOI_IMAGE_SET_AOI, rect_aoi, ueye.sizeof(rect_aoi))
print(f"AOI IS_AOI_IMAGE_SET_AOI returns {ret}")
# allocate memory
mem_ptr = ueye.c_mem_p()
mem_id = ueye.int()
bitspixel = 24 # for colormode = IS_CM_BGR8_PACKED
ret = ueye.is_AllocImageMem(hcam, width, height, bitspixel,
mem_ptr, mem_id)
print(f"AllocImageMem returns {ret}")
# set active memory region
ret = ueye.is_SetImageMem(hcam, mem_ptr, mem_id)
print(f"SetImageMem returns {ret}")
# 設定曝光時間
exposure_time = 5 # 曝光時間,單位為毫秒
param = ueye.double(exposure_time)
cb_size = ueye.sizeof(param) # 獲取參數的大小
ret = ueye.is_Exposure(hcam, ueye.IS_EXPOSURE_CMD_SET_EXPOSURE, param, cb_size)
print(f"設定曝光時間 returns {ret}")
# 獲取相機曝光時間
exposure_time = ueye.double()
cb_size = ueye.sizeof(exposure_time)
ret = ueye.is_Exposure(hcam, ueye.IS_EXPOSURE_CMD_GET_EXPOSURE, exposure_time, cb_size)
print(f"曝光時間: {exposure_time.value}")
# 設定新的幀率例如30fps
fps_ = 30
# 將fps轉換為ueye.double類型
fps = ueye.double(fps_)
# 設定新的幀率
nRet = ueye.is_SetFrameRate(hcam, ueye.IS_GET_DEFAULT_FRAMERATE, fps)
if nRet != ueye.IS_SUCCESS:
print("is_SetFrameRate -> ERROR")
else:
print(f"Frame rate set to {fps_} fps")
# 設定新的增益值
# gain_value = 10
# # 設定增益
# nRet = ueye.is_SetHWGainFactor(hcam, ueye.IS_SET_MASTER_GAIN, ueye.INT(gain_value))
# if nRet != ueye.IS_SUCCESS:
# print("is_SetHWGainFactor -> ERROR")
# else:
# print(f"Gain set to {gain_value}")
# continuous capture to memory
ret = ueye.is_CaptureVideo(hcam, ueye.IS_DONT_WAIT)
print(f"CaptureVideo returns {ret}")
# get data from camera and display
lineinc = width * int((bitspixel + 7) / 8)
while True:
img = ueye.get_data(mem_ptr, width, height, bitspixel, lineinc, copy=True)
img = np.reshape(img, (height, width, 3))
self.rawdata.emit(img)
time.sleep(0.01)
# cv2.imshow('uEye Python Example (q to exit)', img)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# cv2.destroyAllWindows()