Search code examples
pyqt4qgraphicsviewbounding-box

Reading frames from a folder and display in QGraphicsView in pyqt4


I am trying to read frame for a folder and display in QGraphics view.

import os
import PyQt4
from PyQt4 import QtCore, QtGui
dir = "frames"
for file in os.listdir(dir):
    grview = QtGui.QGraphicsView()
    scene = QtGui.QGraphicsScene()
    #pixmap = QtGui.QPixmap(os.path.join(dir, file))
    pixmap = QtGui.QPixmap(file)    
    item = QtGui.QGraphicsPixmapItem(pixmap)
    self.scene.addItem(item)
    grview.setScene()    
    grview.show()
    #self.scene.update()

The folder named "frames" contains jpg files and is in the same folder as the code. But still I am not able to read the frames.

I have also tried general display without graphicsview to check if it works using the code below but it too doesn't work.

import cv2
import os
import PyQt4
from PyQt4 import QtGui,QtCore
import matplotlib.pyplot as plt
from PIL import Image

def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder,filename))
        if img is not None:
            images.append(img)
    return images

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)    

    img = load_images_from_folder('/frames')
    image = Image.open(img)
    image.show()

I am not able to understand what am I missing. I want to display the images sequentially in QGraphicsView resembling a video and in some particular frame I have to select the object of interest by draw a rectangle around it which has to hold in the subsequent frames as well. Is the task i am attempting possible? if yes any help is appreciated


Solution

  • Sorry, but I have PyQt5. Try it:

    import os
    import sys
    
    from PyQt5 import Qt
    #from PyQt4 import QtCore, QtGui
    
    class MainWindow(Qt.QMainWindow):
        def __init__(self, parent=None):
            Qt.QMainWindow.__init__(self, parent)
    
            self.scene  = Qt.QGraphicsScene()
            self.grview = Qt.QGraphicsView(self.scene)
            self.setCentralWidget(self.grview)
    
            dir = "frames"
            self.listFiles = os.listdir(dir)
    
            self.timer = Qt.QTimer(self)    
            self.n = 0
            self.timer.timeout.connect(self.on_timeout)
            self.timer.start(1000)
    
            self.setGeometry(700, 150, 300, 300)
            self.show()
    
        def on_timeout(self):
    
            if self.n < len(self.listFiles):
                self.scene.clear()
                file = self.listFiles[self.n]
                pixmap = Qt.QPixmap("frames\{}".format(file))    # !!!  "frames\{}"
                item   = Qt.QGraphicsPixmapItem(pixmap)
                self.scene.addItem(item)
                self.grview.setScene(self.scene)                 # !!! (self.scene)
                self.n += 1
            else:
                self.timer.stop()
    
    app = Qt.QApplication(sys.argv)
    GUI = MainWindow()
    sys.exit(app.exec_())
    

    enter image description here