From 7f3430f95dda8bc60e00b07e22c0de6cd9fba760 Mon Sep 17 00:00:00 2001
From: wangzhibo <wangzhibo@shsening.com>
Date: 星期一, 14 三月 2022 15:25:46 +0800
Subject: [PATCH] change countours method

---
 debug/2_check_video_digits.py |   29 ++++++++++++-----------------
 1 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/debug/2_check_video_digits.py b/debug/2_check_video_digits.py
index 0ee6707..b3419e9 100644
--- a/debug/2_check_video_digits.py
+++ b/debug/2_check_video_digits.py
@@ -3,15 +3,9 @@
 import time
 import cv2
 from cv2 import waitKey
-import uuid
-
-# -*- coding: utf-8 -*-
-import datetime
-import cv2
-from cv2 import waitKey
-import getopt
-import sys
 import numpy as np
+import uuid
+from _image_clip_setting import image_clip
 
 
 def task_init_load_digits():
@@ -70,8 +64,7 @@
 
 
 def get_digit(frame):
-    image = frame[177:233,250:370]  #frame[176:234,280:426]
-    # cv2.imshow('src',image)
+    image = frame[image_clip[0]:image_clip[1], image_clip[2]:image_clip[3]]
     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     thresh = cv2.threshold(gray, 0, 255,
                            cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
@@ -79,12 +72,13 @@
     counter = cv2.morphologyEx(thresh.copy(), cv2.MORPH_OPEN, kernel)
     image_h = image.shape[0]
     contours, hierarchy = cv2.findContours(
-        counter, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
+        counter, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
+    contours_count = 0
     return_numbers = []
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        print((x, y, w, h))
-        if image_h*0.98 > h > image_h * 0.3:
+        if hierarchy[0][contours_count][3] ==0 :
+        #if image_h*0.98 > h > image_h * 0.3:
             #print((x, y, w, h))
             #cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
             one_number = get_most_simmilar_digit(cv2.threshold(cv2.resize(
@@ -94,6 +88,7 @@
             else:
                 return return_numbers, False
             cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
+        contours_count += 1 
     cv2.imshow('frame', image)
     return_numbers = np.array(sorted(return_numbers))
     return return_numbers[:, -1], True
@@ -116,23 +111,23 @@
             # do got another image
             print('fails') 
             fail_count += 1
-            cv2.rectangle(frame, (250, 177), (370, 233), (0, 255, 0), 2)
+            cv2.rectangle(frame, (image_clip[2], image_clip[0]), (image_clip[3], image_clip[1]), (0, 255, 0), 2)
             cv2.imwrite('./fails/count_fail_'+str(uuid.uuid1())+'.jpg',frame)
 
     else:
         print('fails')
         fail_count +=1
-        cv2.rectangle(frame, (250, 177), (370, 233), (0, 255, 0), 2)
+        cv2.rectangle(frame, (image_clip[2], image_clip[0]), (image_clip[3], image_clip[1]), (0, 255, 0), 2)
         cv2.imwrite('./fails/status_fail_'+str(uuid.uuid1())+'.jpg',frame)
     print(round(digit,2))
-    cv2.putText(frame, str(round(digit,2)), (280, 170),
+    cv2.putText(frame, str(round(digit,2)), (image_clip[2], image_clip[0]-50),
                 cv2.FONT_HERSHEY_COMPLEX, 1.0, (100, 200, 200), 5)
     cv2.imshow('add_text', frame)
     if cv2.waitKey(5) & 0xFF == ord('q'):  # 鎸塹閫�鍑�
         cv2.waitKey(0)
 
 
-cap = cv2.VideoCapture("video2.mp4")  # for testing
+cap = cv2.VideoCapture("video.mp4")  # for testing
 total_frame = 0 
 while(cap.isOpened()):
     ret, frame = cap.read()

--
Gitblit v1.9.1