def get_pose(image_dir, img_name, pose_dir): inference = tf_pose.infer(image=os.path.join(image_dir, img_name))[0] body_parts = inference.body_parts person_data = [] for i in range(18): try: person = body_parts[i] x = person.x * 192 y = person.y * 256 confidence = person.score except: x = 0 y = 0 confidence = 0 data = [x, y, confidence] person_data += data output = {} output['version'] = 1.0 people = dict() people['face_keypoints'] = [] people['pose_keypoints'] = person_data people['hand_right_keypoints'] = [] people['hand_left_keypoints'] = [] _people = [people] output['people'] = _people with open( os.path.join(pose_dir, img_name.replace('.jpg', '_keypoints.json')), 'w') as file: json.dump(output, file)
def get_pose(): # load video path_list = ["Sub04_03_30_FS_R11.avi", "Sub05_01_30_FK_R21.avi", "Sub05_02_30_KS_R11.avi", "Sub08_04_60_KS_R21.avi"] for i, img_path in enumerate(path_list): print(i) print(img_path) # dummy pose_all = [] # video cap = cv2.VideoCapture(img_path) while(True): # Capture frame-by-frame ret, frame = cap.read() if ret: # pose detection frame = frame[0:480, 50:480, :] coco_style = tf_pose.infer(frame) # check if len(coco_style)<1: temp = np.ones((34,1))*-1. else: temp = np.array(coco_style[0][0]) temp = np.expand_dims(np.delete(temp, range(2,51,3)), axis=1) temp=temp.tolist() pose_all.append(temp) else: break # print(i) np.save("./"+str(img_path[0:-4])+".npy", pose_all) # When everything done, release the capture cap.release()
def all_image(self, img_dir="/home/externalDisk/RULA_image/images_lift/side/"): from PIL import Image import glob image_list = [] fname_list = [] pose_list = [] for i, file_path in enumerate(glob.glob(img_dir+"*.png")): print(i) ## append file name fname=file_path[47:] fname_list.append(fname) print(fname) ## append image im = np.array(Image.open(file_path)) image_list.append(im) ## append predicted pose pose = tf_pose.infer(im) try: pose = np.array(pose[0][0]) except: pose = np.zeros((51,)) pose_list.append(pose) all3=[] all3.append(fname_list) all3.append(image_list) all3.append(pose_list) print(len(all3)) import pickle with open("img_pose", "wb") as f: pickle.dump(all3, f)
def continuous_predict(self, verbose=False): while (self.predicting): self.file_helper.update_image_library() image_path, image_timestamp_name = self.file_helper.get_new_image() if image_path is not None: coco_style = tf_pose.infer(image_path) # coco_style = [ 'asdf', 'asdf2'] self.file_helper.save_misc(coco_style, image_timestamp_name) if verbose: print(coco_style) time.sleep(0.001)
def load_save_h36(): # load pose import sys sys.path.append("/home/lli40/PyCode/Downloaded/tf-pose-estimation") import tf_pose img_path = "/home/lli40/PyCode/MyProject/RULA_2DImage/data/pose100.npy" img = np.load(img_path, allow_pickle=True, encoding="latin1") cnt = 0 pose_all = [] # import time for i in range(len(img)): # start_time = time.time() cnt += 1 frame = img[i] * 255. frame = frame.astype(np.uint8) coco_style = tf_pose.infer(frame) # elapsed_time = time.time() - start_time # print("FPS: " + str(1. / elapsed_time)) if len(coco_style) < 1: temp = np.ones((34, 1)) * -1. else: temp = np.array(coco_style[0][0]) temp = np.expand_dims(np.delete(temp, range(2, 51, 3)), axis=1) print(temp.shape) temp = temp.tolist() pose_all.append(temp) exit() pose_all = np.array(pose_all) print(pose_all.shape) # load gscore gscore_name = np.loadtxt( "/home/lli40/PyCode/MyProject/RULA_2DImage/data/gscore-name-human36.txt", dtype=str) gscore_name = np.delete(gscore_name, 0) # dummy gscore_all = [] for i in range(len(gscore_name)): idx = gscore_name[i][0:-3] temp = gscore_name[i][-1] gscore_all.append(int(temp)) # to np array gscore_all = np.array(gscore_all) # save np.save("/home/lli40/PyCode/MyProject/RULA_2DImage/data/x_all_h36_raw.npy", pose_all) np.save("/home/lli40/PyCode/MyProject/RULA_2DImage/data/y_all_h36_raw.npy", gscore_all)
def data_upload_lift(): ## for lift lift_img_dir = "/home/externalDisk/RULA_image/images_lift/side/" from PIL import Image import glob image_list = [] fname_list = [] import cv2 import sys sys.path.append("/home/lli40/PyCode/Downloaded/tf-pose-estimation") import tf_pose for i, file_path in enumerate(glob.glob(lift_img_dir + "*.png")): print(i) ## append file name fname = file_path[47:] fname_list.append(fname) print(fname) ## append image im = np.array(Image.open(file_path)) image = im[0:480, 50:480, :] coco_style = tf_pose.infer(image) if len(coco_style) > 0: joints = np.array(coco_style[0][0]) joints = np.round(joints).astype(int) image = image.copy() # draw rectangle on face if joints[0] != 0 or joints[0] != -1: cv2.rectangle(image, (joints[0] - 30, joints[1] - 30), (joints[0] + 5, joints[1] + 30), (255, 255, 255), -1) image_list.append(image) # cv2.imshow("sample", image) # cv2.waitKey(0) print(np.array(image_list).shape) np.save( "/home/lli40/PyCode/MyProject/RULA_2DImage/data_upload/lift_image.npy", image_list)
def img2img(self): img_path="/home/lli40/PyCode/MyProject/2DPose_detect/Data/Human80K/GTD2P/x_test.npy" img = np.load(img_path, allow_pickle=True, encoding="latin1") cnt=0 for i in range(len(img)): cnt+=1 frame=img[i]*255. frame=frame.astype(np.uint8) coco_style = tf_pose.infer(frame) if len(coco_style)<1: continue joints = np.array(coco_style[0][0]) cv2.imwrite("./human80/" + str(cnt) + ".png", frame) visualizer = vis() new_frame = visualizer.vis_pose(image=frame, joints=joints, is_display = False) cv2.imwrite("./human80/" + str(cnt) + "_n.png", new_frame) print(cnt)
def video2video(self, source_path, target_path): cap = cv2.VideoCapture(source_path) cnt= 0 while (cap.isOpened()): cnt += 1 ret, frame = cap.read() frame = frame[0:480, 50:480, :] coco_style = tf_pose.infer(frame) print(len(coco_style)) joints = np.array(coco_style[0][0]) visualizer = vis() new_frame = visualizer.vis_pose(image=frame, joints=joints) # new_frame = frame new_frame = new_frame[:, 200:-1, :] frame = frame [:, 200:-1, :] cv2.imwrite("./lift/" + str(cnt) + ".png", frame) cv2.imwrite("./lift/"+str(cnt)+"_n.png", new_frame) print(cnt) # cv2.imshow('frame', new_frame) # # if cv2.waitKey(0) == ord("q"): # continue # else: # exit() # When everything done, release the capture cap.release() cv2.destroyAllWindows()
def get_video(): # load video path_list = ["Sub04_01_30_FK_R11.avi", "Sub04_03_30_FS_R11.avi", "Sub04_02_30_KS_R21.avi", "Sub05_01_30_FK_R21.avi", "Sub05_09_30_FS_R11.avi", "Sub05_02_30_KS_R11.avi", "Sub08_01_00_FK_R11.avi,", "Sub08_11_30_FS_R11.avi", "Sub08_04_60_KS_R21.avi"] path_list=[ "Sub08_01_00_FS_R11.avi"] # load gscore gscore_name = np.loadtxt("/home/lli40/PyCode/MyProject/RULA_2DImage/data/gscore-name-lift.txt", dtype=str) gscore_name = np.delete(gscore_name, 0) for i, video_path in enumerate(path_list): print(video_path) cap = cv2.VideoCapture(video_path) fid = 0 fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(video_path[0:-4]+"_p.avi", fourcc, 20.0, (1440, 480)) # cv2.namedWindow("output", cv2.WINDOW_NORMAL) ret = True while ret: # Capture frame-by-frame ret, frame = cap.read() if ret: print(str(fid)+" th frame") fid+=1 # pose detection # frame = frame[0:480, 50:480, :] coco_style = tf_pose.infer(frame) # check if len(coco_style)<1: temp = np.ones((34,1))*-1. else: temp = np.array(coco_style[0][0]) print(temp.shape) visualizer = vis() new_frame = visualizer.vis_pose(image=frame, joints=temp) print(new_frame.shape) # cv2.imshow("a",new_frame) # cv2.waitKey(0) # exit() out.write(new_frame) # cv2.imshow("output", new_frame) # cv2.waitKey(0) cap.release() out.release()
def h36_video(): img = np.load("/home/lli40/PyCode/MyProject/RULA_2DImage/data/pose100.npy", allow_pickle=True, encoding="latin1") fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter("H36_p.avi", fourcc, 30.0, (200, 150)) for i in range(len(img)): frame = img[i] * 255. frame = frame.astype(np.uint8) coco_style = tf_pose.infer(frame) if len(coco_style) < 1: temp = np.ones((34, 1)) * -1. else: temp = np.array(coco_style[0][0]) print(temp.shape) visualizer = vis() new_frame = visualizer.vis_pose(image=frame, joints=temp) print(new_frame.shape) for j in range(100): out.write(new_frame) out.release()
handpose = HandPose(input_tensor) handpose.load_weights('checkpoints/pretrain') # TEST_IMAGE_PATH = 'hand1.jpg' # image_orig = cv2.imread(TEST_IMAGE_PATH) # handpose.predict(image_orig, 1.5, True, True) import pdb import matplotlib.pyplot as plt import math TEST_IMAGE_PATH = 'hand2.jpg' image = cv2.imread(TEST_IMAGE_PATH) img_w = image.shape[1] img_h = image.shape[0] bodys = tf_pose.infer('hand2.jpg') for body in bodys: # l_roi_image = handpose.getLeftHandRoiImage(body.body_parts, image) # r_roi_image = handpose.getRightHandRoiImage(body.body_parts, image) # handpose.fix_predict(l_roi_image) lhpts, rhpts = handpose.getHandsPart(image, body) for partid in lhpts: part = lhpts[partid] cv2.circle(image, (int(img_w * part.x), int(img_h * part.y)), 10, (255, 0, 0), 2) for partid in rhpts: part = rhpts[partid] cv2.circle(image, (int(img_w * part.x), int(img_h * part.y)), 10, (255, 0, 0), 2)
dnames = [d["photo"].split("/")[-1] for d in data] print("Start loading photos") for d in tqdm(data): load(d["photo"]) print("Start preparing photos:", len(done) - len(photos_name)) import tensorflow.compat.v1 as tf tf.disable_v2_behavior() import tf_pose for i, d in tqdm(enumerate(data)): if d['photo'] in photos_name: continue try: coco_style = tf_pose.infer( f"/home/petr/Documents/Projects/Recognition/data/{d['photo']}") except Exception as e: print(e, f"data/{d['photo']}") continue photos.append({"name": d['photo'], "points": coco_style, "shape": None}) photos_name.append(d['photo']) if i % 150 == 0: with open("photos.pk", "wb") as f: pickle.dump(photos, f) with open("photos.pk", "wb") as f: pickle.dump(photos, f) print("Complete", len(data), "total:", len(photos))