quangnhat185 / Plate_detect_and_recognize

Detecting and Reading vehicle's license plate from various countries (Germany, Vietnam, Japan, Thailand, Saudi, Russia, Korea, Usa, India, China)
MIT License
169 stars 125 forks source link

bounding boxes not found #12

Open kubasiak opened 4 years ago

kubasiak commented 4 years ago

Hi, First of all thank you for this repo. I enjoyed it a lot. I see that there is an issue with the boxes around characters. When using cv2.findContours sometimes the contours are not found. I find that it is in large extent because of the frames that are around the plates. I have solved it with removing a margin of 5% from the plate picture. ( Later of course the coordinates of the frames must be adjusted for this margin in respect to the original picture.)

It is not the most elegant solution but it helped:

    cont, _  = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    marginh=0
    marginw=0
    if(len(cont)<=1):
        print('havent found any characters -  cutting margings')
        m=0.05
        marginh=round(m*maxh)
        marginw=round(m*maxw)
        cut_plate_image=plate_image[marginh:maxh-marginh,marginw:maxw-marginw]
        cut_binary=binary[marginh:maxh-marginh,marginw:maxw-marginw]
        cont, _  = cv2.findContours(cut_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        test_roi = cut_plate_image.copy()

    cut_plate_image=plate_image[marginh:maxh-marginh,marginw:maxw-marginw]
    cut_binary=binary[marginh:maxh-marginh,marginw:maxw-marginw]

And then of course adjust when cropping images:


# extract the img
            curr_num = cut_binary[y:y+h,x:x+w]
            curr_num = cv2.resize(curr_num, dsize=(digit_w, digit_h))
            _, curr_num = cv2.threshold(curr_num, 220, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
            curr_num_orig = cut_plate_image[y:y+h,x:x+w]
            crop_characters.append(curr_num) 
            crop_characters_orig.append(curr_num_orig)
modevera commented 4 years ago

I have the same issue how didi you define maxh ?

eaglemas commented 3 years ago

Instead of : ,cont, = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

write : ,cont, = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

and everything is fixed!!!!

atharva21jadhav commented 3 years ago

@eaglemas I'm working with RETR_TREE too currently. Although it can work okay at such scenarios, RETR_TREE might also create extra contours. For example, for digits like 0 since all the contours hierarchy is calculated we might get 2 contours one inside and the other on the external. It would be better if we go with RETR_EXTERNAL and crop the portions of image.

amandp13 commented 3 years ago

@kubasiak Can you please provide me the optimal code for this issue as I am getting confused by your soluton?