Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. import cv2
  2. import pytesseract
  3. import numpy as np
  4. class ResultBoxDimensions:
  5. def __init__(self, playerCount, anchor, width, padding, margin):
  6. self.playerCount = playerCount
  7. self.anchor = anchor
  8. self.width = width
  9. self.padding = padding
  10. self.margin = margin
  11.  
  12. dimensions = []
  13. dimensions.append(ResultBoxDimensions(2, (279, 532), 572, 17, 184))
  14. dimensions.append(ResultBoxDimensions(3, ( 46, 532), 572, 17, 22))
  15. dimensions.append(ResultBoxDimensions(4, ( 17, 533), 446, 17, 0))
  16. dimensions.append(ResultBoxDimensions(5, (126, 538), 264, 13, 60))
  17. dimensions.append(ResultBoxDimensions(6, ( 38, 538), 264, 13, 26))
  18. dimensions.append(ResultBoxDimensions(7, ( 34, 538), 220, 10, 32))
  19. dimensions.append(ResultBoxDimensions(8, ( 10, 538), 220, 10, 0))
  20. dimensions.sort(key=lambda x: x.anchor[0])
  21. #Methods
  22. def RotateImage(image, angle):
  23. image_center = tuple(np.array(image.shape[1::-1]) / 2)
  24. rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
  25. result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
  26. return result
  27. def GetPlayersFromResultScreen(image,
  28. threshold = 3,
  29. pointChecks = 3,
  30. topIdealColor = 28,
  31. bottomIdealColor = 72,
  32. drawCheckPoints = True):
  33. for d in dimensions:
  34. for p in range(d.playerCount):
  35. isResultScreen = True
  36. for c in range(pointChecks):
  37. koTopColor = image[d.anchor[1], d.anchor[0] + (d.width/pointChecks)*c + (d.width + 2*d.padding + d.margin)*p]
  38. koBottomColor = image[d.anchor[1] + 42, d.anchor[0] + (d.width/pointChecks)*c + (d.width + 2*d.padding + d.margin)*p]
  39. fallsTopColor = image[d.anchor[1] + 112 , d.anchor[0] + (d.width/pointChecks)*c + (d.width + 2*d.padding + d.margin)*p]
  40. fallsBottomColor = image[d.anchor[1] + 112 + 42, d.anchor[0] + (d.width/pointChecks)*c + (d.width + 2*d.padding + d.margin)*p]
  41. if ( abs(topIdealColor - koTopColor) <= threshold and
  42. abs(topIdealColor - fallsTopColor) <= threshold and
  43. abs(bottomIdealColor - koBottomColor) <= threshold and
  44. abs(bottomIdealColor - fallsBottomColor) <= threshold ):
  45. continue
  46. isResultScreen = False
  47. break
  48. if(isResultScreen):
  49. for count in range(pointChecks):
  50. radius = 5
  51. color = (0, 0, 255)
  52. thickness = 1
  53. cv2.circle(image, ((d.anchor[0] + ((d.width-2)/pointChecks)*count + (d.width + 2*d.padding + d.margin)*p), d.anchor[1]), radius, color, thickness)
  54. cv2.circle(image, ((d.anchor[0] + ((d.width-2)/pointChecks)*count + (d.width + 2*d.padding + d.margin)*p), d.anchor[1] + 42), radius, color, thickness)
  55. cv2.circle(image, ((d.anchor[0] + ((d.width-2)/pointChecks)*count + (d.width + 2*d.padding + d.margin)*p), d.anchor[1] + 112), radius, color, thickness)
  56. cv2.circle(image, ((d.anchor[0] + ((d.width-2)/pointChecks)*count + (d.width + 2*d.padding + d.margin)*p), d.anchor[1] + 112 + 42), radius, color, thickness)
  57. return (p+1)
  58. return 0
  59. pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
  60. cap = cv2.VideoCapture(1) #cv2.VideoCapture("C:/tools/SmashParser/SmashVideo_Trim.mp4")
  61. i=0
  62. while(True):
  63. ret, frame = cap.read()
  64. if ret == False or (not cap.isOpened()):
  65. break
  66. i+=1
  67. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  68.  
  69. # add frame count
  70. cv2.putText(gray, str(i), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
  71. # export frame if it's result screen
  72. playerCount = GetPlayersFromResultScreen(gray)
  73. if (playerCount > 0):
  74. print("RESULT SCREEN: " + str(playerCount))
  75. #cv2.imwrite(('C:/tools/SmashParser/Images/'+str(i)+'.png'),gray)
  76. cv2.imshow('frame', gray)
  77. # get player names and print them to stdout
  78. if cv2.waitKey(1) & 0xFF == ord('s'):
  79. cv2.imwrite(('C:/tools/SmashParser/Images/'+str(i)+'_'+str(0)+'.png'),gray)
  80. #for x in range(8):
  81. #crop_img = gray[0:250, (240*x):(240*x)+240]
  82. #ret, crop_img = cv2.threshold(crop_img,210,255,cv2.THRESH_BINARY)
  83. #crop_img = RotateImage(crop_img, -5.1)
  84. #print(pytesseract.image_to_string(crop_img))
  85. #cv2.imwrite(('C:/tools/SmashParser/Images/'+str(i)+'_'+str(x)+'.png'),crop_img)
  86. if cv2.waitKey(1) & 0xFF == ord('q'):
  87. break
  88. cap.release()
  89. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement