From be2902ca24ca613f5e3be544a035bdde82f3db2d Mon Sep 17 00:00:00 2001 From: Seth Ladygo Date: Thu, 17 Oct 2019 00:40:19 -0700 Subject: [PATCH] markup/img.py: cleanup --- markup/img.py | 88 ++++++++++++------------------------ markup/work/test_scribble.py | 3 +- 2 files changed, 31 insertions(+), 60 deletions(-) diff --git a/markup/img.py b/markup/img.py index e4fb7bd..8b0901c 100644 --- a/markup/img.py +++ b/markup/img.py @@ -1,6 +1,4 @@ -#from __future__ import absolute_import, unicode_literals - -import sys, os.path, re, json, pickle, subprocess +import os import shutil from PIL import Image, ImageFilter @@ -27,83 +25,55 @@ def find_shapes(image_path): 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)) + # alternate method + # blurred = cv2.GaussianBlur(gray, (5, 5), 0) + # thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1] thresh_path = str(path.with_suffix('.thresh.png')) - print('write to', thresh_path) + # print('write to', thresh_path) cv2.imwrite(thresh_path, threshold) + os.chmod(thresh_path, 0o664) + shutil.chown(thresh_path, group='procat') - # 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 = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = imutils.grab_contours(contours) - print("{} shapes".format(len(contours))) - #cv2.imshow("Mask", shapeMask) + # print("{} shapes".format(len(contours))) - # Find the convex hull object for each contour - hull_list = [] - #for i in range(len(contours)): + bboxes = [] for c in contours: - hull = cv2.convexHull(c) - hull_list.append(hull) + # bounding rect + x, y, w, h = cv2.boundingRect(c) + # essentially center of mass + # NOT center of the bbox! + # M = cv2.moments(c) + # 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}) - # Draw contours + hull results - #contour_image = shapeMask.copy() # np.zeros((shapeMask.shape[0], shapeMask.shape[1], 3), dtype=numpy.uint8) + # draw contours 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) + box = bboxes[i] + cv2.rectangle(contour_image, (box['x'],box['y']), (box['x']+box['w'],box['y']+box['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) + #print('write to', contour_path) cv2.imwrite(contour_path, contour_image) + os.chmod(contour_path, 0o664) + shutil.chown(contour_path, group='procat') - # 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) + return bboxes diff --git a/markup/work/test_scribble.py b/markup/work/test_scribble.py index 8fa9389..8d33ad7 100755 --- a/markup/work/test_scribble.py +++ b/markup/work/test_scribble.py @@ -32,7 +32,8 @@ def main(argv): for (k, v) in opts: if k == '-d': debug += 1 - find_shapes(args[0]) + boxes = find_shapes(args[0]) + print(boxes) if __name__ == '__main__': sys.exit(main(sys.argv))