User avatar
CarlD26
Posts: 12
Joined: Thu May 02, 2013 12:16 pm

Camera Module (Video) In Tkinter ?????

Wed Sep 11, 2013 11:04 am

I am facing a problem with the camera module. The thing is that I have been able to get it up and running in the terminal window. But my basic problem is that I want to include the camera module into the GUI. The camera is being used to just give a feed, it does not have to record or grab images all it has to do is show the live feed on the GUI. I am using Python Programming Language and Tkinter for my GUI.
So i would like to know if anyone can help me out with displaying the "raspivid" camera output in the GUI ???
All help is really appreciated in advance.
Thanks.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Camera Module (Video) In Tkinter ?????

Thu Sep 12, 2013 2:17 am

This is something I had been thinking about doing myself, but due to having lots of other projects on the go I never actually got round to starting it.
But here's the approach I was planning to take (dunno how feasible it all actually is)...
  1. Modify raspivid so that it has some kind of "communication channel" (whether that's via a pipe/socket/shared memory/whatever) so that while it's running the x-offset, y-offset, width and height of the preview window can be modified in "realtime"
  2. In your GUI app, create a "virtual rectangle" where you want the video preview displayed
  3. Calculate the screen-coordinates of this virtual rectangle, and tell raspivid to draw the preview window here
  4. Every time the GUI window moves or resizes, recalculate the new screen coordinates of the virtual rectangle, and update the raspivid preview location again
  5. And finally on application exit you'd obviously need to shut down raspivid

User avatar
CarlD26
Posts: 12
Joined: Thu May 02, 2013 12:16 pm

Re: Camera Module (Video) In Tkinter ?????

Thu Sep 12, 2013 5:12 am

Thanks for the information andrew.

Based on what u said i would have to use the tkinter canvas widget to create a virtual rectangle and the start the raspivid option of the camera with the location of the rectangle co-ordinates. Right ???

Thats where the problem arises. Bcoz i am just a beginner using python and tkinter i dont know if i can get a video onto my GUI app.

In my app the moment i call the raspivid command using the subprocess.call command the camera module turns on but the GUI app does not startup.

I actually took help from the below specified forum topic on how to call the raspivid command in Tkinter:
http://www.raspberrypi.org/phpBB3/viewt ... 3&p=407086

So i just tried it. It was a helpful topic but this was done for an image i want to do the same thing for a video only diffrence i do not want to record the video i just want the live feed. So for that i used the command raspivid -t 0.

So this is what i want to do is this possible?????

Thanks in advance.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Camera Module (Video) In Tkinter ?????

Thu Sep 12, 2013 9:38 am

Looking at the documentation http://docs.python.org/2/library/subprocess.html
For subprocess.call it says "Run the command described by args. Wait for command to complete, then return the returncode attribute."
So it looks like maybe you want to use subprocess.Popen instead, but obviously you shouldn't call 'wait' or 'communicate' on the returned object, because they both "Wait for process to terminate."

User avatar
CarlD26
Posts: 12
Joined: Thu May 02, 2013 12:16 pm

Re: Camera Module (Video) In Tkinter ?????

Thu Sep 12, 2013 10:58 am

hi,
I have already tried using the subprocess module but it does not seem to work for incorporating the camera module into tkinter GUI app.
What happens is that if the camera module starts then the GUI app does not load. And if i re-run my code then the camera module gets stuck and GUI is up and running. Both the GUI and Camera do not work simultaneously.
So basically i just need to know if there is a way that i can get the camera video onto a canvas widget using Tkinter so that both the GUI as well as the camera run simultaneously.
Also after and if this is done i would like to make a crosswire marking on the video showing the live feed to get the centre point.
Any recommendations of a software that can do that ????

technoducky
Posts: 62
Joined: Sat Jan 26, 2013 7:29 pm
Contact: Website

Re: Camera Module (Video) In Tkinter ?????

Thu Sep 12, 2013 11:20 am

Maybe have a look at having raspivid send output to stdout, other then that you would have to modify the raspivid source code to get communication between your app and raspivid.
I have never used the camera module at a high level such as Python before so not sure how easy it is.
You could also just run an MJPG stream from the camera and have Python read it.
And for overlaying a crosshair, I'm sure OpenCV could do that no problem.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Camera Module (Video) In Tkinter ?????

Thu Sep 12, 2013 11:29 am

Did you read what I actually wrote? ;)
If you're using subprocess.call then it'll wait until the command you're executing (in this case raspivid) exits, and as raspivid doesn't exit then your program will block (i.e. the rest of the GUI won't get drawn). That's why I suggested trying using subprocess.Popen instead (which looks like it doesn't block, as long as you don't call wait).

For drawing extra stuff on top of the preview video have a look at some of the stuff here http://www.raspberrypi.org/phpBB3/viewt ... 43&t=44474 or http://www.raspberrypi.org/phpBB3/viewt ... 43&t=54632

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Camera Module (Video) In Tkinter ?????

Thu Sep 12, 2013 11:31 am

technoducky wrote:You could also just run an MJPG stream from the camera and have Python read it.
And for overlaying a crosshair, I'm sure OpenCV could do that no problem.
AFAIK, both those approaches would lead to drastically reduced frame rates. However of course this may actually be acceptable to the OP? ;)

