forked from Ghustwb/Hi3559_NNIE_SSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
894 lines (776 loc) · 38.7 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/prctl.h>
#include <math.h>
#include <time.h>
#include "hi_common.h"
#include "hi_comm_sys.h"
#include "hi_comm_svp.h"
#include "sample_comm.h"
#include "sample_comm_svp.h"
#include "sample_comm_nnie.h"
//#include "sample_nnie_main.h"
#include "sample_svp_nnie_software.h"
#include "sample_comm_ive.h"
#include "opencv2/imgcodecs/imgcodecs_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/core_c.h"
struct timeval start,end;
// struct resultBbox{
// int xmin;
// int yminl
// int xmax;
// int ymax;
// float conf;
// }bBox;
/*ssd para*/
static SAMPLE_SVP_NNIE_MODEL_S s_stSsdModel = {0};
static SAMPLE_SVP_NNIE_PARAM_S s_stSsdNnieParam = {0};
static SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S s_stSsdSoftwareParam = {0};
SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S stInputDataIdx = {0};
HI_S32 s32Ret = HI_SUCCESS;
SAMPLE_SVP_NNIE_CFG_S stNnieCfg = {0};
SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S stProcSegIdx = {0};
/******************************************************************************
* function : NNIE Forward
******************************************************************************/
static HI_S32 SAMPLE_SVP_NNIE_Forward(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S* pstInputDataIdx,
SAMPLE_SVP_NNIE_PROCESS_SEG_INDEX_S* pstProcSegIdx,HI_BOOL bInstant)
{
HI_S32 s32Ret = HI_SUCCESS;
HI_U32 i = 0, j = 0;
HI_BOOL bFinish = HI_FALSE;
SVP_NNIE_HANDLE hSvpNnieHandle = 0;
HI_U32 u32TotalStepNum = 0;
SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].stTskBuf.u64PhyAddr,
(HI_VOID *) pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].stTskBuf.u64VirAddr,
pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].stTskBuf.u32Size);
/*set input blob according to node name*/
if(pstInputDataIdx->u32SegIdx != pstProcSegIdx->u32SegIdx)
{
for(i = 0; i < pstNnieParam->pstModel->astSeg[pstProcSegIdx->u32SegIdx].u16SrcNum; i++)
{
for(j = 0; j < pstNnieParam->pstModel->astSeg[pstInputDataIdx->u32SegIdx].u16DstNum; j++)
{
if(0 == strncmp(pstNnieParam->pstModel->astSeg[pstInputDataIdx->u32SegIdx].astDstNode[j].szName,
pstNnieParam->pstModel->astSeg[pstProcSegIdx->u32SegIdx].astSrcNode[i].szName,
SVP_NNIE_NODE_NAME_LEN))
{
pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astSrc[i] =
pstNnieParam->astSegData[pstInputDataIdx->u32SegIdx].astDst[j];
break;
}
}
SAMPLE_SVP_CHECK_EXPR_RET((j == pstNnieParam->pstModel->astSeg[pstInputDataIdx->u32SegIdx].u16DstNum),
HI_FAILURE,SAMPLE_SVP_ERR_LEVEL_ERROR,"Error,can't find %d-th seg's %d-th src blob!\n",
pstProcSegIdx->u32SegIdx,i);
}
}
/*NNIE_Forward*/
s32Ret = HI_MPI_SVP_NNIE_Forward(&hSvpNnieHandle,
pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astSrc,
pstNnieParam->pstModel, pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst,
&pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx], bInstant);
SAMPLE_SVP_CHECK_EXPR_RET(HI_SUCCESS != s32Ret,s32Ret,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error,HI_MPI_SVP_NNIE_Forward failed!\n");
if(bInstant)
{
/*Wait NNIE finish*/
while(HI_ERR_SVP_NNIE_QUERY_TIMEOUT == (s32Ret = HI_MPI_SVP_NNIE_Query(pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].enNnieId,
hSvpNnieHandle, &bFinish, HI_TRUE)))
{
usleep(100);
SAMPLE_SVP_TRACE(SAMPLE_SVP_ERR_LEVEL_INFO,
"HI_MPI_SVP_NNIE_Query Query timeout!\n");
}
}
bFinish = HI_FALSE;
for(i = 0; i < pstNnieParam->astForwardCtrl[pstProcSegIdx->u32SegIdx].u32DstNum; i++)
{
if(SVP_BLOB_TYPE_SEQ_S32 == pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].enType)
{
for(j = 0; j < pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Num; j++)
{
u32TotalStepNum += *((HI_U32*)(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stSeq.u64VirAddrStep)+j);
}
SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64PhyAddr,
(HI_VOID *) pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64VirAddr,
u32TotalStepNum*pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Stride);
}
else
{
SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64PhyAddr,
(HI_VOID *) pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u64VirAddr,
pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Num*
pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stWhc.u32Chn*
pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].unShape.stWhc.u32Height*
pstNnieParam->astSegData[pstProcSegIdx->u32SegIdx].astDst[i].u32Stride);
}
}
return s32Ret;
}
/******************************************************************************
* function : Fill Src Data
******************************************************************************/
static HI_S32 SAMPLE_SVP_NNIE_FillSrcData(SAMPLE_SVP_NNIE_CFG_S* pstNnieCfg,
SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S* pstInputDataIdx)
{
FILE* fp = NULL;
HI_U32 i =0, j = 0, n = 0;
HI_U32 u32Height = 0, u32Width = 0, u32Chn = 0, u32Stride = 0, u32Dim = 0;
HI_U32 u32VarSize = 0;
HI_S32 s32Ret = HI_SUCCESS;
HI_U8*pu8PicAddr = NULL;
HI_U32*pu32StepAddr = NULL;
HI_U32 u32SegIdx = pstInputDataIdx->u32SegIdx;
HI_U32 u32NodeIdx = pstInputDataIdx->u32NodeIdx;
HI_U32 u32TotalStepNum = 0;
/*open file*/
if (NULL != pstNnieCfg->pszPic)
{
fp = fopen(pstNnieCfg->pszPic,"rb");
SAMPLE_SVP_CHECK_EXPR_RET(NULL == fp,HI_INVALID_VALUE,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error, open file failed!\n");
}
/*get data size*/
if(SVP_BLOB_TYPE_U8 <= pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType &&
SVP_BLOB_TYPE_YVU422SP >= pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType)
{
u32VarSize = sizeof(HI_U8);
}
else
{
u32VarSize = sizeof(HI_U32);
}
/*fill src data*/
if(SVP_BLOB_TYPE_SEQ_S32 == pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType)
{
u32Dim = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stSeq.u32Dim;
u32Stride = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Stride;
pu32StepAddr = (HI_U32*)(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stSeq.u64VirAddrStep);
pu8PicAddr = (HI_U8*)(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr);
for(n = 0; n < pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num; n++)
{
for(i = 0;i < *(pu32StepAddr+n); i++)
{
s32Ret = fread(pu8PicAddr,u32Dim*u32VarSize,1,fp);
SAMPLE_SVP_CHECK_EXPR_GOTO(1 != s32Ret,FAIL,SAMPLE_SVP_ERR_LEVEL_ERROR,"Error,Read image file failed!\n");
pu8PicAddr += u32Stride;
}
u32TotalStepNum += *(pu32StepAddr+n);
}
SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64PhyAddr,
(HI_VOID *) pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr,
u32TotalStepNum*u32Stride);
}
else
{
u32Height = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Height;
u32Width = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Width;
u32Chn = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Chn;
u32Stride = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Stride;
pu8PicAddr = (HI_U8*)(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr);
for(n = 0; n < pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num; n++)
{
for(i = 0;i < u32Chn; i++)
{
for(j = 0; j < u32Height; j++)
{
s32Ret = fread(pu8PicAddr,u32Width*u32VarSize,1,fp);
SAMPLE_SVP_CHECK_EXPR_GOTO(1 != s32Ret,FAIL,SAMPLE_SVP_ERR_LEVEL_ERROR,"Error,Read image file failed!\n");
pu8PicAddr += u32Stride;
}
}
}
SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64PhyAddr,
(HI_VOID *) pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr,
pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num*u32Chn*u32Height*u32Stride);
}
fclose(fp);
return HI_SUCCESS;
FAIL:
fclose(fp);
return HI_FAILURE;
}
/******************************************************************************
* function : Fill Src Data from Frame
******************************************************************************/
static HI_S32 SAMPLE_SVP_NNIE_FillSrcDataFromFrame(HI_U8* pImage,
SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, SAMPLE_SVP_NNIE_INPUT_DATA_INDEX_S* pstInputDataIdx)
{
HI_U32 i =0, j = 0, n = 0;
HI_U32 u32Height = 0, u32Width = 0, u32Chn = 0, u32Stride = 0, u32Dim = 0;
HI_U32 u32VarSize = 0;
HI_S32 s32Ret = HI_SUCCESS;
HI_U8*pu8PicAddr = NULL;
HI_U32*pu32StepAddr = NULL;
HI_U32 u32SegIdx = pstInputDataIdx->u32SegIdx;
HI_U32 u32NodeIdx = pstInputDataIdx->u32NodeIdx;
HI_U32 u32TotalStepNum = 0;
HI_U32 u32SrcPerLine = 0;
if(NULL == pImage)
{
SAMPLE_SVP_CHECK_EXPR_GOTO(1 != s32Ret,FAIL,SAMPLE_SVP_ERR_LEVEL_ERROR,"Error, input image is NULL!\n");
}
/*get data size*/
if(SVP_BLOB_TYPE_U8 <= pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType &&
SVP_BLOB_TYPE_YVU422SP >= pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType)
{
u32VarSize = sizeof(HI_U8);
}
else
{
u32VarSize = sizeof(HI_U32);
}
/*fill src data*/
if(SVP_BLOB_TYPE_SEQ_S32 == pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].enType)
{
u32Dim = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stSeq.u32Dim;
u32Stride = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Stride;
pu32StepAddr = (HI_U32*)(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stSeq.u64VirAddrStep);
pu8PicAddr = (HI_U8*)(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr);
u32SrcPerLine = u32Dim*u32VarSize;
for(n = 0; n < pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num; n++)
{
for(i = 0;i < *(pu32StepAddr+n); i++)
{
//s32Ret = fread(pu8PicAddr,u32Dim*u32VarSize,1,fp);
memcpy(pu8PicAddr,pImage,u32SrcPerLine);
printf("mem copy\n");
pImage += u32SrcPerLine;
pu8PicAddr += u32Stride;
}
u32TotalStepNum += *(pu32StepAddr+n);
}
SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64PhyAddr,
(HI_VOID *) pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr,
u32TotalStepNum*u32Stride);
}
else
{
u32Height = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Height;
u32Width = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Width;
u32Chn = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].unShape.stWhc.u32Chn;
u32Stride = pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Stride;
pu8PicAddr = (HI_U8*)(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr);
u32SrcPerLine = u32Width*u32VarSize;
for(n = 0; n < pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num; n++)
{
for(i = 0;i < u32Chn; i++)
{
for(j = 0; j < u32Height; j++)
{
//s32Ret = fread(pu8PicAddr,u32Width*u32VarSize,1,fp);
memcpy(pu8PicAddr,pImage,u32SrcPerLine);
pImage += u32SrcPerLine;
pu8PicAddr += u32Stride;
}
}
}
SAMPLE_COMM_SVP_FlushCache(pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64PhyAddr,
(HI_VOID *) pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u64VirAddr,
pstNnieParam->astSegData[u32SegIdx].astSrc[u32NodeIdx].u32Num*u32Chn*u32Height*u32Stride);
}
return HI_SUCCESS;
FAIL:
return HI_FAILURE;
}
/******************************************************************************
* function : print detection result
******************************************************************************/
static HI_S32 SAMPLE_SVP_NNIE_Detection_PrintResult(SVP_BLOB_S *pstDstScore,
SVP_BLOB_S *pstDstRoi, SVP_BLOB_S *pstClassRoiNum, HI_FLOAT f32PrintResultThresh)
{
HI_U32 i = 0, j = 0;
HI_U32 u32RoiNumBias = 0;
HI_U32 u32ScoreBias = 0;
HI_U32 u32BboxBias = 0;
HI_FLOAT f32Score = 0.0f;
HI_S32* ps32Score = (HI_S32*)pstDstScore->u64VirAddr;
HI_S32* ps32Roi = (HI_S32*)pstDstRoi->u64VirAddr;
HI_S32* ps32ClassRoiNum = (HI_S32*)pstClassRoiNum->u64VirAddr;
HI_U32 u32ClassNum = pstClassRoiNum->unShape.stWhc.u32Width;
HI_S32 s32XMin = 0,s32YMin= 0,s32XMax = 0,s32YMax = 0;
u32RoiNumBias += ps32ClassRoiNum[0];
for (i = 1; i < u32ClassNum; i++)
{
u32ScoreBias = u32RoiNumBias;
u32BboxBias = u32RoiNumBias * SAMPLE_SVP_NNIE_COORDI_NUM;
/*if the confidence score greater than result threshold, the result will be printed*/
if((HI_FLOAT)ps32Score[u32ScoreBias] / SAMPLE_SVP_NNIE_QUANT_BASE >=
f32PrintResultThresh && ps32ClassRoiNum[i]!=0)
{
if(i == 2)
SAMPLE_SVP_TRACE_INFO("==== Bicycle====\n");
if(i == 6)
SAMPLE_SVP_TRACE_INFO("==== BUS====\n");
if(i == 7)
SAMPLE_SVP_TRACE_INFO("==== Car====\n");
if(i == 14)
SAMPLE_SVP_TRACE_INFO("==== Motor====\n");
if(i == 15)
SAMPLE_SVP_TRACE_INFO("==== Person====\n");
}
for (j = 0; j < (HI_U32)ps32ClassRoiNum[i]; j++)
{
f32Score = (HI_FLOAT)ps32Score[u32ScoreBias + j] / SAMPLE_SVP_NNIE_QUANT_BASE;
if (f32Score < f32PrintResultThresh)
{
break;
}
s32XMin = ps32Roi[u32BboxBias + j*SAMPLE_SVP_NNIE_COORDI_NUM];
s32YMin = ps32Roi[u32BboxBias + j*SAMPLE_SVP_NNIE_COORDI_NUM + 1];
s32XMax = ps32Roi[u32BboxBias + j*SAMPLE_SVP_NNIE_COORDI_NUM + 2];
s32YMax = ps32Roi[u32BboxBias + j*SAMPLE_SVP_NNIE_COORDI_NUM + 3];
SAMPLE_SVP_TRACE_INFO("%d %d %d %d %f\n", s32XMin, s32YMin, s32XMax, s32YMax, f32Score);
}
u32RoiNumBias += ps32ClassRoiNum[i];
}
return HI_SUCCESS;
}
/******************************************************************************
* function : roi to rect
******************************************************************************/
HI_S32 SAMPLE_SVP_NNIE_RoiToRect(SVP_BLOB_S *pstDstScore,
SVP_BLOB_S *pstDstRoi, SVP_BLOB_S *pstClassRoiNum, HI_FLOAT *paf32ScoreThr,
HI_BOOL bRmBg,SAMPLE_SVP_NNIE_RECT_ARRAY_S *pstRect,
HI_U32 u32SrcWidth, HI_U32 u32SrcHeight,HI_U32 u32DstWidth,HI_U32 u32DstHeight)
{
HI_U32 i = 0, j = 0;
HI_U32 u32RoiNumBias = 0;
HI_U32 u32ScoreBias = 0;
HI_U32 u32BboxBias = 0;
HI_FLOAT f32Score = 0.0f;
HI_S32* ps32Score = (HI_S32*)pstDstScore->u64VirAddr;
HI_S32* ps32Roi = (HI_S32*)pstDstRoi->u64VirAddr;
HI_S32* ps32ClassRoiNum = (HI_S32*)pstClassRoiNum->u64VirAddr;
HI_U32 u32ClassNum = pstClassRoiNum->unShape.stWhc.u32Width;
HI_U32 u32RoiNumTmp = 0;
SAMPLE_SVP_CHECK_EXPR_RET(u32ClassNum > SAMPLE_SVP_NNIE_MAX_CLASS_NUM ,HI_ERR_SVP_NNIE_ILLEGAL_PARAM,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error(%#x),u32ClassNum(%u) must be less than or equal %u to!\n",HI_ERR_SVP_NNIE_ILLEGAL_PARAM,u32ClassNum, SAMPLE_SVP_NNIE_MAX_CLASS_NUM);
pstRect->u32TotalNum = 0;
pstRect->u32ClsNum = u32ClassNum;
if (bRmBg)
{
pstRect->au32RoiNum[0] = 0;
u32RoiNumBias += ps32ClassRoiNum[0];
for (i = 1; i < u32ClassNum; i++)
{
u32ScoreBias = u32RoiNumBias;
u32BboxBias = u32RoiNumBias * SAMPLE_SVP_NNIE_COORDI_NUM;
u32RoiNumTmp = 0;
/*if the confidence score greater than result thresh, the result will be drawed*/
if(((HI_FLOAT)ps32Score[u32ScoreBias] / SAMPLE_SVP_NNIE_QUANT_BASE >=
paf32ScoreThr[i]) && (ps32ClassRoiNum[i] != 0))
{
for (j = 0; j < (HI_U32)ps32ClassRoiNum[i]; j++)
{
/*Score is descend order*/
f32Score = (HI_FLOAT)ps32Score[u32ScoreBias + j] / SAMPLE_SVP_NNIE_QUANT_BASE;
if ((f32Score < paf32ScoreThr[i]) || (u32RoiNumTmp >= SAMPLE_SVP_NNIE_MAX_ROI_NUM_OF_CLASS))
{
break;
}
pstRect->astRect[i][u32RoiNumTmp].astPoint[0].s32X = (HI_U32)((HI_FLOAT)ps32Roi[u32BboxBias + j*SAMPLE_SVP_NNIE_COORDI_NUM] / (HI_FLOAT)u32SrcWidth * (HI_FLOAT)u32DstWidth) & (~1) ;
pstRect->astRect[i][u32RoiNumTmp].astPoint[0].s32Y = (HI_U32)((HI_FLOAT)ps32Roi[u32BboxBias + j*SAMPLE_SVP_NNIE_COORDI_NUM + 1] / (HI_FLOAT)u32SrcHeight * (HI_FLOAT)u32DstHeight) & (~1);
pstRect->astRect[i][u32RoiNumTmp].astPoint[1].s32X = (HI_U32)((HI_FLOAT)ps32Roi[u32BboxBias + j*SAMPLE_SVP_NNIE_COORDI_NUM + 2]/ (HI_FLOAT)u32SrcWidth * (HI_FLOAT)u32DstWidth) & (~1);
pstRect->astRect[i][u32RoiNumTmp].astPoint[1].s32Y = pstRect->astRect[i][u32RoiNumTmp].astPoint[0].s32Y;
pstRect->astRect[i][u32RoiNumTmp].astPoint[2].s32X = pstRect->astRect[i][u32RoiNumTmp].astPoint[1].s32X;
pstRect->astRect[i][u32RoiNumTmp].astPoint[2].s32Y = (HI_U32)((HI_FLOAT)ps32Roi[u32BboxBias + j*SAMPLE_SVP_NNIE_COORDI_NUM + 3] / (HI_FLOAT)u32SrcHeight * (HI_FLOAT)u32DstHeight) & (~1);
pstRect->astRect[i][u32RoiNumTmp].astPoint[3].s32X = pstRect->astRect[i][u32RoiNumTmp].astPoint[0].s32X;
pstRect->astRect[i][u32RoiNumTmp].astPoint[3].s32Y = pstRect->astRect[i][u32RoiNumTmp].astPoint[2].s32Y;
u32RoiNumTmp++;
}
}
pstRect->au32RoiNum[i] = u32RoiNumTmp;
pstRect->u32TotalNum += u32RoiNumTmp;
u32RoiNumBias += ps32ClassRoiNum[i];
}
}
return HI_SUCCESS;
}
/******************************************************************************
* function : SSD software deinit
******************************************************************************/
static HI_S32 SAMPLE_SVP_NNIE_Ssd_SoftwareDeinit(SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S* pstSoftWareParam)
{
HI_S32 s32Ret = HI_SUCCESS;
SAMPLE_SVP_CHECK_EXPR_RET(NULL== pstSoftWareParam,HI_INVALID_VALUE,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error, pstSoftWareParam can't be NULL!\n");
if(0!=pstSoftWareParam->stPriorBoxTmpBuf.u64PhyAddr && 0!=pstSoftWareParam->stPriorBoxTmpBuf.u64VirAddr)
{
SAMPLE_SVP_MMZ_FREE(pstSoftWareParam->stPriorBoxTmpBuf.u64PhyAddr,
pstSoftWareParam->stPriorBoxTmpBuf.u64VirAddr);
pstSoftWareParam->stPriorBoxTmpBuf.u64PhyAddr = 0;
pstSoftWareParam->stPriorBoxTmpBuf.u64VirAddr = 0;
}
return s32Ret;
}
/******************************************************************************
* function : Ssd Deinit
******************************************************************************/
static HI_S32 SAMPLE_SVP_NNIE_Ssd_Deinit(SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam,
SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S* pstSoftWareParam,SAMPLE_SVP_NNIE_MODEL_S *pstNnieModel)
{
HI_S32 s32Ret = HI_SUCCESS;
/*hardware deinit*/
if(pstNnieParam!=NULL)
{
s32Ret = SAMPLE_COMM_SVP_NNIE_ParamDeinit(pstNnieParam);
SAMPLE_SVP_CHECK_EXPR_TRACE(HI_SUCCESS != s32Ret,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error,SAMPLE_COMM_SVP_NNIE_ParamDeinit failed!\n");
}
/*software deinit*/
if(pstSoftWareParam!=NULL)
{
s32Ret = SAMPLE_SVP_NNIE_Ssd_SoftwareDeinit(pstSoftWareParam);
SAMPLE_SVP_CHECK_EXPR_TRACE(HI_SUCCESS != s32Ret,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error,SAMPLE_SVP_NNIE_Ssd_SoftwareDeinit failed!\n");
}
/*model deinit*/
if(pstNnieModel!=NULL)
{
s32Ret = SAMPLE_COMM_SVP_NNIE_UnloadModel(pstNnieModel);
SAMPLE_SVP_CHECK_EXPR_TRACE(HI_SUCCESS != s32Ret,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error,SAMPLE_COMM_SVP_NNIE_UnloadModel failed!\n");
}
return s32Ret;
}
/******************************************************************************
* function : Ssd software para init
******************************************************************************/
static HI_S32 SAMPLE_SVP_NNIE_Ssd_SoftwareInit(SAMPLE_SVP_NNIE_CFG_S* pstCfg,
SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S* pstSoftWareParam)
{
HI_U32 i = 0;
HI_S32 s32Ret = HI_SUCCESS;
HI_U32 u32ClassNum = 0;
HI_U32 u32TotalSize = 0;
HI_U32 u32DstRoiSize = 0;
HI_U32 u32DstScoreSize = 0;
HI_U32 u32ClassRoiNumSize = 0;
HI_U32 u32TmpBufTotalSize = 0;
HI_U64 u64PhyAddr = 0;
HI_U8* pu8VirAddr = NULL;
/*Set Conv Parameters*/
/*the SSD sample report resule is after permute operation,
conv result is (C, H, W), after permute, the report node's
(C1, H1, W1) is (H, W, C), the stride of report result is aligned according to C dim*/
for(i = 0; i < 12; i++)
{
pstSoftWareParam->au32ConvHeight[i] = pstNnieParam->pstModel->astSeg[0].astDstNode[i].unShape.stWhc.u32Chn;
pstSoftWareParam->au32ConvWidth[i] = pstNnieParam->pstModel->astSeg[0].astDstNode[i].unShape.stWhc.u32Height;
pstSoftWareParam->au32ConvChannel[i] = pstNnieParam->pstModel->astSeg[0].astDstNode[i].unShape.stWhc.u32Width;
if(i%2==1)
{
pstSoftWareParam->au32ConvStride[i/2] = SAMPLE_SVP_NNIE_ALIGN16(pstSoftWareParam->au32ConvChannel[i]*sizeof(HI_U32))/sizeof(HI_U32);
}
}
/*Set PriorBox Parameters*/
pstSoftWareParam->au32PriorBoxWidth[0] = 38;
pstSoftWareParam->au32PriorBoxWidth[1] = 19;
pstSoftWareParam->au32PriorBoxWidth[2] = 10;
pstSoftWareParam->au32PriorBoxWidth[3] = 5;
pstSoftWareParam->au32PriorBoxWidth[4] = 3;
pstSoftWareParam->au32PriorBoxWidth[5] = 1;
pstSoftWareParam->au32PriorBoxHeight[0] = 38;
pstSoftWareParam->au32PriorBoxHeight[1] = 19;
pstSoftWareParam->au32PriorBoxHeight[2] = 10;
pstSoftWareParam->au32PriorBoxHeight[3] = 5;
pstSoftWareParam->au32PriorBoxHeight[4] = 3;
pstSoftWareParam->au32PriorBoxHeight[5] = 1;
pstSoftWareParam->u32OriImHeight = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Height;
pstSoftWareParam->u32OriImWidth = pstNnieParam->astSegData[0].astSrc[0].unShape.stWhc.u32Width;
pstSoftWareParam->af32PriorBoxMinSize[0][0] = 30.0f;
pstSoftWareParam->af32PriorBoxMinSize[1][0] = 60.0f;
pstSoftWareParam->af32PriorBoxMinSize[2][0] = 111.0f;
pstSoftWareParam->af32PriorBoxMinSize[3][0] = 162.0f;
pstSoftWareParam->af32PriorBoxMinSize[4][0] = 213.0f;
pstSoftWareParam->af32PriorBoxMinSize[5][0] = 264.0f;
pstSoftWareParam->af32PriorBoxMaxSize[0][0] = 60.0f;
pstSoftWareParam->af32PriorBoxMaxSize[1][0] = 111.0f;
pstSoftWareParam->af32PriorBoxMaxSize[2][0] = 162.0f;
pstSoftWareParam->af32PriorBoxMaxSize[3][0] = 213.0f;
pstSoftWareParam->af32PriorBoxMaxSize[4][0] = 264.0f;
pstSoftWareParam->af32PriorBoxMaxSize[5][0] = 315.0f;
pstSoftWareParam->u32MinSizeNum = 1;
pstSoftWareParam->u32MaxSizeNum = 1;
pstSoftWareParam->bFlip= HI_TRUE;
pstSoftWareParam->bClip= HI_FALSE;
pstSoftWareParam->au32InputAspectRatioNum[0] = 1;
pstSoftWareParam->au32InputAspectRatioNum[1] = 2;
pstSoftWareParam->au32InputAspectRatioNum[2] = 2;
pstSoftWareParam->au32InputAspectRatioNum[3] = 2;
pstSoftWareParam->au32InputAspectRatioNum[4] = 1;
pstSoftWareParam->au32InputAspectRatioNum[5] = 1;
pstSoftWareParam->af32PriorBoxAspectRatio[0][0] = 2;
pstSoftWareParam->af32PriorBoxAspectRatio[0][1] = 0;
pstSoftWareParam->af32PriorBoxAspectRatio[1][0] = 2;
pstSoftWareParam->af32PriorBoxAspectRatio[1][1] = 3;
pstSoftWareParam->af32PriorBoxAspectRatio[2][0] = 2;
pstSoftWareParam->af32PriorBoxAspectRatio[2][1] = 3;
pstSoftWareParam->af32PriorBoxAspectRatio[3][0] = 2;
pstSoftWareParam->af32PriorBoxAspectRatio[3][1] = 3;
pstSoftWareParam->af32PriorBoxAspectRatio[4][0] = 2;
pstSoftWareParam->af32PriorBoxAspectRatio[4][1] = 0;
pstSoftWareParam->af32PriorBoxAspectRatio[5][0] = 2;
pstSoftWareParam->af32PriorBoxAspectRatio[5][1] = 0;
pstSoftWareParam->af32PriorBoxStepWidth[0] = 8;
pstSoftWareParam->af32PriorBoxStepWidth[1] = 16;
pstSoftWareParam->af32PriorBoxStepWidth[2] = 32;
pstSoftWareParam->af32PriorBoxStepWidth[3] = 64;
pstSoftWareParam->af32PriorBoxStepWidth[4] = 100;
pstSoftWareParam->af32PriorBoxStepWidth[5] = 300;
pstSoftWareParam->af32PriorBoxStepHeight[0] = 8;
pstSoftWareParam->af32PriorBoxStepHeight[1] = 16;
pstSoftWareParam->af32PriorBoxStepHeight[2] = 32;
pstSoftWareParam->af32PriorBoxStepHeight[3] = 64;
pstSoftWareParam->af32PriorBoxStepHeight[4] = 100;
pstSoftWareParam->af32PriorBoxStepHeight[5] = 300;
pstSoftWareParam->f32Offset = 0.5f;
pstSoftWareParam->as32PriorBoxVar[0] = (HI_S32)(0.1f*SAMPLE_SVP_NNIE_QUANT_BASE);
pstSoftWareParam->as32PriorBoxVar[1] = (HI_S32)(0.1f*SAMPLE_SVP_NNIE_QUANT_BASE);
pstSoftWareParam->as32PriorBoxVar[2] = (HI_S32)(0.2f*SAMPLE_SVP_NNIE_QUANT_BASE);
pstSoftWareParam->as32PriorBoxVar[3] = (HI_S32)(0.2f*SAMPLE_SVP_NNIE_QUANT_BASE);
/*Set Softmax Parameters*/
pstSoftWareParam->u32SoftMaxInHeight = 21;
pstSoftWareParam->au32SoftMaxInChn[0] = 121296;
pstSoftWareParam->au32SoftMaxInChn[1] = 45486;
pstSoftWareParam->au32SoftMaxInChn[2] = 12600;
pstSoftWareParam->au32SoftMaxInChn[3] = 3150;
pstSoftWareParam->au32SoftMaxInChn[4] = 756;
pstSoftWareParam->au32SoftMaxInChn[5] = 84;
pstSoftWareParam->u32ConcatNum = 6;
pstSoftWareParam->u32SoftMaxOutWidth = 1;
pstSoftWareParam->u32SoftMaxOutHeight = 21;
pstSoftWareParam->u32SoftMaxOutChn = 8732;
/*Set DetectionOut Parameters*/
pstSoftWareParam->u32ClassNum = 21;
pstSoftWareParam->u32TopK = 400;
pstSoftWareParam->u32KeepTopK = 200;
pstSoftWareParam->u32NmsThresh = (HI_U16)(0.3f*SAMPLE_SVP_NNIE_QUANT_BASE);
pstSoftWareParam->u32ConfThresh = 1;
pstSoftWareParam->au32DetectInputChn[0] = 23104;
pstSoftWareParam->au32DetectInputChn[1] = 8664;
pstSoftWareParam->au32DetectInputChn[2] = 2400;
pstSoftWareParam->au32DetectInputChn[3] = 600;
pstSoftWareParam->au32DetectInputChn[4] = 144;
pstSoftWareParam->au32DetectInputChn[5] = 16;
/*Malloc assist buffer memory*/
u32ClassNum = pstSoftWareParam->u32ClassNum;
u32TotalSize = SAMPLE_SVP_NNIE_Ssd_GetResultTmpBuf(pstNnieParam,pstSoftWareParam);
u32DstRoiSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum*pstSoftWareParam->u32TopK*sizeof(HI_U32)*SAMPLE_SVP_NNIE_COORDI_NUM);
u32DstScoreSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum*pstSoftWareParam->u32TopK*sizeof(HI_U32));
u32ClassRoiNumSize = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum*sizeof(HI_U32));
u32TotalSize = u32TotalSize+u32DstRoiSize+u32DstScoreSize+u32ClassRoiNumSize;
s32Ret = SAMPLE_COMM_SVP_MallocCached("SAMPLE_SSD_INIT",NULL,(HI_U64*)&u64PhyAddr,
(void**)&pu8VirAddr,u32TotalSize);
SAMPLE_SVP_CHECK_EXPR_RET(HI_SUCCESS != s32Ret,s32Ret,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error,Malloc memory failed!\n");
memset(pu8VirAddr,0, u32TotalSize);
SAMPLE_COMM_SVP_FlushCache(u64PhyAddr,(void*)pu8VirAddr,u32TotalSize);
/*set each tmp buffer addr*/
pstSoftWareParam->stPriorBoxTmpBuf.u64PhyAddr = u64PhyAddr;
pstSoftWareParam->stPriorBoxTmpBuf.u64VirAddr = (HI_U64)(pu8VirAddr);
pstSoftWareParam->stSoftMaxTmpBuf.u64PhyAddr = u64PhyAddr+
pstSoftWareParam->stPriorBoxTmpBuf.u32Size;
pstSoftWareParam->stSoftMaxTmpBuf.u64VirAddr = (HI_U64)(pu8VirAddr+
pstSoftWareParam->stPriorBoxTmpBuf.u32Size);
pstSoftWareParam->stGetResultTmpBuf.u64PhyAddr = u64PhyAddr+
pstSoftWareParam->stPriorBoxTmpBuf.u32Size+pstSoftWareParam->stSoftMaxTmpBuf.u32Size;
pstSoftWareParam->stGetResultTmpBuf.u64VirAddr = (HI_U64)(pu8VirAddr+
pstSoftWareParam->stPriorBoxTmpBuf.u32Size+ pstSoftWareParam->stSoftMaxTmpBuf.u32Size);
u32TmpBufTotalSize = pstSoftWareParam->stPriorBoxTmpBuf.u32Size+
pstSoftWareParam->stSoftMaxTmpBuf.u32Size + pstSoftWareParam->stGetResultTmpBuf.u32Size;
/*set result blob*/
pstSoftWareParam->stDstRoi.enType = SVP_BLOB_TYPE_S32;
pstSoftWareParam->stDstRoi.u64PhyAddr = u64PhyAddr+u32TmpBufTotalSize;
pstSoftWareParam->stDstRoi.u64VirAddr = (HI_U64)(pu8VirAddr+u32TmpBufTotalSize);
pstSoftWareParam->stDstRoi.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum*
pstSoftWareParam->u32TopK*sizeof(HI_U32)*SAMPLE_SVP_NNIE_COORDI_NUM);
pstSoftWareParam->stDstRoi.u32Num = 1;
pstSoftWareParam->stDstRoi.unShape.stWhc.u32Chn = 1;
pstSoftWareParam->stDstRoi.unShape.stWhc.u32Height = 1;
pstSoftWareParam->stDstRoi.unShape.stWhc.u32Width = u32ClassNum*
pstSoftWareParam->u32TopK*SAMPLE_SVP_NNIE_COORDI_NUM;
pstSoftWareParam->stDstScore.enType = SVP_BLOB_TYPE_S32;
pstSoftWareParam->stDstScore.u64PhyAddr = u64PhyAddr+u32TmpBufTotalSize+u32DstRoiSize;
pstSoftWareParam->stDstScore.u64VirAddr = (HI_U64)(pu8VirAddr+u32TmpBufTotalSize+u32DstRoiSize);
pstSoftWareParam->stDstScore.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum*
pstSoftWareParam->u32TopK*sizeof(HI_U32));
pstSoftWareParam->stDstScore.u32Num = 1;
pstSoftWareParam->stDstScore.unShape.stWhc.u32Chn = 1;
pstSoftWareParam->stDstScore.unShape.stWhc.u32Height = 1;
pstSoftWareParam->stDstScore.unShape.stWhc.u32Width = u32ClassNum*
pstSoftWareParam->u32TopK;
pstSoftWareParam->stClassRoiNum.enType = SVP_BLOB_TYPE_S32;
pstSoftWareParam->stClassRoiNum.u64PhyAddr = u64PhyAddr+u32TmpBufTotalSize+
u32DstRoiSize+u32DstScoreSize;
pstSoftWareParam->stClassRoiNum.u64VirAddr = (HI_U64)(pu8VirAddr+u32TmpBufTotalSize+
u32DstRoiSize+u32DstScoreSize);
pstSoftWareParam->stClassRoiNum.u32Stride = SAMPLE_SVP_NNIE_ALIGN16(u32ClassNum*sizeof(HI_U32));
pstSoftWareParam->stClassRoiNum.u32Num = 1;
pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Chn = 1;
pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Height = 1;
pstSoftWareParam->stClassRoiNum.unShape.stWhc.u32Width = u32ClassNum;
return s32Ret;
}
/******************************************************************************
* function : Ssd init
******************************************************************************/
static HI_S32 SAMPLE_SVP_NNIE_Ssd_ParamInit(SAMPLE_SVP_NNIE_CFG_S* pstCfg,
SAMPLE_SVP_NNIE_PARAM_S *pstNnieParam, SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S* pstSoftWareParam)
{
HI_S32 s32Ret = HI_SUCCESS;
/*init hardware para*/
s32Ret = SAMPLE_COMM_SVP_NNIE_ParamInit(pstCfg,pstNnieParam);
SAMPLE_SVP_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret,INIT_FAIL_0,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error(%#x),SAMPLE_COMM_SVP_NNIE_ParamInit failed!\n",s32Ret);
/*init software para*/
s32Ret = SAMPLE_SVP_NNIE_Ssd_SoftwareInit(pstCfg,pstNnieParam,
pstSoftWareParam);
SAMPLE_SVP_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret,INIT_FAIL_0,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error(%#x),SAMPLE_SVP_NNIE_Ssd_SoftwareInit failed!\n",s32Ret);
return s32Ret;
INIT_FAIL_0:
s32Ret = SAMPLE_SVP_NNIE_Ssd_Deinit(pstNnieParam,pstSoftWareParam,NULL);
SAMPLE_SVP_CHECK_EXPR_RET(HI_SUCCESS != s32Ret,s32Ret,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error(%#x),SAMPLE_SVP_NNIE_Ssd_Deinit failed!\n",s32Ret);
return HI_FAILURE;
}
void detect(HI_CHAR * pcSrcFile,HI_FLOAT f32PrintResultThresh)
{
//------------------------------------------------------
//从这里填充图像数据到下面forward拿到结果
//计时 lcg
IplImage *imgSrc = 0;
imgSrc = cvLoadImage(pcSrcFile, 1);
if (imgSrc == 0) {
printf("Load image error\n");
return -1;
}
//--resize to 300*300
IplImage *img;
CvSize dstSize;
dstSize.height = 300;
dstSize.width = 300;
img = cvCreateImage(dstSize,imgSrc->depth,imgSrc->nChannels);
cvResize(imgSrc,img,CV_INTER_CUBIC);
HI_U8 *data = (HI_U8*)img->imageData;
int step = img->widthStep;
int h = img->height;
int w = img->width;
int c = img->nChannels;
IplImage *bgrImg = 0;
CvSize sz;
sz.width = w;
sz.height = h;
bgrImg = cvCreateImage(sz,img->depth,img->nChannels);
HI_U8* bgr = (HI_U8*)bgrImg->imageData;
int offset = 0;
//注意遍历顺序
for (int k=0; k<c; k++) {
for (int i=0; i<h; i++) {
for(int j=0; j<w; j++) {
bgr[offset] = data[i*step + j*c + k];
offset++;
}
}
}
/*Fill src data*/
SAMPLE_SVP_TRACE_INFO("Ssd start!\n");
stInputDataIdx.u32SegIdx = 0;
stInputDataIdx.u32NodeIdx = 0;
s32Ret = SAMPLE_SVP_NNIE_FillSrcDataFromFrame(bgr,&s_stSsdNnieParam,&stInputDataIdx);
SAMPLE_SVP_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret,SSD_FAIL_0,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error,SAMPLE_SVP_NNIE_FillSrcData failed!\n");
/*NNIE process(process the 0-th segment)*/
stProcSegIdx.u32SegIdx = 0;
s32Ret = SAMPLE_SVP_NNIE_Forward(&s_stSsdNnieParam,&stInputDataIdx,&stProcSegIdx,HI_TRUE);
SAMPLE_SVP_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret,SSD_FAIL_0,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error,SAMPLE_SVP_NNIE_Forward failed!\n");
/*software process*/
/*if user has changed net struct, please make sure SAMPLE_SVP_NNIE_Ssd_GetResult
function's input datas are correct*/
s32Ret = SAMPLE_SVP_NNIE_Ssd_GetResult(&s_stSsdNnieParam,&s_stSsdSoftwareParam);
SAMPLE_SVP_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret,SSD_FAIL_0,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error,SAMPLE_SVP_NNIE_Ssd_GetResult failed!\n");
/*print result, this sample has 21 classes:
class 0:background class 1:plane class 2:bicycle
class 3:bird class 4:boat class 5:bottle
class 6:bus class 7:car class 8:cat
class 9:chair class10:cow class11:diningtable
class 12:dog class13:horse class14:motorbike
class 15:person class16:pottedplant class17:sheep
class 18:sofa class19:train class20:tvmonitor*/
SAMPLE_SVP_TRACE_INFO("Ssd result:\n");
(void)SAMPLE_SVP_NNIE_Detection_PrintResult(&s_stSsdSoftwareParam.stDstScore,
&s_stSsdSoftwareParam.stDstRoi, &s_stSsdSoftwareParam.stClassRoiNum,f32PrintResultThresh);
SSD_FAIL_0:
SAMPLE_SVP_NNIE_Ssd_Deinit(&s_stSsdNnieParam,&s_stSsdSoftwareParam,&s_stSsdModel);
SAMPLE_COMM_SVP_CheckSysExit();
}
/******************************************************************************
* function : SSD sample signal handle
******************************************************************************/
void SAMPLE_SVP_NNIE_Ssd_HandleSig(void)
{
SAMPLE_SVP_NNIE_Ssd_Deinit(&s_stSsdNnieParam,&s_stSsdSoftwareParam,&s_stSsdModel);
memset(&s_stSsdNnieParam,0,sizeof(SAMPLE_SVP_NNIE_PARAM_S));
memset(&s_stSsdSoftwareParam,0,sizeof(SAMPLE_SVP_NNIE_SSD_SOFTWARE_PARAM_S));
memset(&s_stSsdModel,0,sizeof(SAMPLE_SVP_NNIE_MODEL_S));
SAMPLE_COMM_SVP_CheckSysExit();
}
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("input error\n./detect picName\n");
return 0;
}
HI_CHAR *pcSrcFile = argv[1];
printf("pic: %s\n",pcSrcFile);
HI_CHAR *pcModelName = "./data/nnie_model/detection/ssd_detect.wk";
HI_U32 u32PicNum = 1;
HI_FLOAT f32PrintResultThresh = 0.0f;
/*Set configuration parameter*/
f32PrintResultThresh = 0.8f;
stNnieCfg.pszPic= pcSrcFile;
stNnieCfg.u32MaxInputNum = u32PicNum; //max input image num in each batch
stNnieCfg.u32MaxRoiNum = 0;
stNnieCfg.aenNnieCoreId[0] = SVP_NNIE_ID_0;//set NNIE core
/*Sys init*/
SAMPLE_COMM_SVP_CheckSysInit();
/*Ssd Load model*/
SAMPLE_SVP_TRACE_INFO("Ssd Load model!\n");
s32Ret = SAMPLE_COMM_SVP_NNIE_LoadModel(pcModelName,&s_stSsdModel);
SAMPLE_SVP_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret,SSD_FAIL_0,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error,SAMPLE_COMM_SVP_NNIE_LoadModel failed!\n");
/*Ssd parameter initialization*/
/*Ssd parameters are set in SAMPLE_SVP_NNIE_Ssd_SoftwareInit,
if user has changed net struct, please make sure the parameter settings in
SAMPLE_SVP_NNIE_Ssd_SoftwareInit function are correct*/
SAMPLE_SVP_TRACE_INFO("Ssd parameter initialization!\n");
s_stSsdNnieParam.pstModel = &s_stSsdModel.stModel;
s32Ret = SAMPLE_SVP_NNIE_Ssd_ParamInit(&stNnieCfg,&s_stSsdNnieParam,&s_stSsdSoftwareParam);
SAMPLE_SVP_CHECK_EXPR_GOTO(HI_SUCCESS != s32Ret,SSD_FAIL_0,SAMPLE_SVP_ERR_LEVEL_ERROR,
"Error,SAMPLE_SVP_NNIE_Ssd_ParamInit failed!\n");
gettimeofday(&start, NULL);
detect(pcSrcFile,f32PrintResultThresh);
gettimeofday(&end, NULL);
long timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
printf("gettimeofday time: %ld\n",timeuse);
SSD_FAIL_0:
SAMPLE_SVP_NNIE_Ssd_Deinit(&s_stSsdNnieParam,&s_stSsdSoftwareParam,&s_stSsdModel);
SAMPLE_COMM_SVP_CheckSysExit();
}