Skip to content

Instantly share code, notes, and snippets.

@ghandic
Created July 26, 2018 11:38
Show Gist options
  • Save ghandic/b9c56dcb974e368f7467f7f92ce40052 to your computer and use it in GitHub Desktop.
Save ghandic/b9c56dcb974e368f7467f7f92ce40052 to your computer and use it in GitHub Desktop.
Function to detect fields from a form, may need fine tuning!
from PIL import Image
import numpy as np
import cv2
import time
# Helper function for jupyter notebooks
def showarray(a):
return Image.fromarray(a)
def detect_boxes(img, min_percent_of_area=0.02, max_percent_of_area=2):
# Make a copy of the image
cp_img = img.copy()
gray = cv2.cvtColor(cp_img, cv2.COLOR_RGB2GRAY)
h, w, _ = img.shape
blank_image = np.zeros((h,w), np.uint8)
# Use edge detection to pull out lines, notice aperture size is not default, 5 works best
edges = cv2.Canny(gray,0, 10, apertureSize=5)
# Detect contours using the default settings
_, contours,_ = cv2.findContours(edges,1,2)
# If we find any contours that have 4 corners and within restrictions then add them onto the blank image
for cnt in contours:
approx = cv2.approxPolyDP(cnt,0.02*cv2.arcLength(cnt,True),True)
if len(approx) == 4:
if cv2.contourArea(cnt) > min_percent_of_area*h*w/100 and cv2.contourArea(cnt) < max_percent_of_area*h*w/100:
cv2.drawContours(blank_image,[cnt],0,(255),-1)
# Find all of the contours on the new image we have formed, this time with no restrictions, this now gets
# one box if the contours were overlapping
_, contours,_ = cv2.findContours(blank_image,1,2)
# Now get the miniumum bounding box for each of the contours and return them as top_left and bottom_right
countours_bb = [cv2.boundingRect(cnt) for cnt in contours]
rects = [[(cnt[0], cnt[1]), (cnt[0]+cnt[2], cnt[1]+cnt[3])] for cnt in countours_bb]
return rects
if __name__ == '__main__':
IMAGE_PATH = 'path/to/image.png'
# Read image in
img = cv2.imread(IMAGE_PATH)
# Detect fields
rects = detect_boxes(img)
# Draw predicted boxes on
_ = [cv2.rectangle(img, rect[0], rect[1], (255,0,0), 2) for rect in rects]
# Show image in Jupyter Notebook
showarray(img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment