Resolving vertically flipped images from webcams in Ubuntu

Posted on July 31st, 2009 at 1:53, in Code, How To, Linux, Ubuntu.

UPDATE: Please check the solution from here as it represents the correct approach for fixing this problem in Ubuntu (all releases). Also, the patch from this post doesn’t work for the kernel included in Ubuntu 9.10.

Usually I do not use my laptop’s webcam. Not that I use another webcam. But when I made the radical switch from Windows to Ubuntu, I was disappointed to see that the images from my webcam were vertically flipped and I had no option of turning them 180°. The almost mythical explanation is that somehow some of the webcams mounted on laptops were mounted flipped and the producers have corrected this by pushing the repairs into the Windows drivers. Today luck seemed to be on my side as I have found a patch that now fixes my problem. To correctly apply it in Ubuntu 9.04 you have to browse that topic until the 12th page and check this post.

Because some of you might not correctly apply the patch, I have built a script that will help you patch the uvcvideo module in order to get the right images from your webcam. There are two patches available (I have chosen only the patches that provide a non-mirrored image) so pick the one that gives you the best image. To be able to compare them you must apply both patches, testing your webcam after each of them and then, for the third time, apply the patch that suites your needs.

To test your webcam, you can use VLC.

Here’s the short how to:

  1. install VLC if you do not have it:
  2. sudo apt-get install vlc
  3. download my script from here (right click, Save link as…) and make it executable:
  4. chmod +x flip_webcam
  5. make sure you have the build-essential package:
  6. sudo apt-get install build-essential
  7. run the script with super-user privileges from where you have downloaded it, providing the right options:
  8. This script must be run with super-user privileges.
    Usage: ./flip_webcam {OPTION}
    	 1		this applies patch1 file
    	 2		this applies patch2 file
    	-h, --help	displays this beautiful help section

The script will automatically download the needed files, will apply the patch and then it will push the module into your kernel. Do the tests by opening VLC and then following these steps:

  1. go to Media
  2. then go to Open capture device…
  3. in the pop-up window that opens, in the “Video device name” text field, insert /dev/video0
  4. click on Play

Remember to run the script whenever your kernel changes. If everything worked okay, drop a Kudos line to me in the comments. :)

Similar Posts:


31 Comments

KoKuToru said

on August 11th, 2009,

at 14:45 hours

Thanks a lot, it’s working :P

linkxs said

on August 24th, 2009,

at 00:12 hours

Sweet man, thanks! worked perfectly!

I’m really glad i found this

Unal said

on September 3rd, 2009,

at 15:19 hours

Thanks, but I try to fix this problem in pardus -linux
both of ./flip_webcam1 and 2 can not solve this flip problem.

the end of building windows is:

Loading the module into your kernel…
./flip_webcam: line 36: modprobe: command not found
Housekeeping…
Done!

is it error..??

Radu said

on September 3rd, 2009,

at 15:32 hours

Hi Unal! Yes, it’s an error… Try installing modeprobe first. It’s very interesting why this program isn’t already installed in your system.

Darkzeus said

on September 6th, 2009,

at 21:36 hours

Hey it took a couple of times doing it to get it to work but after following the directions again it now works. Thanks alot.

Radu said

on September 6th, 2009,

at 23:10 hours

Hi Darkzeus, can you please provide more details? I mean if it’s something that I can improve in the script please tell me. By the way, you are welcome! It’s my way of helping the open-source community by giving something in return.

Tohia said

on September 10th, 2009,

at 09:08 hours

Brilliant! Took a while to download but worked like a charm.

[...] webcam fonctionne dès la première utilisation mais l'image est retournée, la solution vient de ce site et m'a été rapportée par edlapoignee du forum [...]

Burak said

on October 3rd, 2009,

at 23:58 hours

this works great !! I’ve been trying to do this for a long time. thank you very much.

Radu said

on October 4th, 2009,

at 15:50 hours

You are welcome, Burak!

christoph said

on October 22nd, 2009,

at 21:56 hours

Great!!
Just works fine entered the command, waited and now the image in the right orientation.
While waiting for completion of the compilation, I continued browsing your blog: Nice Job you do. Felicitations!!

Radu said

on October 22nd, 2009,

at 21:57 hours

Thank you, Christoph!

ba2toot said

on October 25th, 2009,

at 15:46 hours

No-one except me seems to have this prob, at least i couldnt find anything, but my webcam is not working AT ALL! and i cant find it in my hw-config either. can anyone help? or does anyone know any forum where this was discussed or any ideas what i could put in google to find something?

Radu said

on October 25th, 2009,

at 16:40 hours

ba2toot, what is the output of lsusb on your system? I get this on the first line:

radu@data:~$ lsusb
Bus 002 Device 003: ID 04f2:b012 Chicony Electronics Co., Ltd 1.3 MPixel UVC webcam

Does your webcam fail to function only in Ubuntu (Linux)?

ba2toot said

on October 25th, 2009,

at 17:08 hours

output is

Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 002: ID 04f2:b071 Chicony Electronics Co., Ltd 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

i dont have a dual boot system on this machine (or any). and i know even less about getting things to work in virtualbox :)

