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

---
 /dev/null                                            |    0 
 debug/0_get_frame.py                                 |    4 +
 debug/2_check_frame_digits.py                        |   18 ++++-----
 extract_voltage_task_new.py                          |    9 ++--
 debug/1_check_video_boundary.py                      |   15 ++++---
 debug/__pycache__/_image_clip_setting.cpython-36.pyc |    0 
 debug/0_get_video.py                                 |    1 
 debug/1_check_frame_boundary.py                      |   16 ++++----
 debug/_image_clip_setting.py                         |    4 ++
 debug/test.mp4                                       |    0 
 debug/2_check_video_digits.py                        |   29 ++++++--------
 11 files changed, 50 insertions(+), 46 deletions(-)

diff --git a/debug/0_get_frame.py b/debug/0_get_frame.py
index 757ccff..148cdb7 100644
--- a/debug/0_get_frame.py
+++ b/debug/0_get_frame.py
@@ -3,6 +3,7 @@
 
 import datetime
 import cv2
+from _image_clip_setting import image_clip
 
 time_now = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
 #cap = cv2.VideoCapture(0)
@@ -12,6 +13,7 @@
 print('鎵嬪姩璁剧疆 鍞竴鐨勫弬鏁�: 鏁板瓧浠〃鐩樼殑浣嶇疆: 瑕佺◢寰暀浜涗綑閲忋��')
 print('鏍煎紡涓哄浘鍍忓尯鍩� [Y杞� min:max, X杞� min:max],濡俒176:234,280:426]')
 print('鍙﹀鍥惧舰 宸︿笂瑙掍负0,0 鍚戝彸 涓篨杞存鍚戯紝鍚戜笅涓篩杞存鍚�')
-cv2.imwrite(time_now+"_clip.jpg",frame[176:234,280:426])
+cv2.imwrite(time_now+"_clip.jpg",
+            frame[image_clip[0]:image_clip[1], image_clip[2]:image_clip[3]])
 cap.release()
 cv2.destroyAllWindows()
diff --git a/debug/0_get_video.py b/debug/0_get_video.py
index 084e27f..844af10 100644
--- a/debug/0_get_video.py
+++ b/debug/0_get_video.py
@@ -7,6 +7,7 @@
 import cv2
 import getopt
 import sys
+from _image_clip_setting import image_clip
 
 
 def start_check_opts(argv):
diff --git a/debug/1_check_frame_boundary.py b/debug/1_check_frame_boundary.py
index f22b3b4..26b4764 100644
--- a/debug/1_check_frame_boundary.py
+++ b/debug/1_check_frame_boundary.py
@@ -4,26 +4,26 @@
 from cv2 import waitKey
 import getopt
 import sys
-
+from _image_clip_setting import image_clip
 
 
 def check_frame(frame):
-    #image = frame[176:234,280:426]     
-    image = frame[180:230,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]
     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
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        if image_h*0.98 > h > image_h * 0.3:
-            #print((x, y, w, h))
+        if hierarchy[0][contours_count][3] ==0 :
+        #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)
+        contours_count += 1
     cv2.imshow('frame',image)
     waitKey(0)
 
diff --git a/debug/1_check_video_boundary.py b/debug/1_check_video_boundary.py
index 769ee1d..90bcb17 100644
--- a/debug/1_check_video_boundary.py
+++ b/debug/1_check_video_boundary.py
@@ -4,28 +4,31 @@
 import cv2
 from cv2 import waitKey
 import uuid
