|
|
@@ -1,6 +1,77 @@
|
|
|
-class BaseVideoAnalyzer:
|
|
|
- def __init__(self):
|
|
|
- pass
|
|
|
|
|
|
- def analyze(self, filename):
|
|
|
+import os
|
|
|
+from typing import Callable
|
|
|
+from config import UPLOAD_DIR
|
|
|
+import cv2
|
|
|
+import logging
|
|
|
+import numpy as np
|
|
|
+
|
|
|
+
|
|
|
+def split_video(video_uuid):
|
|
|
+ video_path = UPLOAD_DIR + video_uuid + ".mp4"
|
|
|
+ frames_dir = UPLOAD_DIR + "frames/" + video_uuid + "/"
|
|
|
+
|
|
|
+ if os.path.exists(frames_dir): # Already split
|
|
|
return
|
|
|
+
|
|
|
+ os.makedirs(frames_dir)
|
|
|
+
|
|
|
+ cap = cv2.VideoCapture(video_path)
|
|
|
+ FPS = cap.get(5) # 5 means fps
|
|
|
+ logging.debug("Splitting video %s, FPS: %s" % (video_path, FPS))
|
|
|
+
|
|
|
+ c = 1
|
|
|
+ sampling_fps = 1
|
|
|
+
|
|
|
+ while True:
|
|
|
+ ret, frame = cap.read()
|
|
|
+ if ret:
|
|
|
+ if c % sampling_fps == 0:
|
|
|
+ target_path = frames_dir + str(c) + ".jpg"
|
|
|
+ cv2.imwrite(target_path, frame)
|
|
|
+ logging.debug("Writing frame %s, %s" % (c, target_path))
|
|
|
+ c += 1
|
|
|
+ else:
|
|
|
+ break
|
|
|
+
|
|
|
+ cap.release()
|
|
|
+
|
|
|
+
|
|
|
+from service.aiutil.torch_openpose import torch_openpose
|
|
|
+from service.aiutil.util import draw_bodypose
|
|
|
+
|
|
|
+
|
|
|
+def run_openpose_for_frames(video_uuid):
|
|
|
+ tp = torch_openpose('body_25')
|
|
|
+
|
|
|
+ frames_dir = UPLOAD_DIR + "frames/" + video_uuid + "/"
|
|
|
+ for i in os.listdir(frames_dir):
|
|
|
+ if i.endswith(".jpg") and "result" not in i:
|
|
|
+
|
|
|
+ logging.debug("Running openpose for %s %s" % (video_uuid, i))
|
|
|
+
|
|
|
+ oriImg = cv2.imread(frames_dir + i)
|
|
|
+ poses = tp(oriImg)
|
|
|
+ np.save(frames_dir + i + ".npy", poses)
|
|
|
+
|
|
|
+ canvas = draw_bodypose(oriImg, poses, 'body_25')
|
|
|
+
|
|
|
+ cv2.imwrite(frames_dir + i.replace(".jpg", ".result.jpg"), canvas)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+class BaseVideoAnalyzer:
|
|
|
+ def __init__(self, video_uuid):
|
|
|
+ self.video_uuid = video_uuid
|
|
|
+
|
|
|
+ def analyze(self, callback: Callable):
|
|
|
+ try:
|
|
|
+ callback("RUNNING")
|
|
|
+ split_video(self.video_uuid)
|
|
|
+ run_openpose_for_frames(self.video_uuid)
|
|
|
+ callback("FINISHED", result={})
|
|
|
+ except Exception as e:
|
|
|
+ callback("ERROR", error=e)
|
|
|
+ raise e
|
|
|
+
|
|
|
+
|