Skip to content

Instantly share code, notes, and snippets.

@joelselvaraj
Last active February 21, 2023 18:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelselvaraj/5fffac1b144dff53f73200fd2eed7889 to your computer and use it in GitHub Desktop.
Save joelselvaraj/5fffac1b144dff53f73200fd2eed7889 to your computer and use it in GitHub Desktop.
import sys
import cv2
import os
from sys import platform
import argparse
import numpy as np
import json
try:
sys.path.append('/usr/local/python')
from openpose import pyopenpose as op
except ImportError as e:
print('Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
raise e
params = dict()
params["model_folder"] = "/home/joel/openpose/models/"
params["disable_blending"] = True #for black background
params["display"] = 0
params["write_json"] = "/home/joel/jsonresults/" #json output folder for the video
try:
# Starting OpenPose
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()
# Process Image
datum = op.Datum()
cap = cv2.VideoCapture("/input/video/path.avi")
fps = cap.get(cv2.CAP_PROP_FPS)
video=None
count=0 #PREFIX FOR JSON output file
while (cap.isOpened()):
count=count+1
hasframe, frame= cap.read()
if hasframe== True:
datum.cvInputData = frame
datum.name=str(count) #for each frame the count is prefixed for the output json file name. This way we get individual file for each frame.
opWrapper.emplaceAndPop([datum])
opframe=datum.cvOutputData
height, width, layers = opframe.shape
if video == None:
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
video = cv2.VideoWriter(outputfilepath, fourcc, fps, (width, height))
video.write(opframe)
else:
break
cv2.destroyAllWindows()
video.release()
except Exception as e:
print(e)
sys.exit(-1)
print("COMPLETED!!!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment