Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any quick Python GUI to display live images from Camera

I am trying to display live images from my 1394 camera. Currently my code is able to obtain images in a loop from the camera and I was looking for any quick GUI that will update dynamically (as a separate thread). I can do this in PyQt maybe using QThreads but is there any recommendation or faster way of doing this?? Here's my code

#Loop capturing frames from camera

for frame in range(1,500):

print 'frame:',frame

TIME.sleep(1) #capture frame every second

image_binary    = pycam.cam.RetrieveBuffer()

#convert to PIL Image

pilimg = PIL.Image.frombuffer("L",(cimg.GetCols(),cimg.GetRows()),image_binary,'raw', "RGBA", 0, 1)
    # At this point I want to send my image data to a GUI window and display it

Thank you.

like image 398
Chenna V Avatar asked Oct 22 '25 16:10

Chenna V


2 Answers

Here's wxPython code that will do it...

import wx
from PIL import Image

SIZE = (640, 480)

def get_image():
    # Put your code here to return a PIL image from the camera.
    return Image.new('L', SIZE)

def pil_to_wx(image):
    width, height = image.size
    buffer = image.convert('RGB').tostring()
    bitmap = wx.BitmapFromBuffer(width, height, buffer)
    return bitmap

class Panel(wx.Panel):
    def __init__(self, parent):
        super(Panel, self).__init__(parent, -1)
        self.SetSize(SIZE)
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.Bind(wx.EVT_PAINT, self.on_paint)
        self.update()
    def update(self):
        self.Refresh()
        self.Update()
        wx.CallLater(15, self.update)
    def create_bitmap(self):
        image = get_image()
        bitmap = pil_to_wx(image)
        return bitmap
    def on_paint(self, event):
        bitmap = self.create_bitmap()
        dc = wx.AutoBufferedPaintDC(self)
        dc.DrawBitmap(bitmap, 0, 0)

class Frame(wx.Frame):
    def __init__(self):
        style = wx.DEFAULT_FRAME_STYLE & ~wx.RESIZE_BORDER & ~wx.MAXIMIZE_BOX
        super(Frame, self).__init__(None, -1, 'Camera Viewer', style=style)
        panel = Panel(self)
        self.Fit()

def main():
    app = wx.PySimpleApp()
    frame = Frame()
    frame.Center()
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()
like image 56
FogleBird Avatar answered Oct 25 '25 06:10

FogleBird


I thought I'd try PyQt4 imageviewer.py example and it worked for me. Thanks for all your help guys. Here's my modified code:

from PyQt4 import QtCore, QtGui
class CameraViewer(QtGui.QMainWindow):
    def __init__(self):
    super(CameraViewer, self).__init__()

    self.imageLabel = QtGui.QLabel()
    self.imageLabel.setBackgroundRole(QtGui.QPalette.Base)
    self.imageLabel.setScaledContents(True)

    self.scrollArea = QtGui.QScrollArea()
    self.scrollArea.setWidget(self.imageLabel)
    self.setCentralWidget(self.scrollArea)

    self.setWindowTitle("Image Viewer")
    self.resize(640, 480)

    timer = QtCore.QTimer(self)
    timer.timeout.connect(self.open)
    timer.start(33) #30 Hz

    def open(self):
    #get data and display
    pilimg = getMyPILImageDatFromCamera()
    image = PILQT.ImageQt.ImageQt(pilimg)
    if image.isNull():
        QtGui.QMessageBox.information(self, "Image Viewer","Cannot load %s." % fileName)
        return

    self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(image))
    self.imageLabel.adjustSize()


if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    CameraViewer = CameraViewer()
    CameraViewer.show()
    sys.exit(app.exec_())
like image 33
Chenna V Avatar answered Oct 25 '25 05:10

Chenna V



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!