当前位置:   article > 正文

手部姿态检测(按视频段)_v1(数据处理)_基于手姿态的异常检测

基于手姿态的异常检测

参考时空图卷积,加上了空间信息,把二维的坐标点加上时间帧的维度,形成三维的矩阵数据。

方法2:st-gcn、hcn、2s-agcn
数据集:20人,7k+段视频
准确度:0.95
训练技巧:
1.自动清洗数据,判断人脸检测是否变形,判断一段视频的有效帧是否足够
2.尝试了1s/2s/4s视频进行预测,最终选择2s 64帧作为一条数据,有15帧有效则数据可用,否则是无效数据
3.数据预处理,把一段视频的所有帧,脸部和手部的检测点转化成3维数据(2d坐标+置信度),等于加上时间信息

骨骼图:

试验方法:

数据处理步骤:

视频分段->计算特征->数据清洗->坐标归一化

1.视频分段

  1. import sys
  2. import os
  3. import dlib
  4. import cv2
  5. from PIL import Image
  6. import numpy as np
  7. import time
  8. import imutils
  9. def cut(file_name,save_name):
  10. cap = cv2.VideoCapture(file_name)
  11. if cap.isOpened():
  12. rate = cap.get(5) # 帧速率
  13. FrameNumber = cap.get(7) # 总帧数
  14. duration = int(FrameNumber/rate) # 总帧数/帧速率=时间
  15. hasFrame, frame = cap.read()
  16. w = frame.shape[1]
  17. h = frame.shape[0]
  18. root=save_name.split(".")[0]
  19. nums=int(FrameNumber/64)
  20. for i in range(nums):
  21. videoWriter =cv2.VideoWriter(root+'_'+str(i)+'.mp4',cv2.VideoWriter_fourcc(*"mp4v"),30,(h, w))
  22. #开始进入循环取帧
  23. n=0
  24. while (hasFrame):
  25. n += 1
  26. if n<=64:
  27. hasFrame, frame = cap.read() #读取视频帧
  28. if hasFrame:
  29. frame=np.rot90(frame)
  30. # frame=np.rot90(frame)
  31. # frame=np.rot90(frame)
  32. videoWriter.write(frame)
  33. else:
  34. break
  35. cap.release()
  36. path="E:/data/src_vedio/6/img3/"
  37. for file in os .listdir(path):
  38. name=file.split('.')[0]
  39. save_root="E:/data/makeup_vedio/class2_2s/6/class3/"+str(name)+"/"
  40. if os.path.exists(save_root) == False:
  41. os.makedirs(save_root)
  42. save_name=save_root+file
  43. file_name=path+file
  44. cut(file_name,save_name)
  45. print(save_root)

