ccpd_process.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import os
  2. import shutil
  3. import cv2
  4. import numpy as np
  5. def allFilePath(rootPath,allFIleList):
  6. fileList = os.listdir(rootPath)
  7. for temp in fileList:
  8. if os.path.isfile(os.path.join(rootPath,temp)):
  9. if temp.endswith(".jpg"):
  10. allFIleList.append(os.path.join(rootPath,temp))
  11. else:
  12. allFilePath(os.path.join(rootPath,temp),allFIleList)
  13. def order_points(pts):
  14. # initialzie a list of coordinates that will be ordered
  15. # such that the first entry in the list is the top-left,
  16. # the second entry is the top-right, the third is the
  17. # bottom-right, and the fourth is the bottom-left
  18. pts=pts[:4,:]
  19. rect = np.zeros((5, 2), dtype = "float32")
  20. # the top-left point will have the smallest sum, whereas
  21. # the bottom-right point will have the largest sum
  22. s = pts.sum(axis = 1)
  23. rect[0] = pts[np.argmin(s)]
  24. rect[2] = pts[np.argmax(s)]
  25. # now, compute the difference between the points, the
  26. # top-right point will have the smallest difference,
  27. # whereas the bottom-left will have the largest difference
  28. diff = np.diff(pts, axis = 1)
  29. rect[1] = pts[np.argmin(diff)]
  30. rect[3] = pts[np.argmax(diff)]
  31. # return the ordered coordinates
  32. return rect
  33. def get_partical_ccpd():
  34. ccpd_dir = r"/mnt/Gpan/BaiduNetdiskDownload/CCPD1/CCPD2020/ccpd_green"
  35. save_Path = r"ccpd/green_plate"
  36. folder_list = os.listdir(ccpd_dir)
  37. for folder_name in folder_list:
  38. count=0
  39. folder_path = os.path.join(ccpd_dir,folder_name)
  40. if os.path.isfile(folder_path):
  41. continue
  42. if folder_name == "ccpd_fn":
  43. continue
  44. name_list = os.listdir(folder_path)
  45. save_folder=save_Path
  46. if not os.path.exists(save_folder):
  47. os.mkdir(save_folder)
  48. for name in name_list:
  49. file_path = os.path.join(folder_path,name)
  50. count+=1
  51. if count>1000:
  52. break
  53. new_file_path =os.path.join(save_folder,name)
  54. shutil.move(file_path,new_file_path)
  55. print(count,new_file_path)
  56. def get_rect_and_landmarks(img_path):
  57. file_name = img_path.split("/")[-1].split("-")
  58. landmarks_np =np.zeros((5,2))
  59. rect = file_name[2].split("_")
  60. landmarks=file_name[3].split("_")
  61. rect_str = "&".join(rect)
  62. landmarks_str= "&".join(landmarks)
  63. rect= rect_str.split("&")
  64. landmarks=landmarks_str.split("&")
  65. rect=[int(x) for x in rect]
  66. landmarks=[int(x) for x in landmarks]
  67. for i in range(4):
  68. landmarks_np[i][0]=landmarks[2*i]
  69. landmarks_np[i][1]=landmarks[2*i+1]
  70. # middle_landmark_w =int((landmarks[4]+landmarks[6])/2)
  71. # middle_landmark_h =int((landmarks[5]+landmarks[7])/2)
  72. # landmarks.append(middle_landmark_w)
  73. # landmarks.append(middle_landmark_h)
  74. landmarks_np_new=order_points(landmarks_np)
  75. # landmarks_np_new[4]=np.array([middle_landmark_w,middle_landmark_h])
  76. return rect,landmarks,landmarks_np_new
  77. def x1x2y1y2_yolo(rect,landmarks,img):
  78. h,w,c =img.shape
  79. rect[0] = max(0, rect[0])
  80. rect[1] = max(0, rect[1])
  81. rect[2] = min(w - 1, rect[2]-rect[0])
  82. rect[3] = min(h - 1, rect[3]-rect[1])
  83. annotation = np.zeros((1, 14))
  84. annotation[0, 0] = (rect[0] + rect[2] / 2) / w # cx
  85. annotation[0, 1] = (rect[1] + rect[3] / 2) / h # cy
  86. annotation[0, 2] = rect[2] / w # w
  87. annotation[0, 3] = rect[3] / h # h
  88. annotation[0, 4] = landmarks[0] / w # l0_x
  89. annotation[0, 5] = landmarks[1] / h # l0_y
  90. annotation[0, 6] = landmarks[2] / w # l1_x
  91. annotation[0, 7] = landmarks[3] / h # l1_y
  92. annotation[0, 8] = landmarks[4] / w # l2_x
  93. annotation[0, 9] = landmarks[5] / h # l2_y
  94. annotation[0, 10] = landmarks[6] / w # l3_x
  95. annotation[0, 11] = landmarks[7] / h # l3_y
  96. # annotation[0, 12] = landmarks[8] / w # l4_x
  97. # annotation[0, 13] = landmarks[9] / h # l4_y
  98. return annotation
  99. def xywh2yolo(rect,landmarks_sort,img):
  100. h,w,c =img.shape
  101. rect[0] = max(0, rect[0])
  102. rect[1] = max(0, rect[1])
  103. rect[2] = min(w - 1, rect[2]-rect[0])
  104. rect[3] = min(h - 1, rect[3]-rect[1])
  105. annotation = np.zeros((1, 12))
  106. annotation[0, 0] = (rect[0] + rect[2] / 2) / w # cx
  107. annotation[0, 1] = (rect[1] + rect[3] / 2) / h # cy
  108. annotation[0, 2] = rect[2] / w # w
  109. annotation[0, 3] = rect[3] / h # h
  110. annotation[0, 4] = landmarks_sort[0][0] / w # l0_x
  111. annotation[0, 5] = landmarks_sort[0][1] / h # l0_y
  112. annotation[0, 6] = landmarks_sort[1][0] / w # l1_x
  113. annotation[0, 7] = landmarks_sort[1][1] / h # l1_y
  114. annotation[0, 8] = landmarks_sort[2][0] / w # l2_x
  115. annotation[0, 9] = landmarks_sort[2][1] / h # l2_y
  116. annotation[0, 10] = landmarks_sort[3][0] / w # l3_x
  117. annotation[0, 11] = landmarks_sort[3][1] / h # l3_y
  118. # annotation[0, 12] = landmarks_sort[4][0] / w # l4_x
  119. # annotation[0, 13] = landmarks_sort[4][1] / h # l4_y
  120. return annotation
  121. def yolo2x1y1x2y2(annotation,img):
  122. h,w,c = img.shape
  123. rect= annotation[:,0:4].squeeze().tolist()
  124. landmarks=annotation[:,4:].squeeze().tolist()
  125. rect_w = w*rect[2]
  126. rect_h =h*rect[3]
  127. rect_x =int(rect[0]*w-rect_w/2)
  128. rect_y = int(rect[1]*h-rect_h/2)
  129. new_rect=[rect_x,rect_y,rect_x+rect_w,rect_y+rect_h]
  130. for i in range(5):
  131. landmarks[2*i]=landmarks[2*i]*w
  132. landmarks[2*i+1]=landmarks[2*i+1]*h
  133. return new_rect,landmarks
  134. def write_lable(file_path):
  135. pass
  136. if __name__ == '__main__':
  137. file_root = r"ccpd"
  138. file_list=[]
  139. count=0
  140. allFilePath(file_root,file_list)
  141. for img_path in file_list:
  142. count+=1
  143. # img_path = r"ccpd_yolo_test/02-90_85-173&466_452&541-452&553_176&556_178&463_454&460-0_0_6_26_15_26_32-68-53.jpg"
  144. text_path= img_path.replace(".jpg",".txt")
  145. img =cv2.imread(img_path)
  146. rect,landmarks,landmarks_sort=get_rect_and_landmarks(img_path)
  147. # annotation=x1x2y1y2_yolo(rect,landmarks,img)
  148. annotation=xywh2yolo(rect,landmarks_sort,img)
  149. str_label = "0 "
  150. for i in range(len(annotation[0])):
  151. str_label = str_label + " " + str(annotation[0][i])
  152. str_label = str_label.replace('[', '').replace(']', '')
  153. str_label = str_label.replace(',', '') + '\n'
  154. with open(text_path,"w") as f:
  155. f.write(str_label)
  156. print(count,img_path)
  157. # get_partical_ccpd()
  158. # file_root = r"ccpd/green_plate"
  159. # file_list=[]
  160. # allFilePath(file_root,file_list)
  161. # count=0
  162. # for img_path in file_list:
  163. # img_name = img_path.split(os.sep)[-1]
  164. # if not "&" in img_name:
  165. # count+=1
  166. # os.remove(img_path)
  167. # print(count,img_path)
  168. # new_rect,new_landmarks=yolo2x1y1x2y2(annotation,img)
  169. # rect= [int(x) for x in new_rect]
  170. # cv2.rectangle(img,(rect[0],rect[1]),(rect[2],rect[3]),(255,0,0),2)
  171. # colors=[(0,255,0),(0,255,255),(255,255,0),(255,255,255),(255,0,255)] #绿 黄 青 白
  172. # for i in range(5):
  173. # cv2.circle(img,(landmarks[2*i],landmarks[2*i+1]),2,colors[i],2)
  174. # cv2.imwrite("1.jpg",img)
  175. # print(rect,landmarks)
  176. # get_partical_ccpd()