OpenPoseExample.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #if !UNITY_WSA_10_0
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using OpenCVForUnity.CoreModule;
  8. using OpenCVForUnity.ImgcodecsModule;
  9. using OpenCVForUnity.DnnModule;
  10. using OpenCVForUnity.ImgprocModule;
  11. using OpenCVForUnity.UnityUtils;
  12. namespace OpenCVForUnityExample
  13. {
  14. /// <summary>
  15. /// OpenPose Example
  16. /// This example uses OpenPose human pose estimation network.
  17. /// Referring to https://github.com/opencv/opencv/blob/master/samples/dnn/openpose.py.
  18. /// </summary>
  19. public class OpenPoseExample : MonoBehaviour
  20. {
  21. const float inWidth = 368;
  22. const float inHeight = 368;
  23. //COCO
  24. // Dictionary<string, int> BODY_PARTS
  25. // = new Dictionary<string, int> () {
  26. // { "Nose", 0 }, { "Neck", 1 }, { "RShoulder", 2 }, { "RElbow", 3 }, {
  27. // "RWrist",
  28. // 4
  29. // },
  30. // { "LShoulder",5 }, { "LElbow", 6 }, { "LWrist", 7 }, { "RHip", 8 }, {
  31. // "RKnee",
  32. // 9
  33. // },
  34. // { "RAnkle", 10 }, { "LHip", 11 }, { "LKnee", 12 }, { "LAnkle", 13 }, {
  35. // "REye",
  36. // 14
  37. // },
  38. // { "LEye", 15 }, { "REar", 16 }, { "LEar", 17 }, {
  39. // "Background",
  40. // 18
  41. // }
  42. // };
  43. //
  44. // string[,] POSE_PAIRS
  45. // = new string[,] {
  46. // { "Neck", "RShoulder" }, { "Neck", "LShoulder" }, {
  47. // "RShoulder",
  48. // "RElbow"
  49. // },
  50. // { "RElbow", "RWrist" }, { "LShoulder", "LElbow" }, {
  51. // "LElbow",
  52. // "LWrist"
  53. // },
  54. // { "Neck", "RHip" }, { "RHip", "RKnee" }, { "RKnee", "RAnkle" }, {
  55. // "Neck",
  56. // "LHip"
  57. // },
  58. // { "LHip", "LKnee" }, { "LKnee", "LAnkle" }, { "Neck", "Nose" }, {
  59. // "Nose",
  60. // "REye"
  61. // },
  62. // { "REye", "REar" }, { "Nose", "LEye" }, { "LEye", "LEar" }
  63. // };
  64. //MPI
  65. Dictionary<string, int> BODY_PARTS
  66. = new Dictionary<string, int> () { { "Head", 0 }, { "Neck", 1 }, {
  67. "RShoulder",
  68. 2
  69. }, {
  70. "RElbow",
  71. 3
  72. }, {
  73. "RWrist",
  74. 4
  75. },
  76. { "LShoulder", 5 }, { "LElbow", 6 }, { "LWrist", 7 }, { "RHip", 8 }, {
  77. "RKnee",
  78. 9
  79. },
  80. { "RAnkle", 10 }, { "LHip", 11 }, { "LKnee", 12 }, { "LAnkle", 13 }, {
  81. "Chest",
  82. 14
  83. },
  84. { "Background", 15 }
  85. };
  86. string[,] POSE_PAIRS = new string[,] {
  87. { "Head", "Neck" }, {
  88. "Neck",
  89. "RShoulder"
  90. }, {
  91. "RShoulder",
  92. "RElbow"
  93. },
  94. { "RElbow", "RWrist" },
  95. { "Neck", "LShoulder" }, {
  96. "LShoulder",
  97. "LElbow"
  98. },
  99. { "LElbow", "LWrist" },
  100. { "Neck", "Chest" },
  101. { "Chest", "RHip" }, {
  102. "RHip",
  103. "RKnee"
  104. },
  105. { "RKnee", "RAnkle" },
  106. { "Chest", "LHip" },
  107. { "LHip", "LKnee" }, {
  108. "LKnee",
  109. "LAnkle"
  110. }
  111. };
  112. string COCO_val2014_000000000589_jpg_filepath;
  113. string pose_iter_160000_caffemodel_filepath;
  114. string openpose_pose_mpi_faster_4_stages_prototxt_filepath;
  115. #if UNITY_WEBGL && !UNITY_EDITOR
  116. IEnumerator getFilePath_Coroutine;
  117. #endif
  118. // Use this for initialization
  119. void Start ()
  120. {
  121. #if UNITY_WEBGL && !UNITY_EDITOR
  122. getFilePath_Coroutine = GetFilePath ();
  123. StartCoroutine (getFilePath_Coroutine);
  124. #else
  125. COCO_val2014_000000000589_jpg_filepath = Utils.getFilePath ("dnn/COCO_val2014_000000000589.jpg");
  126. pose_iter_160000_caffemodel_filepath = Utils.getFilePath ("dnn/pose_iter_160000.caffemodel");
  127. openpose_pose_mpi_faster_4_stages_prototxt_filepath = Utils.getFilePath ("dnn/openpose_pose_mpi_faster_4_stages.prototxt");
  128. Run ();
  129. #endif
  130. }
  131. #if UNITY_WEBGL && !UNITY_EDITOR
  132. private IEnumerator GetFilePath ()
  133. {
  134. var getFilePathAsync_0_Coroutine = Utils.getFilePathAsync ("dnn/COCO_val2014_000000000589.jpg", (result) => {
  135. COCO_val2014_000000000589_jpg_filepath = result;
  136. });
  137. yield return getFilePathAsync_0_Coroutine;
  138. var getFilePathAsync_1_Coroutine = Utils.getFilePathAsync ("dnn/pose_iter_160000.caffemodel", (result) => {
  139. pose_iter_160000_caffemodel_filepath = result;
  140. });
  141. yield return getFilePathAsync_1_Coroutine;
  142. var getFilePathAsync_2_Coroutine = Utils.getFilePathAsync ("dnn/openpose_pose_mpi_faster_4_stages.prototxt", (result) => {
  143. openpose_pose_mpi_faster_4_stages_prototxt_filepath = result;
  144. });
  145. yield return getFilePathAsync_2_Coroutine;
  146. getFilePath_Coroutine = null;
  147. Run ();
  148. }
  149. #endif
  150. // Use this for initialization
  151. void Run ()
  152. {
  153. //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
  154. Utils.setDebugMode (true);
  155. Mat img = Imgcodecs.imread (COCO_val2014_000000000589_jpg_filepath);
  156. #if !UNITY_WSA_10_0
  157. if (img.empty ()) {
  158. Debug.LogError ("dnn/COCO_val2014_000000000589.jpg is not loaded.The image file can be downloaded here: \"https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/examples/media/COCO_val2014_000000000589.jpg\" folder. ");
  159. img = new Mat (368, 368, CvType.CV_8UC3, new Scalar (0, 0, 0));
  160. }
  161. #endif
  162. //Adust Quad.transform.localScale.
  163. gameObject.transform.localScale = new Vector3 (img.width (), img.height (), 1);
  164. Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
  165. float imageWidth = img.width ();
  166. float imageHeight = img.height ();
  167. float widthScale = (float)Screen.width / imageWidth;
  168. float heightScale = (float)Screen.height / imageHeight;
  169. if (widthScale < heightScale) {
  170. Camera.main.orthographicSize = (imageWidth * (float)Screen.height / (float)Screen.width) / 2;
  171. } else {
  172. Camera.main.orthographicSize = imageHeight / 2;
  173. }
  174. Net net = null;
  175. if (string.IsNullOrEmpty (pose_iter_160000_caffemodel_filepath) || string.IsNullOrEmpty (openpose_pose_mpi_faster_4_stages_prototxt_filepath)) {
  176. Debug.LogError ("model file is not loaded. The model and prototxt file can be downloaded here: \"http://posefs1.perception.cs.cmu.edu/OpenPose/models/pose/mpi/pose_iter_160000.caffemodel\",\"https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/openpose_pose_mpi_faster_4_stages.prototxt\". Please copy to “Assets/StreamingAssets/dnn/” folder. ");
  177. } else {
  178. net = Dnn.readNetFromCaffe (openpose_pose_mpi_faster_4_stages_prototxt_filepath, pose_iter_160000_caffemodel_filepath);
  179. //Intel's Deep Learning Inference Engine backend is supported on Windows 64bit platform only. Please refer to ReadMe.pdf for the setup procedure.
  180. //net.setPreferableBackend (Dnn.DNN_BACKEND_INFERENCE_ENGINE);
  181. }
  182. if (net == null) {
  183. Imgproc.putText (img, "model file is not loaded.", new Point (5, img.rows () - 30), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar (255, 255, 255), 2, Imgproc.LINE_AA, false);
  184. Imgproc.putText (img, "Please read console message.", new Point (5, img.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar (255, 255, 255), 2, Imgproc.LINE_AA, false);
  185. } else {
  186. float frameWidth = img.cols ();
  187. float frameHeight = img.rows ();
  188. Mat input = Dnn.blobFromImage (img, 1.0 / 255, new Size (inWidth, inHeight), new Scalar (0, 0, 0), false, false);
  189. net.setInput (input);
  190. // TickMeter tm = new TickMeter ();
  191. // tm.start ();
  192. Mat output = net.forward ();
  193. // tm.stop ();
  194. // Debug.Log ("Inference time, ms: " + tm.getTimeMilli ());
  195. output = output.reshape (1, 16);
  196. float[] data = new float[46 * 46];
  197. List<Point> points = new List<Point> ();
  198. for (int i = 0; i < BODY_PARTS.Count; i++) {
  199. output.get (i, 0, data);
  200. Mat heatMap = new Mat (1, data.Length, CvType.CV_32FC1);
  201. heatMap.put (0, 0, data);
  202. //Originally, we try to find all the local maximums. To simplify a sample
  203. //we just find a global one. However only a single pose at the same time
  204. //could be detected this way.
  205. Core.MinMaxLocResult result = Core.minMaxLoc (heatMap);
  206. heatMap.Dispose ();
  207. double x = (frameWidth * (result.maxLoc.x % 46)) / 46;
  208. double y = (frameHeight * (result.maxLoc.x / 46)) / 46;
  209. if (result.maxVal > 0.1) {
  210. points.Add (new Point (x, y));
  211. } else {
  212. points.Add (null);
  213. }
  214. }
  215. for (int i = 0; i < POSE_PAIRS.GetLength (0); i++) {
  216. string partFrom = POSE_PAIRS [i, 0];
  217. string partTo = POSE_PAIRS [i, 1];
  218. int idFrom = BODY_PARTS [partFrom];
  219. int idTo = BODY_PARTS [partTo];
  220. if (points [idFrom] != null && points [idTo] != null) {
  221. Imgproc.line (img, points [idFrom], points [idTo], new Scalar (0, 255, 0), 3);
  222. Imgproc.ellipse (img, points [idFrom], new Size (3, 3), 0, 0, 360, new Scalar (0, 0, 255), Core.FILLED);
  223. Imgproc.ellipse (img, points [idTo], new Size (3, 3), 0, 0, 360, new Scalar (0, 0, 255), Core.FILLED);
  224. }
  225. }
  226. MatOfDouble timings = new MatOfDouble ();
  227. long t = net.getPerfProfile (timings);
  228. Debug.Log ("t: " + t);
  229. Debug.Log ("timings.dump(): " + timings.dump ());
  230. double freq = Core.getTickFrequency () / 1000;
  231. Debug.Log ("freq: " + freq);
  232. Imgproc.putText (img, (t / freq) + "ms", new Point (10, img.height () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.6, new Scalar (0, 0, 255), 2);
  233. }
  234. Imgproc.cvtColor (img, img, Imgproc.COLOR_BGR2RGB);
  235. Texture2D texture = new Texture2D (img.cols (), img.rows (), TextureFormat.RGBA32, false);
  236. Utils.matToTexture2D (img, texture);
  237. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  238. Utils.setDebugMode (false);
  239. }
  240. // Update is called once per frame
  241. void Update ()
  242. {
  243. }
  244. /// <summary>
  245. /// Raises the disable event.
  246. /// </summary>
  247. void OnDisable ()
  248. {
  249. #if UNITY_WEBGL && !UNITY_EDITOR
  250. if (getFilePath_Coroutine != null) {
  251. StopCoroutine (getFilePath_Coroutine);
  252. ((IDisposable)getFilePath_Coroutine).Dispose ();
  253. }
  254. #endif
  255. }
  256. /// <summary>
  257. /// Raises the back button click event.
  258. /// </summary>
  259. public void OnBackButtonClick ()
  260. {
  261. SceneManager.LoadScene ("OpenCVForUnityExample");
  262. }
  263. }
  264. }
  265. #endif