def his_image(im, size, joint, data): joints = { 'lankle': 13, 'rankle': 10, 'lknee': 12, 'rknee': 9, 'lthi': 11, 'rthi': 8 } # médian #img_m=cv2.medianBlur(im,5) #font=cv2.FONT_HERSHEY_SIMPLEX img_m = im[0:828, 0:512] ''' img_m_h=img_m[0:424, 0:512] img_m_b=img_m[424:828, 0:512] for i in range(424): for j in range(512): img_m[i,j]=img_m_h[i,j]*256/6+img_m_b[i,j] ''' xjoints = data[0:18] yjoints = data[18:] x = xjoints[joints[joint]] y = yjoints[joints[joint]] (x_d, y_d) = tools.RGBtoD((x, y)) ndg_h1 = img_m[y_d, x_d][0] # Original Depth_high ndg_b1 = img_m[y_d + 424, x_d][0] # Depth Original_low ng = [] #print(img_m[(y_d-int((size-1)/2)):(y_d+int((size-1)/2)),(x_d-int((size-1)/2)):(x_d+int((size-1)/2))] ) for i in range(y_d - int((size - 1) / 2), y_d + int((size - 1) / 2) + 1): for j in range(x_d - int((size - 1) / 2), x_d + int( (size - 1) / 2) + 1): z0 = (img_m[i, j] * 256 / 6 + img_m[i + 424, j])[0] / 10 if z0 > 120 and z0 < 750: ng.append(int(round(z0))) print(len(ng)) ngh = [0] * 750 for h in set(ng): ngh[h] = ng.count(h) zm = ngh.index(max(ngh)) * 10 print(zm) return (zm, ndg_h1 * 256 / 6 + ndg_b1)
def his(nb_video, frame, size, joint): files = os.listdir('testVedios/test' + nb_video + '/') for i in files: if (len(re.findall('.*C\.json', i)) != 0): filejson = i if (len(re.findall('.*D.mp4', i)) != 0): filedepth = i v = cv2.VideoCapture('testVedios/test' + nb_video + '/' + filedepth) with open('testVedios/test' + nb_video + '/' + filejson) as json_data: d = json.load(json_data) #for t in range(frames[0],frames[1]): #print(t) v.set(cv2.CAP_PROP_POS_FRAMES, frame) ret, im = v.read() joints = { 'lankle': 1, 'rankle': 0, 'lknee': 3, 'rknee': 2, 'lthi': 5, 'rthi': 4 } # médian img_m = cv2.medianBlur(im, 5) font = cv2.FONT_HERSHEY_SIMPLEX img_m = img_m[0:828, 0:512] ng = [] xjoints = d['frames'][frame][0]['pose2d'][:13] yjoints = d['frames'][frame][0]['pose2d'][13:] x = xjoints[joints[joint]] y = yjoints[joints[joint]] (x_d, y_d) = tools.RGBtoD((x, y), frame) ndg_h1 = img_m[y_d, x_d][0] # Original Depth_high ndg_b1 = img_m[y_d + 424, x_d][0] # Depth Original_low #print(img_m[(y_d-int((size-1)/2)):(y_d+int((size-1)/2)),(x_d-int((size-1)/2)):(x_d+int((size-1)/2))] ) for i in range(y_d - int((size - 1) / 2), y_d + int((size - 1) / 2) + 1): for j in range(x_d - int((size - 1) / 2), x_d + int( (size - 1) / 2) + 1): z0 = (img_m[i, j] * 256 / 6 + img_m[i + 424, j])[0] / 10 if z0 > 120 and z0 < 750: ng.append(int(round(z0))) print(len(ng)) print(ng) ngh = [0] * 750 for h in set(ng): ngh[h] = ng.count(h) zm = ngh.index(max(ngh)) * 10 print('max: ', zm) print('original: ', ndg_h1 * 256 / 6 + ndg_b1) # mask first 8 bits (high) mask = np.zeros(img_m.shape[:2], np.uint8) mask[(y_d - int((size - 1) / 2)):(y_d + int((size - 1) / 2)) + 1, (x_d - int((size - 1) / 2)):(x_d + int((size - 1) / 2)) + 1] = 255 masked_img = cv2.bitwise_and(img_m, img_m, mask=mask) # mask_bas last 8 bits (low) mask_bas = np.zeros(img_m.shape[:2], np.uint8) mask_bas[(y_d + 424 - int((size - 1) / 2)):(y_d + 424 + int((size - 1) / 2)) + 1, (x_d - int((size - 1) / 2)):(x_d + int((size - 1) / 2) + 1)] = 255 masked_bas_img = cv2.bitwise_and(img_m, img_m, mask=mask_bas) #opencv方法读取-cv2.calcHist(速度最快) #图像,通道[0]-灰度图,掩膜-无,灰度级,像素范围 hist_bas = cv2.calcHist([img_m], [0], mask_bas, [256], [0, 256]) #11178 hist_mask = cv2.calcHist([img_m], [0], mask, [256], [0, 256]) fig = plt.figure() ax1 = fig.add_subplot(221) ax1.imshow(img_m, 'gray') ax1.set_title('Depth map') ax1.scatter(x_d, y_d, s=1, color='red') ax1.plot([ x_d - size / 2, x_d - size / 2, x_d + size / 2, x_d + size / 2, x_d - size / 2 ], [ y_d - size / 2, y_d + size / 2, y_d + size / 2, y_d - size / 2, y_d - size / 2 ], color='orange') ax1.scatter(x_d, y_d + 424, s=3, color='red') ax1.plot([ x_d - size / 2, x_d - size / 2, x_d + size / 2, x_d + size / 2, x_d - size / 2 ], [ y_d + 424 - size / 2, y_d + 424 + size / 2, y_d + 424 + size / 2, y_d + 424 - size / 2, y_d + 424 - size / 2 ], color='orange') ax2 = fig.add_subplot(222) ax2.set_title('Histogramme') ax2.set_xlabel('Distance(cm)') #ax2.imshow(mask_bas,'gray') ax2.hist(np.array(ng), bins=size**2 + 1) #ax2.scatter(ndg_b1*256/6+ndg_b1, 15, color='orange') # low ax3 = fig.add_subplot(223) ax3.set_title('Histogramme bas') ax3.plot(hist_bas) #print (hist_bas[signal.argrelextrema(hist_bas, np.greater)])# signal.argrelextrema(hist_mask, np.greater) jizhidexiabiao #print (signal.argrelextrema(hist_bas, np.greater) ) ax3.plot( signal.argrelextrema(hist_bas, np.greater)[0], hist_bas[signal.argrelextrema(hist_bas, np.greater)], 'o') ax3.plot( signal.argrelextrema(-hist_bas, np.greater)[0], hist_bas[signal.argrelextrema(-hist_bas, np.greater)], '+') ndg_b = ndg_b1 number = hist_bas[ndg_b] for i in signal.argrelextrema(hist_bas, np.greater)[0]: #print(i, hist_bas[i]) if (hist_bas[i] >= number): number = hist_bas[i] ndg_b = i ax3.scatter(ndg_b1, hist_bas[ndg_b1], color='black', s=50) #print('max: ', ndg_b, hist_bas[ndg_b]) # hight ax4 = fig.add_subplot(224) ax4.plot(hist_mask) ax4.set_title('Histogramme haut') #print (hist_mask[signal.argrelextrema(hist_mask, np.greater)])# signal.argrelextrema(hist_mask, np.greater) jizhidexiabiao #print (signal.argrelextrema(hist_mask, np.greater) ) ax4.plot( signal.argrelextrema(hist_mask, np.greater)[0], hist_mask[signal.argrelextrema(hist_mask, np.greater)[0]], 'o') ax4.plot( signal.argrelextrema(-hist_mask, np.greater)[0], hist_mask[signal.argrelextrema(-hist_mask, np.greater)[0]], '+') ndg_h = ndg_h1 number = hist_mask[ndg_h] for i in signal.argrelextrema(hist_mask, np.greater)[0]: #print(i, hist_mask[i]) if (hist_mask[i] >= number and ((i >= 35) and i < 155)): number = hist_mask[i] ndg_h = i ax4.scatter(ndg_h1, hist_mask[ndg_h1], color='black', s=50) #print('max: ', ndg_h, hist_mask[ndg_h]) plt.show() return (zm, ndg_h1 * 256 / 6 + ndg_b1)
def show_angle3d(nb_video, frames): b = str(frames[0]) if frames[0] <= 200: direction = 'Front' else: direction = 'Back' files = os.listdir('testVideos/test' + nb_video + '/') for i in files: if (len(re.findall('.*C\.json', i)) != 0): filejson = i if (len(re.findall('.*D.mp4', i)) != 0): filedepth = i if (len(re.findall('.*C.mp4', i)) != 0): filecolor = i if (len( re.findall( '_GT-Vicon_PifPaf_3DKinect_' + direction + '_generalTF', i)) != 0): filegtfm = i if (len( re.findall( '_GT-Vicon_PifPaf_3DKinect_' + direction + '_respectiveTF', i)) != 0): filegtf = i if (len( re.findall( '_GT-Vicon_PifPaf_2DColor_' + direction + '_generalTF', i)) != 0): filegtfcm = i if (len( re.findall( '_GT-Vicon_PifPaf_2DColor_' + direction + '_respectiveTF', i)) != 0): filegtfc = i if (len(re.findall('_PifPaf_joints_3DKinect_front.csv', i)) != 0): filekinect = i print(filejson) print(filedepth) print(filecolor) print(filekinect) fig = plt.figure() fig2 = plt.figure() fig3 = plt.figure() datagtm = pd.DataFrame( pd.read_csv('testVideos/test' + nb_video + '/' + filegtfm)) datagt = pd.DataFrame( pd.read_csv('testVideos/test' + nb_video + '/' + filegtf)) datagtcm = pd.DataFrame( pd.read_csv('testVideos/test' + nb_video + '/' + filegtfcm)) datagtc = pd.DataFrame( pd.read_csv('testVideos/test' + nb_video + '/' + filegtfc)) datakin = pd.DataFrame( pd.read_csv('testVideos/test' + nb_video + '/' + filekinect)) def call_back(event): axtemp = event.inaxes x_min, x_max = axtemp.get_xlim() y_min, y_max = axtemp.get_ylim() z_min, z_max = axtemp.get_zlim() dx = (x_max - x_min) / 10 dy = (y_max - y_min) / 10 dz = (z_max - z_min) / 10 if event.button == 'up': axtemp.set(xlim=(x_min + dx, x_max - dx)) axtemp.set(ylim=(y_min + dy, y_max - dy)) axtemp.set(zlim=(z_min + dz, z_max - dz)) elif event.button == 'down': axtemp.set(xlim=(x_min - dx, x_max + dx)) axtemp.set(ylim=(y_min - dy, y_max + dy)) axtemp.set(zlim=(z_min - dz, z_max + dz)) fig2.canvas.draw_idle() # 绘图动作实时反映在图像上 fig2.canvas.mpl_connect('scroll_event', call_back) fig2.canvas.mpl_connect('button_press_event', call_back) # Depthmap ax = fig2.add_subplot(111, projection='3d') ax.set_title('PifPaf 2D to Kinect 3D_' + b) # Color Image ax2 = fig.add_subplot(111) # fig=plt.figure() ax2.set_title('PifPaf 2D in Color image_' + b) ax5 = fig3.add_subplot(111) ax5.set_title('PifPaf 2D in Depth image_' + b) # sk2d in depthmap #ax4=fig.add_subplot(223, projection='3d') #ax4.set_title('LCR-NET SK 2D in Depth map_'+b+'m front') v_c = cv2.VideoCapture('testVideos/test' + nb_video + '/' + filecolor) with open('testVideos/test' + nb_video + '/' + filejson) as json_data: d = json.load(json_data) v = cv2.VideoCapture('testVideos/test' + nb_video + '/' + filedepth) anglesl = [] anglesr = [] for t in range(frames[0], frames[1]): #print(t) v.set(cv2.CAP_PROP_POS_FRAMES, t) ret, im = v.read() v_c.set(cv2.CAP_PROP_POS_FRAMES, t) retc, im_c = v_c.read() # médian img_m = cv2.medianBlur(im, 5) font = cv2.FONT_HERSHEY_SIMPLEX x = [] y = [] z = [] # show 3d claud points for i in range(600, 1000, 3): for j in range(200, 960, 6): a = tools.RGBto3D((i, j), img_m, t) x.append(a[0]) y.append(a[1]) z.append(a[2]) ax.scatter(x, z, y, s=1) # Show GT if int(datagt['frame'][0]) <= t and int( datagt['frame'][len(datagt['frame']) - 1] >= t): xg = [] yg = [] zg = [] xgm = [] ygm = [] zgm = [] for i in datagt.keys(): if i.find('x') != -1: print((datagt[i][datagt['frame'] == t])) xg.append(float(datagt[i][datagt['frame'] == t])) xgm.append(float(datagtm[i][datagtm['frame'] == t])) #xg.append(datagt[i][]) #xg.append(datagt[i][datagt.frame==t]) if i.find('y') != -1: yg.append(float(datagt[i][datagt['frame'] == t])) ygm.append(float(datagtm[i][datagtm['frame'] == t])) if i.find('z') != -1: zg.append(float(datagt[i][datagt['frame'] == t])) zgm.append(float(datagtm[i][datagtm['frame'] == t])) print(xg, yg, zg) # GT joints 3D ax.scatter(xg, zg, yg, color='black') ax.scatter(xgm, zgm, ygm, color='y') #GT SK 3D ax.plot([xg[k] for k in [0, 2, 4, 5, 3, 1]], [zg[k] for k in [0, 2, 4, 5, 3, 1]], [yg[k] for k in [0, 2, 4, 5, 3, 1]], color='black') ax.plot([xg[k] for k in [6, 8, 10, 11, 9, 7]], [zg[k] for k in [6, 8, 10, 11, 9, 7]], [yg[k] for k in [6, 8, 10, 11, 9, 7]], color='black') ax.plot([(xg[10] + xg[11]) / 2, (xg[4] + xg[5]) / 2], [(zg[10] + zg[11]) / 2, (zg[4] + zg[5]) / 2], [(yg[10] + yg[11]) / 2, (yg[4] + yg[5]) / 2], color='black') ax.plot([xgm[k] for k in [0, 2, 4, 5, 3, 1]], [zgm[k] for k in [0, 2, 4, 5, 3, 1]], [ygm[k] for k in [0, 2, 4, 5, 3, 1]], color='y') ax.plot([xgm[k] for k in [6, 8, 10, 11, 9, 7]], [zgm[k] for k in [6, 8, 10, 11, 9, 7]], [ygm[k] for k in [6, 8, 10, 11, 9, 7]], color='y') ax.plot([(xgm[10] + xgm[11]) / 2, (xgm[4] + xgm[5]) / 2], [(zgm[10] + zgm[11]) / 2, (zgm[4] + zgm[5]) / 2], [(ygm[10] + ygm[11]) / 2, (ygm[4] + ygm[5]) / 2], color='y') # Show GT 2D xgc = [] ygc = [] xgcm = [] ygcm = [] for i in datagtc.keys(): if i.find('x') != -1: print((datagtc[i][datagtc['frame'] == t])) xgc.append(float(datagtc[i][datagtc['frame'] == t])) xgcm.append(float(datagtcm[i][datagtcm['frame'] == t])) #xg.append(datagt[i][]) #xg.append(datagt[i][datagt.frame==t]) if i.find('y') != -1: ygc.append(float(datagtc[i][datagtc['frame'] == t])) ygcm.append(float(datagtcm[i][datagtcm['frame'] == t])) print(xgc, ygc) # GT joints 2D color ax2.scatter(xgc, ygc, color='black') ax2.scatter(xgcm, ygcm, color='y') #GT SK 2D color ax2.plot([xgc[k] for k in [0, 2, 4, 5, 3, 1]], [ygc[k] for k in [0, 2, 4, 5, 3, 1]], color='black') ax2.plot([xgc[k] for k in [6, 8, 10, 11, 9, 7]], [ygc[k] for k in [6, 8, 10, 11, 9, 7]], color='black') ax2.plot([(xgc[10] + xgc[11]) / 2, (xgc[4] + xgc[5]) / 2], [(ygc[10] + ygc[11]) / 2, (ygc[4] + ygc[5]) / 2], color='black') ax2.plot([xgcm[k] for k in [0, 2, 4, 5, 3, 1]], [ygcm[k] for k in [0, 2, 4, 5, 3, 1]], color='y') ax2.plot([xgcm[k] for k in [6, 8, 10, 11, 9, 7]], [ygcm[k] for k in [6, 8, 10, 11, 9, 7]], color='y') ax2.plot([(xgcm[10] + xgcm[11]) / 2, (xgcm[4] + xgcm[5]) / 2], [(ygcm[10] + ygcm[11]) / 2, (ygcm[4] + ygcm[5]) / 2], color='y') # Show corrected Kinect 3D if int(datakin['frames'][0]) <= t and int( datakin['frames'][len(datakin['frames']) - 1] >= t): xk = [] yk = [] zk = [] k = 0 for i in datakin.keys(): if i.find('x') != -1: print((datakin[i][datakin['frames'] == t])) xk.append(float(datakin[i][datakin['frames'] == t])) if i.find('y_') != -1: yk.append(float(datakin[i][datakin['frames'] == t])) if i.find('z') != -1: zk.append(float(datakin[i][datakin['frames'] == t])) print('Corrected kinect: ', len(xk), len(yk), len(zk)) #corrected joints 3D ax.scatter(xk, zk, yk, color='#800080') #corrected SK 3D ax.plot([xk[k] for k in [10, 8, 6, 5, 7, 9]], [zk[k] for k in [10, 8, 6, 5, 7, 9]], [yk[k] for k in [10, 8, 6, 5, 7, 9]], color='#800080') ax.plot([xk[k] for k in [16, 14, 12, 11, 13, 15]], [zk[k] for k in [16, 14, 12, 11, 13, 15]], [yk[k] for k in [16, 14, 12, 11, 13, 15]], color='#800080') ax.plot([xk[0], (xk[5] + xk[6]) / 2, (xk[11] + xk[12]) / 2], [zk[0], (zk[5] + zk[6]) / 2, (zk[11] + zk[12]) / 2], [yk[0], (yk[5] + yk[6]) / 2, (yk[11] + yk[12]) / 2], color='#800080') if len(d['frames'][t]) != 0: #SK 2D xjoints = d['frames'][t][0]['pose2d'][:17] yjoints = d['frames'][t][0]['pose2d'][17:] #SK 2D in Depth xjoints_3d = [] yjoints_3d = [] zjoints_3d = [] for i in range(17): joints = tools.RGBto3D((xjoints[i], yjoints[i]), img_m, t, True, 7) xjoints_3d.append(joints[0]) yjoints_3d.append(joints[1]) zjoints_3d.append(joints[2]) print(xjoints_3d, yjoints_3d, zjoints_3d) # show joints ax.scatter(xjoints_3d, zjoints_3d, yjoints_3d, color='r') #ax4.scatter(xjoints_3d, zjoints_3d, yjoints_3d , color='r') ''' anglesl.append(180-tools.angle((xjoints_3d[1],yjoints_3d[1],zjoints_3d[1]),(xjoints_3d[3],yjoints_3d[3],zjoints_3d[3]),(xjoints_3d[5],yjoints_3d[5],zjoints_3d[5]))) anglesr.append(180-tools.angle((xjoints_3d[0],yjoints_3d[0],zjoints_3d[0]),(xjoints_3d[2],yjoints_3d[2],zjoints_3d[2]),(xjoints_3d[4],yjoints_3d[4],zjoints_3d[4]))) gt=(54.115166+1100, -1168.320923+27124, 484.712952+1100) #ax.scatter(gt[0],gt[1],gt[2], color='y') print(xjoints_3d[3], zjoints_3d[3], yjoints_3d[3]) print(anglesl) print(anglesr) ''' # ax show 3d skelecton ax.plot([xjoints_3d[k] for k in [10, 8, 6, 5, 7, 9]], [zjoints_3d[k] for k in [10, 8, 6, 5, 7, 9]], [yjoints_3d[k] for k in [10, 8, 6, 5, 7, 9]], color='#C71585') ax.plot([xjoints_3d[k] for k in [16, 14, 12, 11, 13, 15]], [zjoints_3d[k] for k in [16, 14, 12, 11, 13, 15]], [yjoints_3d[k] for k in [16, 14, 12, 11, 13, 15]], color='#C71585') ax.plot([ xjoints_3d[0], (xjoints_3d[5] + xjoints_3d[6]) / 2, (xjoints_3d[11] + xjoints_3d[12]) / 2 ], [ zjoints_3d[0], (zjoints_3d[5] + zjoints_3d[6]) / 2, (zjoints_3d[11] + zjoints_3d[12]) / 2 ], [ yjoints_3d[0], (yjoints_3d[5] + yjoints_3d[6]) / 2, (yjoints_3d[11] + yjoints_3d[12]) / 2 ], color='#C71585') # ax4 show 3d skelecton #ax4.plot([xjoints_3d[k] for k in [0,2,4,5,3,1]], [zjoints_3d[k] for k in [0,2,4,5,3,1]], [yjoints_3d[k] for k in [0,2,4,5,3,1]], color='orange' ) #ax4.plot([xjoints_3d[k] for k in [6,8,10,11,9,7]], [zjoints_3d[k] for k in [6,8,10,11,9,7]], [yjoints_3d[k] for k in [6,8,10,11,9,7]], color='orange' ) #ax4.plot([xjoints_3d[12] , (xjoints_3d[10]+xjoints_3d[11])/2 ,(xjoints_3d[4]+xjoints_3d[5])/2], [zjoints_3d[12], (zjoints_3d[10]+zjoints_3d[11])/2, (zjoints_3d[4]+zjoints_3d[5])/2], [yjoints_3d[12], (yjoints_3d[10]+yjoints_3d[11])/2, (yjoints_3d[4]+yjoints_3d[5])/2], color='orange' ) # show depth image and the joints ax5.imshow(im) xjoints_d = [ tools.RGBtoD((xjoints[i], yjoints[i]), t)[0] for i in range(17) ] yjoints_d = [ tools.RGBtoD((xjoints[i], yjoints[i]), t)[1] for i in range(17) ] ax5.plot([xjoints_d[k] for k in [10, 8, 6, 5, 7, 9]], [yjoints_d[k] for k in [10, 8, 6, 5, 7, 9]], color='#C71585') ax5.plot([xjoints_d[k] for k in [16, 14, 12, 11, 13, 15]], [yjoints_d[k] for k in [16, 14, 12, 11, 13, 15]], color='#C71585') ax5.plot([ xjoints_d[0], (xjoints_d[5] + xjoints_d[6]) / 2, (xjoints_d[11] + xjoints_d[12]) / 2 ], [ yjoints_d[0], (yjoints_d[5] + yjoints_d[6]) / 2, (yjoints_d[11] + yjoints_d[12]) / 2 ], color='#C71585') ax5.scatter(xjoints_d, yjoints_d, color='r', s=5) # show color image and the joints with 3d skelecton ax2.imshow(im_c) ax2.plot([xjoints[k] for k in [10, 8, 6, 5, 7, 9]], [yjoints[k] for k in [10, 8, 6, 5, 7, 9]], color='#C71585') ax2.plot([xjoints[k] for k in [16, 14, 12, 11, 13, 15]], [yjoints[k] for k in [16, 14, 12, 11, 13, 15]], color='#C71585') ax2.plot([ xjoints[0], (xjoints[5] + xjoints[6]) / 2, (xjoints[11] + xjoints[12]) / 2 ], [ yjoints[0], (yjoints[5] + yjoints[6]) / 2, (yjoints[11] + yjoints[12]) / 2 ], color='#C71585') ax2.scatter(xjoints, yjoints, color='r', s=5) # show the joints' positions ''' for a in [6,12,14,16,8]: ax2.text(xjoints[a], yjoints[a], '(%.1f, %.1f, %.1f)'%(xjoints_3d[a],yjoints_3d[a], zjoints_3d[a])+'point'+str(a), color='blue') for a in [5,7,11,13,15]: ax2.text(xjoints[a], yjoints[a], '(%.1f, %.1f, %.1f)'%(xjoints_3d[a],yjoints_3d[a], zjoints_3d[a])+'point'+str(a), color='orange') ax2.text(xjoints[0], yjoints[0], '(%.1f, %.1f, %.1f)'%(xjoints_3d[0],yjoints_3d[0], zjoints_3d[0])+'point'+str(0), color='g') ax2.text(xjoints[10], yjoints[10], '(%.1f, %.1f, %.1f)'%(xjoints_3d[10],yjoints_3d[10], zjoints_3d[10])+'point'+str(10), color='g') ax2.text(xjoints[9], yjoints[9], '(%.1f, %.1f, %.1f)'%(xjoints_3d[9],yjoints_3d[9], zjoints_3d[9])+'point'+str(9), color='g') ''' for a in [6, 12, 14, 16, 8]: ax2.text(xjoints[a] - 35, yjoints[a], str(a).zfill(2), color='blue') for a in [5, 7, 11, 13, 15]: ax2.text(xjoints[a], yjoints[a], str(a).zfill(2), color='orange') ax2.text(xjoints[0] + 10, yjoints[0] - 10, str(0).zfill(2), color='g') ax2.text(xjoints[10] - 35, yjoints[10], str(10).zfill(2), color='blue') ax2.text(xjoints[9], yjoints[9], str(9).zfill(2), color='orange') ''' print(xjoints) print(yjoints) print(zjoints) ax2.scatter(xjoints, zjoints, yjoints, color='r') ''' v.release() frames = range(frames[0], frames[1]) #ax.plot(frames, anglesl) #ax2.plot(frames, anglesr) plt.show()
def his(nb_video, frame, size, joint): files = os.listdir('testVideos/test' + nb_video + '/') for i in files: if (len(re.findall('.*C\.json', i)) != 0): filejson = i if (len(re.findall('.*D.mp4', i)) != 0): filedepth = i v = cv2.VideoCapture('testVideos/test' + nb_video + '/' + filedepth) with open('testVideos/test' + nb_video + '/' + filejson) as json_data: d = json.load(json_data) #for t in range(frames[0],frames[1]): #print(t) v.set(cv2.CAP_PROP_POS_FRAMES, frame) ret, im = v.read() joints = { 'lankle': 13, 'rankle': 10, 'lknee': 12, 'rknee': 9, 'lthi': 11, 'rthi': 8 } # médian img_m = cv2.medianBlur(im, 5) font = cv2.FONT_HERSHEY_SIMPLEX img_m = img_m[0:828, 0:512] ng = [] xjoints = d['frames'][frame][0]['pose2d'][:18] yjoints = d['frames'][frame][0]['pose2d'][18:] x = xjoints[joints[joint]] y = yjoints[joints[joint]] (x_d, y_d) = tools.RGBtoD((x, y)) ndg_h1 = img_m[y_d, x_d][0] # Original Depth_high ndg_b1 = img_m[y_d + 424, x_d][0] # Depth Original_low #print(img_m[(y_d-int((size-1)/2)):(y_d+int((size-1)/2)),(x_d-int((size-1)/2)):(x_d+int((size-1)/2))] ) for i in range(y_d - int((size - 1) / 2), y_d + int((size - 1) / 2) + 1): for j in range(x_d - int((size - 1) / 2), x_d + int( (size - 1) / 2) + 1): z0 = (img_m[i, j] * 256 / 6 + img_m[i + 424, j])[0] / 10 if z0 > 200 and z0 < 700: ng.append(int(round(z0)) - 200) print(len(ng)) img_hist = np.zeros((1, 500), np.uint8) for h in set(ng): img_hist[0, h] = ng.count(h) maxi = np.max(img_hist) mini = np.min(img_hist) for im in range(500): img_hist[0, im] = round((img_hist[0, im] - mini) / (maxi - mini) * 255) #print(img_hist) # mask first 8 bits (high) mask = np.zeros(img_m.shape[:2], np.uint8) mask[(y_d - int((size - 1) / 2)):(y_d + int((size - 1) / 2)) + 1, (x_d - int((size - 1) / 2)):(x_d + int((size - 1) / 2)) + 1] = 255 masked_img = cv2.bitwise_and(img_m, img_m, mask=mask) # mask_bas last 8 bits (low) mask_bas = np.zeros(img_m.shape[:2], np.uint8) mask_bas[(y_d + 424 - int((size - 1) / 2)):(y_d + 424 + int((size - 1) / 2)) + 1, (x_d - int((size - 1) / 2)):(x_d + int((size - 1) / 2) + 1)] = 255 masked_bas_img = cv2.bitwise_and(img_m, img_m, mask=mask_bas) #opencv方法读取-cv2.calcHist(速度最快) #图像,通道[0]-灰度图,掩膜-无,灰度级,像素范围 hist_bas = cv2.calcHist([img_m], [0], mask_bas, [256], [0, 256]) #11178 hist_mask = cv2.calcHist([img_m], [0], mask, [256], [0, 256]) ''' ndg_b=ndg_b1 number=hist_bas[ndg_b] for i in signal.argrelextrema(hist_bas, np.greater)[0]: #print(i, hist_bas[i]) if(hist_bas[i]>=number): number=hist_bas[i] ndg_b=i ndg_h=ndg_h1 number=hist_mask[ndg_h] for i in signal.argrelextrema(hist_mask, np.greater)[0]: #print(i, hist_mask[i]) if(hist_mask[i]>=number and ((i>=35) and i<155)): number=hist_mask[i] ndg_h=i ''' return (ndg_h1, ndg_b1, ndg_h1 * 256 / 6 + ndg_b1, img_hist)
def show_angle3d(nb_vedio, frames): files = os.listdir('testVedios/test' + nb_vedio + '/') for i in files: if (len(re.findall('.*C\.json', i)) != 0): filejson = i if (len(re.findall('.*D.mp4', i)) != 0): filedepth = i number = filedepth[:-5] #if (len(re.findall('_distance_data.csv', i))!=0): # filedis=i print(number) #print(filedis) print(filejson) #disdata=pd.DataFrame(pd.read_csv('testVedios/test'+nb_vedio+'/'+filedis)) fig = plt.figure() ax = fig.add_subplot(211) ax2 = fig.add_subplot(212) ax.set_title('3d kinect LCR-NET_Improved') ax.set_xlabel('Frame') ax.set_ylabel('Angle_left') ax2.set_xlabel('Frame') ax2.set_ylabel('Angle_right') with open('testVedios/test' + nb_vedio + '/' + filejson) as json_data: d = json.load(json_data) v = cv2.VideoCapture('testVedios/test' + nb_vedio + '/' + filedepth) anglesl = [] anglesr = [] points3dx = [[] for p in range(13)] points3dy = [[] for p in range(13)] points3dz = [[] for p in range(13)] points2dx = [[] for p in range(13)] points2dy = [[] for p in range(13)] points2ddx = [[] for p in range(13)] points2ddy = [[] for p in range(13)] akdis = [] kndis = [] step = [] bad_frame = [0] * (frames[1] - frames[0]) l_kn_ak = [] r_kn_ak = [] for t in tqdm(range(frames[0], frames[1])): #print(t) v.set(cv2.CAP_PROP_POS_FRAMES, t) ret, im = v.read() img_m = cv2.medianBlur(im, 7) font = cv2.FONT_HERSHEY_SIMPLEX ''' for i in range(800,980,2): for j in range(300,860,5): a=RGBto3D((i,j),im) x.append(a[0]) y.append(a[1]) z.append(a[2]) print(i) #ax.scatter(x,z,y) ''' if len(d['frames'][t]) != 0: xjoints2d = d['frames'][t][0]['pose2d'][:13] yjoints2d = d['frames'][t][0]['pose2d'][13:] xjoints3d = [] yjoints3d = [] zjoints3d = [] xjoints2dd = [] yjoints2dd = [] ###!!!!!!!!!11111!!!! leg = ['z_rak', 'z_lak', 'z_rkn', 'z_lkn', 'z_ras', 'z_las'] for i in range(13): if i <= -5: z3d = float(disdata[leg[i]][disdata['frame'] == t]) print(z3d) joints = tools.RGBto3D((xjoints2d[i], yjoints2d[i], z3d), im, t) xjoints3d.append(joints[0]) yjoints3d.append(joints[1]) zjoints3d.append(joints[2]) else: joints = tools.RGBto3D((xjoints2d[i], yjoints2d[i]), im, t, True, 7) xjoints3d.append(joints[0]) yjoints3d.append(joints[1]) zjoints3d.append(joints[2]) joints2dd = tools.RGBtoD((xjoints2d[i], yjoints2d[i]), t) xjoints2dd.append(joints2dd[0]) yjoints2dd.append(joints2dd[1]) for p in range(13): points3dx[p].append(xjoints3d[p]) points3dy[p].append(yjoints3d[p]) points3dz[p].append(zjoints3d[p]) points2dx[p].append(xjoints2d[p]) points2dy[p].append(yjoints2d[p]) points2ddx[p].append(xjoints2dd[p]) points2ddy[p].append(yjoints2dd[p]) anglesl.append(180 - tools.angle((xjoints3d[1], yjoints3d[1], zjoints3d[1]), (xjoints3d[3], yjoints3d[3], zjoints3d[3]), (xjoints3d[5], yjoints3d[5], zjoints3d[5]), False)) anglesr.append(180 - tools.angle((xjoints3d[0], yjoints3d[0], zjoints3d[0]), (xjoints3d[2], yjoints3d[2], zjoints3d[2]), (xjoints3d[4], yjoints3d[4], zjoints3d[4]), False)) # static analysis # save the important information:dis between ankles, as height, leg length l_kn_ak.append( tools.get_distance((xjoints3d[1], yjoints3d[1], zjoints3d[1]), (xjoints3d[3], yjoints3d[3], zjoints3d[3]))) r_kn_ak.append( tools.get_distance((xjoints3d[0], yjoints3d[0], zjoints3d[0]), (xjoints3d[2], yjoints3d[2], zjoints3d[2]))) dak = (zjoints3d[0] - zjoints3d[1]) dkn = (zjoints3d[2] - zjoints3d[3]) akdis.append(dak) kndis.append(dkn) # dynamic analysis # gen qian mian de zhen xiang bi jiao # Model : step automatic by Vicon !!!!front back!!!!! condition 1 2 3 # dis>0 : left joint is in front, dis<0 : right joint is in front if dkn <= 0 and dak > -100: step.append(0) elif dak <= -100 and dkn < 0: step.append(1) elif dkn >= 0 and dak < 100: step.append(2) elif dak >= 100 and dkn > 0: step.append(3) else: step.append(-1) # find the wrong frame # +++++++++++ # correct the wrong ponts # +++++++++++ # angle 2d # anglesl.append(180-tools.angle((yjoints[1],zjoints[1]),(yjoints[3],zjoints[3]),(yjoints[5],zjoints[5]))) # anglesr.append(180-tools.angle((yjoints[0],zjoints[0]),(yjoints[2],zjoints[2]),(yjoints[4],zjoints[4]))) ''' print(xjoints) print(yjoints) print(zjoints) ax2.scatter(xjoints, zjoints, yjoints, color='r') ''' v.release() #b, a = signal.butter(8, 0.3, 'lowpass') #anglesl = signal.filtfilt(b, a, anglesl) #anglesr = signal.filtfilt(b, a, anglesr) frames = range(frames[0], frames[1]) ax.plot(frames, anglesl) ax2.plot(frames, anglesr, color='r') #, marker='.') #for i in range(50): y1.append(i) # 每迭代一次,将i放入y1中画出来 ax.cla() # 清除键 ax.bar(y1, label='test', height=y1, width=0.3) ax.legend() plt.pause(0.1) data2d = {} data = {} data2dd = {} data2dd['frames'] = frames data2d['frames'] = frames data['frames'] = frames data['kangle_l'] = anglesl data['kangle_r'] = anglesr data['akdis'] = akdis data['kndis'] = kndis data['l_kn_ak'] = l_kn_ak data['r_kn_ak'] = r_kn_ak data['bad_frame'] = bad_frame print(len(step)) data['step'] = step j = [ 'rak', 'lak', 'rkn', 'lkn', 'ras', 'las', 'rwr', 'lwr', 'rel', 'lel', 'rsh', 'lsh', 'head' ] for p in range(13): data['x_' + j[p]] = points3dx[p] data['y_' + j[p]] = points3dy[p] data['z_' + j[p]] = points3dz[p] data2d['x_' + j[p]] = points2dx[p] data2d['y_' + j[p]] = points2dy[p] data2dd['x_' + j[p]] = points2ddx[p] data2dd['y_' + j[p]] = points2ddy[p] data = pd.DataFrame(data) data2d = pd.DataFrame(data2d) data2dd = pd.DataFrame(data2dd) if frames[0] > 250: data.to_csv('testVedios' + '/test' + nb_vedio + '/' + number + '_LCR-NET_joints_3DKinect_back.csv', encoding='gbk') data2d.to_csv('testVedios' + '/test' + nb_vedio + '/' + number + '_LCR-NET_joints_2DColor_back.csv', encoding='gbk') data2dd.to_csv('testVedios' + '/test' + nb_vedio + '/' + number + '_LCR-NET_joints_2DDepth_back.csv', encoding='gbk') #data1=pd.DataFrame({'frame':frames, 'angle_left':anglesl, 'angle_right':anglesr}) #data1.to_csv('testVedios'+'/test'+nb_vedio+'/'+number+'_LCR-NET_angles_3DKinect_back.csv',encoding='gbk') else: data2dd.to_csv('testVedios' + '/test' + nb_vedio + '/' + number + '_LCR-NET_joints_2DDepth_front.csv', encoding='gbk') data2d.to_csv('testVedios' + '/test' + nb_vedio + '/' + number + '_LCR-NET_joints_2DColor_front.csv', encoding='gbk') data.to_csv('testVedios' + '/test' + nb_vedio + '/' + number + '_LCR-NET_joints_3DKinect_front.csv', encoding='gbk')
def show_angle3d(nb_video, frames): files=os.listdir('testVideos/test'+nb_video+'/') for i in files: if (len(re.findall('.*C\.json', i))!=0): filejson=i if (len(re.findall('.*D.mp4', i))!=0): filedepth=i number=filedepth[:-5] if (len(re.findall('_PifPaf_joints_3DKinect_front.csv', i))!=0): filedis=i print(number) print(filejson) print(filedis) print('testVideos'+'/test'+nb_video+'/'+number+'_PifPaf_joints_3DKinect_back.csv') disdata=pd.DataFrame(pd.read_csv('testVideos/test'+nb_video+'/'+filedis)) fig=plt.figure() ax=fig.add_subplot(211) ax2=fig.add_subplot(212) ax.set_title('3d kinect PifPaf_Original') ax.set_xlabel('Frame') ax.set_ylabel('Angle_left') ax2.set_xlabel('Frame') ax2.set_ylabel('Angle_right') with open('testVideos/test'+nb_video+'/'+filejson) as json_data: d = json.load(json_data) v=cv2.VideoCapture('testVideos/test'+nb_video+'/'+filedepth) anglesl=[] anglesr=[] points3dx=[[] for p in range(17)] points3dy=[[] for p in range(17)] points3dz=[[] for p in range(17)] points2dx=[[] for p in range(17)] points2dy=[[] for p in range(17)] points2ddx=[[] for p in range(17)] points2ddy=[[] for p in range(17)] akdis=[] kndis=[] step=[] bad_frame=[0]*(frames[1]-frames[0]) l_kn_ak=[] r_kn_ak=[] for t in tqdm(range(frames[0],frames[1])): #print(t) v.set(cv2.CAP_PROP_POS_FRAMES, t) ret, im=v.read() img_m=cv2.medianBlur(im,5) font=cv2.FONT_HERSHEY_SIMPLEX ''' for i in range(800,980,2): for j in range(300,860,5): a=RGBto3D((i,j),im) x.append(a[0]) y.append(a[1]) z.append(a[2]) print(i) #ax.scatter(x,z,y) ''' if len(d['frames'][t])!=0: xjoints2d=d['frames'][t][0]['pose2d'][:17] yjoints2d=d['frames'][t][0]['pose2d'][17:] zjoints3d=[] xjoints3d=[] yjoints3d=[] xjoints2dd=[] yjoints2dd=[] leg=['z_las','z_ras','z_lkn','z_rkn','z_lak','z_rak'] for i in range(17): if i in [11,13,15,12,14,16]: z3d=float(disdata[leg[i-11]][disdata['frames']==t]) print(z3d) joints=tools.RGBto3D((xjoints2d[i], yjoints2d[i], z3d), im, t) xjoints3d.append(joints[0]) yjoints3d.append(joints[1]) zjoints3d.append(joints[2]) else: joints=tools.RGBto3D((xjoints2d[i], yjoints2d[i]), im, t, True, 7) xjoints3d.append(joints[0]) yjoints3d.append(joints[1]) zjoints3d.append(joints[2]) joints2dd=tools.RGBtoD((xjoints2d[i], yjoints2d[i]), t) xjoints2dd.append(joints2dd[0]) yjoints2dd.append(joints2dd[1]) anglesl.append(180-tools.angle((xjoints3d[11],yjoints3d[11],zjoints3d[11]),(xjoints3d[13],yjoints3d[13],zjoints3d[13]),(xjoints3d[15],yjoints3d[15],zjoints3d[15]),False)) anglesr.append(180-tools.angle((xjoints3d[12],yjoints3d[12],zjoints3d[12]),(xjoints3d[14],yjoints3d[14],zjoints3d[14]),(xjoints3d[16],yjoints3d[16],zjoints3d[16]),False)) # static analysis # save the important information:dis between ankles, as height, leg length l_kn_ak.append(tools.get_distance((xjoints3d[12],yjoints3d[12],zjoints3d[12]),(xjoints3d[13],yjoints3d[13],zjoints3d[13]))) r_kn_ak.append(tools.get_distance((xjoints3d[9],yjoints3d[9],zjoints3d[9]),(xjoints3d[10],yjoints3d[10],zjoints3d[10]))) dak=(zjoints3d[10]-zjoints3d[13]) dkn=(zjoints3d[9]-zjoints3d[12]) akdis.append(dak) kndis.append(dkn) for p in range(17): points3dx[p].append(xjoints3d[p]) points3dy[p].append(yjoints3d[p]) points3dz[p].append(zjoints3d[p]) points2dx[p].append(xjoints2d[p]) points2dy[p].append(yjoints2d[p]) points2ddx[p].append(xjoints2dd[p]) points2ddy[p].append(yjoints2dd[p]) v.release() #b, a = signal.butter(8, 0.3, 'lowpass') #anglesl = signal.filtfilt(b, a, anglesl) #anglesr = signal.filtfilt(b, a, anglesr) frames=range(frames[0],frames[1]) ax.plot(frames, anglesl) ax2.plot(frames, anglesr, color='r') data2d={} data={} data2dd={} data2dd['frames']=frames data2d['frames']=frames data['frames']=frames data['kangle_l']=anglesl data['kangle_r']=anglesr data['akdis']=akdis data['kndis']=kndis data['l_kn_ak']=l_kn_ak data['r_kn_ak']=r_kn_ak data['bad_frame']=bad_frame j=['head','leye','reye','lear','rear','lsh','rsh','lel','rel','lwr','rwr','las','ras','lkn','rkn','lak','rak'] for p in range(17): data['x_'+j[p]]=points3dx[p] data['y_'+j[p]]=points3dy[p] data['z_'+j[p]]=points3dz[p] data2d['x_'+j[p]]=points2dx[p] data2d['y_'+j[p]]=points2dy[p] data2dd['x_'+j[p]]=points2ddx[p] data2dd['y_'+j[p]]=points2ddy[p] data=pd.DataFrame(data) data2d=pd.DataFrame(data2d) data2dd=pd.DataFrame(data2dd) if frames[0]>250: data.to_csv('testVideos'+'/test'+nb_video+'/'+number+'_PifPaf_joints_3DKinect_back.csv',encoding='gbk') data2d.to_csv('testVideos'+'/test'+nb_video+'/'+number+'_PifPaf_joints_2DColor_back.csv',encoding='gbk') data2dd.to_csv('testVideos'+'/test'+nb_video+'/'+number+'_PifPaf_joints_2DDepth_back.csv',encoding='gbk') #data1=pd.DataFrame({'frame':frames, 'angle_left':anglesl, 'angle_right':anglesr}) #data1.to_csv('testVedios'+'/test'+nb_vedio+'/'+number+'_LCR-NET_angles_3DKinect_back.csv',encoding='gbk') else: data2dd.to_csv('testVideos'+'/test'+nb_video+'/'+number+'_PifPaf_joints_2DDepth_front.csv',encoding='gbk') data2d.to_csv('testVideos'+'/test'+nb_video+'/'+number+'_PifPaf_joints_2DColor_front.csv',encoding='gbk') data.to_csv('testVideos'+'/test'+nb_video+'/'+number+'_PifPaf_joints_3DKinect_front.csv',encoding='gbk')