diff --git a/src/shapedetect/Picture.py b/src/shapedetect/Picture.py index 00f2cb9d6c7d7b6a9824bacc758b8143f11ce801..62f1361daa3b0391394bc6217e1ca003f505c91c 100644 --- a/src/shapedetect/Picture.py +++ b/src/shapedetect/Picture.py @@ -3,11 +3,13 @@ import imutils import numpy as np from PIL import Image, ImageDraw, ImageFont from cv2 import imread +import argparse def getCntShapeName(cnt) -> str: # https://www.jianshu.com/p/2731f42882f4 # 初始化图片名称与大概的形状 + global shapeName peri = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, 0.04 * peri, True) @@ -52,7 +54,7 @@ def getCntShapeColor(cnt, img) -> str: return color -def putChineseText(img, text, position=(100, 100), textColor=(0, 255, 0), textSize=20) -> np.ndarray: +def putChineseText(img, text, position=(100, 100), textColor=(0, 255, 0), textSize=15) -> np.ndarray: if isinstance(img, np.ndarray): # 判断是否OpenCV图片类型 img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # 创建一个可以在给定图像上绘图的对象 @@ -71,8 +73,10 @@ class Picture: # 针对图片的操作 def __init__(self, path_to_img) -> None: # 初始化函数,要实例化一个新的对象需要输入文件路径 self.resized = None self.cnts = None + self.position = None self.ratio = 1 self.raw = None + self.hierarchy = None if isinstance(path_to_img, str): self.raw = imread(path_to_img, cv2.IMREAD_COLOR) elif isinstance(path_to_img, np.ndarray): # 判断是否OpenCV图片类型 @@ -108,33 +112,31 @@ class Picture: # 针对图片的操作 def threshold(self, thresh=60, maxval=255) -> None: # 二值化 self.modify = cv2.threshold(self.modify, thresh, maxval, cv2.THRESH_BINARY)[1] - def getCnts(self, resize=300, thresh=60, maxval=255): + def getCnts(self, resize=300, thresh=60, maxval=255, ): + kernel = np.ones((31, 31), np.uint8) self.resize(resize) self.gray() self.blur() - # self.threshold(thresh, maxval) + self.modify = cv2.morphologyEx(self.modify, cv2.MORPH_OPEN, kernel) edge = cv2.Canny(self.modify, 75, 200) - self.cnts = cv2.findContours(edge, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0] + cv2.imshow("canny", edge) + self.cnts, self.hierarchy = cv2.findContours(edge, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + # print(np.size(self.cnts)) + # print(self.hierarchy) def drawShape(self, resize=300, thresh=60, maxval=255): self.getCnts(resize, thresh, maxval) + global shapeName, color, messageOrg for c in self.cnts: - # compute the center of the contour, then detect the name of the - # shape using only the contour - M = cv2.moments(c) cX = int(M["m10"] / (M["m00"] + 1)) cY = int(M["m01"] / (M["m00"] + 1)) shape = getCntShapeName(c) - # multiply the contour (x, y)-coordinates by the resize ratio, # then draw the contours and the name of the shape on the image c = c.astype("float") - # c *= self.ratio c = c.astype("int") cv2.drawContours(self.resized, [c], -1, (0, 255, 0), 2) - # cv2.putText(self.resized, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, - # 0.5, (255, 255, 255), 2) (b, g, r) = self.resized[cY][cX] if r > (g + b): color = "红色" @@ -144,8 +146,11 @@ class Picture: # 针对图片的操作 color = "蓝色" else: color = "色盲了" - # print(r, g, b) self.resized = putChineseText(self.resized, shape + color, (cX, cY), textColor=(255, 0, 255), textSize=20) - # show the output image - # cv2.imshow("Image", self.resized) - # cv2.waitKey(0) + messageOrg = shapeName + color + self.sendMessage() + + def sendMessage(self): + global messageaOrg + message = messageOrg + print(message)