add scribble image processing

This commit is contained in:
2019-10-16 23:55:42 -07:00
parent 6be415a1df
commit 68a658dfe8
5 changed files with 157 additions and 6 deletions

109
markup/img.py Normal file
View File

@ -0,0 +1,109 @@
#from __future__ import absolute_import, unicode_literals
import sys, os.path, re, json, pickle, subprocess
import shutil
from PIL import Image, ImageFilter
import numpy
import imutils
import cv2
import dumper
import random as rng
from pathlib import Path
from django.conf import settings
WORKDIR = os.path.join(settings.ASSET_DIR, 'markup', 'work')
# https://www.pyimagesearch.com/2014/10/20/finding-shapes-images-using-python-opencv/
def find_shapes(image_path):
path = Path(image_path)
img = Image.open(image_path, 'r')
if not img.mode in ('RGBA', 'LA'):
print('no alpha channel: {}'.format(img.mode))
return None
alpha_layer = img.convert('RGBA').split()[-1]
alpha_layer = alpha_layer.filter(ImageFilter.GaussianBlur(5))
threshold = 5
alpha_layer = alpha_layer.point(lambda p: p > threshold and 255)
threshold = numpy.array(alpha_layer)
#bnw = Image.new('RGBA', img.size, (255, 255, 255))
#src = cv2.imread(image_path)
# TODO just get alpha, 1 bit
#threshold = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
#threshold = cv2.blur(threshold, (4,4))
thresh_path = str(path.with_suffix('.thresh.png'))
print('write to', thresh_path)
cv2.imwrite(thresh_path, threshold)
# find all the 'not black' shapes in the image
# upper = np.array([255, 255, 255])
# lower = np.array([15, 15, 15])
# shapeMask = cv2.inRange(image, lower, upper)
# # alternate to below:
# #imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# #ret, thresh = cv2.threshold(imgray, 127, 255, 0)
# #im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# #cv2.imshow('Thresh', thresh)
# #cv2.waitKey(0)
#threshold = 100
#canny_output = cv2.Canny(threshold, threshold, threshold * 2)
#contours = cv2.findContours(canny_output, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#print("contours: {}".format(contours))
# find the contours in the mask
#contours = cv2.findContours(shapeMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#contours = cv2.findContours(shapeMask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
print("{} shapes".format(len(contours)))
#cv2.imshow("Mask", shapeMask)
# Find the convex hull object for each contour
hull_list = []
#for i in range(len(contours)):
for c in contours:
hull = cv2.convexHull(c)
hull_list.append(hull)
# Draw contours + hull results
#contour_image = shapeMask.copy() # np.zeros((shapeMask.shape[0], shapeMask.shape[1], 3), dtype=numpy.uint8)
contour_image = numpy.zeros((threshold.shape[0], threshold.shape[1], 3), dtype=numpy.uint8)
for i in range(len(contours)):
# compute the center of the contour
M = cv2.moments(contours[i])
if M["m00"] == 0: M["m00"] = 0.00001
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
color = (rng.randint(0,512), rng.randint(0,512), rng.randint(0,512))
cv2.drawContours(contour_image, contours, i, color)
#cv2.drawContours(contour_image, hull_list, i, color)
x, y, w, h = cv2.boundingRect(contours[i])
cv2.rectangle(contour_image, (x,y), (x+w,y+h), color, 1)
cv2.circle(contour_image, (cX, cY), 2, color, -1)
# cv2.putText(contour_image, "center", (cX - 20, cY - 15),
# cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
contour_path = str(path.with_suffix('.contour.png'))
print('write to', contour_path)
cv2.imwrite(contour_path, contour_image)
# for c in contours:
# #print json.dumps(c)
# print(dumper.dump(c))
# # draw the contour and show it
# # cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
# # cv2.imshow("Image", image)
# # cv2.waitKey(0)

View File

@ -130,4 +130,4 @@ def parse_pdf(fname, debug=0):
fp.close()
return [prod_boxes, scribbles]
return [list(filter(None, prod_boxes)), list(filter(None, scribbles))]

View File

@ -6,9 +6,8 @@ import inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
parentparentdir = os.path.dirname(parentdir)
sys.path.insert(0, parentparentdir)
sys.path.insert(0, parentparentdir)
import dumper
import getopt
import django
from django.conf import settings
@ -16,8 +15,7 @@ from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'procat2.settings')
django.setup()
from markup.utils import parse_pdf
from procat2.settings import ASSET_DIR
from markup.pdf import parse_pdf
def main(argv):
@ -33,6 +31,8 @@ def main(argv):
for (k, v) in opts:
if k == '-d': debug += 1
parse_pdf(args[0], debug)
(prods, scribbles) = parse_pdf(args[0], debug)
print('prods', scribbles)
if __name__ == '__main__': sys.exit(main(sys.argv))

38
markup/work/test_scribble.py Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env python3
import sys
import os
import inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
parentparentdir = os.path.dirname(parentdir)
sys.path.insert(0, parentparentdir)
#import dumper
import getopt
import django
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'procat2.settings')
django.setup()
from markup.img import find_shapes
def main(argv):
def usage():
print('usage: %s [-d] file ...' % argv[0])
return 100
try:
(opts, args) = getopt.getopt(argv[1:], 'd')
except getopt.GetoptError:
return usage()
if not args: return usage()
debug = 0
for (k, v) in opts:
if k == '-d': debug += 1
find_shapes(args[0])
if __name__ == '__main__': sys.exit(main(sys.argv))