【question】关于显示摄像头图像
@CVHub520 大佬中午好,我又来麻烦您了,希望您不要介意
我想把摄像头的实时影像展示在软件上,我应该怎么操作,或者应该调用哪个控件?
ret, frame = cap.read() self.label.setPixmap(QPixmap.fromImage(frame))
希望您可以指点我一下
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:
- Capture Frames: Use a library like OpenCV to capture frames from the camera.
- Convert Frames: Convert the frames to a format that can be displayed in PyQt, such as
QImage. - Display Frames: Use a
QLabelwidget to display the frames. Your code snippet seems to be on the right track. However, you need to ensure that theframeis converted to aQImagebefore setting it as a pixmap for theQLabel. 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
QLabelwidget to display the video frames. Here's a basic outline of the steps you would follow:
- Capture Frames: Use a library like OpenCV to capture frames from the camera.
- Convert Frames: Convert the frames to a format that can be displayed in PyQt, such as
QImage.- Display Frames: Use a
QLabelwidget to display the frames. Your code snippet seems to be on the right track. However, you need to ensure that theframeis converted to aQImagebefore setting it as a pixmap for theQLabel. 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
QLabelwith 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
QLabelwidget to display the video frames. Here's a basic outline of the steps you would follow:
- Capture Frames: Use a library like OpenCV to capture frames from the camera.
- Convert Frames: Convert the frames to a format that can be displayed in PyQt, such as
QImage.- Display Frames: Use a
QLabelwidget to display the frames. Your code snippet seems to be on the right track. However, you need to ensure that theframeis converted to aQImagebefore setting it as a pixmap for theQLabel. 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
QLabelwith 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]
)

@CVHub520 您好~~早上好