2.计算特征(主要获取关键点的坐标)

  1. import sys
  2. import os
  3. import dlib
  4. import cv2
  5. from PIL import Image
  6. import numpy as np
  7. import time
  8. import imutils
  9. def crop(img, point_face):
  10. top=point_face[24][1]
  11. mins=top-200
  12. if mins<=0:
  13. mins=0
  14. h=img.shape[0]
  15. w=img.shape[1]
  16. img2=img[mins:h,0:w]
  17. return img2
  18. def face_detect(pic):
  19. detector = dlib.get_frontal_face_detector()
  20. predictor = dlib.shape_predictor('class/shape_predictor_68_face_landmarks.dat')
  21. img = np.copy(pic)
  22. # img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
  23. # 人脸数rects
  24. rects = detector(img, 1)
  25. if len(rects)!=1:
  26. # print(len(rects),"face detection fail!")
  27. return -1,-1
  28. landmarks = np.matrix([[p.x, p.y] for p in predictor(img,rects[0]).parts()])
  29. point_face=[]
  30. for idx, point in enumerate(landmarks):
  31. x=point[0, 0]
  32. y=point[0, 1]
  33. point_face.append((x,y))
  34. # 画图和点
  35. cv2.circle(img, (x,y), 2, (0, 255, 0), thickness=-1, lineType=cv2.FILLED)
  36. cv2.putText(img, str(idx), (x,y), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), 1,cv2.LINE_AA)
  37. return img, point_face
  38. def hand_detect(pic, img_face):
  39. protoFile = "class/hand/pose_deploy.prototxt"
  40. weightsFile = "class/hand/pose_iter_102000.caffemodel"
  41. nPoints = 22
  42. POSE_PAIRS = [[0, 1], [1, 2], [2, 3], [3, 4], [0, 5], [5, 6], [6, 7], [7, 8], [0, 9], [9, 10], [10, 11], [11, 12],
  43. [0, 13], [13, 14], [14, 15], [15, 16], [0, 17], [17, 18], [18, 19], [19, 20]]
  44. net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
  45. img_hand=np.copy(img_face) #用来画手的坐标点
  46. frame = np.copy(pic) #用来做手部检测
  47. frameWidth = frame.shape[1]
  48. frameHeight = frame.shape[0]
  49. aspect_ratio = frameWidth / frameHeight
  50. threshold = 0.1
  51. t = time.time()
  52. # input image dimensions for the network
  53. inHeight = 368
  54. inWidth = int(((aspect_ratio * inHeight) * 8) // 8)
  55. inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)
  56. net.setInput(inpBlob)
  57. output = net.forward()
  58. # print("time taken by network : {:.3f}".format(time.time() - t))
  59. # Empty list to store the detected keypoints
  60. point_hand = []
  61. for i in range(nPoints):
  62. # 对应身体部位的置信度图
  63. probMap = output[0, i, :, :]
  64. probMap = cv2.resize(probMap, (frameWidth, frameHeight))
  65. # cv2.minMaxLoc:返回矩阵的最小值,最大值,并得到最大值,最小值的索引
  66. minVal, prob, minLoc, point = cv2.minMaxLoc(probMap) #找最大值及其下标(即置信度最大的点)
  67. # 获取坐标点
  68. x,y=(int(point[0]), int(point[1]))
  69. point_hand.append((x,y,prob))
  70. # 画图和点
  71. cv2.circle(img_hand, (x,y), 2, (255, 255, 0), thickness=-1, lineType=cv2.FILLED)
  72. cv2.putText(img_hand, str(i), (x,y), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 0), 1,cv2.LINE_AA)
  73. return img_hand,point_hand
  74. def get_feature(point_face, point_hand,img):
  75. circle_center=point_face[30]
  76. # 1.只取15个关键点
  77. # face_key_point=[0,16,2,14,48,54,39,42,27,30,57]
  78. # hand_key_point=[8,12,16,20,6,10,14,18,5,9,13,17]
  79. # face_key_point=[36,45,2,14,39,42,30,57]
  80. face_key_point=[36,45,2,14,39,42,30,57]
  81. hand_key_point=[8,12,16,6,10,14,9]
  82. for i in face_key_point:
  83. x=point_face[i][0]-circle_center[0]
  84. y=point_face[i][1]-circle_center[1]
  85. confidence=1
  86. f1.write(str(x)+' '+str(y)+' '+str(confidence)+' ')
  87. # 画图和点
  88. cv2.circle(img, (point_face[i][0],point_face[i][1]), 2, (255, 255, 0), thickness=-1, lineType=cv2.FILLED)
  89. cv2.putText(img, str(i), (point_face[i][0],point_face[i][1]), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 0), 1,cv2.LINE_AA)
  90. for i in hand_key_point:
  91. x=point_hand[i][0]-circle_center[0]
  92. y=point_hand[i][1]-circle_center[1]
  93. confidence=point_hand[i][2]
  94. f1.write(str(x)+' '+str(y)+' '+str(confidence)+' ')
  95. # 画图和点
  96. cv2.circle(img, (point_hand[i][0],point_hand[i][1]), 2, (0, 255, 0), thickness=-1, lineType=cv2.FILLED)
  97. cv2.putText(img, str(i), (point_hand[i][0],point_hand[i][1]), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 0), 1,cv2.LINE_AA)
  98. f1.write('\n')
  99. return img
  100. # # 2.取所有点
  101. # face_len=len(point_face)
  102. # hand_len=len(point_hand)
  103. # for i in range(face_len):
  104. # x=point_face[i][0]-circle_center[0]
  105. # y=point_face[i][1]-circle_center[1]
  106. # confidence=1
  107. # f2.write(str(x)+' '+str(y)+' '+str(confidence)+' ')
  108. # for i in range(hand_len):
  109. # x=point_hand[i][0]-circle_center[0]
  110. # y=point_hand[i][1]-circle_center[1]
  111. # confidence=point_hand[i][2]
  112. # f2.write(str(x)+' '+str(y)+' '+str(confidence)+' ')
  113. # f2.write('\n')
  114. def get_feature2(point_face, point_hand):
  115. circle_center=point_face[30]
  116. # 2.取所有点
  117. face_len=len(point_face)
  118. hand_len=len(point_hand)
  119. for i in range(face_len):
  120. x=point_face[i][0]-circle_center[0]
  121. y=point_face[i][1]-circle_center[1]
  122. confidence=1
  123. f2.write(str(x)+' '+str(y)+' '+str(confidence)+' ')
  124. for i in range(hand_len):
  125. x=point_hand[i][0]-circle_center[0]
  126. y=point_hand[i][1]-circle_center[1]
  127. confidence=point_hand[i][2]
  128. f2.write(str(x)+' '+str(y)+' '+str(confidence)+' ')
  129. f2.write('\n')
  130. def get_feature3(point_face, point_hand):
  131. circle_center=point_face[30]
  132. # 2.取所有点
  133. face_len=len(point_face)
  134. hand_len=len(point_hand)
  135. for i in range(face_len):
  136. x=point_face[i][0]
  137. y=point_face[i][1]
  138. confidence=1
  139. f3.write(str(x)+','+str(y)+','+str(confidence)+' ')
  140. f3.write('\n')
  141. for i in range(hand_len):
  142. x=point_hand[i][0]-circle_center[0]
  143. y=point_hand[i][1]-circle_center[1]
  144. confidence=point_hand[i][2]
  145. f4.write(str(x)+','+str(y)+','+str(confidence)+' ')
  146. f4.write('\n')
  147. def main(file_root, v_id, a_id, label):
  148. video_path = file_root
  149. cap = cv2.VideoCapture(video_path)
  150. hasFrame, frame = cap.read()
  151. frameWidth = frame.shape[1]
  152. frameHeight = frame.shape[0]
  153. save_path = "E:/data/makeup_vedio/class2_2s/gg/"
  154. #开始进入循环取帧
  155. n=0
  156. while (hasFrame):
  157. n += 1
  158. hasFrame, frame = cap.read() #读取视频帧
  159. if hasFrame==True:
  160. h,w,_=frame.shape
  161. frame=cv2.resize(frame,(int(w*0.5),int(h*0.5)))
  162. img_face, point_face=face_detect(frame) #人脸检测
  163. if point_face !=-1:
  164. img=crop(frame, point_face)
  165. img=cv2.resize(img,(500,700))
  166. # pic=np.copy(img_face)
  167. img_face2, point_face2=face_detect(img)
  168. if point_face2 !=-1:
  169. f1.write(str(a_id)+' '+str(v_id)+' '+str(label)+' ')
  170. f2.write(str(a_id)+' '+str(v_id)+' '+str(label)+' ')
  171. f3.write(str(a_id)+' '+str(v_id)+' '+str(label)+' ')
  172. f4.write(str(a_id)+' '+str(v_id)+' '+str(label)+' ')
  173. img_hand, point_hand=hand_detect(img, img_face2) #手部检测
  174. gg=get_feature(point_face2, point_hand,img)
  175. get_feature2(point_face2, point_hand)
  176. get_feature3(point_face2, point_hand)
  177. name=file_root.split('/')[-1].split('.')[0]
  178. # cv2.imwrite(save_path+name+'-'+str(n)+'.jpg',gg)
  179. cap.release()
  180. path="E:/data/makeup_vedio/class2_2s/6/6.1/"
  181. txt_path="E:/data/makeup_vedio/class2_2s/6/6.1/"
  182. f1=open(txt_path+"data_15.txt","w+")
  183. f2=open(txt_path+"data_all.txt","w+")
  184. f3=open(txt_path+"face.txt","w+")
  185. f4=open(txt_path+"hand.txt","w+")
  186. n=0
  187. for root,dirs,files in os.walk(path):
  188. for file in files: # 遍历文件
  189. if file.endswith('mp4'):
  190. label=root.split("/")[-1]
  191. file_root=root+'/'+file
  192. n+=1
  193. star=time.time()
  194. name=file.split(".")[0]
  195. a_id=name.split("_")[0]
  196. v_id=name.split("_")[1]
  197. # print("vid:",v_id, "a_id:",a_id, "label:",label)
  198. main(file_root, v_id, a_id, label)
  199. end=time.time()-star
  200. print(n,"---time:",end)

