• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "camera_manager.h"
17 #include "ic_callback.h"
18 #include "ic_constants.h"
19 #include "ic_retcode.h"
20 #include "ic_sdk.h"
21 #include "picture_utils.h"
22 
23 using namespace std;
24 using namespace IC;
25 using namespace OHOS;
26 using namespace OHOS::AI;
27 using namespace OHOS::Media;
28 
29 namespace {
__anonb82cb5a60202(uint8_t *&buffer) 30 const auto DELETE_PIC_BUFFER = [](uint8_t *&buffer) {
31     if (buffer != nullptr) {
32         delete[] buffer;
33         buffer = nullptr;
34     }
35 };
36 }
37 
38 class MyIcCallback : public IcCallback {
39 public:
MyIcCallback()40     MyIcCallback() {}
~MyIcCallback()41     ~MyIcCallback() {}
42 
OnError(const IcRetCode errorCode)43     void OnError(const IcRetCode errorCode)
44     {
45         printf("Executing error, error code: %d\n", errorCode);
46     }
47 
OnResult(const IcOutput & result)48     void OnResult(const IcOutput &result)
49     {
50         if (result.size % 2 == 0) {
51             int numClass = result.size / 2;
52             for (size_t i = 0; i < numClass; ++i) {
53                 printf("class index [%d] score: %d\n", result.data[i], result.data[i + numClass]);
54             }
55         }
56     }
57 };
58 
CameraCapture()59 static int CameraCapture()
60 {
61     printf("Camera sample begin.\n");
62     CameraKit *camKit = CameraKit::GetInstance();
63     if (camKit == nullptr) {
64         printf("Can not get CameraKit instance\n");
65         return IC_RETCODE_FAILURE;
66     }
67     list<string> camList = camKit->GetCameraIds();
68     string camId;
69     for (auto &cam : camList) {
70         printf("camera name: %s\n", cam.c_str());
71         const CameraAbility *ability = camKit->GetCameraAbility(cam);
72         list<CameraPicSize> sizeList = ability->GetSupportedSizes(0);
73         for (auto &pic : sizeList) {
74             if (pic.width == CAMERA_PIC_WIDTH && pic.height == CAMERA_PIC_HEIGHT) {
75                 camId = cam;
76                 break;
77             }
78         }
79         if (!camId.empty()) {
80             break;
81         }
82     }
83     if (camId.empty()) {
84         printf("No available camera.(1080p wanted)\n");
85         return IC_RETCODE_FAILURE;
86     }
87 
88     EventHandler eventHdlr;
89     SampleCameraStateMng camStateMng(eventHdlr);
90     camKit->CreateCamera(camId, camStateMng, eventHdlr);
91     std::unique_lock<std::mutex> cameraCreateLock(g_mutex);
92     g_cv.wait(cameraCreateLock);
93     cameraCreateLock.unlock();
94     camStateMng.Capture();
95     std::unique_lock<std::mutex> captureLock(g_mutex);
96     g_cv.wait(captureLock);
97     captureLock.unlock();
98     printf("Camera sample end.\n");
99     return IC_RETCODE_SUCCESS;
100 }
101 
ImageClassificationExecute(const IcInput & picData)102 static int ImageClassificationExecute(const IcInput &picData)
103 {
104     if (picData.data == nullptr || picData.size == 0) {
105         printf("ImageClassificationExecute: picData is empty.\n");
106         return IC_RETCODE_FAILURE;
107     }
108     shared_ptr<IcSdk> detector = make_shared<IcSdk>();
109     shared_ptr<MyIcCallback> callback = make_shared<MyIcCallback>();
110     if (detector == nullptr || callback == nullptr) {
111         return IC_RETCODE_FAILURE;
112     }
113     detector->SetCallback(callback);
114     int retcode = detector->Create();
115     if (retcode != IC_RETCODE_SUCCESS) {
116         printf("ImageClassificationExecute: IcSdk Create failed.\n");
117         return IC_RETCODE_FAILURE;
118     }
119     retcode = detector->SyncExecute(picData);
120     if (retcode != IC_RETCODE_SUCCESS) {
121         printf("ImageClassificationExecute: IcSdk SyncExecute failed.\n");
122     }
123     (void)detector->Destroy();
124     return retcode;
125 }
126 
GetPictureData(const string & sourcePath,Array<uint8_t> & picData)127 static int GetPictureData(const string &sourcePath, Array<uint8_t> &picData)
128 {
129     int srcWidth = 0;
130     int srcHeight = 0;
131     uint8_t *rawData = ReadJpegFile(sourcePath, srcWidth, srcHeight);
132     if (rawData == nullptr) {
133         printf("ReadJpegFile failed.\n");
134         return IC_RETCODE_FAILURE;
135     }
136     uint8_t *srcData = Resize(WIDTH_DEST, HEIGHT_DEST, rawData, srcWidth, srcHeight);
137     if (srcData == nullptr) {
138         printf("Resize failed.\n");
139         DELETE_PIC_BUFFER(rawData);
140         return IC_RETCODE_FAILURE;
141     }
142     int srcDataSize = WIDTH_DEST * HEIGHT_DEST * NUM_CHANNELS;
143     uint8_t *input = ConvertToCaffeInput(srcData, srcDataSize);
144     if (input == nullptr) {
145         printf("Convert to caffe input failed.\n");
146         DELETE_PIC_BUFFER(rawData);
147         DELETE_PIC_BUFFER(srcData);
148         return IC_RETCODE_FAILURE;
149     }
150     picData.data = input;
151     picData.size = srcDataSize;
152     DELETE_PIC_BUFFER(rawData);
153     DELETE_PIC_BUFFER(srcData);
154     return IC_RETCODE_SUCCESS;
155 }
156 
GetPicFromCamera(IcInput & picData)157 static int GetPicFromCamera(IcInput &picData)
158 {
159     int retcode = CameraCapture();
160     if (retcode != IC_RETCODE_SUCCESS) {
161         printf("Capture picture failed.\n");
162         return IC_RETCODE_FAILURE;
163     }
164     retcode = GetPictureData(CAMERA_SAVE_PATH, picData);
165     if (retcode != IC_RETCODE_SUCCESS) {
166         printf("GetPictureData failed.\n");
167         return IC_RETCODE_FAILURE;
168     }
169     return IC_RETCODE_SUCCESS;
170 }
171 
GetPicFromLocal(IcInput & picData)172 static int GetPicFromLocal(IcInput &picData)
173 {
174     int retcode = GetPictureData(JPEG_SRC_PATH, picData);
175     if (retcode != IC_RETCODE_SUCCESS) {
176         printf("GetPictureData failed.\n");
177         return IC_RETCODE_FAILURE;
178     }
179     return IC_RETCODE_SUCCESS;
180 }
181 
SampleHelp()182 static void SampleHelp()
183 {
184     printf("****************************************\n");
185     printf("Select the image source.\n");
186     printf("1: Camera capture\n");
187     printf("2: Local\n");
188     printf("****************************************\n");
189 }
190 
main()191 int main()
192 {
193     IcInput picData = {
194         .data = nullptr,
195         .size = 0
196     };
197     int retcode = IC_RETCODE_SUCCESS;
198     char input = ' ';
199     SampleHelp();
200     cin >> input;
201     switch (input) {
202         case '1':
203             retcode = GetPicFromCamera(picData);
204             break;
205         case '2':
206             retcode = GetPicFromLocal(picData);
207             break;
208         default:
209             SampleHelp();
210             break;
211     }
212     if (retcode != IC_RETCODE_SUCCESS) {
213         printf("GetPicture failed.\n");
214         return -1;
215     }
216     if (retcode != IC_RETCODE_SUCCESS) {
217         printf("WriteBgrFile failed.\n");
218         DELETE_PIC_BUFFER(picData.data);
219         return -1;
220     }
221     retcode = ImageClassificationExecute(picData);
222     if (retcode != IC_RETCODE_SUCCESS) {
223         printf("ImageClassification failed.\n");
224     } else {
225         printf("ImageClassification successed.\n");
226     }
227     DELETE_PIC_BUFFER(picData.data);
228     return 0;
229 }