api.nrengine.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. class NREngine {
  3. /**
  4. * HTTP API abstraction layer placeholder
  5. *
  6. * @var object
  7. */
  8. protected $api = '';
  9. /**
  10. * Contains alter config as key=>value
  11. *
  12. * @var array
  13. */
  14. protected $altCfg = array();
  15. /**
  16. * Contains base recognition service URL
  17. *
  18. * @var string
  19. */
  20. protected $baseApiUrl = '';
  21. /**
  22. * Contains recognition service engine
  23. *
  24. * @var string
  25. */
  26. protected $engine = 'pytorch';
  27. /**
  28. * Object recognition confidence
  29. *
  30. * @var int
  31. */
  32. protected $confidence = 40;
  33. /**
  34. * some predefined stuff
  35. */
  36. const ROUTE_DETECT = '/detect';
  37. const ROUTE_IMAGE = '/image';
  38. const ROUTE_STREAM = '/stream';
  39. const ROUTE_DETECTORS = '/detectors';
  40. public function __construct() {
  41. $this->loadConfigs();
  42. $this->initApi();
  43. }
  44. /**
  45. * Loads some required configs
  46. *
  47. * @global object $ubillingConfig
  48. *
  49. * @return void
  50. */
  51. protected function loadConfigs() {
  52. global $ubillingConfig;
  53. $this->altCfg = $ubillingConfig->getAlter();
  54. }
  55. /**
  56. * Inits omae http abstraction layer
  57. *
  58. * @return void
  59. */
  60. protected function initApi() {
  61. if ($this->altCfg['NEURAL_ENABLED'] AND $this->altCfg['NEURAL_API_URL']) {
  62. $this->api = new OmaeUrl();
  63. $this->baseApiUrl = $this->altCfg['NEURAL_API_URL'];
  64. }
  65. }
  66. /**
  67. * Makes request body as JSON
  68. *
  69. * @param string $url
  70. *
  71. * @return string
  72. */
  73. protected function getRequest($url) {
  74. $result = array();
  75. $result['id'] = 'wr';
  76. $result['detector_name'] = $this->engine;
  77. $result['preprocess'] = array();
  78. $result['detect'] = array('*' => $this->confidence);
  79. $regions = array();
  80. $regions[] = array(
  81. 'top' => 0.1,
  82. 'left' => 0.1,
  83. 'bottom' => 0.9,
  84. 'right' => 0.9,
  85. 'detect' => array('*' => $this->confidence),
  86. 'covers' => false
  87. );
  88. $result = array(
  89. 'regions' => $regions,
  90. 'data' => $url
  91. );
  92. $result = json_encode($result);
  93. return($result);
  94. }
  95. /**
  96. * Performs recognition request for some endpoint
  97. *
  98. * @param string $url
  99. * @param string $endpoint
  100. *
  101. * @return string
  102. */
  103. protected function requestDetection($url, $endpoint) {
  104. $requestJson = $this->getRequest($url);
  105. $this->api->dataPostRaw($requestJson);
  106. $this->api->dataHeader('Content-Type', 'application/json;charset=UTF-8');
  107. $requestUrl = $this->baseApiUrl . $endpoint;
  108. $result = $this->api->response($requestUrl);
  109. return($result);
  110. }
  111. /**
  112. * Performs recognition request for video stream
  113. *
  114. * @param string $url
  115. *
  116. * @return string
  117. */
  118. protected function requestStreamDetection($url) {
  119. $requestJson = $this->getRequest($url);
  120. //$this->api->dataHeader('Content-Type', 'application/json;charset=UTF-8');
  121. $requestUrl = $this->baseApiUrl . self::ROUTE_STREAM . '?detect_request=' . urlencode($requestJson);
  122. $result = $this->api->response($requestUrl);
  123. return($result);
  124. }
  125. /**
  126. * Returns available detectors
  127. *
  128. * @return array
  129. */
  130. public function getDetectors() {
  131. $result = array();
  132. $rawResult = $this->api->response($this->baseApiUrl . self::ROUTE_DETECTORS);
  133. if (!empty($rawResult)) {
  134. $result = @json_decode($rawResult, true);
  135. }
  136. return($result);
  137. }
  138. /**
  139. * Check is neural detection service alive or not
  140. *
  141. * @return bool
  142. */
  143. public function isAlive() {
  144. $detectors = $this->getDetectors();
  145. $result = false;
  146. if (is_array($detectors)) {
  147. if (isset($detectors['detectors'])) {
  148. $result = true;
  149. }
  150. }
  151. return($result);
  152. }
  153. /**
  154. * Returns marked objects as JPEG image body
  155. *
  156. * @param string $url
  157. *
  158. * @return string
  159. */
  160. public function detectImage($url) {
  161. $result = $this->requestDetection($url, self::ROUTE_IMAGE);
  162. return($result);
  163. }
  164. /**
  165. * Returns detected objects as array
  166. *
  167. * @param string $url
  168. *
  169. * @return array
  170. */
  171. public function detectObjects($url) {
  172. $result = array();
  173. $result = $this->requestDetection($url, self::ROUTE_DETECT);
  174. if (!empty($result)) {
  175. $result = json_decode($result, true);
  176. }
  177. return($result);
  178. }
  179. /**
  180. * Returns mjpeg detection stream
  181. *
  182. * @param string $url
  183. *
  184. * @return stream
  185. */
  186. public function detectStream($url) {
  187. $result = $this->requestStreamDetection($url);
  188. return($result);
  189. }
  190. }