3.数据清洗v1

  1. import shutil
  2. import os
  3. import dlib
  4. import cv2
  5. from PIL import Image
  6. import numpy as np
  7. import time
  8. import imutils
  9. # 清洗规则:
  10. # 1.手部特征点置信度>=0.1, 7个点要超过4个;
  11. # 2.人脸检测30帧通过15帧以上。
  12. def count_label(label_arr):
  13. label_0=0
  14. label_1=0
  15. label_3=0
  16. for label in label_arr:
  17. if label==0:
  18. label_0+=1
  19. elif label==1:
  20. label_1+=1
  21. elif label==3:
  22. label_3+=1
  23. return (label_0,label_1,label_3)
  24. def count_pass(point,label):
  25. n=0
  26. points=point[8:]
  27. for data in points:
  28. confidence=float(data[2])
  29. # print(confidence)
  30. if confidence>=0.1:
  31. n+=1
  32. # 类别3的数据不做判断,直接通过
  33. if label == 3:
  34. n=7
  35. # 判断人脸,若人脸检测变形,则不通过
  36. w=int(point[3][0])-int(point[2][0]) # 14-2
  37. if w<=50:
  38. n=0
  39. return n
  40. def clear(txt1,txt2):
  41. f1 = open(txt1, "r")
  42. f2 = open(txt2, "w+")
  43. lines1=f1.readlines()
  44. data_nums=len(lines1)
  45. total1=0
  46. total2=0
  47. total3=0
  48. label1=[]
  49. label2=[]
  50. data=[]
  51. data2=[]
  52. new_vid=lines1[0].strip().split(" ")[:3] # 视频标签
  53. for i in range(data_nums):
  54. line1=lines1[i].strip().split(" ")
  55. label=int(lines1[i-1].strip().split(" ")[2]) # 类别标签
  56. now_vid=line1[:3] # 视频标签
  57. point=line1[3:] #坐标点数据
  58. point = np.reshape(np.asarray(point), (-1,3))
  59. pass_nums=count_pass(point,label) #判断有多少点通过了阈值
  60. # print(pass_nums)
  61. # 标签相同则写进去
  62. if new_vid==now_vid:
  63. if pass_nums>=4:
  64. data.append(lines1[i])
  65. data2.append(lines1[i])
  66. # 标签不同的时候,判断data的数据,超过15条则通过,写入新txt
  67. else:
  68. total1+=1
  69. label1.append(label)
  70. nums=len(data)
  71. if nums>=15:
  72. for feature in data2:
  73. f2.write(feature)
  74. total2+=1
  75. label2.append(label)
  76. nums2=len(data2)
  77. if nums2>=15:
  78. total3+=1
  79. # 重置data,并判断新视频的第一条数据,是否写入data
  80. data=[]
  81. if pass_nums>=4:
  82. data.append(lines1[i])
  83. data2=[]
  84. data2.append(lines1[i])
  85. new_vid=now_vid
  86. f2.close()
  87. f3 = open(txt2, "r")
  88. new_nums=len(f3.readlines())
  89. label_old=count_label(label1)
  90. label_new=count_label(label2)
  91. return data_nums,new_nums,total1,total2,total3,label_old,label_new
  92. txt1="txt2/src/data_15.txt"
  93. txt2="txt2/clear/data_15.txt"
  94. data_nums, new_nums, total1, total2, total3, label_old, label_new=clear(txt1,txt2)
  95. print("数据总量:",data_nums,new_nums)
  96. print("视频数量:",total1,total2)
  97. print("人脸检测问题减少:",total1-total3)
  98. print("手部检测问题减少:",(total1-total2)-(total1-total3))
  99. print("清洗前三类数据:",label_old)
  100. print("清洗后三类数据:",label_new)