ba2toot said

on October 25th, 2009,

at 17:12 hours

just checked, though: camera does not work ootb in virtualbox and windows hw assistant cant seem to find it. (winXP)
Also, it’s in none of the menus in virtualbox.

ba2toot said

on October 25th, 2009,

at 20:46 hours

IT WORKS!!!!! THNAKS VERY VERY MUCH!

Luca Béla Palkovics said

on October 31st, 2009,

at 12:28 hours

Is there a possibility that it wont work anymore on Ubuntu 9.10 ?

I tried it now 2 times and didn’t work..

Andrew said

on October 31st, 2009,

at 18:24 hours

I just tried it on a clean Ubuntu 9.10 desktop (Asus U6V laptop with built-in webcam) and it doesn’t work, both option 1 and 2. Scripts run fine, with no errors, but no change to the WebCam, it’s still upside-down.
My cam is:
Bus 002 Device 003: ID 04f2:b036 Chicony Electronics Co., Ltd

Any suggestions?

In any case, thank you for your effort, as you’ve helped quite a few people already.

Radu said

on November 1st, 2009,

at 13:16 hours

Hi Andrew… It’s been brought up to my attention that the script doesn’t work anymore and I’ve promised I will try to find a solution. I will start investigating now…

qw105 said

on November 5th, 2009,

at 16:53 hours

thanks a lot for this Post, waiting updates :)

Francesco said

on November 6th, 2009,

at 12:51 hours

Salut Radu,
ubuntu karmic with 2.6.31-14-generic kernel, when trying to compile patched module i obtain these errors:

/home/franz/flip_webcam_tmp/uvcvideo/v4l/bttv-driver.c: In function 'bttv_open':
/home/franz/flip_webcam_tmp/uvcvideo/v4l/bttv-driver.c:3241: error: implicit declaration of function 'lock_kernel'
/home/franz/flip_webcam_tmp/uvcvideo/v4l/bttv-driver.c:3248: error: implicit declaration of function 'unlock_kernel'
make[3]: *** [/home/franz/flip_webcam_tmp/uvcvideo/v4l/bttv-driver.o] Error 1
make[2]: *** [_module_/home/franz/flip_webcam_tmp/uvcvideo/v4l] Error 2
make[2]: Leaving directory `/usr/src/linux-headers-2.6.31-14-generic'
make[1]: *** [default] Error 2
make[1]: Leaving directory `/home/franz/flip_webcam_tmp/uvcvideo/v4l'
make: *** [all] Error 2

Have you please some advice about that?

Radu said

on November 6th, 2009,

at 13:10 hours

Francesco, please follow the update from the post’s beginning.

Francesco Conti said

on November 6th, 2009,

at 15:22 hours

It was just my curiosity while I was waiting for Hans’s response to my notebook logs. Now I’ve tested a new version of its library and that’s alright.

mulțumesc :)

Radu said

on November 6th, 2009,

at 15:45 hours

You are welcome! I am glad that your webcam works. As I have written in the new post, the solution posted by me here – while it made the things work in Jaunty’s kernels – it’s not the best way to handle these problems. Hans’ way of fixing this is far more elegant and actually it’s the only way webcam problems should be fixed. Patching the kernel is not okay under these circumstances.

Mario Saleiro said

on November 9th, 2009,

at 04:38 hours

Hi! I have tested your patch with Karmic Koala and now the webcam doesn’t work anymore. I should have read the whole list of comments before testing it :( Do you have any suggestion on how to at least restore the uvc driver so that the webcam may work again, even if it keeps upside down? Thanks in advance

Radu said

on November 9th, 2009,

at 12:54 hours

Mario, you should have followed the update written in bold at this post’s beginning.

To reinstall uvcvideo do this:

wget http://linuxtv.org/hg/~pinchartl/uvcvideo/archive/tip.tar.bz2 -O uvcvideo.tar.bz2
tar xfvj uvcvideo.tar.bz2
cd uvcvideo-*
make
sudo make install
sudo make unload
sudo modprobe uvcvideo

Then follow the update from here.

[...] certain laptop webcams on Ubuntu because some of them were mounted upside down. I have posted a solution for fixing this on the blog, but apparently it’s not quite the best way to obtain the desired [...]

christoph said

on November 19th, 2009,

at 22:34 hours

I’m back after having done my upgrade to 9.10. Which in itself was quite a nightmare:
1) The upgrade tentative ended badly with the system hanging with the Ubunutu logo sitting on the screen.

Never mind I thought, I’ll do a fresh install from scratch (this isn’t my main computer). That worked, but I wasn’t able to retrieve my user settings from the existing accounts?

Question: Any chance to find a working patch to the webcam problem, for the kernel in 9.10. Or should I revert to 9.04?

Radu said

on November 20th, 2009,

at 00:51 hours

Christoph, follow the update. It’s written with red and in bold at this post’s beginning.

Christoph said

on November 30th, 2009,

at 18:54 hours

Thanks Radu – following the instructions hepped me solving the problem – guess I was a bit too fast complaining ;-

Think you've got something to say?

Subscribe without commenting