close

https://github.com/basler/pypylon

from PySide6.QtCore import QTimer
from PySide6.QtGui import QImage, QPixmap
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel
import cv2
from pypylon import pylon
def configure_camera():
    # Initialize the first available camera
    camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
    camera.Open()

    # Set camera parameters
    camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)

    return camera

camera = configure_camera()
app = QApplication([])
window = QMainWindow()
window.setWindowTitle("OpenCV + PyPylon")
window.setGeometry(100, 100, 800, 600)
label = QLabel(window)
label.setGeometry(0, 0, 800, 600)
window.show()
def update_image():
    # Retrieve the next image from the camera
    grab_result = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
    if grab_result.GrabSucceeded():
        # Convert the grabbed image to an OpenCV format
        image = grab_result.Array

        # Convert the OpenCV image to a QImage
        height, width = image.shape

        # convert grayscale image to rgb image
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
       
        bytes_per_line =  3*width

        # Convert the OpenCV image to a QImage
        q_image = QImage(
            image.data, width, height, bytes_per_line, QImage.Format_RGB888
        )

        # Convert the QImage to QPixmap and display it in the label
        pixmap = QPixmap.fromImage(q_image)
        label.setPixmap(pixmap)

    grab_result.Release()

timer = QTimer()
timer.timeout.connect(update_image)
timer.start(30)  # Update the image every 30 milliseconds (adjust as needed)
app.exec()
arrow
arrow
    全站熱搜

    me1237guy 發表在 痞客邦 留言(0) 人氣()