def generate_cooridnates(extracted_coordinates_path, clip_file_path, keras_weights_file, ending_frame=None): video_file_name = clip_file_path.rsplit("/", 1)[1].rsplit(".", 1)[0] if os.path.isfile(extracted_coordinates_path + "/" + video_file_name + "_x.csv"): return model, params, model_params = load_model(keras_weights_file) scale_search = [1] params['scale_search'] = scale_search cam = cv2.VideoCapture(clip_file_path) input_fps = cam.get(cv2.CAP_PROP_FPS) video_length = int(cam.get(cv2.CAP_PROP_FRAME_COUNT)) print("Total frame in the video: ", video_length) if ending_frame is None: ending_frame = video_length ret_val, orig_image = cam.read() i = 0 # default is 0 cc = [] start = time.time() while (cam.isOpened()) and ret_val is True and i < ending_frame: input_image = cv2.cvtColor(orig_image, cv2.COLOR_RGB2BGR) tic = time.time() # generate image with body parts body_parts, all_peaks, subset, candidate = extract_parts( input_image, params, model, model_params) # print('Processing frame: ', i) toc = time.time() # print('Processing time is %.5f' % (toc - tic)) cc.append(body_parts) ret_val, orig_image = cam.read() i += 1 xx = [{k: v[0] for k, v in d.items()} for d in cc] yy = [{k: v[1] for k, v in d.items()} for d in cc] xx_ = pd.DataFrame(xx) yy_ = pd.DataFrame(yy) end = time.time() print('Total time taken is %.5f' % (end - start)) xx_.to_csv(extracted_coordinates_path + "/" + video_file_name + "_x.csv", index=False) yy_.to_csv(extracted_coordinates_path + "/" + video_file_name + "_y.csv", index=False)
def get_pose(video_file, output_path): # data out output_data = [] # Output location output_format = '.mp4' video = os.path.basename(video_file).split('.')[0] video_output = output_path + '/' + video + str(start_datetime) + output_format # Video reader cam = cv2.VideoCapture(video_file) input_fps = cam.get(cv2.CAP_PROP_FPS) ret_val, orig_image = cam.read() video_length = int(cam.get(cv2.CAP_PROP_FRAME_COUNT)) ending_frame = video_length # Video writer output_fps = input_fps / frame_rate_ratio fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(video_output, fourcc, output_fps, (orig_image.shape[1], orig_image.shape[0])) # scale_search = [1, .5, 1.5, 2] # [.5, 1, 1.5, 2] # scale_search = scale_search[0:process_speed] # # params['scale_search'] = scale_search i = 0 # default is 0 while(cam.isOpened()) and ret_val is True and i < ending_frame: if i % frame_rate_ratio == 0: input_image = cv2.cvtColor(orig_image, cv2.COLOR_RGB2BGR) tic = time.time() # generate image with body parts body_parts, all_peaks, subset, candidate = extract_parts(input_image, params, model, model_params) output_data.append({'frame_id': i, 'body_parts': body_parts, 'all_peaks': all_peaks, 'subset': subset, 'candidate': candidate}) canvas = draw(orig_image, all_peaks, subset, candidate) print('Processing frame: ', i) toc = time.time() print('processing time is %.5f' % (toc - tic)) out.write(canvas) ret_val, orig_image = cam.read() i += 1 return output_data
def generate(self, p_frame=-1): """ 生成骨架 """ cap = cv2.VideoCapture(self.vid_path) n_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) fps = int(cap.get(cv2.CAP_PROP_FPS)) # 24 print('[Info] 总帧数: {}, 帧率: {}'.format(n_frame, fps)) skt_prev = None # 前一个骨骼点,用于补充 # 写入的frame值 if p_frame == -1 or p_frame > n_frame: p_frame = n_frame for i in range(0, p_frame): cap.set(cv2.CAP_PROP_POS_FRAMES, i) ret, frame = cap.read() all_peaks, subset, candidate = extract_parts( frame, self.params, self.model, self.model_params) # 原始的骨架 ms = MatchstickSkeleton(subset, candidate, frame) skt = ms.get_skeleton() # 修改之后的骨架 skt_m = MatchstickSkeleton.generate_skeleton_v2( skt, skt_prev, None) # 根据前一个骨骼点补充 skt_prev = skt_m # 写入原始骨架 skt_json = json.dumps(skt) write_line(self.skt_file, skt_json) # 写入之后的骨架 skt_f_json = json.dumps(skt_m) write_line(self.skt_m_file, skt_f_json) print('[Info] 已处理帧数: {} / {}'.format(i, n_frame)) print('[Info] 视频提取完成! ')
def std_main(): parser = argparse.ArgumentParser() parser.add_argument('--image', type=str, required=True, help='input image') parser.add_argument('--output', type=str, default='result.png', help='output image') parser.add_argument('--model', type=str, default='model/keras/model.h5', help='path to the weights file') args = parser.parse_args() image_path = args.image output = args.output keras_weights_file = args.model tic = time.time() print('start processing...') # load model # authors of original model don't use # vgg normalization (subtracting mean) on input images model = get_testing_model() model.load_weights(keras_weights_file) # load config params, model_params = config_reader() input_image = cv2.imread(image_path) # B,G,R order all_peaks, subset, candidate = extract_parts(input_image, params, model, model_params) canvas = draw(input_image, all_peaks, subset, candidate) toc = time.time() print('processing time is %.5f' % (toc - tic)) cv2.imwrite(output, canvas) cv2.destroyAllWindows()
def predict_img(image_path, model, output_img, output_pos): # load config params, model_params = config_reader() input_image = cv2.imread(image_path) # B,G,R order all_peaks, subset, candidate = extract_parts(input_image, params, model, model_params) for peak in all_peaks: if peak: write_line(output_pos, "{}".format(peak[0])) else: write_line(output_pos, "") # canvas = draw(input_image, all_peaks, subset, candidate) canvas = draw_skeleton(input_image, subset, candidate) cv2.imwrite(output_img, canvas) cv2.destroyAllWindows()
print('start processing...') # load model # authors of original model don't use # vgg normalization (subtracting mean) on input images model = get_testing_model() model.load_weights(keras_weights_file) # load config params, model_params = config_reader() #immagine da classificare input_image = cv2.imread('images.jpg') # B,G,R order body_parts, all_peaks, subset, candidate = extract_parts( input_image, params, model, model_params) canvas, dict, lis1, lis2 = draw(input_image, all_peaks, subset, candidate) cv2.imwrite(output, canvas) Concatena.salva_csv_dist(lis1, lis2, 'none') dataframe = pandas.read_csv("dataset_dist.csv") dataset = dataframe.values riga = dataframe.shape[0] X = dataset[:, 0:93] toc = time.time()
cv2.waitKey(10) if cam.isOpened() is False or ret_val is False: break if mirror: orig_image = cv2.flip(orig_image, 1) cropped = _crop(orig_image, width, factor) input_image = cv2.resize(cropped, (0, 0), fx=1 / resize_fac, fy=1 / resize_fac, interpolation=cv2.INTER_CUBIC) input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR) # generate image with body parts subsets, candidates = extract_parts(input_image, params, model, model_params) obj = _prepare_object(subsets, candidates) print(obj) canvas = draw(cropped, subsets, candidates, resize_fac=resize_fac) headers = {'content-type': 'application/json'} r = requests.post(url=service_url, headers=headers, json=obj) print('Service POST response: {}'.format(r)) if out is not None: out.write(canvas) cv2.imshow('frame', canvas)
def video_cap(Excersise): path_model_h5 = 'model.h5' keras_weights_file = path_model_h5 #Analysis for the every n frames frame_rate_ratio = 1 #Int 1 (fastest, lowest quality) to 4 (slowest, highest quality) process_speed = 1 #ending_frame = args.end print('start processing...') # Video input video_file = '/home/saireddy/SaiReddy/Desk/Flask/OrbitPose/videos/push.mp4' print(video_file) # Output location video_output = '/home/saireddy/SaiReddy/Desk/Flask/OrbitPose/videos/output/push4.avi' model = get_testing_model() model.load_weights(keras_weights_file) # load config params, model_params = config_reader() # Video reader #cam = cv2.VideoCapture(video_file) cam = cv2.VideoCapture(video_file) input_fps = cam.get(cv2.CAP_PROP_FPS) print("input frames per second:-", input_fps) ret_val, orig_image = cam.read() video_length = int(cam.get(cv2.CAP_PROP_FRAME_COUNT)) print("total frames in a video:-", video_length) # Video writer output_fps = input_fps / frame_rate_ratio print("out put frames:-", output_fps) frame_width = int(cam.get(3)) frame_height = int(cam.get(4)) print("width:-", frame_width) print("Height:-", frame_height) out = cv2.VideoWriter(video_output, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), output_fps, (frame_width, frame_height)) #out = cv2.VideoWriter(video_output, cv2.VideoWriter_fourcc(*'DIVX'),output_fps, (frame_width, frame_height)) scale_search = [1, .5, 1.5, 2] # [.5, 1, 1.5, 2] scale_search = scale_search[0:process_speed] params['scale_search'] = scale_search i = 0 # default is 0 count = 0 while (cam.isOpened()) and ret_val is True: if ret_val is None: break if i % frame_rate_ratio == 0: input_image = cv2.cvtColor(orig_image, cv2.COLOR_RGB2BGR) tic = time.time() if Excersise == 'Normal': all_peaks, subset, candidate = extract_parts( input_image, params, model, model_params) canvas, theta, theta1, theta2, theta3, Angle1, Angle2, Angle3, Angle4 = draw( orig_image, all_peaks, subset, candidate) cv2.rectangle(canvas, (0, 0), (265, 35), color=(0, 255, 0), thickness=2) cv2.putText(canvas, "right Hand angle :- {0:.2f}".format(float(theta)), (30, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255)) cv2.putText(canvas, "right leg angle :- {0:.2f}".format(float(theta2)), (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255)) cv2.rectangle(canvas, (645, 0), (900, 35), color=(0, 255, 0), thickness=2) cv2.putText(canvas, "left Hand angle :- {0:.2f}".format(float(theta1)), (650, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 225)) cv2.putText(canvas, "left leg angle :- {0:.2f}".format(float(theta3)), (650, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255)) elif Excersise == 'Fat': all_peaks1, subset1, candidate1 = extract_parts_angle( input_image, params, model, model_params) canvas, theta5, theta6, Angle5, Angle6 = draw_angle( orig_image, all_peaks1, subset1, candidate1) cv2.rectangle(canvas, (645, 0), (900, 35), color=(0, 255, 0), thickness=2) cv2.putText(canvas, "Left :- {0:.2f}".format(float(theta5)), (650, 15), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 225)) cv2.putText(canvas, "Right :- {0:.2f}".format(float(theta6)), (650, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255)) elif Excersise == 'Pushups': print("HIIIIIII") all_peaks2, subset2, candidate2 = extract_parts_push( input_image, params, model, model_params) canvas, theta7, theta8, Angle7, Angle8 = draw_push( orig_image, all_peaks2, subset2, candidate2) print("THeta 7, theta8 ", theta7, theta8) if float(theta7) > 170.0 or float(theta8) > 170.0: count = count + 1 cv2.rectangle(canvas, (645, 0), (900, 60), color=(255, 25, 100), thickness=2) cv2.putText(canvas, "Left :- {0:.2f}".format(float(theta7)), (650, 30), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 225)) cv2.putText(canvas, "Right :- {0:.2f}".format(float(theta8)), (650, 45), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255)) cv2.putText(canvas, f"count :- {count}".format(float(theta8)), (300, 35), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255)) else: cv2.rectangle(canvas, (645, 0), (900, 60), color=(255, 25, 100), thickness=2) cv2.putText(canvas, "Left :- {0:.2f}".format(float(theta7)), (650, 30), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 255, 0)) cv2.putText(canvas, "Right :- {0:.2f}".format(float(theta8)), (650, 45), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 255, 0)) else: print("Sorry Wrong Excersise was given") print('Processing frame: ', i) toc = time.time() out.write(canvas) ret_val, orig_image = cam.read() i += 1 if cv2.waitKey(25) & 0xFF == ord('q'): break cv2.destroyAllWindows() cam.release() convert_video( '/home/saireddy/SaiReddy/Desk/Flask/OrbitPose/videos/output/pose14.avi', '/home/saireddy/SaiReddy/Desk/Flask/OrbitPose/videos/output/pose14.mp4' ) return Angle1, Angle2, Angle3, Angle4
def cameraLoad(): parser = argparse.ArgumentParser() parser.add_argument('--device', type=int, default=0, help='ID of the device to open') #parser에서 받는거 parser.add_argument('--model', type=str, default='model/keras/model.h5', help='path to the weights file') #모델경로 parser.add_argument('--frame_ratio', type=int, default=7, help='analyze every [n] frames') # --process_speed changes at how many times the model analyzes each frame at a different scale parser.add_argument( '--process_speed', type=int, default=1, help= 'Int 1 (fastest, lowest quality) to 4 (slowest, highest quality)') parser.add_argument('--out_name', type=str, default=None, help='name of the output file to write') parser.add_argument('--mirror', type=bool, default=True, help='whether to mirror the camera') # 받은 값들 저장하는것 args = parser.parse_args() device = args.device keras_weights_file = args.model frame_rate_ratio = args.frame_ratio process_speed = args.process_speed out_name = args.out_name mirror = args.mirror print('start processing...') # load model # authors of original model don't use # vgg normalization (subtracting mean) on input images model = get_testing_model() model.load_weights(keras_weights_file) # load config params, model_params = config_reader() # Video reader cam = cv2.VideoCapture(device) # 디바이스=0은 카메라, 파일넣고싶으면 파일 경로 넣으면 됨 # CV_CAP_PROP_FPS input_fps = cam.get(cv2.CAP_PROP_FPS) # 해당카메라의 프레임으로 추측 print("Running at {} fps.".format(input_fps)) ret_val, orig_image = cam.read( ) #ret = 제대로 read됐는지확인하는 부울타입, orig = 실질적인 프레임단위의 이미지 width = orig_image.shape[1] height = orig_image.shape[0] factor = 0.3 out = None # Output location if out_name is not None and ret_val is not None: # out_name이 parser 40번째줄/ out_name이 잇으면 파일생성/ output_path = 'videos/outputs/' output_format = '.mp4' video_output = output_path + out_name + output_format # Video writer output_fps = input_fps / frame_rate_ratio tmp = crop(orig_image, width, factor) fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(video_output, fourcc, output_fps, (tmp.shape[1], tmp.shape[0])) del tmp scale_search = [0.22, 0.25, .5, 1, 1.5, 2] # [.5, 1, 1.5, 2] scale_search = scale_search[0:process_speed] params['scale_search'] = scale_search i = 0 # default is 0 resize_fac = 8 # while(cam.isOpened()) and ret_val is True: while True: cv2.waitKey(10) if cam.isOpened() is False or ret_val is False: break if mirror: orig_image = cv2.flip(orig_image, 1) tic = time.time() cropped = crop(orig_image, width, factor) # 파일 자름 #opencv함수로 사이즈 조절하는 것 input_image = cv2.resize(cropped, (0, 0), fx=1 / resize_fac, fy=1 / resize_fac, interpolation=cv2.INTER_CUBIC) input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR) # generate image with body parts # extract_part all_peaks, subset, candidate = extract_parts( input_image, params, model, model_params) canvas = draw(cropped, all_peaks, subset, candidate, resize_fac=resize_fac) print('Processing frame: ', i) toc = time.time() print('processing time is %.5f' % (toc - tic)) if out is not None: # 이게 있으면 계속 출력하는 것 out.write(canvas) # 이미지 위에 만들어진 스켈레톤! # canvas = cv2.resize(canvas, (0, 0), fx=4, fy=4, interpolation=cv2.INTER_CUBIC) cv2.imshow('frame', canvas) if cv2.waitKey(1) & 0xFF == ord('q'): break ret_val, orig_image = cam.read() i += 1