hzt_asc 1.0.9 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/ask.cc +70 -25
  2. package/package.json +1 -1
  3. package/sample/demo.js +19 -3
package/ask.cc CHANGED
@@ -152,6 +152,10 @@ Napi::Value ASK::faceDetectUrl(const Napi::CallbackInfo& info) {
152
152
  string filePath = info[0].As<Napi::String>().ToString();
153
153
  int Width, Height;
154
154
  MUInt8* imageData = processFileUrl(filePath.c_str(), Width, Height);
155
+ if (!imageData) {
156
+ Napi::TypeError::New(env, "image download error").ThrowAsJavaScriptException();
157
+ return errorData(env, "image download error"); //Napi::Number::New(info.Env(), -1);
158
+ }
155
159
  ASVLOFFSCREEN offscreen = { 0 };
156
160
  ColorSpaceConversion(Width, Height, ASVL_PAF_NV21, imageData, offscreen);
157
161
  ASF_MultiFaceInfo detectedFaces = { 0 };
@@ -244,6 +248,10 @@ Napi::Value ASK::faceFeatureExtractUrl(const Napi::CallbackInfo& info) {
244
248
 
245
249
  int Width, Height;
246
250
  MUInt8* imageData = processFileUrl(filePath.c_str(), Width, Height);
251
+ if (!imageData) {
252
+ Napi::TypeError::New(env, "image download error").ThrowAsJavaScriptException();
253
+ return errorData(env, "image download error"); //Napi::Number::New(info.Env(), -1);
254
+ }
247
255
  ASVLOFFSCREEN offscreen = { 0 };
248
256
  ColorSpaceConversion(Width, Height, ASVL_PAF_NV21, imageData, offscreen);
249
257
 
@@ -562,6 +570,10 @@ Napi::Value ASK::ImageFaceCompareUrl2(const Napi::CallbackInfo& info) {
562
570
  string filePath = info[0].As<Napi::String>().ToString();
563
571
  int Width, Height;
564
572
  MUInt8* imageData = processFileUrl(filePath.c_str(), Width, Height);
573
+ if (!imageData) {
574
+ Napi::TypeError::New(env, "image download error").ThrowAsJavaScriptException();
575
+ return errorData(env, "image download error"); //Napi::Number::New(info.Env(), -1);
576
+ }
565
577
  ASVLOFFSCREEN offscreen = { 0 };
566
578
  ColorSpaceConversion(Width, Height, ASVL_PAF_NV21, imageData, offscreen);
567
579
 
@@ -589,6 +601,10 @@ Napi::Value ASK::ImageFaceCompareUrl(const Napi::CallbackInfo& info) {
589
601
  string filePath = info[0].As<Napi::String>().ToString();
590
602
  int Width, Height;
591
603
  MUInt8* imageData = processFileUrl(filePath.c_str(), Width, Height);
604
+ if (!imageData) {
605
+ Napi::TypeError::New(env, "image download error").ThrowAsJavaScriptException();
606
+ return errorData(env, "image download error"); //Napi::Number::New(info.Env(), -1);
607
+ }
592
608
  ASVLOFFSCREEN offscreen = { 0 };
593
609
  ColorSpaceConversion(Width, Height, ASVL_PAF_NV21, imageData, offscreen);
594
610
 
@@ -718,7 +734,7 @@ MUInt8* ASK::processFile(const char* filePath, int& picWidth, int& picHeight) {
718
734
  cout<<filePath<<endl;
719
735
  int Width = src.cols;
720
736
  int Height = src.rows;
721
- cout << "Width:" << Width << ",Height:"<<Height<<endl;
737
+ cout << "Width:" << Width << ",Height:"<<Height << " flags: "<< src.flags << " dims: " << src.dims <<endl;
722
738
  int wScore = Width % 4;
723
739
  int hScore = Height % 2;
724
740
  cv::Mat dest;
@@ -762,7 +778,7 @@ size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata)
762
778
  //function to retrieve the image as cv::Mat data type
763
779
  cv::Mat curlImg(const char *img_url, int timeout=100000)
764
780
  {
765
- cout<<img_url<<endl;
781
+ //cout<<img_url<<endl;
766
782
  vector<uchar> stream;
767
783
  CURL *curl = curl_easy_init();
768
784
  curl_easy_setopt(curl, CURLOPT_URL, img_url); //the img url
@@ -771,48 +787,77 @@ cv::Mat curlImg(const char *img_url, int timeout=100000)
771
787
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); // timeout if curl_easy hangs,
772
788
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //重定向一次
773
789
  CURLcode res = curl_easy_perform(curl); // start curl
790
+
774
791
  curl_easy_cleanup(curl); // cleanup
775
- cout<<res<<" xxxx " << stream.size() << " " << stream.max_size()<<endl;
792
+ if (res != CURLE_OK) {
793
+ cout << "curl errcode=" << res << img_url <<endl;
794
+ cv::Mat dest(0, 0, 1);
795
+ return dest;
796
+ }
797
+ cout<<res<<" xxxx " << stream.size() << img_url << stream.max_size()<<endl;
776
798
  return imdecode(stream, -1); // 'keep-as-is'
777
799
  }
778
800
 
801
+ void modifyImageFormat(cv::Mat& out, cv::Mat& in, int channel)
802
+ {
803
+ if (channel == 4) {
804
+ in.convertTo(out, CV_8UC4);
805
+ } else if (channel == 3) {
806
+ in.convertTo(out, CV_8UC3);
807
+ } else if (channel == 2) {
808
+ in.convertTo(out, CV_8UC2);
809
+ } else {
810
+ in.convertTo(out, CV_8U);
811
+ }
812
+ }
813
+
779
814
  MUInt8* ASK::processFileUrl(const char* url, int& picWidth, int& picHeight) {
780
815
  cv::Mat src = curlImg(url);
781
816
  int Width = src.cols;
817
+ if (Width == 0) {
818
+ src.release();
819
+ return NULL;
820
+ }
782
821
  int Height = src.rows;
783
- //const int Depth = src.depth;
784
- cout << "Width:" << Width << ",Height:"<<Height << " " <<endl;
822
+ int depth = src.depth();
823
+ int channel = src.channels();
824
+ cout << "Width:" << Width << ",Height:"<<Height <<endl;
825
+ cout << "dims: " << src.dims << endl;
826
+ cout << "channels: " << channel << endl;
827
+ cout << "depth: " << depth << endl;
828
+ //src.reshape(channels, rows)
829
+ //src.assignTo(mid, depth)
785
830
  int wScore = Width % 4;
786
831
  int hScore = Height % 2;
787
832
  cv::Mat dest;
788
833
  if (wScore != 0 || hScore != 0) {
834
+ //cout << "processFileUrl ou:" <<endl;
789
835
  Width -= wScore;
790
836
  Height -= hScore;
791
837
  cv::Mat dst;
792
838
  cv::resize(src, dst, cv::Size(Width, Height));
793
- src.release();
794
-
795
- cv::Mat mid;
796
- dst.convertTo(mid, CV_8UC3);
797
- cvtColor(mid, dest, COLOR_BGR2YUV_I420);
798
- //cout << "processFileUrl3:" << mid.cols << ",mid.rows:"<<Height<<endl;
799
- mid.release();
839
+ if (depth != 0) { //bit数仅支持8
840
+ cv::Mat mid;
841
+ modifyImageFormat(mid, dst, channel);
842
+ cvtColor(mid, dest, COLOR_BGR2YUV_I420);
843
+ mid.release();
844
+ } else {
845
+ cvtColor(dst, dest, COLOR_BGR2YUV_I420);
846
+ }
800
847
 
801
- //cvtColor(dst, dest, COLOR_BGR2YUV_I420);
802
848
  dst.release();
803
849
  } else {
804
- //cvtColor(src, dest, COLOR_BGR2YUV_I420);
805
- cv::Mat mid;
806
- src.convertTo(mid, CV_8UC3);
807
- cvtColor(mid, dest, COLOR_BGR2YUV_I420);
808
- //cout << "processFileUrl3:" << mid.cols << ",mid.rows:"<<Height<<endl;
809
- mid.release();
810
- }
811
- // cv::Mat mid;
812
- // src.convertTo(mid, CV_8UC3);
813
- // cout << "processFileUrl2:" << Width << ",Height:"<<Height<<endl;
814
- // cvtColor(mid, dest, COLOR_BGR2YUV_I420);
815
- // cout << "processFileUrl3:" << mid.cols << ",mid.rows:"<<Height<<endl;
850
+ //cout << "processFileUrl ji:" <<endl;
851
+ if (depth != 0) { //bit数仅支持8
852
+ cv::Mat mid;
853
+ modifyImageFormat(mid, src, channel);
854
+ cvtColor(mid, dest, COLOR_BGR2YUV_I420);
855
+ mid.release();
856
+ } else {
857
+ cvtColor(src, dest, COLOR_BGR2YUV_I420);
858
+ }
859
+ }
860
+
816
861
  int len = Height*Width*3/2;
817
862
  MUInt8* imageData = (MUInt8*)malloc(len);
818
863
  memset(imageData, 0, len);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hzt_asc",
3
- "version": "1.0.9",
3
+ "version": "1.1.1",
4
4
  "main": "index.js",
5
5
  "description": "hzt_asc_v3.",
6
6
  "author": {
package/sample/demo.js CHANGED
@@ -17,10 +17,26 @@ let start = Date.now();
17
17
  let ask = new addon.ASK(APPID, SDKKEY, ACTIVEKEY);
18
18
  //let ask2 = new addon.ASK(APPID, SDKKEY, ACTIVEKEY);
19
19
  printMem()
20
+ // let xx2 = ask.FaceDetectUrl("http://image.haizitong.com/cc123be3667143e3af5c53a5906c6b42");
21
+ // console.log("init end", xx2, Date.now() - start);
20
22
 
21
- let xx = ask.FaceDetectUrl("http://upload-file.haizitong.com/api/getFile/119c44a5-193f-4953-b59c-c1e55cd29f1a")
22
- let xx2 = ask.FaceDetectUrl("http://image.haizitong.com/cc123be3667143e3af5c53a5906c6b42");
23
- console.log("init end", xx, xx2, Date.now() - start);
23
+ //let xx = ask.FaceDetectUrl("http://upload-file.haizitong.com/api/getFile/119c44a5-193f-4953-b59c-c1e55cd29f1a")
24
+ //let xx = ask.FaceDetectUrl("https://min.haizitong.com/2/ali/i/fbad726e022742a78a2666580a9a0f0f139317")
25
+ //http://upload-file.haizitong.com/api/getCacheFile/62e23c73991e856c4411bbe4
26
+ //let xx = ask.FaceDetectUrl("https://upload-file.haizitong.com/api/getCacheFile/62e23c73991e856c4411bbe4")
27
+
28
+ const urls = [
29
+ "http://image.haizitong.com/cc123be3667143e3af5c53a5906c6b42",
30
+ "http://upload-file.haizitong.com/api/getFile/119c44a5-193f-4953-b59c-c1e55cd29f1a",
31
+ "https://min.haizitong.com/2/ali/i/fbad726e022742a78a2666580a9a0f0f139317",
32
+ "http://upload-file.haizitong.com/api/getCacheFile/62e23c73991e856c4411bbe4"
33
+ ]
34
+ for(let o of urls) {
35
+ let xx2 = ask.FaceDetectUrl(o);
36
+ console.log("init end", xx2, Date.now() - start);
37
+ }
38
+
39
+ //console.log("init end", xx, Date.now() - start);
24
40
 
25
41
  // for(let i = 0; i < 10000; ++i) {
26
42
  // let num = ask.FaceFeatureExtractUrl("http://min.haizitong.com/2/ali/i/a5f91d52783d49989777e8b82b545e2c");