4.提取骨骼特征+关节特征(双流法2s-agcn时使用)

  1. import shutil
  2. import os
  3. import dlib
  4. import cv2
  5. from PIL import Image
  6. import numpy as np
  7. import time
  8. import imutils
  9. # 获取原来的15个坐标点,不要做相对坐标
  10. def gen_joint(txt1,txt2,txt3):
  11. face_key_point=[36,45,2,14,39,42,30,57]
  12. hand_key_point=[8,12,16,6,10,14,9]
  13. # 合并txt
  14. f1 = open(txt1, "r")
  15. f2 = open(txt2, "r")
  16. f3 = open(txt3, "w+")
  17. lines1=f1.readlines()
  18. lines2=f2.readlines()
  19. nums=len(lines1)
  20. print(nums)
  21. for i in range(nums):
  22. line1=lines1[i].strip().split(" ")
  23. line2=lines2[i].strip().split(" ")
  24. # print(len(line1),len(line2))
  25. if line1[:3]==line2[:3]:
  26. data_face=line1[3:]
  27. data_hand=line2[3:]
  28. label=line1[:3]
  29. center_point=data_face[30].split(',')
  30. txt=""
  31. for a in label:
  32. txt+=str(a)+" "
  33. # 脸部关键点
  34. for index in face_key_point:
  35. x=data_face[index].split(',')[0]
  36. y=data_face[index].split(',')[1]
  37. z=data_face[index].split(',')[2]
  38. txt+=str(x)+" "+str(y)+" "+str(z)+" "
  39. # 手部关键点
  40. for index in hand_key_point:
  41. x=int(data_hand[index].split(',')[0])+int(center_point[0])
  42. y=int(data_hand[index].split(',')[1])+int(center_point[1])
  43. z=data_hand[index].split(',')[2]
  44. txt+=str(x)+" "+str(y)+" "+str(z)+" "
  45. # print(x,y,z)
  46. txt+="\n"
  47. f3.write(txt)
  48. f1.close()
  49. f2.close()
  50. f3.close()
  51. # 获取骨骼数据
  52. def gen_bone(txt1,txt2,txt4):
  53. face_key_point=[36,45,2,14,39,42,30,57]
  54. hand_key_point=[8,12,16,6,10,14,9]
  55. point_bone=[(39, 30), (36, 39), (42, 30), (45, 42), (2, 30), (14, 30), (57, 30), (30, 30), # face
  56. (9, 30), (14, 9), (16, 14), (10, 9), (12, 10), (6, 9), (8, 6)] # hand
  57. # 合并txt
  58. f1 = open(txt1, "r")
  59. f2 = open(txt2, "r")
  60. f4 = open(txt4, "w+")
  61. lines1=f1.readlines()
  62. lines2=f2.readlines()
  63. nums=len(lines1)
  64. print(nums)
  65. for i in range(nums):
  66. line1=lines1[i].strip().split(" ")
  67. line2=lines2[i].strip().split(" ")
  68. if line1[:3]==line2[:3]:
  69. data_face=line1[3:]
  70. data_hand=line2[3:]
  71. label=line1[:3]
  72. center_point=data_face[30].split(',')
  73. txt=""
  74. for a in label:
  75. txt+=str(a)+" "
  76. for index in point_bone:
  77. index1=index[0] # 一根骨骼中的远中心点
  78. index2=index[1] # 一根骨骼中的近中心点
  79. # 判断关键点是脸部还是手部
  80. if index1 in face_key_point:
  81. x1=data_face[index1].split(",")[0]
  82. y1=data_face[index1].split(",")[1]
  83. z1=data_face[index1].split(",")[2]
  84. else:
  85. x1=int(data_hand[index1].split(",")[0])+int(center_point[0])
  86. y1=int(data_hand[index1].split(",")[1])+int(center_point[1])
  87. z1=float(data_hand[index1].split(",")[2])
  88. if index2 in face_key_point:
  89. x2=data_face[index2].split(",")[0]
  90. y2=data_face[index2].split(",")[1]
  91. z2=data_face[index2].split(",")[2]
  92. else:
  93. x2=int(data_hand[index2].split(",")[0])+int(center_point[0])
  94. y2=int(data_hand[index2].split(",")[1])+int(center_point[1])
  95. z2=float(data_hand[index2].split(",")[2])
  96. # 获取骨骼向量:远点-近点
  97. x=int(x1)-int(x2)
  98. y=int(y1)-int(y2)
  99. z=float(z1)-float(z2)
  100. txt+=str(x)+" "+str(y)+" "+str(z)+" "
  101. txt+="\n"
  102. f4.write(txt)
  103. f1.close()
  104. f2.close()
  105. f4.close()
  106. txt1="txt2/src/face.txt"
  107. txt2="txt2/src/hand.txt"
  108. txt3="txt2/src/data_joint.txt"
  109. txt4="txt2/src/data_bone.txt"
  110. gen_joint(txt1,txt2,txt3)
  111. gen_bone(txt1,txt2,txt4)
  112. f4 = open(txt4, "r")
  113. lines3=f4.readlines()
  114. print(len(lines3),len(lines3[0].strip().split(" ")))

