| | |
| | | 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 |
| | |
| | | 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]] |
| | |
| | | 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 |
| | |
| | | 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 |
| | |
| | | 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: |
| | |
| | | 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]) |
| | |
| | | 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) |
| | |
| | | import numpy as np |
| | | import uuid |
| | | from _image_clip_setting import image_clip |
| | | import sys |
| | | |
| | | |
| | | def task_init_load_digits(): |
| | |
| | | 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]) |
| | |
| | | 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 |
| | |
| | | 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) |
| | | 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: |
| | | 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 != '@': |
| | |
| | | # do got another image |
| | | 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.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.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.waitKey(0) |
| | | |
| | | |
| | | cap = cv2.VideoCapture("video.mp4") # for testing |
| | | cap = cv2.VideoCapture(sys.argv[1]) # for testing |
| | | total_frame = 0 |
| | | while(cap.isOpened()): |
| | | ret, frame = cap.read() |
| | |
| | | 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) |
| | | 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: |
| | |
| | | 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]) |
| | |
| | | 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) # 读取图片 |
| | | |
| | |
| | | # -*- 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 |
| | |
| | | |
| | | 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) |
| | |
| | | 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]) |
| | |
| | | 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) |
| | | 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: |
| | | 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)) |