Scan notes with laptop webcam
September 16, 2018 Updated: July 26, 2020 #blogI was solving a pset, and realized that I needed to draw a graph by hand and include it in my solution. I didn't want to deal with taking a picture with my phone and sending the picture to my computer in order to use it. I wanted to be able to quickly scan it using my webcam.
So I decided to write a scrip to just do that, I press Ctrl-Print and the magic happens!
It sets my screen brightness to max. Takes a pic with my webcam. Sets the screen brightness to where it was. Prompts me to crop the picture it took. Uses textcleaner to clean the picture up. And at last, it automatically inserts the picture in my org document, and displays the picture.
In other words, a quick and dirty scanner with computer screen flash to scan papers.
Controlling the brightness
First I needed to allow me to control the brightness without sudo. Here I added my group as a group with permissions to edit the brightness.
sudo chgrp ivanaf /sys/class/backlight/intel_backlight/brightness
sudo /bin/chmod g+w /sys/class/backlight/intel_backlight/brightness
Then I could just use
echo brightness /sys/class/backlight/intel_backlight/brightness
Taking the picture
I use pygame to take the picture.
=
=
#https://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/
#try:
= True
# compute the Laplacian of the image and then return the focus
# measure, which is simply the variance of the Laplacian
=
return
return
return
#https://gist.github.com/jpanganiban/3844261
"""Convert a pygame surface into a cv image"""
## convert to pillow
=
=
=
=
=
return
#except ImportError:
= False
return
#based on https://gist.github.com/snim2/255151
#and https://www.pygame.org/docs/tut/CameraIntro.html
=
=
=
=
=
#Needed for the camera to adjust brightness and focus
=
=
=
=
=
=
#from: http://discourse.techart.online/t/pil-wait-for-image-save/3994/6
Cropping the picture
Again, I use pygame, using the code from these online answers: https://coderwall.com/p/hmp8uw/image-cropping-using-pygame https://stackoverflow.com/questions/6136588/image-cropping-using-python/8696558
#https://coderwall.com/p/hmp8uw/image-cropping-using-pygame
#https://stackoverflow.com/questions/6136588/image-cropping-using-python/8696558
# ensure that the rect always has positive width, height
, =
= -
= -
+=
=
+=
=
# eliminate redundant drawing cycles (when mouse isn't moving)
= , , ,
return
return
# draw transparent box and blit it onto canvas
=
# return current box extents
return
=
=
return ,
= = = None
=0
=
=
=1
=
return
=
=
, =
, , , =
# ensure output rect always has positive width, height
, = ,
, = ,
=
=
=
#from: http://discourse.techart.online/t/pil-wait-for-image-save/3994/6
Cleaning the image
To clean the image, I was trying to remove the background by blurring the image, and dividing the original image by the blurred one.
This method is very effective. However, I found something even better, Fred's ImageMagick Scripts for text cleaning.
Putting it all together
Results:
Here is a "scan" of the back of the mit career fair book from last year, taken in my dark room.
Which is about as good as possible, give the quality of the light / camera.