+from _image_clip_setting import image_clip
 
 
 def check_frame(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]]
     #cv2.imshow('src',image)
     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     thresh = cv2.threshold(gray, 0, 255,
                        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
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        if image_h*0.98 > h > image_h * 0.3:
-            #print((x, y, w, h))
+        if hierarchy[0][contours_count][3] ==0 :
+        #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)
+        contours_count += 1
     cv2.imshow('frame',image)
     if cv2.waitKey(100) & 0xFF == ord('q'): #鎸塹閫�鍑�
         cv2.waitKey(0)
         
-cap = cv2.VideoCapture("video2.mp4")  # for testing
+cap = cv2.VideoCapture("video.mp4")  # for testing
 while(cap.isOpened()):
     ret, frame = cap.read()
     if ret==True:
diff --git a/debug/2022-03-12-20-15-24.jpg b/debug/2022-03-12-20-15-24.jpg
deleted file mode 100644
index 6dfaf5e..0000000
--- a/debug/2022-03-12-20-15-24.jpg
+++ /dev/null
Binary files differ
diff --git a/debug/2022-03-12-20-15-24_clip.jpg b/debug/2022-03-12-20-15-24_clip.jpg
deleted file mode 100644
index 6083d89..0000000
--- a/debug/2022-03-12-20-15-24_clip.jpg
+++ /dev/null
Binary files differ
diff --git a/debug/2_check_frame_digits.py b/debug/2_check_frame_digits.py
index 15c1956..762e625 100644
--- a/debug/2_check_frame_digits.py
+++ b/debug/2_check_frame_digits.py
@@ -5,6 +5,7 @@
 import getopt
 import sys
 import numpy as np
+from _image_clip_setting import image_clip
 
 
 def task_init_load_digits():
@@ -62,23 +63,20 @@
 
 
 def check_frame(frame):
-    #image = frame[176:234, 280:426]
-    image = frame[170:240,250:370]  #frame[176:234,280:426]
-    cv2.rectangle(image, (250, 170), (370, 240), (0, 255, 0), 2)
-    cv2.imshow('rect',image)
-    waitKey(0)
+    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]
     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
     return_numbers = []
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        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.6:
             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(
@@ -87,6 +85,7 @@
                 return_numbers.append([x, one_number])
             else:
                 return return_numbers, False
+        contours_count += 1     
     return_numbers = np.array(sorted(return_numbers))
     return return_numbers[:, -1], True
 
@@ -111,5 +110,4 @@
     print(digit)
     cv2.putText(image, str(digit), (280, 170), cv2.FONT_HERSHEY_COMPLEX, 1.0, (100, 200, 200), 5)
     cv2.imshow('add_text',image)
-    waitKey(0)
-
+    waitKey(0)
\ No newline at end of file
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()
diff --git a/debug/__pycache__/_image_clip_setting.cpython-36.pyc b/debug/__pycache__/_image_clip_setting.cpython-36.pyc
new file mode 100644
index 0000000..6af57c6
--- /dev/null
+++ 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
new file mode 100644
index 0000000..f23a759
--- /dev/null
+++ b/debug/_image_clip_setting.py
@@ -0,0 +1,4 @@
+锘�# -*- coding: utf-8 -*-
+__author__ = '涓婃捣鑹插畞'
+
+image_clip = [176,234,278,422]     # for  video.mp4
\ No newline at end of file
diff --git a/debug/test.mp4 b/debug/test.mp4
new file mode 100644
index 0000000..d3ceb04
--- /dev/null
+++ b/debug/test.mp4
Binary files differ
diff --git a/extract_voltage_task_new.py b/extract_voltage_task_new.py
index 41dd2bd..cf30f1a 100644
--- a/extract_voltage_task_new.py
+++ b/extract_voltage_task_new.py
@@ -69,13 +69,14 @@
                            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)
+    #image_h = image.shape[0]
+    contours, hierarchy = cv2.findContours(counter, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
     return_numbers = []
+    contours_count = 0
     for c in contours:
         x, y, w, h = cv2.boundingRect(c)
-        if image_h*0.98 > h > image_h * 0.3:
+        #if image_h*0.98 > h > image_h * 0.3:
+        if hierarchy[0][contours_count][3] ==0 :
             #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(

--
Gitblit v1.9.1