Photometrics / PyVCAM

Python3.X wrapper for Photometrics and QImaging PVCAM based cameras
MIT License
37 stars 17 forks source link

Issue with PyQt5 #49

Open llooiicc opened 3 hours ago

llooiicc commented 3 hours ago

Hi,

I tried to display an image with PyQt5 using a simple get_frame() but it doesn't work. The method get_frame doesn't return any array.

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget, QPushButton
from PyQt5.QtGui import QPixmap, QImage
import numpy as np
from pyvcam import pvc
from pyvcam.camera import Camera

class CameraApp(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

        pvc.init_pvcam()
        self.cam = Camera('PMUSBCam00')  # Remplacez par le nom de votre caméra
        self.cam.open()

    def init_ui(self):
        self.setWindowTitle('PyVCAM Camera App')
        self.setGeometry(100, 100, 800, 600)

        # Layout
        self.layout = QVBoxLayout()

        # Label to display image
        self.image_label = QLabel(self)
        self.layout.addWidget(self.image_label)

        # Button to capture image
        self.capture_button = QPushButton('Capture Image', self)
        self.capture_button.clicked.connect(self.capture_image)
        self.layout.addWidget(self.capture_button)

        # Set layout
        self.setLayout(self.layout)

    def capture_image(self):

        if not self.cam.is_open:
           print("Camera is not open!")
           return None

        # Capture une image en 2D
        #self.cam.start_live(exp_time=10)  # Temps d'exposition en millisecondes
        print("test1")
        frame = self.cam.get_frame(exp_time=0.2)
        print("test2")
        print(frame)

        # Convertir l'image en QImage pour l'affichage
        height, width = frame.shape
        image = QImage(frame.data, width, height, QImage.Format_Grayscale8)

        # Afficher l'image dans le QLabel
        pixmap = QPixmap.fromImage(image)
        self.image_label.setPixmap(pixmap)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = CameraApp()
    ex.show()
    sys.exit(app.exec_())

I tried with Tk and then it work well.

import tkinter as tk
from tkinter import Label, Button
from PIL import Image, ImageTk
import numpy as np
from pyvcam import pvc
from pyvcam.camera import Camera

class CameraApp:
    def __init__(self, root):
        self.root = root
        self.root.title('PyVCAM Camera with Tkinter')
        self.root.geometry("800x600")

        self.cam = None

        # Label to display the image
        self.image_label = Label(self.root)
        self.image_label.pack()

        # Button to capture image
        self.capture_button = Button(self.root, text='Capture Image', command=self.capture_image)
        self.capture_button.pack()

        # Initialize the camera
        pvc.init_pvcam()
        self.cam = Camera('PMUSBCam00')  # Remplacez par le nom de votre caméra
        self.cam.open()

    def capture_image(self):
        try:
            # Capture an image
            frame = self.cam.get_frame(exp_time=0.2)
            print(frame)

            # Check if frame is valid
            if frame is not None and frame.size > 0:
                self.display_image(frame)
            else:
                print("No frame captured.")
        except Exception as e:
            print(f"Error capturing image: {e}")

    def display_image(self, frame):
        # Convertir l'image en un objet PIL Image
        height, width = frame.shape
        image = Image.fromarray(frame)

        # Convertir l'image en un format utilisable par Tkinter
        image_tk = ImageTk.PhotoImage(image)

        # Mettre à jour l'affichage dans le label
        self.image_label.configure(image=image_tk)
        self.image_label.image = image_tk  # Sauvegarder une référence pour éviter la collecte par le garbage collector

    def close(self):
        # Fermer la caméra
        if self.cam:
            self.cam.close()
        pvc.uninit_pvcam()

if __name__ == '__main__':
    root = tk.Tk()
    app = CameraApp(root)
    root.protocol("WM_DELETE_WINDOW", app.close)  # Fermer proprement la caméra lors de la fermeture de l'application
    root.mainloop()

Has anyone had this problem and solved it?

Thanks

llooiicc commented 2 hours ago

UPDATE : I delete all PyQt classes and run just pyvcam :

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget, QPushButton
from PyQt5.QtGui import QPixmap, QImage
import numpy as np
from pyvcam import pvc
from pyvcam.camera import Camera

pvc.init_pvcam()

cam = Camera('PMUSBCam00')  # Remplacez par le nom de votre caméra
cam.open()

def getframe():
    print("test1")
    print(cam.is_open)
    frame = cam.get_frame(exp_time=0.2)
    print("test2")
    print(frame)

if __name__ == '__main__':

    getframe()

Same problem occur...

Then I delete all PyQt imports:

import sys
import numpy as np
from pyvcam import pvc
from pyvcam.camera import Camera

pvc.init_pvcam()

cam = Camera('PMUSBCam00')  # Remplacez par le nom de votre caméra
cam.open()

def getframe():
    print("test1")
    print(cam.is_open)
    frame = cam.get_frame(exp_time=0.2)
    print("test2")
    print(frame)

if __name__ == '__main__':

    getframe()

It works