def process_frame(txt_file, frame, body=True, hands=False): canvas = copy.deepcopy(frame) if body: candidate, subset = body_estimation(frame) if (is_data_valid(subset)): body_points = get_18_body_points(candidate, subset) list_to_file(txt_file, body_points) else: canvas = copy.deepcopy(frame) canvas = util.draw_bodypose(canvas, candidate, subset) # plt.imshow(canvas[:, :, [2, 1, 0]]) # plt.axis('off') # plt.show() print(subset) if hands: hands_list = util.handDetect(candidate, subset, frame) all_hand_peaks = [] for x, y, w, is_left in hands_list: peaks = hand_estimation(frame[y:y + w, x:x + w, :]) peaks[:, 0] = np.where(peaks[:, 0] == 0, peaks[:, 0], peaks[:, 0] + x) peaks[:, 1] = np.where(peaks[:, 1] == 0, peaks[:, 1], peaks[:, 1] + y) all_hand_peaks.append(peaks) canvas = util.draw_handpose(canvas, all_hand_peaks) return canvas
def main(oriImg): shape0 = oriImg.shape candidate, subset = body_estimation(oriImg) canvas = copy.deepcopy(oriImg) shape1 = canvas.shape canvas = util.draw_bodypose(canvas, candidate, subset) # detect hand hands_list = util.handDetect(candidate, subset, oriImg) all_hand_peaks = [] shape2 = canvas.shape for x, y, w, is_left in hands_list: # cv2.rectangle(canvas, (x, y), (x+w, y+w), (0, 255, 0), 2, lineType=cv2.LINE_AA) # cv2.putText(canvas, 'left' if is_left else 'right', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) # if is_left: # plt.imshow(oriImg[y:y+w, x:x+w, :][:, :, [2, 1, 0]]) # plt.show() peaks = hand_estimation(oriImg[y:y+w, x:x+w, :]) peaks[:, 0] = np.where(peaks[:, 0]==0, peaks[:, 0], peaks[:, 0]+x) peaks[:, 1] = np.where(peaks[:, 1]==0, peaks[:, 1], peaks[:, 1]+y) # else: # peaks = hand_estimation(cv2.flip(oriImg[y:y+w, x:x+w, :], 1)) # peaks[:, 0] = np.where(peaks[:, 0]==0, peaks[:, 0], w-peaks[:, 0]-1+x) # peaks[:, 1] = np.where(peaks[:, 1]==0, peaks[:, 1], peaks[:, 1]+y) # print(peaks) all_hand_peaks.append(peaks) shape3 = canvas.shape canvas = util.draw_handpose(canvas, all_hand_peaks) shape4 = canvas.shape cv2.imwrite("test.png", canvas) canvas = cv2.resize(canvas, (shape0[1],shape0[0])) # raise AttributeError(shape0, shape1, shape2, shape3, shape4) return canvas[:, :, [2, 1, 0]]
def detect_keypoint(test_image, is_vis): body_estimation = Body('model/body_pose_model.pth') hand_estimation = Hand('model/hand_pose_model.pth') oriImg = cv2.imread(test_image) # B,G,R order # detect body # subset: n*20 array, n is the human_number in the index, 0-17 is the index in candidate, 18 is the total score, 19 is the total parts # candidate: m*4, m is the keypoint number in the image, [x, y, confidence, id] candidate, subset = body_estimation( oriImg ) # candidate: output the keypoints([25, 4]), x, y, score, keypoint_index canvas = copy.deepcopy(oriImg) canvas, bodypoints = util.draw_bodypose(canvas, candidate, subset) # detect hand hands_list = util.handDetect(candidate, subset, oriImg) all_hand_peaks = [] hand_personid_isleft = [] for x, y, w, is_left, person_id in hands_list: # cv2.rectangle(canvas, (x, y), (x+w, y+w), (0, 255, 0), 2, lineType=cv2.LINE_AA) # cv2.putText(canvas, 'left' if is_left else 'right', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) # if is_left: # plt.imshow(oriImg[y:y+w, x:x+w, :][:, :, [2, 1, 0]]) # plt.show() peaks = hand_estimation(oriImg[y:y + w, x:x + w, :]) peaks[:, 0] = np.where(peaks[:, 0] == 0, peaks[:, 0], peaks[:, 0] + x) peaks[:, 1] = np.where(peaks[:, 1] == 0, peaks[:, 1], peaks[:, 1] + y) # else: # peaks = hand_estimation(cv2.flip(oriImg[y:y+w, x:x+w, :], 1)) # peaks[:, 0] = np.where(peaks[:, 0]==0, peaks[:, 0], w-peaks[:, 0]-1+x) # peaks[:, 1] = np.where(peaks[:, 1]==0, peaks[:, 1], peaks[:, 1]+y) # print(peaks) all_hand_peaks.append(peaks) hand_personid_isleft.append([person_id, is_left]) # all_hand_peaks: [p, 21, 2] p is the hand number in the image # hand_personid_isleft: [p, 2] is_isleft, person_id all_hand_peaks = np.asarray(all_hand_peaks) hand_personid_isleft = np.asarray(hand_personid_isleft) canvas = util.draw_handpose(canvas, all_hand_peaks) if is_vis: plt.imshow(canvas[:, :, [2, 1, 0]]) plt.axis('off') plt.show() return bodypoints, all_hand_peaks, hand_personid_isleft
def process_frame(frame, body=True, hands=True): canvas = copy.deepcopy(frame) if body: candidate, subset = body_estimation(frame) canvas = util.draw_bodypose(canvas, candidate, subset) if hands: hands_list = util.handDetect(candidate, subset, frame) all_hand_peaks = [] for x, y, w, is_left in hands_list: peaks = hand_estimation(frame[y:y + w, x:x + w, :]) peaks[:, 0] = np.where(peaks[:, 0] == 0, peaks[:, 0], peaks[:, 0] + x) peaks[:, 1] = np.where(peaks[:, 1] == 0, peaks[:, 1], peaks[:, 1] + y) all_hand_peaks.append(peaks) canvas = util.draw_handpose(canvas, all_hand_peaks) return canvas
cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height)) frame_count = 0 start = time.time() while True: isTrue, frame = capture.read() if isTrue == True: frame_count += 1 print('Processing %dth frame...' % frame_count) candidate, subset = body_estimation(frame) canvas = copy.deepcopy(frame) canvas = util.draw_bodypose(canvas, candidate, subset) util.toTempList(candidate) # Write the frame into the file 'output.avi' out.write(canvas) # Display the resulting frame cv2.imshow('Preview', canvas) # Press Q on keyboard to stop recording if cv2.waitKey(1) & 0xFF == ord('d'): break # Break the loop else:
elif not found and k < 17: row = -1 * np.ones(20) row[indexA] = partAs[i] row[indexB] = partBs[i] row[-1] = 2 row[-2] = sum( candidate[connection_all[k][i, :2].astype(int), 2]) + connection_all[k][i][2] subset = np.vstack([subset, row]) # delete some rows of subset which has few parts occur deleteIdx = [] for i in range(len(subset)): if subset[i][-1] < 4 or subset[i][-2] / subset[i][-1] < 0.4: deleteIdx.append(i) subset = np.delete(subset, deleteIdx, axis=0) # subset: n*20 array, 0-17 is the index in candidate, 18 is the total score, 19 is the total parts # candidate: x, y, score, id return candidate, subset, heatmap_list, heatmap_list_converted_list if __name__ == "__main__": body_estimation = Body('../model/body_pose_model.pth') test_image = '../images/ski.jpg' oriImg = cv2.imread(test_image) # B,G,R order candidate, subset = body_estimation(oriImg) canvas = util.draw_bodypose(oriImg, candidate, subset) plt.imshow(canvas[:, :, [2, 1, 0]]) plt.show()
def process_one(video_input): output_fname = './output-vid/' + video_input.split('/')[-1].split( '.')[0] + '_openpose_inference_kp.avi' print(output_fname) if os.path.isfile(output_fname): print("file alreay exist, return and process next") return cap = cv2.VideoCapture(video_input) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) frames_per_second = cap.get(cv2.CAP_PROP_FPS) # cap.set(3, 640) # cap.set(4, 480) output_file = cv2.VideoWriter( filename=output_fname, # some installation of opencv may not support x264 (due to its license), # you can try other format (e.g. MPEG) # apiPreference=1, fourcc=cv2.VideoWriter_fourcc(*'XVID'), # fourcc=cv2.VideoWriter_fourcc(*"x264"), fps=float(frames_per_second), frameSize=(width, height), isColor=True) # people = {} # people['pose_keypoints_2d'] = {} # cnt = 0 while True: # cnt += 1 ret, oriImg = cap.read() if oriImg is None: break candidate, subset = body_estimation(oriImg) # save the 2d-pose results to JSON as the OpenPose output format # people['pose_keypoints_2d'][cnt] = [] # for i in range(18): # for n in range(len(subset)): # index = int(subset[n][i]) # xyc = [-1, -1, -1] if index == -1 else candidate[index][0:3] # people['pose_keypoints_2d'][cnt].extend(xyc) # # canvas = copy.deepcopy(oriImg) # canvas = util.draw_bodypose(canvas, candidate, subset) canvas = util.draw_bodypose(oriImg, candidate, subset) # detect hand # hands_list = util.handDetect(candidate, subset, oriImg) # # all_hand_peaks = [] # for x, y, w, is_left in hands_list: # peaks = hand_estimation(oriImg[y:y + w, x:x + w, :]) # peaks[:, 0] = np.where(peaks[:, 0] == 0, peaks[:, 0], peaks[:, 0] + x) # peaks[:, 1] = np.where(peaks[:, 1] == 0, peaks[:, 1], peaks[:, 1] + y) # all_hand_peaks.append(peaks) # # canvas = util.draw_handpose(canvas, all_hand_peaks) # cv2.imshow('demo', canvas) # 一个窗口用以显示原视频 output_file.write(canvas) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() output_file.release() cv2.destroyAllWindows()
test_image1 = 'images/aa.jpg' # 비교할 이미지 1 test_image2 = 'images/dragon2.jpg' # 비교할 이미지 2 oriImg1 = cv2.imread(test_image1) # 이미지 불러오기 candidate1, subset1 = body_estimation( oriImg1) # candidate: x, y, score, id | subset: index # x와 y가 관절의 위치, index는 관절의 이름이라고 생각하면 됨 # score는 단순하게 확실 정도라고 보면됨 # id는 무시해도 되고 oriImg2 = cv2.imread(test_image2) # 이미지2 불러오기 candidate2, subset2 = body_estimation(oriImg2) canvas1 = copy.deepcopy(oriImg1) # 이미지 복사 canvas1 = util.draw_bodypose(canvas1, candidate1, subset1) # 화면에 예측한 스켈레톤을 보여줌 canvas2 = copy.deepcopy(oriImg2) canvas2 = util.draw_bodypose(canvas2, candidate2, subset2) mae = pose_compare.mae(candidate1, subset1, candidate2, subset2) # 관절들의 위치를 통해 뼈의 각도를 알아내고 # 두이미지의 각도의 mean average error를 구한다 print("자세 오류: ", mae) plt.subplot(121) plt.imshow(canvas1[:, :, [2, 1, 0]]) plt.axis('off') plt.subplot(122) plt.imshow(canvas2[:, :, [2, 1, 0]])
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Apr 28 19:17:47 2020 @author: joe """ from src import torch_openpose, util import cv2 if __name__ == "__main__": tp = torch_openpose.torch_openpose('body_25') img = cv2.imread("images/timg.jpeg") poses = tp(img) img = util.draw_bodypose(img, poses, 'body_25') cv2.imshow('v', img) cv2.waitKey(0) cv2.destroyAllWindows()