Advertisement
Guest User

a

a guest
Nov 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.88 KB | None | 0 0
  1. import cv2
  2. import pytesseract
  3. import sys
  4. import numpy as np
  5. from PIL import Image
  6. from HELPERS import Functions
  7.  
  8. possibleChars = []
  9.  
  10. # list of possible char groups
  11. listOfGroups = []
  12.  
  13. # list of possible chars next to each other
  14. newGroup = []
  15.  
  16. errorChance = 20
  17.  
  18. colors = [(0,255,0),(0,0, 255),(255,0,0)]
  19.  
  20. cap = cv2.VideoCapture(0)
  21.  
  22. while True:
  23.     (sucesso, frame) = cap.read()
  24.     if not sucesso:
  25.         break
  26.  
  27.     maxRight = frame.shape[1]
  28.     maxBottom = frame.shape[0]
  29.  
  30.     #Converting to Gray Scale
  31.     frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  32.  
  33.     frame_gray = cv2.GaussianBlur(frame_gray,(3,3),0)
  34.  
  35.     ret, thresh = cv2.threshold(frame_gray, 120, 255, cv2.THRESH_BINARY)
  36.  
  37.     _,contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  38.  
  39.     for contour in contours:
  40.         possibleChar = Functions.possibleChar(contour)
  41.  
  42.         if(Functions.checkIfChar(possibleChar)):
  43.             possibleChars.append(possibleChar)
  44.  
  45.  
  46.     color = 0
  47.     for possibleChar in possibleChars:
  48.         x = possibleChar.boundingRectX
  49.         y = possibleChar.boundingRectY
  50.         h = possibleChar.boundingRectH
  51.         w = possibleChar.boundingRectW
  52.  
  53.         # step 1 : possible chars
  54.         # step1 = frame
  55.         # cv2.rectangle(step1, (x,y), (x+w,y+h), colors[color], 2)
  56.         # cv2.imshow("Step 1", step1)
  57.  
  58.         if(color == 2):
  59.             color = 0
  60.         else:
  61.             color = color + 1
  62.  
  63.     counter = 0
  64.  
  65.     for possibleChar in possibleChars:
  66.  
  67.         if(len(possibleChars) == counter):
  68.             break
  69.  
  70.  
  71.         if(counter == 0):
  72.             distanceNext = Functions.distanceBetween(possibleChar, possibleChars[counter+1])
  73.             if(distanceNext <= possibleChar.boundingRectH):
  74.                 newGroup.append(possibleChar)
  75.  
  76.  
  77.         if(counter > 1):
  78.             possibleCharPrevious = possibleChars[counter-1]
  79.  
  80.             distancePrevious = Functions.distanceBetween(possibleCharPrevious, possibleChar)
  81.  
  82.             greaterH = possibleCharPrevious.boundingRectH
  83.             if(possibleCharPrevious.boundingRectH < possibleChar.boundingRectH ):
  84.                 greaterH = possibleChar.boundingRectH
  85.  
  86.             if(distancePrevious <= greaterH*1.5):
  87.                 newGroup.append(possibleChar)
  88.             else:
  89.                 listOfGroups.append(newGroup)
  90.                 newGroup = []
  91.                 newGroup.append(possibleChar)
  92.  
  93.         counter = counter + 1
  94.  
  95.     color = 0
  96.  
  97.     for group in listOfGroups:
  98.         for possibleChar in group:
  99.             x = possibleChar.boundingRectX
  100.             y = possibleChar.boundingRectY
  101.             h = possibleChar.boundingRectH
  102.             w = possibleChar.boundingRectW
  103.  
  104.  
  105.             # step 2 group of chars
  106.             # step2 = frame
  107.             # cv2.rectangle(step2, (x,y), (x+w,y+h), colors[color], 2)
  108.             # cv2.imshow("Step 2", step2)
  109.  
  110.         if(color == 2):
  111.             color = 0
  112.         else:
  113.             color = color + 1
  114.  
  115.  
  116.     # remove groups with length <= 1
  117.     newListOfGroups = []
  118.     for group in listOfGroups:
  119.         if(len(group) > 1):
  120.             newListOfGroups.append(group)
  121.  
  122.  
  123.     listOfGroups = newListOfGroups
  124.  
  125.     color = 0
  126.     for group in listOfGroups:
  127.         for possibleChar in group:
  128.             x = possibleChar.boundingRectX
  129.             y = possibleChar.boundingRectY
  130.             h = possibleChar.boundingRectH
  131.             w = possibleChar.boundingRectW
  132.  
  133.             # # step 3 removing some groups
  134.             # step3 = frame
  135.             # cv2.rectangle(step3, (x,y), (x+w,y+h), colors[color], 2)
  136.             # cv2.imshow("Step 3", step3)
  137.  
  138.         if(color == 2):
  139.             color = 0
  140.         else:
  141.             color = color + 1
  142.  
  143.     # reestructuring to array of group objects
  144.     newListGroups = []
  145.     for group in listOfGroups:
  146.         top = 10000
  147.         left = 10000
  148.         right = 0
  149.         bottom = 0
  150.  
  151.         for item in group:
  152.             x = item.boundingRectX
  153.             y = item.boundingRectY
  154.             h = item.boundingRectH
  155.             w = item.boundingRectW
  156.             if(y < top):
  157.                 top = y
  158.             if(x < left):
  159.                 left = x
  160.             if(x+w > right):
  161.                 right = x+w
  162.             if(y+h > bottom):
  163.                 bottom = y+h
  164.  
  165.         if(top-errorChance < 0):
  166.             top = 0
  167.         else:
  168.             top = top-errorChance
  169.  
  170.         if(left-errorChance < 0):
  171.             left = 0
  172.         else:
  173.             left = left-errorChance
  174.  
  175.         if(right+errorChance > maxRight):
  176.             right = maxRight
  177.         else:
  178.             right = right+errorChance
  179.        
  180.         if(bottom+errorChance > maxBottom):
  181.             bottom = maxBottom
  182.         else:
  183.             bottom = bottom+errorChance
  184.  
  185.         groupObject = Functions.group(top, left, right, bottom)
  186.  
  187.         newListGroups.append(groupObject)
  188.  
  189.  
  190.     listOfGroups = newListGroups
  191.  
  192.     # for group in listOfGroups:
  193.     #   # step 4 groups with error chance
  194.     #   step4 = frame
  195.     #   cv2.rectangle(step4, (group.left,group.top), (group.right,group.bottom), (255,255,255), 2)
  196.     #   cv2.imshow("Step 4", step4)
  197.  
  198.     # join groups
  199.  
  200.     joined = True
  201.  
  202.     aux = [ 0, 2 ]
  203.  
  204.     while joined == True:
  205.     # for a in aux:
  206.         joined = False
  207.         counter = 0
  208.         newJoinGroup = []
  209.         listOfJoinGroups = []
  210.  
  211.         for group in listOfGroups:
  212.            
  213.             if(counter == 0):
  214.                 newJoinGroup.append(group)
  215.                 if(len(listOfGroups) == 1):
  216.                     listOfJoinGroups.append(newJoinGroup)
  217.                     break
  218.             else:
  219.                 groupPrevious = listOfGroups[counter-1]
  220.  
  221.                 if( Functions.inGroups(groupPrevious, group)):
  222.                     newJoinGroup.append(group)
  223.                     joined = True
  224.                 elif(Functions.inGroups(group , groupPrevious)):
  225.                     newJoinGroup.append(group)
  226.                     joined = True
  227.                 else:
  228.                     listOfJoinGroups.append(newJoinGroup)
  229.                     newJoinGroup = []
  230.                     newJoinGroup.append(group)
  231.  
  232.             counter = counter + 1
  233.  
  234.             if(len(listOfGroups) == counter):
  235.                 listOfJoinGroups.append(newJoinGroup)
  236.                 break
  237.  
  238.             # cv2.rectangle(frame, (group.left,group.top), (group.right,group.bottom), (0,0,255), 2)
  239.  
  240.  
  241.         # show joinend groups
  242.  
  243.         newListGroups = []
  244.  
  245.         for joinedGroups in listOfJoinGroups:
  246.             top = 10000
  247.             left = 10000
  248.             right = 0
  249.             bottom = 0
  250.  
  251.             for group in joinedGroups:
  252.                 _top = group.top
  253.                 _left = group.left
  254.                 _right = group.right
  255.                 _bottom = group.bottom
  256.  
  257.                 if(_top < top):
  258.                     top = _top
  259.                 if(_left < left):
  260.                     left = _left
  261.                 if(_right > right):
  262.                     right = _right
  263.                 if(_bottom > bottom):
  264.                     bottom = _bottom
  265.  
  266.             groupObject = Functions.group(top, left, right, bottom)
  267.  
  268.             newListGroups.append(groupObject)
  269.  
  270.             # cv2.rectangle(frame, (left,top), (right,bottom), (225,255,0), 2)
  271.  
  272.         listOfGroups = newListGroups
  273.  
  274.     newListOfGroups = []
  275.  
  276.     # remove groups where no character found
  277.     for group in listOfGroups:
  278.  
  279.         roi = thresh[group.top:group.bottom, group.left:group.right]
  280.         # cv2.rectangle(frame, (group.left,group.top), (group.right,group.bottom), (255,0,255), 2)
  281.         # cv2.imshow("roi", roi)
  282.  
  283.         text = pytesseract.image_to_string(roi, config='-l eng')
  284.         if(len(text) > 0):
  285.             # print text
  286.             newListOfGroups.append(group)
  287.  
  288.     listOfGroups = newListOfGroups
  289.  
  290.     # removing not possible plates
  291.     newListOfGroups = []
  292.  
  293.     for group in listOfGroups:
  294.         if(Functions.checkPossiblePlate(group)):
  295.             newListOfGroups.append(group)
  296.  
  297.             listOfGroups = newListOfGroups
  298.  
  299.             for group in listOfGroups:
  300.                 cv2.rectangle(frame, (group.left,group.top), (group.right,group.bottom), (255,0,255), 2)
  301.                 roi = thresh[group.top:group.bottom, group.left:group.right]
  302.                 # cv2.imshow("roi", roi)
  303.  
  304.  
  305.             _,contours, hierarchy = cv2.findContours(roi, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  306.  
  307.             possibleChars = []
  308.  
  309.             for contour in contours:
  310.                 possibleChar = Functions.possibleChar(contour)
  311.  
  312.                 if(Functions.checkIfChar(possibleChar)):
  313.                     possibleChars.append(possibleChar)
  314.  
  315.             color = 0
  316.             counter = -1
  317.  
  318.             plateHeight = 160
  319.             plateWidth = 390
  320.  
  321.             plate = False
  322.  
  323.             possibleChars = Functions.filterByGreater(possibleChars, 0.8)
  324.             possibleChars = Functions.orderByX(possibleChars)
  325.  
  326.             margin = 1
  327.             margin_offset = 5
  328.  
  329.  
  330.             default_x_offset = 40
  331.             x_offset = default_x_offset
  332.             y_offset = 40
  333.  
  334.             # if changing expectedHeight to 40, It will work for car3.jpg but It wll broke result for the other.
  335.             expectedHeight = 60
  336.             heightGap = 5
  337.  
  338.             plate_found = False
  339.             plate_text = "cannotBeIdentified"
  340.  
  341.             while (plate_found == False and expectedHeight >= 20):
  342.  
  343.                 x_offset = default_x_offset
  344.                 plate = Image.new('RGB', (plateWidth, plateHeight))
  345.  
  346.                 for possibleChar in possibleChars:
  347.                     counter += 1
  348.                     x = possibleChar.boundingRectX + listOfGroups[0].left
  349.                     y = possibleChar.boundingRectY + listOfGroups[0].top
  350.                     h = possibleChar.boundingRectH
  351.                     w = possibleChar.boundingRectW
  352.  
  353.                     # step 6 : possible chars
  354.                     # step6 = frame
  355.                     # cv2.rectangle(step6, (x,y), (x+w,y+h), colors[color], 1)
  356.                     # cv2.imshow("Step 6", step6)
  357.  
  358.  
  359.                     roi = thresh[y-margin:y+h+margin, x-margin:x+w+margin]
  360.                     roi = Functions.applyBitwise_not(roi, w+margin*2, h+margin*2, 0.7)
  361.                     roi = Functions.resizeChar(Image.fromarray(roi), expectedHeight)
  362.  
  363.                     area = (x_offset,y_offset)
  364.  
  365.                     plate.paste(roi, area)
  366.                     x_offset += roi.size[0]+margin_offset
  367.  
  368.                     if(color == 2):
  369.                         color = 0
  370.                     else:
  371.                         color = color + 1
  372.  
  373.                 text = pytesseract.image_to_string(plate, config='-l eng')
  374.  
  375.                 if(Functions.checkPlateText(text)):
  376.                     plate_found = True
  377.                     plate_text = text
  378.                 else:
  379.                     expectedHeight -= heightGap
  380.  
  381.             print("Car Plate: " + Functions.formatPlate(plate_text))
  382.  
  383.             cv2.imshow("Plate", np.array(plate))
  384.         else:
  385.             pass
  386.  
  387. # Finalizing
  388. cv2.waitKey(0)
  389. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement