{"id":348124,"date":"2023-05-30T21:00:50","date_gmt":"2023-05-30T21:00:50","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=348124"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=348124","title":{"rendered":"<span>\u0423\u0440\u043e\u043a\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f \u043d\u0430 Python + OpenCV \u0441 \u0441\u0430\u043c\u044b\u0445 \u0430\u0437\u043e\u0432. \u0427\u0430\u0441\u0442\u044c 10. \u041c\u043e\u0439 \u043f\u044d\u0442-\u043f\u0440\u043e\u0435\u043a\u0442<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<p>\u041e\u0433\u043b\u0430\u0432\u043b\u0435\u043d\u0438\u0435: <a href=\"https:\/\/habr.com\/ru\/articles\/688316\/\" rel=\"noopener noreferrer nofollow\">\u0423\u0440\u043e\u043a\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f. \u041e\u0433\u043b\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \/ \u0425\u0430\u0431\u0440 (habr.com)<\/a><\/p>\n<p>\u041d\u0430 <a href=\"https:\/\/habr.com\/ru\/articles\/733444\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0443\u0440\u043e\u043a\u0435<\/a> \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043b \u043e \u0441\u0432\u043e\u0435\u043c \u043f\u044d\u0442-\u043f\u0440\u043e\u0435\u043a\u0442\u0435, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u043c \u0441 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u044b\u043c \u0437\u0440\u0435\u043d\u0438\u0435\u043c. \u0412 \u044d\u0442\u043e\u043c \u0443\u0440\u043e\u043a\u0435 \u0432\u044b \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043b\u0438\u0441\u044c \u0438\u0434\u0435\u0439 \u0438 \u043d\u0430\u0431\u0440\u043e\u0441\u043a\u043e\u043c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u044d\u0442\u043e\u0433\u043e \u043f\u044d\u0442-\u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0443 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c, \u043a\u0430\u043a \u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u043b \u0432 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u043e\u0432\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0438 \u0447\u0442\u043e \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0448\u043b\u043e.&nbsp; \u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u0438\u0434\u0435\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043b\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u043d\u0430\u0447\u0430\u0432 \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u0435 \u043d\u043e\u043c\u0435\u0440\u043e\u0432. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0430 \u0432\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 tesseract \u043f\u043b\u043e\u0445\u043e \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0435\u0442 \u0446\u0438\u0444\u0440\u044b. \u0411\u044b\u043b\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0441\u0432\u043e\u044e \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043b\u043a\u0443 \u0434\u043b\u044f \u0446\u0438\u0444\u0440. \u041d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u0434\u043e \u043a\u0430\u043a-\u0442\u043e \u043d\u0430\u0439\u0442\u0438, \u0433\u0434\u0435 \u044d\u0442\u0438 \u0446\u0438\u0444\u0440\u044b \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438. <\/p>\n<p>\u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u043a\u0430\u043a\u0438\u0435 \u0448\u0430\u0433\u0438 \u0431\u044b\u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u043d\u044b \u043d\u0430 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0443\u0440\u043e\u043a\u0435:<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u043d\u0443\u044e \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e.<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u041f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u0431\u0438\u043d\u0430\u0440\u0438\u0437\u0430\u0446\u0438\u044e.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043f\u043e\u0439\u0434\u0435\u043c \u0447\u0443\u0442\u044c \u0434\u0430\u043b\u044c\u0448\u0435: \u0432\u044b\u0434\u0435\u043b\u0438\u043c \u043a\u043e\u043d\u0442\u0443\u0440 \u0438 \u043d\u0430\u0439\u0434\u0435\u043c \u043d\u0430 \u043d\u0435\u043c \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u043a\u0430. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0443\u0440\u0430:<\/p>\n<pre><code class=\"python\">class ContourProcessingStep(ImageProcessingStep):     \"\"\"\u0428\u0430\u0433, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0443\u0440\u043e\u0432\"\"\"      def process(self,info):         \"\"\"\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443\"\"\"          contours, hierarchy  = cv2.findContours(info.image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)          height, width = info.image.shape[:2]         contours_image = np.zeros((height, width, 3), dtype=np.uint8)          # \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0443\u0440\u044b         cv2.drawContours(contours_image, contours, -1, (255, 0, 0), 1, cv2.LINE_AA, hierarchy, 1)          #\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435         new_info=ImageInfo(contours_image)         new_info.contours=contours         new_info.hierarchy=hierarchy          return new_info<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u043d\u0442\u0443\u0440 \u043d\u0430\u0434\u043e \u0430\u043f\u043f\u0440\u043e\u043a\u0441\u0438\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043b\u0430\u0441\u0441:<\/p>\n<pre><code class=\"python\">class ContourApproximationProcessingStep(ImageProcessingStep):     \"\"\"\u0428\u0430\u0433, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u0430\u043f\u0440\u043e\u043a\u0441\u0438\u043c\u0430\u0446\u0438\u044e \u043a\u043e\u043d\u0442\u0443\u0440\u043e\u0432\"\"\"      def __init__(self,eps = 0.005, filter=None):         \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440         eps - \u0440\u0430\u0437\u043c\u0435\u0440 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u043d\u0442\u0443\u0440\u0430 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043e\u0431\u0449\u0435\u0439 \u0434\u0443\u0433\u0438\"\"\"          self.eps=eps         self.filter=filter      def process(self, info):         \"\"\"\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443\"\"\"          approx_countours=[]         img_contours = np.uint8(np.zeros((info.image.shape[0], info.image.shape[1])))         for countour in info.contours:             arclen = cv2.arcLength(countour, True)             epsilon = arclen * self.eps             approx = cv2.approxPolyDP(countour, epsilon, True)             append=False             if not(self.filter is None):                 if self.filter(approx):                     append=True             else:                 append=True             if append:                 approx_countours.append(approx)          cv2.drawContours(img_contours, approx_countours, -1, (255, 255, 255), 1)          #\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435         new_info=ImageInfo(img_contours)         new_info.contours=approx_countours          return new_info<\/code><\/pre>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e \u043a\u0430\u043a\u043e\u043c\u0443-\u043b\u0438\u0431\u043e \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u044e \u043e\u0442\u0431\u0435\u0440\u0435\u0442 \u043a\u043e\u043d\u0442\u0443\u0440\u044b (\u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0443\u0440).<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0438\u0441\u043f\u044b\u0442\u044b\u0432\u0430\u0435\u043c, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0431\u0435\u0437 \u0444\u0438\u043b\u044c\u0442\u0440\u0430:<\/p>\n<pre><code class=\"python\">import cv2  from Libraries.Core import Engine from Libraries.ImageProcessingSteps import MedianBlurProcessingStep, ThresholdProcessingStep, ContourProcessingStep, \\     ContourApproximationProcessingStep  def my_filter(approx):     if len(approx)==4:         return True     return False  my_photo = cv2.imread('..\/Photos\/car.jpg') core=Engine() core.steps.append(MedianBlurProcessingStep(5)) core.steps.append(ThresholdProcessingStep()) core.steps.append(ContourProcessingStep()) core.steps.append(ContourApproximationProcessingStep(0.02)) #core.steps.append(ContourApproximationProcessingStep(0.02,my_filter)) res,history=core.process(my_photo)  i=1 for info in history:     cv2.imshow('image'+str(i), info.image) # \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u043e\u043a\u043d\u043e     i=i+1 cv2.imshow('res', res.image)  cv2.waitKey() cv2.destroyAllWindows()<\/code><\/pre>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c40\/b4b\/96a\/c40b4b96acc73660bd6e3ca92edb977f.png\" width=\"647\" height=\"348\"><\/figure>\n<p>\u0417\u0434\u0435\u0441\u044c \u0434\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u043d\u043e\u0435 \u0444\u043e\u0442\u043e \u043c\u0430\u0448\u0438\u043d\u044b. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0440\u0430\u0437\u043c\u0435\u0440\u043d\u0443\u044e \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u044e:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d5f\/d57\/f1a\/d5fd57f1a8c4823efff2169c0f3a626c.png\" width=\"646\" height=\"497\"><\/figure>\n<p>\u0418\u0442\u0430\u043a, \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a (\u0434\u0430, \u043e\u043d \u043a\u0440\u0438\u0432\u043e\u0439, \u043d\u043e \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u0438\u0433\u0443\u0440\u044b \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a). <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0442\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441: \u0430 \u043a\u0430\u043a \u043d\u0430\u043c \u0441\u0440\u0435\u0434\u0438 \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445 \u043b\u0438\u043d\u0438\u0439 \u043d\u0430\u0439\u0442\u0438 \u043d\u0430\u0448 \u00ab\u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u00bb? \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0444\u0438\u043b\u044c\u0442\u0440 (\u0432\u043e\u0442 \u043e\u043d \u043d\u0430\u043c \u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0441\u044f), \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0432 \u0432\u0441\u0435 \u0444\u0438\u0433\u0443\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u0435\u0442\u044b\u0440\u0435\u0445\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430\u043c\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0432\u044b \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438, \u0432 \u0442\u0435\u043a\u0441\u0442\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f:<\/p>\n<pre><code class=\"python\">def my_filter(approx):     if len(approx)==4:         return True     return False<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0432\u043e\u0442 \u044d\u0442\u0443 \u0441\u0442\u0440\u043e\u0447\u043a\u0443 \u043a\u043e\u0434\u0430<\/p>\n<pre><code class=\"python\">core.steps.append(ContourApproximationProcessingStep(0.02))<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u0443:<\/p>\n<pre><code class=\"python\">core.steps.append(ContourApproximationProcessingStep(0.02,my_filter))<\/code><\/pre>\n<p>\u0418 \u0432\u0443\u0430\u043b\u044f, \u0443 \u043d\u0430\u0441 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0442\u044b\u0440\u0435\u0445\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0438:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8ee\/fb3\/f6d\/8eefb3f6d0672681fe89507071fa2ba7.png\" width=\"643\" height=\"495\"><\/figure>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u043d\u043e \u0432\u0441\u0435 \u0435\u0449\u0435 \u043c\u043d\u043e\u0433\u043e \u043c\u0443\u0441\u043e\u0440\u0430. \u041e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u043c \u0435\u0433\u043e, \u0443\u0431\u0440\u0430\u0432 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b:<\/p>\n<pre><code class=\"python\">def my_filter(approx):     if len(approx)==4:         if abs(approx[2,0,0]-approx[0,0,0])&lt;10:             return False         if abs(approx[2,0,1]-approx[0,0,1])&lt;10:             return False         return True     return False<\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0f8\/d76\/15e\/0f8d7615e43bb701851c7db8ab8cc6e0.png\" width=\"653\" height=\"511\"><\/figure>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0432\u0441\u0435\u0433\u043e 5 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u041f\u043e \u0438\u0434\u0435\u0435, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u0438\u043c\u0435\u044e\u0449\u0438\u0435 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u044b \u0438 \u0448\u0438\u0440\u0438\u043d\u044b (\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0439 \u0437\u043d\u0430\u043a \u0438\u043c\u0435\u0435\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043f\u043e \u0413\u041e\u0421\u0422, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0438 \u0448\u0438\u0440\u0438\u043d\u044b \u0443 \u043d\u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435). \u041c\u043e\u0436\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u044f\u0432\u043d\u043e \u00ab\u043a\u0440\u0438\u0432\u044b\u0435 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0438\u00bb \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u0434\u043b\u0438\u043d\u0430\u0445 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0442\u043e\u0440\u043e\u043d \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0448\u0435 \u0443\u0440\u043e\u0432\u043d\u044f \u043f\u043e\u0433\u0440\u0435\u0448\u043d\u043e\u0441\u0442\u0438. \u041f\u0440\u0430\u0432\u0434\u0430, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u0432 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043e\u0434\u043d\u043e \u0438 \u043d\u0443\u0436\u043d\u044b\u0435 \u0434\u043e \u043a\u0443\u0447\u0438 \u0432\u044b\u043a\u0438\u043d\u0443\u0442\u044c. \u0422\u0430\u043a \u0447\u0442\u043e \u0442\u0443\u0442 \u043d\u0430\u0434\u043e \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u044c. <\/p>\n<p>\u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u043e\u0440\u043e\u043d:<\/p>\n<pre><code class=\"python\">def dist(point1,point2):     d1 = point1[0] - point2[0]     d2 = point1[1] - point2[1]     return math.sqrt(d1*d1+d2*d2)<\/code><\/pre>\n<p>\u0418 \u0432\u043d\u0435\u0441\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u0444\u0438\u043b\u044c\u0442\u0440:<\/p>\n<pre><code class=\"python\">def my_filter(approx):     if len(approx)==4:         if abs(approx[2,0,0]-approx[0,0,0])&lt;10:             return False         if abs(approx[2,0,1]-approx[0,0,1])&lt;10:             return False         if abs(dist(approx[0,0],approx[1,0])\/dist(approx[2,0],approx[3,0])-1)&gt;0.4:             return False         if abs(dist(approx[0,0],approx[3,0])\/dist(approx[1,0],approx[2,0])-1)&gt;0.4:             return False         return True     return False<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9ef\/e2e\/181\/9efe2e181ed7aefd86337c818552cf1e.png\" width=\"638\" height=\"493\"><\/figure>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0440\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041e\u0434\u0438\u043d \u0438\u0437 \u043d\u0438\u0445, \u043a\u0441\u0442\u0430\u0442\u0438, \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0440\u0430\u0441\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0441 \u043f\u0440\u044f\u043c\u044b\u043c\u0438 \u0443\u0433\u043b\u0430\u043c\u0438 (\u0435\u0441\u043b\u0438 \u0443\u0433\u043e\u043b \u0441\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u043a\u043b\u043e\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0442 90 \u0433\u0440\u0430\u0434\u0443\u0441\u043e\u0432). \u041d\u043e \u043c\u044b \u043f\u043e\u043a\u0430 \u044d\u0442\u043e\u0433\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c, \u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u043b\u0438\u0448\u043d\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u2013 \u043d\u0435 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u043e. <\/p>\n<p>\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u043d\u043e\u043c\u0435\u0440\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0434\u043e \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043a\u043e\u043d\u0442\u0443\u0440\u0430 \u0442\u043e\u0447\u043a\u0438. \u0412\u043e\u0442 \u043a\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043a\u043e\u043d\u0442\u0443\u0440\u0430 \u0434\u0432\u0443\u0445 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"python\">x1=res.contours[0][0][0][0] y1=res.contours[0][0][0][1] x2=res.contours[0][2][0][0] y2=res.contours[0][2][0][1] cv2.rectangle(finish_result,(x1,y1),(x2,y2),(255,0,0),3)<\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u0447\u0442\u043e \u0432 \u0438\u0442\u043e\u0433\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u043d\u043e:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/70d\/c3e\/5e7\/70dc3e5e712c974fe2544bbf72932d2c.png\" width=\"533\" height=\"298\"><\/figure>\n<p>\u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043d\u0430\u0434\u043e. \u0410 \u043d\u0430\u0434\u043e, \u043d\u0443, \u0445\u043e\u0442\u044f \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u044b \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u043b\u0430 \u044d\u0442\u0438 \u0442\u043e\u0447\u043a\u0438:<\/p>\n<pre><code class=\"python\">def get_rect(countur_item):     x1 = countur_item[0][0][0]     y1 = countur_item[0][0][1]     x2 = countur_item[2][0][0]     y2 = countur_item[2][0][1]     return (x1,y1), (x2,y2)<\/code><\/pre>\n<p>\u0418 \u0442\u043e\u0433\u0434\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0444\u0438\u0433\u0443\u0440\u0443 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">p1,p2=get_rect(res.contours[0]) cv2.rectangle(finish_result,p1,p2,(255,0,0),3)<\/code><\/pre>\n<p>\u0410 \u0432\u0441\u0435 \u0444\u0438\u0433\u0443\u0440\u044b \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">for item in res.contours:     p1,p2=get_rect(item)     cv2.rectangle(finish_result,p1,p2,(255,0,0),3)<\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/23e\/8d6\/b75\/23e8d6b752be50afdd21f3ba2cde5e6f.png\" width=\"648\" height=\"415\"><\/figure>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u0438 \u0442\u0440\u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u0438, \u043f\u043e\u0438\u0441\u043a\u0430\u0442\u044c \u0442\u0430\u043c \u0431\u0443\u043a\u043e\u0432\u043a\u0438 \u0438 \u0446\u0438\u0444\u0435\u0440\u043a\u0438. \u041d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430\u0432\u0435\u0441\u0442\u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432 \u043a\u043e\u0434\u0435. \u0412\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0443 \u043d\u0430\u0441 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b run2.py:<\/p>\n<pre><code class=\"python\">import cv2 import math  from Libraries.Core import Engine from Libraries.ImageProcessingSteps import MedianBlurProcessingStep, ThresholdProcessingStep, ContourProcessingStep, \\     ContourApproximationProcessingStep  def dist(point1,point2):     d1 = point1[0] - point2[0]     d2 = point1[1] - point2[1]     return math.sqrt(d1*d1+d2*d2)  def my_filter(approx):     if len(approx)==4:         if abs(approx[2,0,0]-approx[0,0,0])&lt;10:             return False         if abs(approx[2,0,1]-approx[0,0,1])&lt;10:             return False         if abs(dist(approx[0,0],approx[1,0])\/dist(approx[2,0],approx[3,0])-1)&gt;0.4:             return False         if abs(dist(approx[0,0],approx[3,0])\/dist(approx[1,0],approx[2,0])-1)&gt;0.4:             return False         return True     return False  def get_rect(countur_item):     x1 = countur_item[0][0][0]     y1 = countur_item[0][0][1]     x2 = countur_item[2][0][0]     y2 = countur_item[2][0][1]     return (x1,y1), (x2,y2)  my_photo = cv2.imread('..\/Photos\/6108249.jpg') #my_photo = cv2.imread('..\/Photos\/car.jpg') core=Engine() core.steps.append(MedianBlurProcessingStep(5)) core.steps.append(ThresholdProcessingStep()) core.steps.append(ContourProcessingStep()) #core.steps.append(ContourApproximationProcessingStep(0.02)) core.steps.append(ContourApproximationProcessingStep(0.02,my_filter)) res,history=core.process(my_photo)  i=1 for info in history:     cv2.imshow('image'+str(i), info.image) # \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u043e\u043a\u043d\u043e     i=i+1 cv2.imshow('res', res.image)  finish_result = history[0].image.copy()  for item in res.contours:     p1,p2=get_rect(item)     cv2.rectangle(finish_result,p1,p2,(255,0,0),3) cv2.imshow('Finish', finish_result)   cv2.waitKey() cv2.destroyAllWindows()<\/code><\/pre>\n<p>\u041d\u0435 \u043e\u0447\u0435\u043d\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u043e, \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 Libraries \u0444\u0430\u0439\u043b Utils.py \u0438 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u043c \u0442\u0443\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 get_rect \u0438 dist. \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">from Libraries.Utils import dist, get_rect<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">import cv2  from Libraries.Core import Engine from Libraries.ImageProcessingSteps import MedianBlurProcessingStep, ThresholdProcessingStep, ContourProcessingStep, \\     ContourApproximationProcessingStep from Libraries.Utils import dist, get_rect, show_history   def my_filter(approx):     if len(approx)==4:         if abs(approx[2,0,0]-approx[0,0,0])&lt;10:             return False         if abs(approx[2,0,1]-approx[0,0,1])&lt;10:             return False         if abs(dist(approx[0,0],approx[1,0])\/dist(approx[2,0],approx[3,0])-1)&gt;0.4:             return False         if abs(dist(approx[0,0],approx[3,0])\/dist(approx[1,0],approx[2,0])-1)&gt;0.4:             return False         return True     return False   my_photo = cv2.imread('..\/Photos\/6108249.jpg') #my_photo = cv2.imread('..\/Photos\/car.jpg') core=Engine() core.steps.append(MedianBlurProcessingStep(5)) core.steps.append(ThresholdProcessingStep()) core.steps.append(ContourProcessingStep()) #core.steps.append(ContourApproximationProcessingStep(0.02)) core.steps.append(ContourApproximationProcessingStep(0.02,my_filter)) res,history=core.process(my_photo)  show_history(res,history)  finish_result = history[0].image.copy()  for item in res.contours:     p1, p2 = get_rect(item)     cv2.rectangle(finish_result, p1, p2, (255, 0, 0), 3) cv2.imshow('Finish', finish_result)   cv2.waitKey() cv2.destroyAllWindows()<\/code><\/pre>\n<p>\u0410 \u0444\u0430\u0439\u043b \u0443\u0442\u0438\u043b\u0438\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">import math import cv2  def get_rect(countur_item):     x1 = countur_item[0][0][0]     y1 = countur_item[0][0][1]     x2 = countur_item[2][0][0]     y2 = countur_item[2][0][1]     return (x1,y1), (x2,y2)  def dist(point1,point2):     d1 = point1[0] - point2[0]     d2 = point1[1] - point2[1]     return math.sqrt(d1*d1+d2*d2)  def show_history(res,history):     i = 1     for info in history:         cv2.imshow('image' + str(i), info.image)  # \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u043e\u043a\u043d\u043e         i = i + 1     cv2.imshow('res', res.image)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u0443\u043c\u0430\u0442\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u00ab\u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u0442\u044c\u00bb \u043d\u043e\u043c\u0435\u0440. \u041d\u0430 \u044d\u0442\u043e\u043c \u0443\u0440\u043e\u043a\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u043a\u0430\u043a \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u0446\u0438\u0444\u0440\u044b, \u0430 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043b\u043a\u0443 \u0431\u0443\u0434\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0443\u0440\u043e\u043a\u0435. <\/p>\n<p>\u0418 \u0442\u0430\u043a, \u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u0435\u0441\u044c, \u0435\u0433\u043e \u0412\u0435\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e Keras. \u0427\u0442\u043e\u0431\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u043f\u043e\u0434 Windows, c\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0442\u0430\u0432\u0438\u043c TensorFlow:<\/p>\n<p><code>pip3 install tensorflow<\/code><\/p>\n<p>\u0430 \u0437\u0430\u0442\u0435\u043c \u0438 \u0441\u0430\u043c Keras:<\/p>\n<p><code>pip3 install Keras<\/code><\/p>\n<p>\u041d\u0443,  \u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u043d\u0430 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u043c \u0432 \u043a\u0435\u0440\u0430\u0441 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0435 minst:<\/p>\n<pre><code class=\"python\">from keras import layers from keras import models  from keras.datasets import mnist import tensorflow as tf   model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(10, activation='softmax'))  (train_images, train_labels), (test_images, test_labels) = mnist.load_data() train_images = train_images.reshape((60000, 28, 28, 1)) train_images = train_images.astype('float32') \/ 255 test_images = test_images.reshape((10000, 28, 28, 1)) test_images = test_images.astype('float32') \/ 255 train_labels = tf.keras.utils.to_categorical(train_labels) test_labels = tf.keras.utils.to_categorical(test_labels)  model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(train_images, train_labels, epochs=5, batch_size=64)  test_loss, test_acc = model.evaluate(test_images, test_labels) print(test_acc)<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u0432\u0435\u0440\u0442\u043e\u0447\u043d\u0430\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c, \u043d\u0430 \u0432\u0445\u043e\u0434\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0447\u0435\u0440\u043d\u043e-\u0431\u0435\u043b\u0430\u044f \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430 28 \u043d\u0430 28 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0434\u0435\u0441\u044f\u0442\u0438\u0437\u043d\u0430\u0447\u043d\u044b\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0435\u0439, \u0447\u0442\u043e \u043d\u0430 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0435 \u0442\u0430 \u0438\u043b\u0438 \u0438\u043d\u0430\u044f \u0446\u0438\u0444\u0440\u0430. \u041c\u043e\u0434\u0435\u043b\u044c \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043a\u0430 99% \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435. <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0432\u0435\u0440\u0438\u0442\u0435, \u0447\u0442\u043e \u0432 mnist \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0446\u0438\u0444\u0440\u044b, \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0432 \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0430:<\/p>\n<pre><code class=\"python\">from keras.datasets import mnist import cv2  (train_images, train_labels), (test_images, test_labels) = mnist.load_data() print(train_labels)  cv2.imshow(\"\u0426\u0438\u0444\u0440\u0430\", train_images[0]) cv2.waitKey(0) cv2.destroyAllWindows()<\/code><\/pre>\n<p>\u0414\u043b\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u043e\u043c\u0435\u0440 \u043d\u0443\u043b\u044c \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c \u0446\u0438\u0444\u0440\u0443 5:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4b7\/c66\/242\/4b7c6624232b33d458ea1be126c65779.png\" width=\"130\" height=\"67\"><\/figure>\n<p>\u0418 \u044d\u0442\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u044d\u0439\u0431\u043b 5:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dce\/016\/647\/dce0166475aedf76de768b9673cea994.png\" width=\"302\" height=\"150\"><\/figure>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043a\u043e\u0440\u043c\u0438\u0442\u044c \u043a\u0430\u043a\u043e\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438. \u041a\u0441\u0442\u0430\u0442\u0438, \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u044b \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u043e\u0431\u0443\u0447\u0438\u043b\u0438, \u0435\u0435 \u0445\u043e\u0440\u043e\u0448\u043e \u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c:<\/p>\n<pre><code class=\"python\">model.save('cats_and_dogs_small_1.h5')<\/code><\/pre>\n<p>\u041d\u0443 \u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u0441 \u0446\u0438\u0444\u0440\u043e\u0439 \u0438 \u0434\u0430\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u043a\u0435 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0442\u044c \u0435\u0435:<\/p>\n<pre><code class=\"python\">import cv2 from keras import models  my_photo = cv2.imread('imgs\/Digit0.png',cv2.IMREAD_GRAYSCALE) #\u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435  #\u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043a \u0444\u043e\u0440\u043c\u0430\u0442\u0443 \u0434\u043b\u044f \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 normal_photo=my_photo\/255.0 input=normal_photo.reshape(1,28,28)  #\u0441\u043a\u043e\u0440\u043c\u0438\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u043a\u0435 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 model = models.load_model('mnist_model.bin') result=model.predict(input)  print(result)<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/135\/122\/87e\/13512287ede41669310003eaec38c6cb.png\" width=\"231\" height=\"234\"><\/figure>\n<p>\u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435:<\/p>\n<p><code>[[9.9990845e-01 1.4144711e-08 8.4316625e-08 3.7920216e-11 2.4454723e-06<\/code><\/p>\n<p><code>&nbsp; 4.7663391e-08 8.7873021e-05 4.1903621e-07 3.8488349e-08 6.0560058e-07]]<\/code><\/p>\n<p>\u0412\u0438\u0434\u043d\u043e, \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 (\u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0443\u043b\u0435\u0432\u043e\u0439, \u0441\u0447\u0435\u0442 \u0441 \u043d\u0443\u043b\u044f) \u044f\u0447\u0435\u0439\u043a\u0435 (\u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0446\u0438\u0444\u0440\u0435 0) \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0447\u0442\u0438 1, \u0432 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0447\u0442\u0438 0.<\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0435\u0442 \u0435\u0434\u0438\u043d\u0438\u0446\u0443:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8dd\/a3a\/93a\/8dda3a93a71c315d8156d980f574ff7e.png\" width=\"235\" height=\"233\"><\/figure>\n<p><code>[[5.2682775e-08 9.9998152e-01 9.0230742e-07 1.2926430e-09 9.7749239e-07<\/code><\/p>\n<p><code>&nbsp; 6.3665328e-07 5.2730784e-06 1.0716837e-05 2.0985880e-08 1.3917042e-11]]<\/code><\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u0438 \u0442\u0443\u0442 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u043b \u0446\u0438\u0444\u0435\u0440\u043a\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e.<\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0435, \u0437\u0430\u0441\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u044c \u0432 \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u00ab\u043a\u0440\u0430\u0441\u0438\u0432\u044b\u043c\u00bb \u043a\u043e\u0434\u043e\u043c \u0431\u0443\u0434\u0435\u043c \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0430\u0437.<\/p>\n<p>\u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0437\u0434\u0435\u0441\u044c: <a href=\"https:\/\/github.com\/megabax\/CVContainer\" rel=\"noopener noreferrer nofollow\">megabax\/CVContainer: It is my pet computer vision project. (github.com)<\/a><\/p>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/738698\/\"> https:\/\/habr.com\/ru\/articles\/738698\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<p>\u041e\u0433\u043b\u0430\u0432\u043b\u0435\u043d\u0438\u0435: <a href=\"https:\/\/habr.com\/ru\/articles\/688316\/\" rel=\"noopener noreferrer nofollow\">\u0423\u0440\u043e\u043a\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f. \u041e\u0433\u043b\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \/ \u0425\u0430\u0431\u0440 (habr.com)<\/a><\/p>\n<p>\u041d\u0430 <a href=\"https:\/\/habr.com\/ru\/articles\/733444\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0443\u0440\u043e\u043a\u0435<\/a> \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043b \u043e \u0441\u0432\u043e\u0435\u043c \u043f\u044d\u0442-\u043f\u0440\u043e\u0435\u043a\u0442\u0435, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u043c \u0441 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u044b\u043c \u0437\u0440\u0435\u043d\u0438\u0435\u043c. \u0412 \u044d\u0442\u043e\u043c \u0443\u0440\u043e\u043a\u0435 \u0432\u044b \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043b\u0438\u0441\u044c \u0438\u0434\u0435\u0439 \u0438 \u043d\u0430\u0431\u0440\u043e\u0441\u043a\u043e\u043c \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u044d\u0442\u043e\u0433\u043e \u043f\u044d\u0442-\u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0443 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c, \u043a\u0430\u043a \u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u043b \u0432 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u043e\u0432\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0438 \u0447\u0442\u043e \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0448\u043b\u043e.&nbsp; \u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u0438\u0434\u0435\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043b\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u043d\u0430\u0447\u0430\u0432 \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u0435 \u043d\u043e\u043c\u0435\u0440\u043e\u0432. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0430 \u0432\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 tesseract \u043f\u043b\u043e\u0445\u043e \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0435\u0442 \u0446\u0438\u0444\u0440\u044b. \u0411\u044b\u043b\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0441\u0432\u043e\u044e \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043b\u043a\u0443 \u0434\u043b\u044f \u0446\u0438\u0444\u0440. \u041d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u0434\u043e \u043a\u0430\u043a-\u0442\u043e \u043d\u0430\u0439\u0442\u0438, \u0433\u0434\u0435 \u044d\u0442\u0438 \u0446\u0438\u0444\u0440\u044b \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438. <\/p>\n<p>\u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u043a\u0430\u043a\u0438\u0435 \u0448\u0430\u0433\u0438 \u0431\u044b\u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u043d\u044b \u043d\u0430 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0443\u0440\u043e\u043a\u0435:<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u043d\u0443\u044e \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e.<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u041f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u0431\u0438\u043d\u0430\u0440\u0438\u0437\u0430\u0446\u0438\u044e.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043f\u043e\u0439\u0434\u0435\u043c \u0447\u0443\u0442\u044c \u0434\u0430\u043b\u044c\u0448\u0435: \u0432\u044b\u0434\u0435\u043b\u0438\u043c \u043a\u043e\u043d\u0442\u0443\u0440 \u0438 \u043d\u0430\u0439\u0434\u0435\u043c \u043d\u0430 \u043d\u0435\u043c \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a \u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u043a\u0430. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0443\u0440\u0430:<\/p>\n<pre><code class=\"python\">class ContourProcessingStep(ImageProcessingStep):     \"\"\"\u0428\u0430\u0433, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0442\u0443\u0440\u043e\u0432\"\"\"      def process(self,info):         \"\"\"\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443\"\"\"          contours, hierarchy  = cv2.findContours(info.image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)          height, width = info.image.shape[:2]         contours_image = np.zeros((height, width, 3), dtype=np.uint8)          # \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u043a\u043e\u043d\u0442\u0443\u0440\u044b         cv2.drawContours(contours_image, contours, -1, (255, 0, 0), 1, cv2.LINE_AA, hierarchy, 1)          #\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435         new_info=ImageInfo(contours_image)         new_info.contours=contours         new_info.hierarchy=hierarchy          return new_info<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u043d\u0442\u0443\u0440 \u043d\u0430\u0434\u043e \u0430\u043f\u043f\u0440\u043e\u043a\u0441\u0438\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043b\u0430\u0441\u0441:<\/p>\n<pre><code class=\"python\">class ContourApproximationProcessingStep(ImageProcessingStep):     \"\"\"\u0428\u0430\u0433, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u0430\u043f\u0440\u043e\u043a\u0441\u0438\u043c\u0430\u0446\u0438\u044e \u043a\u043e\u043d\u0442\u0443\u0440\u043e\u0432\"\"\"      def __init__(self,eps = 0.005, filter=None):         \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440         eps - \u0440\u0430\u0437\u043c\u0435\u0440 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u043d\u0442\u0443\u0440\u0430 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043e\u0431\u0449\u0435\u0439 \u0434\u0443\u0433\u0438\"\"\"          self.eps=eps         self.filter=filter      def process(self, info):         \"\"\"\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443\"\"\"          approx_countours=[]         img_contours = np.uint8(np.zeros((info.image.shape[0], info.image.shape[1])))         for countour in info.contours:             arclen = cv2.arcLength(countour, True)             epsilon = arclen * self.eps             approx = cv2.approxPolyDP(countour, epsilon, True)             append=False             if not(self.filter is None):                 if self.filter(approx):                     append=True             else:                 append=True             if append:                 approx_countours.append(approx)          cv2.drawContours(img_contours, approx_countours, -1, (255, 255, 255), 1)          #\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435         new_info=ImageInfo(img_contours)         new_info.contours=approx_countours          return new_info<\/code><\/pre>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e \u043a\u0430\u043a\u043e\u043c\u0443-\u043b\u0438\u0431\u043e \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u044e \u043e\u0442\u0431\u0435\u0440\u0435\u0442 \u043a\u043e\u043d\u0442\u0443\u0440\u044b (\u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0443\u0440).<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0438\u0441\u043f\u044b\u0442\u044b\u0432\u0430\u0435\u043c, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0431\u0435\u0437 \u0444\u0438\u043b\u044c\u0442\u0440\u0430:<\/p>\n<pre><code class=\"python\">import cv2  from Libraries.Core import Engine from Libraries.ImageProcessingSteps import MedianBlurProcessingStep, ThresholdProcessingStep, ContourProcessingStep, \\     ContourApproximationProcessingStep  def my_filter(approx):     if len(approx)==4:         return True     return False  my_photo = cv2.imread('..\/Photos\/car.jpg') core=Engine() core.steps.append(MedianBlurProcessingStep(5)) core.steps.append(ThresholdProcessingStep()) core.steps.append(ContourProcessingStep()) core.steps.append(ContourApproximationProcessingStep(0.02)) #core.steps.append(ContourApproximationProcessingStep(0.02,my_filter)) res,history=core.process(my_photo)  i=1 for info in history:     cv2.imshow('image'+str(i), info.image) # \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u043e\u043a\u043d\u043e     i=i+1 cv2.imshow('res', res.image)  cv2.waitKey() cv2.destroyAllWindows()<\/code><\/pre>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0417\u0434\u0435\u0441\u044c \u0434\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u044f \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u043d\u043e\u0435 \u0444\u043e\u0442\u043e \u043c\u0430\u0448\u0438\u043d\u044b. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0440\u0430\u0437\u043c\u0435\u0440\u043d\u0443\u044e \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u044e:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0418\u0442\u0430\u043a, \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a (\u0434\u0430, \u043e\u043d \u043a\u0440\u0438\u0432\u043e\u0439, \u043d\u043e \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u0438\u0433\u0443\u0440\u044b \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a). <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0442\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441: \u0430 \u043a\u0430\u043a \u043d\u0430\u043c \u0441\u0440\u0435\u0434\u0438 \u0432\u0441\u0435\u0445 \u044d\u0442\u0438\u0445 \u043b\u0438\u043d\u0438\u0439 \u043d\u0430\u0439\u0442\u0438 \u043d\u0430\u0448 \u00ab\u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u00bb? \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0444\u0438\u043b\u044c\u0442\u0440 (\u0432\u043e\u0442 \u043e\u043d \u043d\u0430\u043c \u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u0441\u044f), \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0432 \u0432\u0441\u0435 \u0444\u0438\u0433\u0443\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u0435\u0442\u044b\u0440\u0435\u0445\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0430\u043c\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0432\u044b \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438, \u0432 \u0442\u0435\u043a\u0441\u0442\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f:<\/p>\n<pre><code class=\"python\">def my_filter(approx):     if len(approx)==4:         return True     return False<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0432\u043e\u0442 \u044d\u0442\u0443 \u0441\u0442\u0440\u043e\u0447\u043a\u0443 \u043a\u043e\u0434\u0430<\/p>\n<pre><code class=\"python\">core.steps.append(ContourApproximationProcessingStep(0.02))<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u0443:<\/p>\n<pre><code class=\"python\">core.steps.append(ContourApproximationProcessingStep(0.02,my_filter))<\/code><\/pre>\n<p>\u0418 \u0432\u0443\u0430\u043b\u044f, \u0443 \u043d\u0430\u0441 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0442\u044b\u0440\u0435\u0445\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0438:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u043d\u043e \u0432\u0441\u0435 \u0435\u0449\u0435 \u043c\u043d\u043e\u0433\u043e \u043c\u0443\u0441\u043e\u0440\u0430. \u041e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u0443\u0435\u043c \u0435\u0433\u043e, \u0443\u0431\u0440\u0430\u0432 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b:<\/p>\n<pre><code class=\"python\">def my_filter(approx):     if len(approx)==4:         if abs(approx[2,0,0]-approx[0,0,0])&lt;10:             return False         if abs(approx[2,0,1]-approx[0,0,1])&lt;10:             return False         return True     return False<\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0432\u0441\u0435\u0433\u043e 5 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432. \u041f\u043e \u0438\u0434\u0435\u0435, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u0438\u043c\u0435\u044e\u0449\u0438\u0435 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u044b \u0438 \u0448\u0438\u0440\u0438\u043d\u044b (\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0439 \u0437\u043d\u0430\u043a \u0438\u043c\u0435\u0435\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u044b \u043f\u043e \u0413\u041e\u0421\u0422, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0438 \u0448\u0438\u0440\u0438\u043d\u044b \u0443 \u043d\u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435). \u041c\u043e\u0436\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u044f\u0432\u043d\u043e \u00ab\u043a\u0440\u0438\u0432\u044b\u0435 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u0438\u00bb \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u0434\u043b\u0438\u043d\u0430\u0445 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0442\u043e\u0440\u043e\u043d \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0448\u0435 \u0443\u0440\u043e\u0432\u043d\u044f \u043f\u043e\u0433\u0440\u0435\u0448\u043d\u043e\u0441\u0442\u0438. \u041f\u0440\u0430\u0432\u0434\u0430, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u0432 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043e\u0434\u043d\u043e \u0438 \u043d\u0443\u0436\u043d\u044b\u0435 \u0434\u043e \u043a\u0443\u0447\u0438 \u0432\u044b\u043a\u0438\u043d\u0443\u0442\u044c. \u0422\u0430\u043a \u0447\u0442\u043e \u0442\u0443\u0442 \u043d\u0430\u0434\u043e \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u044c. <\/p>\n<p>\u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u043e\u0440\u043e\u043d:<\/p>\n<pre><code class=\"python\">def dist(point1,point2):     d1 = point1[0] - point2[0]     d2 = point1[1] - point2[1]     return math.sqrt(d1*d1+d2*d2)<\/code><\/pre>\n<p>\u0418 \u0432\u043d\u0435\u0441\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u0444\u0438\u043b\u044c\u0442\u0440:<\/p>\n<pre><code class=\"python\">def my_filter(approx):     if len(approx)==4:         if abs(approx[2,0,0]-approx[0,0,0])&lt;10:             return False         if abs(approx[2,0,1]-approx[0,0,1])&lt;10:             return False         if abs(dist(approx[0,0],approx[1,0])\/dist(approx[2,0],approx[3,0])-1)&gt;0.4:             return False         if abs(dist(approx[0,0],approx[3,0])\/dist(approx[1,0],approx[2,0])-1)&gt;0.4:             return False         return True     return False<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0440\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041e\u0434\u0438\u043d \u0438\u0437 \u043d\u0438\u0445, \u043a\u0441\u0442\u0430\u0442\u0438, \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0440\u0430\u0441\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0441 \u043f\u0440\u044f\u043c\u044b\u043c\u0438 \u0443\u0433\u043b\u0430\u043c\u0438 (\u0435\u0441\u043b\u0438 \u0443\u0433\u043e\u043b \u0441\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u043a\u043b\u043e\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0442 90 \u0433\u0440\u0430\u0434\u0443\u0441\u043e\u0432). \u041d\u043e \u043c\u044b \u043f\u043e\u043a\u0430 \u044d\u0442\u043e\u0433\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c, \u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u043b\u0438\u0448\u043d\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u2013 \u043d\u0435 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u043e. <\/p>\n<p>\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u043d\u043e\u043c\u0435\u0440\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0434\u043e \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043a\u043e\u043d\u0442\u0443\u0440\u0430 \u0442\u043e\u0447\u043a\u0438. \u0412\u043e\u0442 \u043a\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043a\u043e\u043d\u0442\u0443\u0440\u0430 \u0434\u0432\u0443\u0445 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430:<\/p>\n<pre><code class=\"python\">x1=res.contours[0][0][0][0] y1=res.contours[0][0][0][1] x2=res.contours[0][2][0][0] y2=res.contours[0][2][0][1] cv2.rectangle(finish_result,(x1,y1),(x2,y2),(255,0,0),3)<\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u0447\u0442\u043e \u0432 \u0438\u0442\u043e\u0433\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u043d\u043e:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0442\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043d\u0430\u0434\u043e. \u0410 \u043d\u0430\u0434\u043e, \u043d\u0443, \u0445\u043e\u0442\u044f \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u044b \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u043b\u0430 \u044d\u0442\u0438 \u0442\u043e\u0447\u043a\u0438:<\/p>\n<pre><code class=\"python\">def get_rect(countur_item):     x1 = countur_item[0][0][0]     y1 = countur_item[0][0][1]     x2 = countur_item[2][0][0]     y2 = countur_item[2][0][1]     return (x1,y1), (x2,y2)<\/code><\/pre>\n<p>\u0418 \u0442\u043e\u0433\u0434\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0444\u0438\u0433\u0443\u0440\u0443 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">p1,p2=get_rect(res.contours[0]) cv2.rectangle(finish_result,p1,p2,(255,0,0),3)<\/code><\/pre>\n<p>\u0410 \u0432\u0441\u0435 \u0444\u0438\u0433\u0443\u0440\u044b \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">for item in res.contours:     p1,p2=get_rect(item)     cv2.rectangle(finish_result,p1,p2,(255,0,0),3)<\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c, \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u0438 \u0442\u0440\u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u0438, \u043f\u043e\u0438\u0441\u043a\u0430\u0442\u044c \u0442\u0430\u043c \u0431\u0443\u043a\u043e\u0432\u043a\u0438 \u0438 \u0446\u0438\u0444\u0435\u0440\u043a\u0438. \u041d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430\u0432\u0435\u0441\u0442\u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432 \u043a\u043e\u0434\u0435. \u0412\u043e\u0442 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0443 \u043d\u0430\u0441 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b run2.py:<\/p>\n<pre><code class=\"python\">import cv2 import math  from Libraries.Core import Engine from Libraries.ImageProcessingSteps import MedianBlurProcessingStep, ThresholdProcessingStep, ContourProcessingStep, \\     ContourApproximationProcessingStep  def dist(point1,point2):     d1 = point1[0] - point2[0]     d2 = point1[1] - point2[1]     return math.sqrt(d1*d1+d2*d2)  def my_filter(approx):     if len(approx)==4:         if abs(approx[2,0,0]-approx[0,0,0])&lt;10:             return False         if abs(approx[2,0,1]-approx[0,0,1])&lt;10:             return False         if abs(dist(approx[0,0],approx[1,0])\/dist(approx[2,0],approx[3,0])-1)&gt;0.4:             return False         if abs(dist(approx[0,0],approx[3,0])\/dist(approx[1,0],approx[2,0])-1)&gt;0.4:             return False         return True     return False  def get_rect(countur_item):     x1 = countur_item[0][0][0]     y1 = countur_item[0][0][1]     x2 = countur_item[2][0][0]     y2 = countur_item[2][0][1]     return (x1,y1), (x2,y2)  my_photo = cv2.imread('..\/Photos\/6108249.jpg') #my_photo = cv2.imread('..\/Photos\/car.jpg') core=Engine() core.steps.append(MedianBlurProcessingStep(5)) core.steps.append(ThresholdProcessingStep()) core.steps.append(ContourProcessingStep()) #core.steps.append(ContourApproximationProcessingStep(0.02)) core.steps.append(ContourApproximationProcessingStep(0.02,my_filter)) res,history=core.process(my_photo)  i=1 for info in history:     cv2.imshow('image'+str(i), info.image) # \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u043e\u043a\u043d\u043e     i=i+1 cv2.imshow('res', res.image)  finish_result = history[0].image.copy()  for item in res.contours:     p1,p2=get_rect(item)     cv2.rectangle(finish_result,p1,p2,(255,0,0),3) cv2.imshow('Finish', finish_result)   cv2.waitKey() cv2.destroyAllWindows()<\/code><\/pre>\n<p>\u041d\u0435 \u043e\u0447\u0435\u043d\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u043e, \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043f\u0430\u043f\u043a\u0443 Libraries \u0444\u0430\u0439\u043b Utils.py \u0438 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u043c \u0442\u0443\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 get_rect \u0438 dist. \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">from Libraries.Utils import dist, get_rect<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">import cv2  from Libraries.Core import Engine from Libraries.ImageProcessingSteps import MedianBlurProcessingStep, ThresholdProcessingStep, ContourProcessingStep, \\     ContourApproximationProcessingStep from Libraries.Utils import dist, get_rect, show_history   def my_filter(approx):     if len(approx)==4:         if<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-348124","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/348124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=348124"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/348124\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=348124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=348124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=348124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}