Skip to content

Instantly share code, notes, and snippets.

@chaeplin
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaeplin/c0cb1fd43fd0cd5f1110 to your computer and use it in GitHub Desktop.
Save chaeplin/c0cb1fd43fd0cd5f1110 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import io
import sys
import time
import picamera
import picamera.array
import cv2
import numpy as np
from tesserwrap import Tesseract
from PIL import Image
from fractions import Fraction
import pyocr
import pyocr.builders
import re
regexpmenu = re.compile("(.*)10163(.*)")
def camimgtostring(image):
kernel_size = 5
scale = 1
delta = 100
ddepth = cv2.CV_32S
img = cv2.GaussianBlur(image,(3,3),0)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray_lap = cv2.Laplacian(gray,ddepth,ksize = kernel_size,scale = scale,delta = delta)
dst = cv2.convertScaleAbs(gray_lap)
upper_img = dst[205:255, 242:283]
m, n = 255, 313
s, t = upper_img.shape
dst[m:m+s, n:n+t] = upper_img
ret,dst = cv2.threshold(dst,204,255,cv2.THRESH_BINARY_INV)
dst = cv2.GaussianBlur(dst,(17,17),0)
crop_img = dst[255:315, 310:610]
tools = pyocr.get_available_tools()
tool = tools[0]
digits = tool.image_to_string(Image.fromarray(crop_img), lang='eng', builder=pyocr.tesseract.DigitBuilder())
a = regexpmenu.findall(digits.strip().replace(' ', ''))
if a:
print '-- menu ---'
else:
print digits
time.sleep(0.1)
with picamera.PiCamera() as camera:
time.sleep(2)
while camera.analog_gain <= 1:
time.sleep(0.1)
camera.shutter_speed = camera.exposure_speed
camera.exposure_mode = 'off'
g = camera.awb_gains
camera.awb_mode = 'off'
camera.awb_gains = g
for i in range(50):
with picamera.array.PiRGBArray(camera) as stream:
camera.capture(stream, format='bgr')
image = stream.array
camimgtostring(image)
stream.truncate(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment