def detect_parts(self, image): start = time.time() imgpath = self._download_image(image) image, thumbnail, input_width, input_height = self.read_img( imgpath, 368, 368) start = print_time("image loaded in: ", start) if not SmartBodyCrop.initialized: print("Loading the model...") self.load_graph_def() with tf.Session() as sess: inputs = tf.get_default_graph().get_tensor_by_name('inputs:0') heatmaps_tensor = tf.get_default_graph().get_tensor_by_name( 'Mconv7_stage6_L2/BiasAdd:0') pafs_tensor = tf.get_default_graph().get_tensor_by_name( 'Mconv7_stage6_L1/BiasAdd:0') heatMat, pafMat = sess.run([heatmaps_tensor, pafs_tensor], feed_dict={inputs: image}) start = print_time("tf session executed in: ", start) humans = estimate_pose(heatMat[0], pafMat[0]) start = print_time("pose estimated in: ", start) # display img1 = draw_humans(thumbnail, humans) return img1
def detect_parts(self, imgpath): img1_raw = Image.open(imgpath) img1 = np.asarray(img1_raw) input_width, input_height = img1.shape[0], img1.shape[1] tf.reset_default_graph() graph_def = graph_pb2.GraphDef() with open('models/optimized_openpose.pb', 'rb') as f: graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='') inputs = tf.get_default_graph().get_tensor_by_name('inputs:0') heatmaps_tensor = tf.get_default_graph().get_tensor_by_name( 'Mconv7_stage6_L2/BiasAdd:0') pafs_tensor = tf.get_default_graph().get_tensor_by_name( 'Mconv7_stage6_L1/BiasAdd:0') image = self.read_img(imgpath, input_width, input_height) with tf.Session() as sess: heatMat, pafMat = sess.run([heatmaps_tensor, pafs_tensor], feed_dict={inputs: image}) heatMat, pafMat = heatMat[0], pafMat[0] humans = estimate_pose(heatMat, pafMat) # display image_h, image_w = img1.shape[:2] img1 = draw_humans(img1_raw, humans) scale = 480.0 / image_h newh, neww = 480, int(scale * image_w + 0.5) return img1
def compute_pose_frame(input_image, sess): # Build the network to produce the poses: image = cv2.resize(input_image, dsize=(input_height, input_width), interpolation=cv2.INTER_CUBIC) global first_time if first_time: # load pretrained weights #print(first_time) s = "%dx%d" % (input_node.shape[2], input_node.shape[1]) ckpts = "./third_party/tf-openpose/models/trained/mobilenet_" + s + "/model-release" vars_in_checkpoint = tf.train.list_variables(ckpts) var_rest = [] for el in vars_in_checkpoint: var_rest.append(el[0]) variables = tf.contrib.slim.get_variables_to_restore() var_list = [v for v in variables if v.name.split(":")[0] in var_rest] loader = tf.train.Saver(var_list=var_list) loader.restore(sess, ckpts) first_time = False # Compute and draw the pose: vec = sess.run(net.get_output(name="concat_stage7"), feed_dict={"image:0": [image]}) pafMat, heatMat = sess.run([ net.get_output(name=last_layer.format(stage=stage_level, aux=1)), net.get_output(name=last_layer.format(stage=stage_level, aux=2)) ], feed_dict={"image:0": [image]}) heatMat, pafMat = heatMat[0], pafMat[0] humans = estimate_pose(heatMat, pafMat) pose_image = np.zeros(tuple(image.shape), dtype=np.uint8) pose_image = draw_humans(pose_image, humans) # Extract argmax along axis 2: heatMat = np.amax(heatMat, axis=2) pafMat = np.amax(pafMat, axis=2) # Resize input_image, heatMat, pafMat to match pose_image: heatMat = cv2.resize(heatMat, dsize=(input_height, input_width), interpolation=cv2.INTER_CUBIC) pafMat = cv2.resize(pafMat, dsize=(input_height, input_width), interpolation=cv2.INTER_CUBIC) # Make sure there is a third dimension: heatMat = np.expand_dims(heatMat, axis=2) pafMat = np.expand_dims(pafMat, axis=2) # Concatenate input_image, pose_image, heatMat and pafMat: pose_image = np.concatenate((image, pose_image, heatMat, pafMat), axis=2) return pose_image
def test_cmu(self): inpmat = np.load('./tests/person3.pickle') heatmat = np.load('./tests/cmu_person3_heatmat.pickle') pafmat = np.load('./tests/cmu_person3_pafmat.pickle') t = time.time() humans = common.estimate_pose(heatmat, pafmat) elapsed = time.time() - t logging.info('[test_mobilenet] elapsed=%f' % elapsed)
def get_prediction(img): preprocessed = preprocess(img, input_width, input_height) pafMat, heatMat = sess.run([ net.get_output(name=last_layer.format(stage=stage_level, aux=1)), net.get_output(name=last_layer.format(stage=stage_level, aux=2)) ], feed_dict={'image:0': [preprocessed]}) heatMat, pafMat = heatMat[0], pafMat[0] humans = estimate_pose(heatMat, pafMat) return humans
def test_mobilenet(self): inpmat = np.load('./tests/person3.pickle') heatmat = np.load('./tests/mobilenet_person3_heatmat.pickle') pafmat = np.load('./tests/mobilenet_person3_pafmat.pickle') t = time.time() humans = common.estimate_pose(heatmat, pafmat) elapsed = time.time() - t logging.info('[test_mobilenet] elapsed=%f' % elapsed) self._show('./images/person3.jpg', inpmat, heatmat, pafmat, humans)
def run(image): pafMat, heatMat = sess.run([ net.get_output(name=last_layer.format(stage=args.stage_level, aux=1)), net.get_output(name=last_layer.format(stage=args.stage_level, aux=2)) ], feed_dict={'image:0': [image]}) heatMat, pafMat = heatMat[0], pafMat[0] humans = estimate_pose(heatMat, pafMat) joints = get_joints(image, humans) return joints
def run(image, show=False, trg_len=0): pafMat, heatMat = sess.run([ net.get_output(name=last_layer.format(stage=args.stage_level, aux=1)), net.get_output(name=last_layer.format(stage=args.stage_level, aux=2)) ], feed_dict={'image:0': [image]}) heatMat, pafMat = heatMat[0], pafMat[0] humans = estimate_pose(heatMat, pafMat) joints = get_joints(image, humans) if show and len(joints) == trg_len: display(image, humans, heatMat, pafMat) return joints
def getHumans(self, img): preprocessed = preprocess(img, self.imgW, self.imgH) run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() pafMat, heatMat = self.sess.run([ self.net.get_output( name=self.last_layer.format(stage=self.stage_level, aux=1)), self.net.get_output( name=self.last_layer.format(stage=self.stage_level, aux=2)) ], feed_dict={'image:0': [img]}, options=run_options, run_metadata=run_metadata) heatMat, pafMat = heatMat[0], pafMat[0] humans = estimate_pose(heatMat, pafMat) return humans
def infer(self, image, upper_body, lower_body): start = time.time() imgpath = self._download_image(image) image, thumbnail, input_width, input_height = self.read_img( imgpath, 368, 368) start = print_time( "image (" + str(input_width) + "x" + str(input_height) + ") loaded in: ", start) if not SmartBodyCrop.initialized: print("Loading the model...") self.load_graph_def() with tf.Session() as sess: inputs = tf.get_default_graph().get_tensor_by_name('inputs:0') heatmaps_tensor = tf.get_default_graph().get_tensor_by_name( 'Mconv7_stage6_L2/BiasAdd:0') pafs_tensor = tf.get_default_graph().get_tensor_by_name( 'Mconv7_stage6_L1/BiasAdd:0') heatMat, pafMat = sess.run([heatmaps_tensor, pafs_tensor], feed_dict={inputs: image}) start = print_time("tf session executed in: ", start) humans = estimate_pose(heatMat[0], pafMat[0]) start = print_time("pose estimated in: ", start) # send the thumbnail to render an initial crop img, crop_position, crop_size = crop_image(thumbnail, humans, upper_body, lower_body) # scale back the crop_coordinates to match the original picture size scale_factor_w = input_width / thumbnail.size[0] scale_factor_h = input_height / thumbnail.size[1] crop_coordinates = { 'x': crop_position[0] * scale_factor_w, 'y': crop_position[1] * scale_factor_h, 'width': crop_size[0] * scale_factor_w, 'height': crop_size[1] * scale_factor_h } start = print_time("image cropped in: ", start) sess.close() return img, crop_coordinates, imgpath
def compute_pose_frame(input_image, sess): image = cv2.resize(input_image, dsize = (input_height, input_width), interpolation = cv2.INTER_CUBIC) global first_time if first_time: print(first_time) s = '%dx%d' % (input_node.shape[2], input_node.shape[1]) ckpts = './openpose/models/trained/mobilenet_' + s + '/model-release' vars_in_checkpoint = tf.train.list_variables(ckpts) var_rest = [] for el in vars_in_checkpoint: var_rest.append(el[0]) variables = tf.contrib.slim.get_variables_to_restore() var_list = [v for v in variables if v.name.split(':')[0] in var_rest] loader = tf.train.Saver(var_list = var_list) loader.restore(sess, ckpts) first_time = False vec = sess.run(net.get_output(name = 'concat_stage7'), feed_dict = {'image:0': [image]}) run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.runMetadata() pafMat, heatMat, sess.run([ net.get_output(name=last_layer.format(stage=stage_level, aux=1)), net.get_output(name=last_layer.format(stage=stage_level, aux=2)) ], feed_dict = {'image:0': [image]}, options = run_options, run_metadata = run_metadata) tl=timeline.Timeline(run_metadata.step_stats) ctf = tl.generate_chrome_trace_format() heatMat, pafMat = heatMat[0], pafMat[0] humans = estimate_pose(heatMat, pafMat) pose_image = np.zeros(tuple(image.shape), dtype = np.uint8) pose_image = draw_humans(pose_image,humans) return pose_image
def infer(self, image, upper_body, lower_body): start = time.time() imgpath = self._download_image(image) img1 = Image.open(imgpath) img1 = np.asarray(img1) input_width, input_height = img1.shape[0], img1.shape[1] image = self.read_img(imgpath, input_width, input_height) start = print_time("image loaded in: ", start) if not SmartBodyCrop.initialized: print("Loading the model...") self.load_graph_def() with tf.Session() as sess: inputs = tf.get_default_graph().get_tensor_by_name('inputs:0') heatmaps_tensor = tf.get_default_graph().get_tensor_by_name( 'Mconv7_stage6_L2/BiasAdd:0') pafs_tensor = tf.get_default_graph().get_tensor_by_name( 'Mconv7_stage6_L1/BiasAdd:0') heatMat, pafMat = sess.run([heatmaps_tensor, pafs_tensor], feed_dict={inputs: image}) start = print_time("tf session executed in: ", start) humans = estimate_pose(heatMat[0], pafMat[0]) start = print_time("pose estimated in: ", start) img, crop_coordinates = crop_image(imgpath, humans, upper_body, lower_body) start = print_time("image cropped in: ", start) sess.close() return img, crop_coordinates
# Read current image img_path = os.path.join(cam_path, img_name) image, IMG_WIDTH, IMG_HEIGHT = read_imgfile( img_path, args.scale_factor) # Run OpenPose-net on current image heatMat, pafMat, baseFeatMat = \ sess.run([heatmaps_tensor, pafs_tensor, base_feat_tensor], feed_dict={inputs: image}) heatMat = heatMat[0] pafMat = pafMat[0] baseFeatMat = baseFeatMat[0] # Compute 2d pose based on OpenPose-net output humans = estimate_pose(heatMat, pafMat) # Go from [0,1]-normalized joint coordinates to instead # match image size humans_formatted = [] for i in range(len(humans)): human = humans[i] human_formatted = [] for key, val in human.items(): hum = list(human[key]) # human[key] = list(human[key]) hum[1] = list(human[key][1]) hum[1][0] *= IMG_WIDTH hum[1][1] *= IMG_HEIGHT tmp = { 'id': hum[0],
with tf.Session() as sess: for imgpath in glob.glob(img_path_raw + "*.jpg"): basename = os.path.basename(imgpath) outpath = img_path_final + basename if os.path.exists(outpath): # in case of crash to not restart from 0 print("Skipping image \t {}".format(basename)) continue else: print("Processing image \t {}".format(basename)) image = read_imgfile(imgpath, img_width, img_height) heatMat, pafMat = sess.run([heatmaps_tensor, pafs_tensor], feed_dict={inputs: image}) humans = estimate_pose(heatMat[0], pafMat[0]) # display image = cv2.imread(imgpath) image_h, image_w = image.shape[:2] image = draw_humans(image, humans) scale = 480.0 / image_h newh, neww = 480, int(scale * image_w + 0.5) image = cv2.resize(image, (neww, newh), interpolation=cv2.INTER_AREA) cv2.imwrite(outpath, image) gc.collect()
def inference(imgpath, j): # input_width = 656 # input_height = 368 input_width = 352 input_height = 288 t0 = time.time() tf.compat.v1.reset_default_graph() from tensorflow.core.framework import graph_pb2 graph_def = graph_pb2.GraphDef() # Download model from https://www.dropbox.com/s/2dw1oz9l9hi9avg/optimized_openpose.pb with open('models/optimized_openpose.pb', 'rb') as f: graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='') t1 = time.time() # print(t1 - t0) inputs = tf.compat.v1.get_default_graph().get_tensor_by_name('inputs:0') heatmaps_tensor = tf.compat.v1.get_default_graph().get_tensor_by_name( 'Mconv7_stage6_L2/BiasAdd:0') pafs_tensor = tf.compat.v1.get_default_graph().get_tensor_by_name( 'Mconv7_stage6_L1/BiasAdd:0') t2 = time.time() # print(t2 - t1) image = read_imgfile(imgpath, input_width, input_height) t3 = time.time() # print(t3 - t2) with tf.compat.v1.Session() as sess: heatMat, pafMat = sess.run([heatmaps_tensor, pafs_tensor], feed_dict={inputs: image}) t4 = time.time() # print(t4 - t3) heatMat, pafMat = heatMat[0], pafMat[0] humans = estimate_pose(heatMat, pafMat) # coordinates for left hand, head, right hand etc. indexes: 0,2,3,4,5,6,7 head_coco_idx = 0 right_shoulder_idx = 2 right_elbow_idx = 3 right_hand_idx = 4 left_shoulder_idx = 5 left_elbow_idx = 6 left_hand_idx = 7 for human in humans: if human[right_hand_idx] is None: break head_coords = human[head_coco_idx][1] right_shoulder_coords = human[right_shoulder_idx][1] right_elbow_coords = human[right_elbow_idx][1] right_hand_coords = human[right_hand_idx][1] left_shoulder_coords = human[left_shoulder_idx][1] left_elbow_coords = human[left_elbow_idx][1] left_hand_coords = human[left_hand_idx][1] fields = [ head_coords, right_shoulder_coords, right_elbow_coords, right_hand_coords, left_shoulder_coords, left_elbow_coords, left_hand_coords ] # with open(r'test.csv', 'a') as f: # writer = csv.writer(f) # writer.writerow(fields) # end of printing # display image = cv2.imread(imgpath) image_h, image_w = image.shape[:2] image = draw_humans(image, humans) scale = 480.0 / image_h newh, neww = 480, int(scale * image_w + 0.5) image = cv2.resize(image, (neww, newh), interpolation=cv2.INTER_AREA) ### Uncomment below to show image with skeleton # cv2.imshow('result', image) ##### save image with coordinates cv2.imwrite('./Outputs/openpos/result_' + str(j) + '.png', image) t5 = time.time() # print(t5 - t4) cv2.waitKey(0) return fields
ret, img = cap.read() if ret: image = read_imgfile(img, args.input_width, args.input_height, web_cam=True) backbone_feature1 = sess1.run(outputs1, feed_dict={inputs1: image}) heatMat1, pafMat1 = sess1.run( [heatmaps_tensor1, pafs_tensor1], feed_dict={outputs1: backbone_feature1}) heatMat1, pafMat1 = heatMat1[0], pafMat1[0] humans1 = estimate_pose(heatMat1, pafMat1) backbone_feature2 = sess2.run(outputs2, feed_dict={inputs2: image}) heatMat2, pafMat2 = sess2.run( [heatmaps_tensor2, pafs_tensor2], feed_dict={outputs2: backbone_feature2}) heatMat2, pafMat2 = heatMat2[0], pafMat2[0] humans2 = estimate_pose(heatMat2, pafMat2) # display image = img image_h, image_w = image.shape[:2] image1 = draw_humans(image, humans1) image2 = draw_humans(image, humans2)