User avatar
CarlD26
Posts: 12
Joined: Thu May 02, 2013 12:16 pm

Re: Camera Module (Video) In Tkinter ?????

Thu Sep 12, 2013 1:14 pm

Thank u very much Andrew for all the help.
The subprocess.Popen() function is working absolutely fine. Both the camera and the gui are working fine simultaneously.
They both come up together.
So my next step is the Crosshair on the video itself.
Will go through the suggestions made by you and technoducky. Will see how it goes.
Thanks a lot. :D

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Camera Module (Video) In Tkinter ?????

Thu Sep 12, 2013 2:11 pm

Great, look forward to seeing what you come up with :D

technoducky
Posts: 62
Joined: Sat Jan 26, 2013 7:29 pm
Contact: Website

Re: Camera Module (Video) In Tkinter ?????

Mon Sep 16, 2013 12:14 pm

AndrewS wrote:
technoducky wrote:You could also just run an MJPG stream from the camera and have Python read it.
And for overlaying a crosshair, I'm sure OpenCV could do that no problem.
AFAIK, both those approaches would lead to drastically reduced frame rates. However of course this may actually be acceptable to the OP? ;)
Oh yeah, you would be lucky to get more than 5 FPS maybe (depending on what the OPs app is doing), I just mentioned it as an easier alternative to my other suggestion (using MMAL), which should give the full FPS delivered by raspivid.

akonstan
Posts: 3
Joined: Fri Sep 13, 2013 9:32 pm

Re: Camera Module (Video) In Tkinter ?????

Thu Oct 03, 2013 9:49 pm

CarlD26 wrote:Thank u very much Andrew for all the help.
The subprocess.Popen() function is working absolutely fine. Both the camera and the gui are working fine simultaneously.
They both come up together.
So my next step is the Crosshair on the video itself.
Will go through the suggestions made by you and technoducky. Will see how it goes.
Thanks a lot. :D
Hi CarlD26,
Can you grab and manipulate every Frame from the video stream? I want to use this method for some CV project that I have in mind. Can you please add some code here as well? How did you managed to call raspivid from subprocess.Popen?

Thanx

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Camera Module (Video) In Tkinter ?????

Thu Oct 03, 2013 11:48 pm

If you are still reading CarlD26 you'll probably be very interested in this thread if you've not already seen it :D

LeinadVetor
Posts: 14
Joined: Sat Aug 30, 2014 2:35 pm

Re: Camera Module (Video) In Tkinter ?????

Sat Aug 30, 2014 2:51 pm

Hey carl!
i am with the same problem that you.
i am new on pyhton and i am trying make a photobooth, but i can't make the preview window show at the same time that tkinter GUI.
i read about subprocess, but still doesn't work! =/
i am using the subprocess comunnicate but idk what subprocess module i can use for it work =/
can anyone help me?

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Camera Module (Video) In Tkinter ?????

Mon Sep 01, 2014 11:41 pm

If you're using Python with the Raspberry Pi camera, you'll find it much easier to just use http://picamera.readthedocs.org/en/release-1.7/ than to try calling the raspivid program using the subprocess module...

nathan_Schwarz
Posts: 1
Joined: Thu Sep 08, 2016 11:30 pm

Re: Camera Module (Video) In Tkinter ?????

Thu Sep 08, 2016 11:42 pm

Hello, I know this post is from a long time ago (2014) but to anywhom who's looking for a solution. I've got the one ;)

To use the rasp cam feedback in tkinter you just have to set a camera.preview_window & set camera.preview_fullscreen to False.

you can use something like this :

Code: Select all

camera = PiCamera()
camera.preview_fullscreen = False
camera.resolution = (1920, 1080)
camera.preview_window = (x, y, h, w)
#with x/y the coordinates & h/w the feedback frame dimension.

camera.start_preview()

you can then put a frame behind & use bind on the frame to "mime" that the videofeedback is on the GUI.

for exemple :

Code: Select all

def take_picture():
      	i = 0
        cwd = os.path.dirname(os.path.abspath(__file__))
	while os.path.exists(cwd + "/pictures/image%s.png" % i):
		i += 1
	camera.capture(cwd + "/pictures/image%s.png" % i)

take_picture_frame = Frame(width = x, height= y, bg = "white", cursor = "none")
take_picture_frame.place(x = x, y = y)
take_picture_frame.bind("<Button-1>", take_picture))

Happy coding ;)

Return to “General discussion”