5.数据清洗(双流法2s-agcn时使用)

  1. import shutil
  2. import os
  3. import dlib
  4. import cv2
  5. from PIL import Image
  6. import numpy as np
  7. import time
  8. import imutils
  9. # 清洗规则:
  10. # 1.手部特征点置信度>=0.1, 7个点要超过4个;
  11. # 2.人脸检测30帧通过15帧以上。
  12. def count_label(label_arr):
  13. label_0=0
  14. label_1=0
  15. label_3=0
  16. for label in label_arr:
  17. if label==0:
  18. label_0+=1
  19. elif label==1:
  20. label_1+=1
  21. elif label==3:
  22. label_3+=1
  23. return (label_0,label_1,label_3)
  24. def count_pass(point,label):
  25. n=0
  26. points=point[8:]
  27. for data in points:
  28. confidence=float(data[2])
  29. # print(confidence)
  30. if confidence>=0.1:
  31. n+=1
  32. # 类别3的数据不做判断,直接通过
  33. if label == 3:
  34. n=7
  35. # 判断人脸,若人脸检测变形,则不通过
  36. w=int(point[3][0])-int(point[2][0]) # 14-2
  37. if w<=50:
  38. n=0
  39. return n
  40. def clear(txt1_1, txt1_2, txt2):
  41. f1_1 = open(txt1_1, "r")
  42. f1_2 = open(txt1_2, "r")
  43. f2 = open(txt2, "w+")
  44. lines1=f1_1.readlines()
  45. lines2=f1_2.readlines()
  46. data_nums=len(lines1)
  47. total1=0
  48. total2=0
  49. total3=0
  50. label1=[]
  51. label2=[]
  52. data=[]
  53. data2=[]
  54. new_vid=lines1[0].strip().split(" ")[:3] # 视频标签
  55. for i in range(data_nums):
  56. line1=lines1[i].strip().split(" ")
  57. line2=lines2[i].strip().split(" ")
  58. label=int(lines1[i-1].strip().split(" ")[2]) # 类别标签
  59. now_vid=line1[:3] # 视频标签
  60. point=line1[3:] #坐标点数据
  61. point = np.reshape(np.asarray(point), (-1,3))
  62. pass_nums=count_pass(point,label) #判断有多少点通过了阈值
  63. # print(pass_nums)
  64. # 标签相同则写进去
  65. if new_vid==now_vid:
  66. if pass_nums>=4:
  67. data.append(lines2[i])
  68. data2.append(lines2[i])
  69. # 标签不同的时候,判断data的数据,超过15条则通过,写入新txt
  70. else:
  71. total1+=1
  72. label1.append(label)
  73. nums=len(data)
  74. if nums>=15:
  75. for feature in data:
  76. f2.write(feature)
  77. total2+=1
  78. label2.append(label)
  79. nums2=len(data2)
  80. if nums2>=15:
  81. total3+=1
  82. # 重置data,并判断新视频的第一条数据,是否写入data
  83. data=[]
  84. if pass_nums>=4:
  85. data.append(lines2[i])
  86. data2=[]
  87. data2.append(lines2[i])
  88. new_vid=now_vid
  89. f2.close()
  90. f3 = open(txt2, "r")
  91. new_nums=len(f3.readlines())
  92. label_old=count_label(label1)
  93. label_new=count_label(label2)
  94. return data_nums,new_nums,total1,total2,total3,label_old,label_new
  95. txt1_1="txt2/src/data_joint.txt" #对标
  96. txt1_2="txt2/src/data_joint.txt" #清洗
  97. txt2="txt2/clear/data_joint.txt" #生成
  98. data_nums, new_nums, total1, total2, total3, label_old, label_new=clear(txt1_1, txt1_2, txt2)
  99. print("数据总量:",data_nums,new_nums)
  100. print("视频数量:",total1,total2)
  101. print("人脸检测问题减少:",total1-total3)
  102. print("手部检测问题减少:",(total1-total2)-(total1-total3))
  103. print("清洗前三类数据:",label_old)
  104. print("清洗后三类数据:",label_new)

6.坐标归一化

  1. import shutil
  2. import os
  3. import dlib
  4. import cv2
  5. from PIL import Image
  6. import numpy as np
  7. import time
  8. import imutils
  9. def face_normal(txt1,txt2,txt3):
  10. # 合并txt
  11. f1 = open(txt1, "r")
  12. f2 = open(txt2, "r")
  13. f3 = open(txt3, "w+")
  14. lines1=f1.readlines()
  15. lines2=f2.readlines()
  16. nums=len(lines1)
  17. print(nums)
  18. base_w=350
  19. base_h=500
  20. for i in range(nums):
  21. # w:26-17 h:57-27 ps:点前面还有三个数,所有+3
  22. line1=lines1[i].strip().split(" ")
  23. w=int(line1[15+3].split(',')[0])-int(line1[2+3].split(',')[0]) # 人脸宽度
  24. h=int(line1[8+3].split(',')[1])-int(line1[19+3].split(',')[1]) # 人脸高度
  25. line2=lines2[i].strip().split(" ")
  26. if line1[:3]==line2[:3]:
  27. data=line2[:3]
  28. point=line2[3:]
  29. point = np.reshape(np.asarray(point), (-1,3))
  30. len_line2=len(point)
  31. # print(point)
  32. for a in range(len_line2):
  33. # # 归一化方法_v1 new=old/人脸
  34. # x=int(point[a][0])/w
  35. # y=int(point[a][1])/h
  36. # 归一化方法_v2 new=old*(标准/人脸)
  37. x=int(point[a][0])*(base_w/w)
  38. y=int(point[a][1])*(base_h/h)
  39. data.append(int(x))
  40. data.append(int(y))
  41. data.append(point[a][2])
  42. txt=""
  43. for b in range(len(data)):
  44. txt+=str(data[b])+" "
  45. txt+="\n"
  46. f3.write(txt)
  47. f3.close()
  48. txt1="txt/2s/src/9/face.txt"
  49. txt2="txt/2s/src/9/data_15.txt"
  50. txt3="txt/2s/src/9/data_15_v2.txt"
  51. face_normal(txt1,txt2,txt3)
  52. f4 = open(txt3, "r")
  53. lines3=f4.readlines()
  54. print(len(lines3))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/447127
推荐阅读
相关标签
  

闽ICP备14008679号