Page boundary detection in historical documents
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

180 lignes
4.6KB

  1. #from grabCutCrop import cropMask
  2. import cv2
  3. import numpy as np
  4. import sys
  5. def cropMaskNo(imgH,imgW):
  6. mask = np.ones((imgH,imgW),np.uint8)
  7. return mask
  8. def cropMaskPortion(imgH,imgW,xp1,yp1,xp2,yp2,xp3,yp3,xp4,yp4):
  9. mask = np.zeros((imgH,imgW),np.uint8)
  10. y1 = yp1*imgH
  11. x1 = xp1*imgW
  12. y2 = yp2*imgH
  13. x2 = xp2*imgW
  14. y3 = yp3*imgH
  15. x3 = xp3*imgW
  16. y4 = yp4*imgH
  17. x4 = xp4*imgW
  18. cv2.fillConvexPoly(mask, np.array([[x1,y1], [x2,y2], [x3,y3], [x4,y4]], np.int32), 1, 8)
  19. return mask
  20. if len(sys.argv)<3:
  21. print('Computes no-crop and mean-crop baselines, generates two files {gtFile}_fullno.res and {gtFile}_fullmean.res')
  22. print('usage: '+sys.argv[0]+' gtFile.csv imageDir [mean_x1 mean_y1 mean_x2 mean_y2 mean_x3 mean_y3 mean_x4 mean_y4]')
  23. print(' (optional mean box)')
  24. exit()
  25. gtFile=sys.argv[1]
  26. imageDir=sys.argv[2]
  27. if imageDir[-1]!='/':
  28. imageDir+='/'
  29. reverse=False
  30. sumIOU_no=0
  31. sumIOU_mean=0
  32. countIOU=0
  33. scale=1
  34. print('eval on '+gtFile)
  35. outFile_no = gtFile+'_fullno.res'
  36. outFile_mean = gtFile+'_fullmean.res'
  37. numLines=0
  38. try:
  39. with open(outFile_no,'r') as f:
  40. numLines = len(f.readlines())
  41. except IOError:
  42. numLines=0
  43. try:
  44. out_no = open(outFile_no,'a')
  45. except IOError:
  46. out_no = open(outFile_no,'w')
  47. try:
  48. out_mean = open(outFile_mean,'a')
  49. except IOError:
  50. out_mean = open(outFile_mean,'w')
  51. with open(gtFile) as f:
  52. cc=0
  53. lines = f.readlines()
  54. xp1=0
  55. yp1=0
  56. xp2=0
  57. yp2=0
  58. xp3=0
  59. yp3=0
  60. xp4=0
  61. yp4=0
  62. imgHW={}
  63. for line in lines:
  64. p = line.split(',')
  65. imagePath = p[0]
  66. x1 = float(p[1])*scale
  67. y1 = float(p[2])*scale
  68. x2 = float(p[3])*scale
  69. y2 = float(p[4])*scale
  70. x3 = float(p[5])*scale
  71. y3 = float(p[6])*scale
  72. x4 = float(p[7])*scale
  73. y4 = float(p[8])*scale
  74. if reverse:
  75. tmpX=x4
  76. tmpY=y4
  77. x4=x3
  78. y4=y3
  79. x3=tmpX
  80. y4=tmpY
  81. #type = p[9]
  82. if x1<0:
  83. continue
  84. cc+=1
  85. image = cv2.imread(imageDir+imagePath)
  86. imgHW[imagePath]=(int(image.shape[0]*scale),int(image.shape[1]*scale))
  87. xp1 += x1/(image.shape[1]*scale)
  88. yp1 += y1/(image.shape[0]*scale)
  89. xp2 += x2/(image.shape[1]*scale)
  90. yp2 += y2/(image.shape[0]*scale)
  91. xp3 += x3/(image.shape[1]*scale)
  92. yp3 += y3/(image.shape[0]*scale)
  93. xp4 += x4/(image.shape[1]*scale)
  94. yp4 += y4/(image.shape[0]*scale)
  95. xp1/=cc
  96. yp1/=cc
  97. xp2/=cc
  98. yp2/=cc
  99. xp3/=cc
  100. yp3/=cc
  101. xp4/=cc
  102. yp4/=cc
  103. if len(sys.argv)>9:
  104. xp1=float(sys.argv[3])
  105. yp1=float(sys.argv[4])
  106. xp2=float(sys.argv[5])
  107. yp2=float(sys.argv[6])
  108. xp3=float(sys.argv[7])
  109. yp3=float(sys.argv[8])
  110. xp4=float(sys.argv[9])
  111. yp4=float(sys.argv[10])
  112. else:
  113. print(str(xp1)+' '+str(yp1)+' '+str(xp2)+' '+str(yp2)+' '+str(xp3)+' '+str(yp3)+' '+str(xp4)+' '+str(yp4))
  114. cc=0
  115. for line in lines:
  116. p = line.split(',')
  117. imagePath = p[0]
  118. x1 = float(p[1])*scale
  119. y1 = float(p[2])*scale
  120. x2 = float(p[3])*scale
  121. y2 = float(p[4])*scale
  122. x3 = float(p[5])*scale
  123. y3 = float(p[6])*scale
  124. x4 = float(p[7])*scale
  125. y4 = float(p[8])*scale
  126. if reverse:
  127. tmpX=x4
  128. tmpY=y4
  129. x4=x3
  130. y4=y3
  131. x3=tmpX
  132. y4=tmpY
  133. #type = p[9]
  134. if x1<0:
  135. continue
  136. cc+=1
  137. if cc<=numLines:
  138. continue
  139. #image = cv2.imread(imageDir+imagePath)
  140. hw = imgHW[imagePath]
  141. #print hw
  142. mask_no = cropMaskNo(hw[0],hw[1])
  143. mask_mean = cropMaskPortion(hw[0],hw[1],xp1,yp1,xp2,yp2,xp3,yp3,xp4,yp4)
  144. gtMask = np.zeros(mask_no.shape,np.uint8)
  145. cv2.fillConvexPoly(gtMask, np.array([[x1,y1], [x2,y2], [x3,y3], [x4,y4]], np.int32), 1, 8)
  146. intersection_no = np.sum(mask_no&gtMask)
  147. union_no = np.sum(mask_no|gtMask)
  148. sumIOU_no += float(intersection_no)/union_no
  149. out_no.write(imagePath+' '+str(float(intersection_no)/union_no)+'\n')
  150. out_no.flush()
  151. intersection_mean = np.sum(mask_mean&gtMask)
  152. union_mean = np.sum(mask_mean|gtMask)
  153. sumIOU_mean += float(intersection_mean)/union_mean
  154. out_mean.write(imagePath+' '+str(float(intersection_mean)/union_mean)+'\n')
  155. out_mean.flush()
  156. countIOU += 1
  157. out_no.write('mean IOU for '+gtFile+': '+str(sumIOU_no/countIOU)+'\n')
  158. out_no.close()
  159. out_mean.write('mean IOU for '+gtFile+': '+str(sumIOU_mean/countIOU)+'\n')
  160. out_mean.close()