Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying Camera Feed in Software: Guidance on Which Control to Use #386

Open
jiangtangaaaa opened this issue Apr 23, 2024 · 3 comments
Open
Labels
question Further information is requested

Comments

@jiangtangaaaa
Copy link

@CVHub520 大佬中午好,我又来麻烦您了,希望您不要介意

我想把摄像头的实时影像展示在软件上,我应该怎么操作,或者应该调用哪个控件?
ret, frame = cap.read() self.label.setPixmap(QPixmap.fromImage(frame))
希望您可以指点我一下

@CVHub520
Copy link
Owner

To display the live feed from a camera in X-AnyLabeling, you would typically use a video capturing library to capture frames from the camera and then display those frames in a suitable widget. In PyQt, you can use the QLabel widget to display the video frames.
Here's a basic outline of the steps you would follow:

  1. Capture Frames: Use a library like OpenCV to capture frames from the camera.
  2. Convert Frames: Convert the frames to a format that can be displayed in PyQt, such as QImage.
  3. Display Frames: Use a QLabel widget to display the frames.
    Your code snippet seems to be on the right track. However, you need to ensure that the frame is converted to a QImage before setting it as a pixmap for the QLabel. Here's a revised version of your code:
import cv2
from PyQt5.QtWidgets import QLabel
from PyQt5.QtGui import QImage, QPixmap
import numpy as np
# Assuming 'cap' is your video capture object
ret, frame = cap.read()
# Convert the frame from BGR to RGB (OpenCV uses BGR, while QImage uses RGB)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Convert the frame to a QImage
h, w, ch = frame.shape
bytesPerLine = ch * w
qImg = QImage(frame.data, w, h, bytesPerLine, QImage.Format_RGB888)
# Set the pixmap of the QLabel to the QImage
self.label.setPixmap(QPixmap.fromImage(qImg))

Make sure to run the frame capture and display code in a separate thread or use a timer to continuously update the QLabel with new frames to avoid freezing app's main thread.

@CVHub520 CVHub520 added the question Further information is requested label Apr 23, 2024
@jiangtangaaaa
Copy link
Author

To display the live feed from a camera in X-AnyLabeling, you would typically use a video capturing library to capture frames from the camera and then display those frames in a suitable widget. In PyQt, you can use the QLabel widget to display the video frames. Here's a basic outline of the steps you would follow:

  1. Capture Frames: Use a library like OpenCV to capture frames from the camera.
  2. Convert Frames: Convert the frames to a format that can be displayed in PyQt, such as QImage.
  3. Display Frames: Use a QLabel widget to display the frames.
    Your code snippet seems to be on the right track. However, you need to ensure that the frame is converted to a QImage before setting it as a pixmap for the QLabel. Here's a revised version of your code:
import cv2
from PyQt5.QtWidgets import QLabel
from PyQt5.QtGui import QImage, QPixmap
import numpy as np
# Assuming 'cap' is your video capture object
ret, frame = cap.read()
# Convert the frame from BGR to RGB (OpenCV uses BGR, while QImage uses RGB)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Convert the frame to a QImage
h, w, ch = frame.shape
bytesPerLine = ch * w
qImg = QImage(frame.data, w, h, bytesPerLine, QImage.Format_RGB888)
# Set the pixmap of the QLabel to the QImage
self.label.setPixmap(QPixmap.fromImage(qImg))

Make sure to run the frame capture and display code in a separate thread or use a timer to continuously update the QLabel with new frames to avoid freezing app's main thread.

To display the live feed from a camera in X-AnyLabeling, you would typically use a video capturing library to capture frames from the camera and then display those frames in a suitable widget. In PyQt, you can use the QLabel widget to display the video frames. Here's a basic outline of the steps you would follow:

  1. Capture Frames: Use a library like OpenCV to capture frames from the camera.
  2. Convert Frames: Convert the frames to a format that can be displayed in PyQt, such as QImage.
  3. Display Frames: Use a QLabel widget to display the frames.
    Your code snippet seems to be on the right track. However, you need to ensure that the frame is converted to a QImage before setting it as a pixmap for the QLabel. Here's a revised version of your code:
import cv2
from PyQt5.QtWidgets import QLabel
from PyQt5.QtGui import QImage, QPixmap
import numpy as np
# Assuming 'cap' is your video capture object
ret, frame = cap.read()
# Convert the frame from BGR to RGB (OpenCV uses BGR, while QImage uses RGB)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Convert the frame to a QImage
h, w, ch = frame.shape
bytesPerLine = ch * w
qImg = QImage(frame.data, w, h, bytesPerLine, QImage.Format_RGB888)
# Set the pixmap of the QLabel to the QImage
self.label.setPixmap(QPixmap.fromImage(qImg))

Make sure to run the frame capture and display code in a separate thread or use a timer to continuously update the QLabel with new frames to avoid freezing app's main thread.

感谢您指点我,首先您说使用多线程是对的,不然会冻结主线程
我的意思是在X-AnyLabeling的标注区域内展示摄像头图像,我现在把转换后的图像让canvas加载出来显示了实时影像,self.canvas.load_pixmap(QPixmap.fromImage(p))
但是有一个问题是:实时影像我不知道该怎么跟缩放联系起来,如果我点击缩放按钮或者按住Ctrl+滚轮,就会导致程序退出
我现在正在研究这段代码,还没有思路把实时影像和缩放联系起来,而且影像画面有点小

# set zoom values
        is_initial_load = not self.zoom_values
        print(self.zoom_values)
        print(is_initial_load)
        if self.filename in self.zoom_values:
            self.zoom_mode = self.zoom_values[self.filename][0]
            self.set_zoom(self.zoom_values[self.filename][1])
        elif is_initial_load or not self._config["keep_prev_scale"]:
            self.adjust_scale(initial=True)
        # set scroll values
        for orientation in self.scroll_values:
            if self.filename in self.scroll_values[orientation]:
                self.set_scroll(
                    orientation, self.scroll_values[orientation][self.filename]
                )

Snipaste_2024-04-24_14-23-36.png

@jiangtangaaaa
Copy link
Author

@CVHub520 您好~~早上好

@CVHub520 CVHub520 changed the title 【question】关于显示摄像头图像 Displaying Camera Feed in Software: Guidance on Which Control to Use May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants