From 69b8cbd754284a2071d06149f0c92d9fe50a744e Mon Sep 17 00:00:00 2001
From: wangzhibo <wangzhibo@shsening.com>
Date: 星期三, 23 三月 2022 12:14:34 +0800
Subject: [PATCH] add height check

---
 /dev/null                                            |    0 
 debug/0_get_frame.py                                 |    2 +-
 debug/2_check_frame_digits.py                        |    3 ++-
 debug/3_using_template.py                            |    2 +-
 debug/1_check_video_boundary.py                      |    5 +++--
 debug/2_split_digit_from_video.py                    |   19 ++++++++++++-------
 debug/__pycache__/_image_clip_setting.cpython-36.pyc |    0 
 debug/1_check_frame_boundary.py                      |    4 ++--
 debug/new.mp4                                        |    0 
 debug/_image_clip_setting.py                         |    4 ++--
 debug/2_check_video_digits.py                        |    8 ++++----
 11 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/debug/0_get_frame.py b/debug/0_get_frame.py
index 00f2e09..e8c350e 100644
--- a/debug/0_get_frame.py
+++ b/debug/0_get_frame.py
@@ -7,7 +7,7 @@
 
 time_now = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
 #cap = cv2.VideoCapture(0)
-cap = cv2.VideoCapture("7122.mp4")
+cap = cv2.VideoCapture("new.mp4")
 ret, frame = cap.read()
 cv2.imwrite(time_now+".jpg", frame)
 print('鎵嬪姩璁剧疆 鍞竴鐨勫弬鏁�: 鏁板瓧浠〃鐩樼殑浣嶇疆: 瑕佺◢寰暀浜涗綑閲忋��')
diff --git a/debug/1_check_frame_boundary.py b/debug/1_check_frame_boundary.py
index 26b4764..b958d23 100644
--- a/debug/1_check_frame_boundary.py
+++ b/debug/1_check_frame_boundary.py
@@ -14,12 +14,12 @@
                        cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
     kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6))
     counter = cv2.morphologyEx(thresh.copy(), cv2.MORPH_OPEN, kernel)
-    image_h = image.shape[0]
     contours, hierarchy = cv2.findContours(counter,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
     contours_count = 0
+    image_h = image.shape[0]
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        if hierarchy[0][contours_count][3] ==0 :
+        if hierarchy[0][contours_count][3] ==0 and h > image_h * 0.3:
         #if image_h*0.98 > h > image_h * 0.6:
             print((x, y, w, h))
             cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
diff --git a/debug/1_check_video_boundary.py b/debug/1_check_video_boundary.py
index 90bcb17..0bafc91 100644
--- a/debug/1_check_video_boundary.py
+++ b/debug/1_check_video_boundary.py
@@ -17,9 +17,10 @@
     counter = cv2.morphologyEx(thresh.copy(), cv2.MORPH_OPEN, kernel)
     contours, hierarchy = cv2.findContours(counter,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
     contours_count = 0
+    image_h = image.shape[0]
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        if hierarchy[0][contours_count][3] ==0 :
+        if hierarchy[0][contours_count][3] ==0 and h > image_h * 0.3:
         #if image_h*0.98 > h > image_h * 0.6:
             print((x, y, w, h))
             cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
@@ -28,7 +29,7 @@
     if cv2.waitKey(100) & 0xFF == ord('q'): #鎸塹閫�鍑�
         cv2.waitKey(0)
         
-cap = cv2.VideoCapture("video.mp4")  # for testing
+cap = cv2.VideoCapture("new.mp4")  # for testing
 while(cap.isOpened()):
     ret, frame = cap.read()
     if ret==True:
diff --git a/debug/2022-03-14-15-40-32.jpg b/debug/2022-03-14-15-40-32.jpg
deleted file mode 100644
index ef17065..0000000
--- a/debug/2022-03-14-15-40-32.jpg
+++ /dev/null
Binary files differ
diff --git a/debug/2022-03-14-15-40-32_clip.jpg b/debug/2022-03-14-15-40-32_clip.jpg
deleted file mode 100644
index 2ac1315..0000000
--- a/debug/2022-03-14-15-40-32_clip.jpg
+++ /dev/null
Binary files differ
diff --git a/debug/2_check_frame_digits.py b/debug/2_check_frame_digits.py
index 762e625..9c7a907 100644
--- a/debug/2_check_frame_digits.py
+++ b/debug/2_check_frame_digits.py
@@ -73,9 +73,10 @@
     contours, hierarchy = cv2.findContours(counter,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
     contours_count = 0
     return_numbers = []
+    image_h = image.shape[0]
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        if hierarchy[0][contours_count][3] ==0 :
+        if hierarchy[0][contours_count][3] ==0 and h > image_h * 0.3:
         #if image_h*0.98 > h > image_h * 0.6:
             print((x, y, w, h))
             cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
diff --git a/debug/2_check_video_digits.py b/debug/2_check_video_digits.py
index b0e4bbb..d1213a4 100644
--- a/debug/2_check_video_digits.py
+++ b/debug/2_check_video_digits.py
@@ -56,8 +56,8 @@
         res = cv2.matchTemplate(target_img, digitROI[0], cv2.TM_CCOEFF_NORMED)
         max_val = cv2.minMaxLoc(res)[1]
         source.append(max_val)
-    # print(max(source))
-    if max(source) > 0.5:
+    print(max(source))
+    if max(source) > 0.3:
         return digits[source.index(max(source))][1]
     else:
         return "@"
@@ -77,7 +77,7 @@
     return_numbers = []
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        if hierarchy[0][contours_count][3] ==0 :
+        if hierarchy[0][contours_count][3] ==0 and h > image_h * 0.3:
         #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)
@@ -127,7 +127,7 @@
         cv2.waitKey(0)
 
 
-cap = cv2.VideoCapture("7122.mp4")  # for testing
+cap = cv2.VideoCapture("new.mp4")  # for testing
 total_frame = 0 
 while(cap.isOpened()):
     ret, frame = cap.read()
diff --git a/debug/2_split_digit_from_video.py b/debug/2_split_digit_from_video.py
index c57089e..c31f746 100644
--- a/debug/2_split_digit_from_video.py
+++ b/debug/2_split_digit_from_video.py
@@ -3,26 +3,31 @@
 import cv2
 from cv2 import waitKey
 import uuid
+from _image_clip_setting import image_clip
 
 
 def save_images(frame):
-    image = frame[180:230,250:370]  #frame[176:234,280:426]  
+    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]
+                           cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
     kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6))
     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)
+    contours, hierarchy = cv2.findContours(
+        counter, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
+    contours_count = 0
+    #print(contours)
+    print(hierarchy)
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        if image_h*0.9 > h > image_h * 0.5:
-            #print((x, y, w, h))
+        print((x, y, w, h))
+        print(hierarchy[0][contours_count][3])
+        if hierarchy[0][contours_count][3] ==0 :
             #cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
             cv2.imwrite("./images52/"+str(uuid.uuid4())+".jpg",cv2.threshold(cv2.resize(gray[y:y+h,x:x+w],(20,30)), 0, 255,
                        cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
         
-cap = cv2.VideoCapture("video2.mp4")  # for testing
+cap = cv2.VideoCapture("new.mp4")  # for testing
 while(cap.isOpened()):
     ret, frame = cap.read()
     if ret==True:
diff --git a/debug/3_using_template.py b/debug/3_using_template.py
index e8f6391..f3313c3 100644
--- a/debug/3_using_template.py
+++ b/debug/3_using_template.py
@@ -14,7 +14,7 @@
 def task_init_load_digits():
 
     # 鍔犺浇鏁板瓧妯℃澘
-    path = './data'    # 杩欎釜鍦版柟灏辨槸鑾峰彇褰撳墠杩愯鐩綍 鑾峰彇鍑芥暟鍦ㄤ富鍑芥暟閲岄潰
+    path = './templates'    # 杩欎釜鍦版柟灏辨槸鑾峰彇褰撳墠杩愯鐩綍 鑾峰彇鍑芥暟鍦ㄤ富鍑芥暟閲岄潰
     #鍜屾ā鏉跨殑鏁板瓧杩涜鍖归厤
     digits.append([cv2.imread(path + "/@-0.jpg",cv2.IMREAD_GRAYSCALE),'@'])
     digits.append([cv2.imread(path + "/@-1.jpg",cv2.IMREAD_GRAYSCALE),'@'])
diff --git a/debug/__pycache__/_image_clip_setting.cpython-36.pyc b/debug/__pycache__/_image_clip_setting.cpython-36.pyc
index 43014df..979dbc2 100644
--- a/debug/__pycache__/_image_clip_setting.cpython-36.pyc
+++ b/debug/__pycache__/_image_clip_setting.cpython-36.pyc
Binary files differ
diff --git a/debug/_image_clip_setting.py b/debug/_image_clip_setting.py
index e7bcfeb..ddbc326 100644
--- a/debug/_image_clip_setting.py
+++ b/debug/_image_clip_setting.py
@@ -1,5 +1,5 @@
 锘�# -*- coding: utf-8 -*-
 __author__ = '涓婃捣鑹插畞'
 
-#image_clip = [176,234,278,422]     # for  video.mp4
-image_clip = [226,266,220,300]     # for  7122.mp4
+image_clip = [176,234,278,422]     # for  video.mp4
+#image_clip = [226,266,200,300]     # for  new.mp4
diff --git a/debug/new.mp4 b/debug/new.mp4
new file mode 100644
index 0000000..50e32c7
--- /dev/null
+++ b/debug/new.mp4
Binary files differ

--
Gitblit v1.9.1