3

Before this question is dismissed as a duplicate, please note:

  • Using OpenCV version 3 (not version 2)
  • Using Python 3 (not C++)
  • Using PyQt 5 (not PyQt4)

Goal: Trying to stream OpenCV webcam video frames in a PyQt5 GUI window

# For testing, I'm simply reading in a single image instead of a video frame

frame = cv2.imread('Koala.jpg',0)     # this is a numpy.ndarray object
height, width = frame.shape         
my_image = QImage(frame.tostring(), width, height, QImage.Format_RGB888)

# By here, things seem okay. print(mimage) basically says <QImage object at 0x32243>

# my_image.rgbSwapped()               # This line crashes program
pixmap = QPixmap.fromImage(mimage)    # This line crashes program as well

I'm not sure how to debug this anymore. No error tracebacks are printed to console. Any help is appreciated. Thanks in advance.

Connor
  • 670
  • 3
  • 9
  • 29

1 Answers1

2

you need to convert the openCv image to QImage first and them use pixmap to display the image as follows this is because pixmap supports QImage format.

   height, width, bytesPerComponent = Img.shape
   bytesPerLine = 3 * width
   cv2.cvtColor(Img, cv2.COLOR_BGR2RGB, Img)                                           
   QImg = QImage(Img.data, width, height, bytesPerLine,QImage.Format_RGB888)

now use this QImg to pixmap

   pixmap = QPixmap.fromImage(QImg)
AdityaIntwala
  • 2,536
  • 1
  • 17
  • 17