markup: matching works

This commit is contained in:
2019-10-18 13:11:53 -07:00
parent be2902ca24
commit 5282f7cb2f
4 changed files with 169 additions and 32 deletions

View File

@ -1,7 +1,7 @@
import os
import shutil
from PIL import Image, ImageFilter
from PIL import Image, ImageFilter, ImageDraw, ImageFont
import numpy
import imutils
import cv2
@ -11,6 +11,8 @@ from pathlib import Path
from django.conf import settings
from .utils import cv2_rect
WORKDIR = os.path.join(settings.ASSET_DIR, 'markup', 'work')
# https://www.pyimagesearch.com/2014/10/20/finding-shapes-images-using-python-opencv/
@ -56,7 +58,8 @@ def find_shapes(image_path):
# if M["m00"] == 0: M["m00"] = 0.00001
# cX = int(M["m10"] / M["m00"])
# cY = int(M["m01"] / M["m00"])
bboxes.append({'x': x, 'y': y, 'w': w, 'h': h})
#print('add contour rect: {}'.format(cv2_rect(x, y, w, h)))
bboxes.append(cv2_rect(x, y, w, h))
# draw contours
contour_image = numpy.zeros((threshold.shape[0], threshold.shape[1], 3), dtype=numpy.uint8)
@ -64,8 +67,8 @@ def find_shapes(image_path):
# compute the center of the contour
color = (rng.randint(0,512), rng.randint(0,512), rng.randint(0,512))
cv2.drawContours(contour_image, contours, i, color)
box = bboxes[i]
cv2.rectangle(contour_image, (box['x'],box['y']), (box['x']+box['w'],box['y']+box['h']), color, 1)
rect = bboxes[i]
cv2.rectangle(contour_image, (rect.left, rect.top), (rect.right, rect.bottom), 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)
@ -76,4 +79,33 @@ def find_shapes(image_path):
os.chmod(contour_path, 0o664)
shutil.chown(contour_path, group='procat')
return bboxes
return img.width, img.height, bboxes
def write_debug_image(cat_name, page_num, prods, scribbles):
path = os.path.join(WORKDIR, "debug-{}-{}.png".format(cat_name, page_num))
pagew = int(11*72)
pageh = int(8.5*72)
img = Image.new('RGBA', (pagew, pageh), 'white')
draw = ImageDraw.Draw(img, 'RGBA')
fnt = ImageFont.truetype('/usr/share/fonts/truetype/lato/Lato-Regular.ttf', 10)
for prod in filter(lambda p: p['page'] == page_num, prods):
rect = prod['rect']
fill_color = "hsv(120, 22%, 100%)" if 'matched' in prod else None
outline_color = "hsv(120, 50%, 100%)"
draw.rectangle((rect.p1(pageh), rect.p2(pageh)),
fill=fill_color, outline=outline_color, width=2)
bl = rect.p1(pageh)
draw.text((bl[0] + 3, bl[1] + 3), prod['material'],
font=fnt, fill="hsv(120, 22%, 50%)")
for scribble in filter(lambda s: s['page'] == page_num, scribbles):
rect = scribble['rect']
draw.rectangle((rect.p1(pageh), rect.p2(pageh)), outline="hsv(210, 22%, 100%)", width=2)
for box in scribble['bboxes']:
draw.rectangle((box.p1(pageh), box.p2(pageh)), outline="hsv(0, 22%, 100%)", width=2)
img.save(path)