Advertisement
Guest User

main.py

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