wangzhibo
2022-03-24 b11e5057bae461476c4392482e23dee9f38c0599
识别宽的情况 人工拆分
10个文件已修改
5个文件已添加
154 ■■■■■ 已修改文件
debug/1_check_frame_boundary.py 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
debug/1_check_video_boundary.py 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
debug/2_check_frame_digits.py 4 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
debug/2_check_video_digits.py 68 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
debug/2_split_digit_from_video.py 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
debug/3_using_template.py 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
debug/7222.mp4 补丁 | 查看 | 原始文档 | blame | 历史
debug/7322.mp4 补丁 | 查看 | 原始文档 | blame | 历史
debug/7422.mp4 补丁 | 查看 | 原始文档 | blame | 历史
debug/__pycache__/_image_clip_setting.cpython-36.pyc 补丁 | 查看 | 原始文档 | blame | 历史
debug/_image_clip_setting.py 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
debug/templates/0-2.jpg 补丁 | 查看 | 原始文档 | blame | 历史
debug/templates/0-3.jpg 补丁 | 查看 | 原始文档 | blame | 历史
do_not_use/sample.py 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
extract_voltage_task.py 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
debug/1_check_frame_boundary.py
@@ -12,7 +12,7 @@
    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, (5, 5))
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6))
    counter = cv2.morphologyEx(thresh.copy(), cv2.MORPH_OPEN, kernel)
    contours, hierarchy = cv2.findContours(counter,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    contours_count = 0
debug/1_check_video_boundary.py
@@ -5,7 +5,7 @@
from cv2 import waitKey
import uuid
from _image_clip_setting import image_clip
import sys
def check_frame(frame):
    image = frame[image_clip[0]:image_clip[1], image_clip[2]:image_clip[3]]
@@ -13,7 +13,7 @@
    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, (5, 5))
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6))
    counter = cv2.morphologyEx(thresh.copy(), cv2.MORPH_OPEN, kernel)
    contours, hierarchy = cv2.findContours(counter,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    contours_count = 0
@@ -21,7 +21,6 @@
    for c in contours:
        x, y, w, h = cv2.boundingRect(c)
        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)
        contours_count += 1
@@ -29,7 +28,7 @@
    if cv2.waitKey(100) & 0xFF == ord('q'): #按q退出
        cv2.waitKey(0)
        
cap = cv2.VideoCapture("new.mp4")  # for testing
cap = cv2.VideoCapture(sys.argv[1])  # for testing
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
debug/2_check_frame_digits.py
@@ -18,6 +18,8 @@
    digits.append([cv2.imread(path + "/@-2.jpg", cv2.IMREAD_GRAYSCALE), '@'])
    digits.append([cv2.imread(path + "/0-0.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/0-1.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/0-2.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/0-3.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/1-0.jpg", cv2.IMREAD_GRAYSCALE), 1])
    digits.append([cv2.imread(path + "/1-1.jpg", cv2.IMREAD_GRAYSCALE), 1])
    digits.append([cv2.imread(path + "/2-0.jpg", cv2.IMREAD_GRAYSCALE), 2])
@@ -67,7 +69,7 @@
    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, (5, 5))
    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)
debug/2_check_video_digits.py
@@ -6,6 +6,7 @@
import numpy as np
import uuid
from _image_clip_setting import image_clip
import sys
def task_init_load_digits():
@@ -18,6 +19,8 @@
    digits.append([cv2.imread(path + "/@-2.jpg", cv2.IMREAD_GRAYSCALE), '@'])
    digits.append([cv2.imread(path + "/0-0.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/0-1.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/0-2.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/0-3.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/1-0.jpg", cv2.IMREAD_GRAYSCALE), 1])
    digits.append([cv2.imread(path + "/1-1.jpg", cv2.IMREAD_GRAYSCALE), 1])
    digits.append([cv2.imread(path + "/2-0.jpg", cv2.IMREAD_GRAYSCALE), 2])
@@ -42,7 +45,7 @@
digits = []
fail_count = 0
fail_count = 0
def get_most_simmilar_digit(target_img):
@@ -68,27 +71,42 @@
    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, (5, 5))
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6))
    counter = cv2.morphologyEx(thresh.copy(), cv2.MORPH_OPEN, kernel)
    image_h = image.shape[0]
    image_w = image.shape[1]
    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 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)
            one_number = get_most_simmilar_digit(cv2.threshold(cv2.resize(
                gray[y:y+h, x:x+w], (20, 30)), 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
            if one_number != '@':
                return_numbers.append([x, one_number])
        if hierarchy[0][contours_count][3] == 0 and h > image_h * 0.3:
            if w > image_w/4:
                half_width = int(w/2)
                one_number = get_most_simmilar_digit(cv2.threshold(cv2.resize(
                    gray[y:y+h, x:x+half_width], (20, 30)), 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
                if one_number != '@':
                    return_numbers.append([x, one_number])
                else:
                    return -1, False
                cv2.rectangle(image, (x, y), (x+half_width, y+h), (0, 255, 0), 2)
                one_number = get_most_simmilar_digit(cv2.threshold(cv2.resize(
                    gray[y:y+h, x+half_width:x+w], (20, 30)), 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
                if one_number != '@':
                    return_numbers.append([x+half_width, one_number])
                else:
                    return -1, False
                cv2.rectangle(image, (x+half_width, y), (x+half_width, y+h), (0, 255, 0), 2)
            else:
                return -1, False
            cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
        contours_count += 1
                one_number = get_most_simmilar_digit(cv2.threshold(cv2.resize(
                    gray[y:y+h, x:x+w], (20, 30)), 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
                if one_number != '@':
                    return_numbers.append([x, one_number])
                else:
                    return -1, 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
@@ -109,30 +127,32 @@
                return_numbers[2]*0.1+return_numbers[3]*0.1
        else:
            # do got another image
            print('fails')
            print('fails')
            fail_count += 1
            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)
            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, (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)), (image_clip[2], image_clip[0]-50),
        fail_count += 1
        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)), (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'):  # 按q退出
        cv2.waitKey(0)
cap = cv2.VideoCapture("video.mp4")  # for testing
total_frame = 0
cap = cv2.VideoCapture(sys.argv[1])  # for testing
total_frame = 0
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        total_frame +=1
        total_frame += 1
        check_one_frame(frame)
    else:
        break
debug/2_split_digit_from_video.py
@@ -4,30 +4,36 @@
from cv2 import waitKey
import uuid
from _image_clip_setting import image_clip
import sys
def save_images(frame):
    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, (5, 5))
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6))
    counter = cv2.morphologyEx(thresh.copy(), cv2.MORPH_OPEN, kernel)
    image_h = image.shape[0]
    image_w = image.shape[1]
    contours, hierarchy = cv2.findContours(
        counter, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    contours_count = 0
    #print(contours)
    print(hierarchy)
    return_numbers = []
    for c in contours:
        x, y, w, h = cv2.boundingRect(c)
        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,
        if hierarchy[0][contours_count][3] == 0 and h > image_h * 0.3:
            if w > image_w/4:
                half_width = int(w/2)
                cv2.imwrite("./images52/"+str(uuid.uuid4())+".jpg",cv2.threshold(cv2.resize(gray[y:y+h,x:x+half_width],(20,30)), 0, 255,
                       cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
                cv2.imwrite("./images52/"+str(uuid.uuid4())+".jpg",cv2.threshold(cv2.resize(gray[y:y+h,x+half_width:x+w],(20,30)), 0, 255,
                       cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
            else:
                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])
        contours_count += 1
        
cap = cv2.VideoCapture("new.mp4")  # for testing
cap = cv2.VideoCapture(sys.argv[1])  # for testing
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
debug/3_using_template.py
@@ -21,6 +21,8 @@
    digits.append([cv2.imread(path + "/@-2.jpg",cv2.IMREAD_GRAYSCALE),'@'])
    digits.append([cv2.imread(path + "/0-0.jpg",cv2.IMREAD_GRAYSCALE),0])
    digits.append([cv2.imread(path + "/0-1.jpg",cv2.IMREAD_GRAYSCALE),0])
    digits.append([cv2.imread(path + "/0-2.jpg",cv2.IMREAD_GRAYSCALE),0])
    digits.append([cv2.imread(path + "/0-3.jpg",cv2.IMREAD_GRAYSCALE),0])
    digits.append([cv2.imread(path + "/1-0.jpg",cv2.IMREAD_GRAYSCALE),1])
    digits.append([cv2.imread(path + "/1-1.jpg",cv2.IMREAD_GRAYSCALE),1])
    digits.append([cv2.imread(path + "/2-0.jpg",cv2.IMREAD_GRAYSCALE),2])
@@ -70,6 +72,7 @@
    for file in filename:
        image = cv2.imread(input_dir + "/" + file,0)    # 读取图片
        ret, binary = cv2.threshold(image,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
        #ret, binary = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
        num = get_most_simmilar_digit(binary)
        cv2.imwrite("images52_infer/" + str(num)+"_" + file,binary)    # 读取图片
debug/7222.mp4
Binary files differ
debug/7322.mp4
Binary files differ
debug/7422.mp4
Binary files differ
debug/__pycache__/_image_clip_setting.cpython-36.pyc
Binary files differ
debug/_image_clip_setting.py
@@ -1,6 +1,10 @@
# -*- coding: utf-8 -*-
__author__ = '上海色宁'
image_clip = [176,234,278,422]     # for  video.mp4
#image_clip = [176,234,278,422]     # for  video.mp4
#image_clip = [226,266,200,300]     # for  new.mp4
#image_clip = [220,275,210,300]     # test image
#image_clip = [220,275,210,300]  # for  7122.mp4
image_clip = [255,308,290,412]  # for  7222.mp4
#image_clip = [229,274,270,380]  # for  7322.mp4
#image_clip = [180,240,154,287]   # for  7322.mp4
debug/templates/0-2.jpg
debug/templates/0-3.jpg
do_not_use/sample.py
@@ -49,7 +49,7 @@
def demo(index):
    rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25))
    sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (6, 6))
    target_path = now_dir + "\\" + "demo_" + str(index) + ".png"
    img_origin = cv2.imread(target_path)
    img_origin = resize(img_origin, width=300)
extract_voltage_task.py
@@ -18,6 +18,8 @@
    digits.append([cv2.imread(path + "/@-2.jpg", cv2.IMREAD_GRAYSCALE), '@'])
    digits.append([cv2.imread(path + "/0-0.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/0-1.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/0-2.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/0-3.jpg", cv2.IMREAD_GRAYSCALE), 0])
    digits.append([cv2.imread(path + "/1-0.jpg", cv2.IMREAD_GRAYSCALE), 1])
    digits.append([cv2.imread(path + "/1-1.jpg", cv2.IMREAD_GRAYSCALE), 1])
    digits.append([cv2.imread(path + "/2-0.jpg", cv2.IMREAD_GRAYSCALE), 2])
@@ -62,30 +64,46 @@
        return "@"
def extrct_digits_from_frame(frame, col_start, col_end, rows_start, rows_end):
    image = frame[col_start:col_end, rows_start:rows_end]
    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, (5, 5))
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (6, 6))
    counter = cv2.morphologyEx(thresh.copy(), cv2.MORPH_OPEN, kernel)
    contours, hierarchy = cv2.findContours(counter, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    return_numbers = []
    contours_count = 0
    image_h = image.shape[0]
    image_w = image.shape[1]
    digit = -1
    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 and 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(
                gray[y:y+h, x:x+w], (20, 30)), 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
            if one_number != '@':
                return_numbers.append([x, one_number])
            if w>w/4:
                half_width = int(w/2)
                one_number = get_most_simmilar_digit(cv2.threshold(cv2.resize(
                gray[y:y+h, x:x+half_width], (20, 30)), 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
                if one_number != '@':
                    return_numbers.append([x, one_number])
                else:
                    return digit, False
                one_number = get_most_simmilar_digit(cv2.threshold(cv2.resize(
                gray[y:y+h, x+half_width:x+w], (20, 30)), 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
                if one_number != '@':
                    return_numbers.append([x+half_width, one_number])
                else:
                    return digit, False
            else:
                return digit, False
                one_number = get_most_simmilar_digit(cv2.threshold(cv2.resize(
                gray[y:y+h, x:x+w], (20, 30)), 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1])
                if one_number != '@':
                    return_numbers.append([x, one_number])
                else:
                    return digit, False
        contours_count +=1
    if len(return_numbers)==0:
            return digit,False
    return_numbers = np.array(sorted(return_numbers))