View Full Version : tiscamera python examples, GTK2 and GST 1.0, linux

August 13, 2017, 17:30:58

I'm working on a GUI built from glade and trying to import the "pipeline" python example getting the error:

ImportError: When using gi.repository you must not import static modules like "gobject". Please change all occurrences of "import gobject" to "from gi.repository import GObject". See: https://bugzilla.gnome.org/show_bug.cgi?id=709183

From what I've read this is because I'm using GTK2 and the python module is requiring Gstreamer 1.0 which supposedly only works on GTK3. This GUI is being built for LinuxCNC which requires GTK2. Is there a way around this? I'm trying to embed the camera's video into the drawing area of the GUI, maybe there's some other way to go about this?

Stefan Geissler
August 14, 2017, 08:35:44

GTK 2 is outdated since 2011. Therefore there is no more support for GTK 2 and no way around.

August 14, 2017, 23:47:29

GTK 2 is outdated since 2011. Therefore there is no more support for GTK 2 and no way around.

This is not very helpful. I have had this camera display perfectly fine within my GTK2 GUI using widgets that were tailored toward certain functionality. Added buttons and overlays made the widget unsuitable. Unfortunately I do not know what mechanism this widget used I assume Gstreamer0.10 maybe. I've also had it display in Mplayer through my GUI. qv4l2 also has no issues with the camera or controls. I find it hard to believe there is "no way around" this. I'm not dependant on gst1.0 only the python examples are. I can use whatever other method can display to a drawing area and this is what I'm asking. I was told there would be Linux support for these but I wouldn't call that response "support".

Stefan Geissler
August 15, 2017, 09:13:10
I know, that this is not very helpful. The problem is not the camera. It is, as far as I understand, a Python incompatibility. However, if you get an image from a pipeline and change it to an numpy array or to an OpenCV mat, you should be done, right?

So you might try in a file e.g. "TIS.py":

import pygst
import gst
import pygtk
#import gtk
import sys
import numpy
import cv2
import os
import gobject

class TIS:
'The Imaging Source Camera'

def __init__(self, w, h, fps,exposure,gain):

self.w = w
self.h = h
self.fps = fps
#gain and exposure will be ignored in this version
self.exposure = exposure
self.gain = gain
self.sample = None
self.samplelocked = False
self.newsample = False

self.pipeline = gst.Pipeline("mypipeline")
self.v4l2src = gst.element_factory_make("v4l2src", "v4l2")

def on_new_buffer( self, appsink ):
self.newsample = True
if (self.samplelocked == False):
self.sample = appsink.get_property('last-buffer')

def Create_pipeline(self):

parameter1 = 'video/x-raw-gray,width=' + str(self.w) + ',height=' + str(self.h) + ',framerate=' + str(self.fps) + '/1'

self.caps = gst.Caps("%s" % parameter1)
self.capsFilter = gst.element_factory_make("capsfilter")

# Instantiate needed modules
self.tisvideobufferfilter = gst.element_factory_make("tisvideobufferfilter","videobuffer")

self.tisautoexeposurefilter = gst.element_factory_make("tis_auto_exposure","autoexposure")

self.tiscolorize = gst.element_factory_make("tiscolorize","colorize")

self.tiswhitebalance = gst.element_factory_make("tiswhitebalance","whitebalance");

self.bayer2rgb = gst.element_factory_make("bayer2rgb","bayer2rgb")

self.bayercaps = gst.element_factory_make("capsfilter")
self.bayercaps.set_property("caps",gst.Caps("video/x-raw-rgb,red_mask=0x0000FF00,green_mask=0x00FF0000,blue _mask=0xFF000000"))

self.appsink = gst.element_factory_make("appsink","sink")

# Link the pipeline

self.tisvideobufferfilter.link(self.tisautoexeposu refilter)

self.appsink.connect('new-buffer',self.on_new_buffer )

def Start_pipeline(self):
# Setting of parameters is disabled, because we run the camera on automatics
#param = 'v4l2-ctl -c exposure_absolute=' + str(self.exposure) + ',gain=' + str(self.gain)

def Get_image(self):
if( self.sample != None and self.newsample == True):
self.samplelocked = True
self.img_mat = numpy.ndarray(shape=(self.h,self.w,4),dtype='uint8 ',buffer=self.sample.data)
self.samplelocked = False
self.newsample = False
return self.img_mat
return None

def Stop_pipeline(self):

... which is somewhat complicated, since all GstElements are created manually.
... and use that in a program as follows

import cv2
import numpy as np
import os
import TIS

# Setting for DMK / DFK 42BUC02 at 10 fps
#Tis = TIS.TIS(1280, 960, 10, 50, 34)

#Setting for DFK 23UV024 at 100fps
Tis = TIS.TIS(752, 480, 100, 50, 34)



cv2.namedWindow('Window', cv2.cv.CV_WINDOW_NORMAL)

