gpt4 book ai didi

python - 视频流变得失真的QImage

转载 作者:行者123 更新时间:2023-12-02 16:09:25 25 4
gpt4 key购买 nike

我正在尝试使用OpenCV制作多摄像机视频流,并已将PyQt用于GUI。该代码在Full HD上运行良好,但是当我更改显示器的分辨率时,流变得失真了。谁能告诉我为什么会这样?

以下是屏幕截图,供您引用:

1920 * 1080的屏幕截图(全高清)

1600 * 900的屏幕截图

1366 * 768中的屏幕截图

代码如下:

from PyQt4 import QtCore, QtGui
from threading import Thread
from collections import deque
from datetime import datetime
import time
import sys
import cv2
import imutils

class CameraWidget(QtGui.QWidget):

def __init__(self, width, height, stream_link=0, aspect_ratio=False, parent=None, deque_size=1):
super(CameraWidget, self).__init__(parent)

# Initialize deque used to store frames read from the stream
self.deque = deque(maxlen=deque_size)

self.screen_width = width
self.screen_height = height
self.maintain_aspect_ratio = aspect_ratio

self.camera_stream_link = stream_link

# Flag to check if camera is valid/working
self.online = False
self.capture = None
self.video_frame = QtGui.QLabel()

self.load_network_stream()

# Start background frame grabbing
self.get_frame_thread = Thread(target=self.get_frame, args=())
self.get_frame_thread.daemon = True
self.get_frame_thread.start()

# Periodically set video frame to display
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.set_frame)
self.timer.start(.5)

print('Started camera: {}'.format(self.camera_stream_link))

def load_network_stream(self):
"""Verifies stream link and open new stream if valid"""

def load_network_stream_thread():
if self.verify_network_stream(self.camera_stream_link):
self.capture = cv2.VideoCapture(self.camera_stream_link)
self.online = True
self.load_stream_thread = Thread(target=load_network_stream_thread, args=())
self.load_stream_thread.daemon = True
self.load_stream_thread.start()

def verify_network_stream(self, link):
"""Attempts to receive a frame from given link"""

cap = cv2.VideoCapture(link)
if not cap.isOpened():
return False
cap.release()
return True

def get_frame(self):
"""Reads frame, resizes, and converts image to pixmap"""

while True:
try:
if self.capture.isOpened() and self.online:
# Read next frame from stream and insert into deque
status, frame = self.capture.read()
if status:
self.deque.append(frame)
else:
self.capture.release()
self.online = False
else:
# Attempt to reconnect
print('attempting to reconnect', self.camera_stream_link)
self.load_network_stream()
self.spin(2)
self.spin(.001)
except AttributeError:
pass

def spin(self, seconds):
"""Pause for set amount of seconds, replaces time.sleep so program doesnt stall"""

time_end = time.time() + seconds
while time.time() < time_end:
QtGui.QApplication.processEvents()

def set_frame(self):
"""Sets pixmap image to video frame"""

if not self.online:
self.spin(1)
return

if self.deque and self.online:
# Grab latest frame
frame = self.deque[-1]

# Keep frame aspect ratio
if self.maintain_aspect_ratio:
self.frame = imutils.resize(frame, width=self.screen_width)
# Force resize
else:
self.frame = cv2.resize(frame, (self.screen_width, self.screen_height))

# Convert to pixmap and set to video frame
self.img = QtGui.QImage(self.frame, self.frame.shape[1], self.frame.shape[0], QtGui.QImage.Format_RGB888).rgbSwapped()
self.pix = QtGui.QPixmap.fromImage(self.img)
self.video_frame.setPixmap(self.pix)

def get_video_frame(self):
return self.video_frame

最佳答案

我找到了解决方案,由QImage负责。

            else:
self.frame = cv2.resize(frame, (self.screen_width, self.screen_height))
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
h, w, ch = self.frame.shape
bytesPerLine = ch * w

# Convert to pixmap and set to video frame

self.img = QtGui.QImage(self.frame, w, h, bytesPerLine, QtGui.QImage.Format_RGB888)
self.pix = QtGui.QPixmap.fromImage(self.img)
self.video_frame.setPixmap(self.pix)

关于python - 视频流变得失真的QImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61629371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com