| | |
| | | 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]) |
| | |
| | | |
| | | |
| | | digits = [] |
| | | fail_count = 0 |
| | | fail_count = 0 |
| | | |
| | | |
| | | def get_most_simmilar_digit(target_img): |
| | |
| | | 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 |
| | |
| | | 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 |