error = 0
print 'Press ctrl-c to stop'
while ( True and error < 5):
image = Tis.Get_image()
if image != None:
error = 0
cv2.imshow('Window', image)

cv2.imwrite('img%d.jpg' % imagecount, image)
if(imagecount == 1 ):
imagecount = 2
imagecount = 1
print "No image reveived "
error = error + 1

except KeyboardInterrupt:

print 'Program ended'

Or you may use OpenCV compiled with GStreamer support and do

import cv2
import numpy as np

cap = cv2.VideoCapture('tcambin ! video/x-raw, width=1280, height=960, format=GRAY8,fps=10.0 ! videoconvert ! appsink')

def capture(src):

_, imgRGB = src.read()

# <!-- Manipulate image here

return imgRGB

def main():
print("Hello World!")
if cap.isOpened() :


while cv2.waitKey(1) & 0xFF != 27 and cap.isOpened():

cv2.imshow('Live', capture(cap))

print("Camera not opened")

if __name__ == '__main__':

Allways adapt serial number, width, height and frame rate to your needs.

I have to admit, I am not as familiar with Python and I never made a GTK application. Therefore, my samples may are not helpful, but give an idea.

August 16, 2017, 12:03:12
Thank you for the examples.

With the first example I always return
"gst.ElementNotFoundError: tisvideobufferfilter"

In the second:
I always return "hello World" followed by "camera not open".

any insight?

Stefan Geissler
August 16, 2017, 16:22:20
The sample was build with the GStreamer 0.10 modules from the gstreamer 0.10 path.

Can you change it to use GStreamer 1.0?

Stefan Geissler
August 16, 2017, 16:47:51
The attached Python sample wraps the GStraemer 1.0 stuff. This may is better for you.

August 16, 2017, 23:45:21
The attached Python sample wraps the GStraemer 1.0 stuff. This may is better for you. when adding the code to my GTK2 GUI it still pumps out the ImportError, Running "program.py" standalone works fine. Looking at the code for the widget I was able to use to display, it pumps out openGL. Any thoughts on that? Is there any negatives to using opengl? I suppose I could still use v4l2 to control the camera?

August 17, 2017, 00:44:06
That's my mistake, I was looking at code for an older version of this widget. This does not appear to reference openGL at all.
Like I said, I have to pull alot out of this to make it suitable but the mechanism for displaying video is solid. It does not appear to use Gstreamer at all. Is there any way to use the TIS stuff with just openvc and v4L2 or whatever is going on in this code? (camview.py)

Stefan Geissler
August 17, 2017, 09:08:42
If I understand correctly, you want to use outdated software, which is not supported by Python etc. I gave you much source code, that allows to get the image data, so you can display that somehow in your outdated GTK 2 software.

Stefan Geissler
August 17, 2017, 10:50:30
I looked into your sample code. It seems it is v4l2 based, at least, that is mentioned in the CamPY_READ.ME. Thus, our USB cameras should work with it without any other installation as the ones mentioned in this file. Also no GStreamer needed.

But I guess, you tested that?

August 17, 2017, 18:27:00
If I understand correctly, you want to use outdated software, which is not supported by Python etc.

No, I don't want to use "outdated" software just because I think it will be fun, I am using LinuxCNC because alot of the project this camera is being used on involves an axis, encoders, and limit switches controlled by the same PC. LinuxCNC uses GTK2 for it's GUI's probably for the fact that it's open source and community driven as is the nature of most things involving linux, I don't believe there are many paid software engineers trying to redesign for GTK3 or Qt. Not sure what you mean it's not supported by Python, programs are written in Python. Your examples all error out when applied, I'm happy to continue working with them if you have suggestions, if not thats fine too.

So yeah the camera works, it works with Gst 1.0, Gst 0.10, V4L2. It works fine, but the packaged python examples pretty much all use Gst1.0 which is fine, but for whatever reason Gst if GST 1.0 does not suit the camera user they either must hire a python programmer to rewrite them or be proficient in python which I am not. You guys should work on some non GST options when providing examples in your packages (like strictly V4L2?). I'm sure there's probably a full blown software package for Windows that does all sorts of neat things. All machine vision manufacturers "support" Linux, but you have to buy the camera to figure out how that goes. It's fantastic hardware and the US customer service is very good, but they don't know *anything* about Linux. So either I get it to work in GTK2 or I don't, either I put it in a box and send it back or I don't, either I order more for the next machines I build or I don't..........just because somebody else may have slightly more diverse linux support

Stefan Geissler
August 18, 2017, 15:08:28

I am not really sure what you expect from us.
However, you could also start with v4l2.
You could also start with gstreamer 0.10 (I showed that)
You could also start with GStreamer 1.0 (I showed that too)
You could also start with OpenCV and Gstreamer (you know...)

And yes, the exact application connecting to your existing system wont exist. Without working on this on your own, you will not get exactly what you would like to have.