Skip to content

Commit 218052e

Browse files
committed
[Example][FD] New Face detect example: execution environment w/o model
1 parent 30a4584 commit 218052e

19 files changed

+490
-1589
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ include(../cmake/settings.cmake)
1313
add_subdirectory(hello_world)
1414
add_subdirectory(example_cifar10_caffe)
1515
add_subdirectory(example_har_smartphone)
16-
17-
# Example needs update
18-
# add_subdirectory(example_face_detect)
16+
add_subdirectory(example_face_detect)
1917

2018
if (ARC AND NOT (${MLI_PLATFORM} STREQUAL EM_HS) AND (NOT "$ENV{TENSORFLOW_DIR}" STREQUAL ""))
2119
add_subdirectory(tutorial_emnist_tflm)
Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,72 @@
11
#
2-
# Copyright 2020, Synopsys, Inc.
2+
# Copyright 2020-2021, Synopsys, Inc.
33
# All rights reserved.
44
#
55
# This source code is licensed under the BSD-3-Clause license found in
66
# the LICENSE file in the root directory of this source tree.
77
#
88

9+
project(face_detect)
10+
911
include(../../cmake/settings.cmake)
1012

11-
add_executable(example_face_detect
13+
set(TARGET example_face_detect)
14+
15+
add_executable(${TARGET}
1216
bmp_file_io.c
13-
face_trigger_model.c
14-
main.c
15-
sliding_scan.c
17+
bmp_file_io.h
18+
face_detect_module.cc
19+
face_detect_module.h
20+
main.cc
1621
)
1722

18-
target_link_libraries(example_face_detect PUBLIC mli)
23+
target_link_libraries(${TARGET} PUBLIC mli)
1924

20-
target_include_directories(example_face_detect PRIVATE
25+
target_include_directories(${TARGET} PRIVATE
2126
../../examples/auxiliary
2227
)
2328

2429
if (ARC)
2530
set(EXAMPLE_FLAGS
2631
-Hnocopyr
2732
-Hpurge
28-
-Hheap=20K
29-
-Hstack=4K
30-
-Hfxapi
33+
-Hheap=50K
34+
-Hstack=8K
3135
-e_start
3236
-Bgrouplib
3337
-Hldopt=-q
3438
-Hsdata0
35-
-Xdsp_ctrl=postshift,guard,convergent
3639
-Hdense_prologue
40+
-Hcl
41+
-Hcrt_argv
42+
-Hcrt_fast_memcpy
43+
-Hcrt_fast_memset
44+
-Hxcheck
45+
-Hcrt_initbss
46+
3747
)
3848
else()
3949
set(EXAMPLE_FLAGS)
4050
endif()
4151

42-
target_compile_options(example_face_detect PRIVATE
52+
target_compile_options(${TARGET} PRIVATE
4353
${MLI_PLATFORM_COMPILE_OPTIONS}
4454
${EXAMPLE_FLAGS}
4555
)
4656

47-
target_link_options(example_face_detect PRIVATE
57+
target_link_options(${TARGET} PRIVATE
4858
${MLI_PLATFORM_LINK_OPTIONS}
4959
${EXAMPLE_FLAGS}
5060
)
5161

5262
if (ARC)
53-
target_link_options(example_face_detect PRIVATE
63+
target_link_options(${TARGET} PRIVATE
5464
-m
55-
-Coutput=./elf_files/example_face_detect.map
65+
-Coutput=./bin/${TARGET}.map
5666
)
5767
endif()
5868

59-
set_target_properties(example_face_detect
69+
set_target_properties(${TARGET}
6070
PROPERTIES
61-
RUNTIME_OUTPUT_DIRECTORY "./elf_files$<0:>"
71+
RUNTIME_OUTPUT_DIRECTORY "./bin$<0:>"
6272
)
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
#
2-
# Copyright 2019-2020, Synopsys, Inc.
2+
# Copyright 2019-2021, Synopsys, Inc.
33
# All rights reserved.
44
#
55
# This source code is licensed under the BSD-3-Clause license found in
66
# the LICENSE file in the root directory of this source tree.
77
#
88

9-
# TCF_FILE: withouts NOT setting this parameter,
10-
# the default native compiler is used to compile
11-
# BUILDLIB_DIR ?= ev71_buildlib can be specified if no MWDT buildlib is available
12-
# See rules.mk for all flags
13-
14-
PUBLIC_DIR = ../..
9+
PUBLIC_DIR = $(CURDIR)/../..
1510
include $(PUBLIC_DIR)/make/rules.mk
1611

1712
BUILD_SUBDIR = examples$(PS)example_face_detect
18-
BUILD_TARGET = example_face_detect
19-
RUN_ARGS ?= "grace_hopper.bmp"
13+
BIN_PATH = $(BUILD_DIR)$(PS)$(BUILD_SUBDIR)$(PS)bin
2014

21-
app: build
15+
BUILD_TARGET = example_face_detect
16+
RUN_ARGS ?=
2217

23-
run: app
2418
ifndef TCF_FILE
25-
$(BUILD_DIR)$(PS)$(BUILD_SUBDIR)$(PS)elf_files$(PS)example_face_detect
19+
RUN_APP_CMD =
2620
else
27-
mdb $(BUILD_DIR)$(PS)$(BUILD_SUBDIR)$(PS)elf_files$(PS)example_face_detect.elf \
28-
-cl -nsim -cmd=run -off=cr_for_more -cmd=exit -tcf=$(TCF_FILE) -- $(RUN_ARGS)
21+
RUN_APP_CMD = mdb64 -cl -nsim -cmd=run -off=cr_for_more -cmd=exit -tcf=$(TCF_FILE)
22+
BIN_EXT = .elf
2923
endif
3024

25+
app: build
26+
27+
run: app
28+
$(RUN_APP_CMD) $(BIN_PATH)$(PS)example_face_detect$(BIN_EXT) $(RUN_ARGS)
29+
3130
.PHONY: app run

examples/example_face_detect/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Face Detect Example
22
==============================================
3+
:building_construction: **The example is under re-construction.** :building_construction:
4+
<!--
35
Example shows very basic implementation of the classic object detection via sliding window paradigm.
46
Small binary NN classifier for 36x36 grayscale images outputs positive decision for the images of face, and negative decision on other images. To process bigger image pyramid scaling and sliding is organized.
57
Such approach still useful for deeply embedded applications as a compact and efficient way of triggering a bigger job. Activation function and the Layer 2 are quite unspecific kind of layers and was implemented in the research activity.
@@ -137,4 +139,6 @@ References
137139
----------------------------
138140
> P. Viola and M. Jones. Rapid object detection using a boosted cascade of simple features. In CVPR, 2001
139141
140-
> R. Vaillant, C. Monrocq, and Y. LeCun. Original approach for the localisation of objects in images. IEE Proc. on Vision, Image, and Signal Processing, 1994.
142+
> R. Vaillant, C. Monrocq, and Y. LeCun. Original approach for the localisation of objects in images. IEE Proc. on Vision, Image, and Signal Processing, 1994.
143+
144+
-->

examples/example_face_detect/arcem9d.lcf

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2020, Synopsys, Inc.
2+
* Copyright 2019-2021, Synopsys, Inc.
33
* All rights reserved.
44
*
55
* This source code is licensed under the BSD-3-Clause license found in
@@ -15,10 +15,9 @@
1515
#include <stdlib.h>
1616
#include <string.h>
1717

18-
// Luma coefficients (ITU-R Recommendation BT. 709) for RGB->GS conversion
19-
#define LUMA_COEFF_R (0.2126f)
20-
#define LUMA_COEFF_G (0.7152f)
21-
#define LUMA_COEFF_B (0.0722f)
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
2221

2322
#pragma pack(1)
2423
typedef struct {
@@ -51,23 +50,20 @@ typedef struct {
5150
} rgb_pixel_t;
5251
#pragma pack()
5352

53+
5454
/*
55-
* Reading input image from BMP file with RGB->gray transformation
56-
*/
57-
uint8_t *bmp_read(const char* filename) {
55+
* Reading input image from BMP file with RGB->gray transformation
56+
*/
57+
uint8_t *bmp_rgb_read(const char* filename, int32_t width, int32_t height) {
58+
const uint32_t pixel_size_bytes = 3;
5859
bmp_header_t bmp_header;
5960
bmp_info_header_t bmp_info_header;
6061
rgb_pixel_t rgb_pixel;
61-
int32_t x, y;
62-
uint32_t pixel_size_bytes;
63-
uint8_t *grayscale_image;
6462
FILE *f;
65-
6663
uint8_t* image = NULL;
6764

6865
/* opening the input file */
6966
f = fopen(filename,"rb");
70-
7167
if (f == NULL) {
7268
printf("Failed to open the input file '%s': %s\n", filename, strerror(errno));
7369
return NULL;
@@ -77,65 +73,66 @@ uint8_t *bmp_read(const char* filename) {
7773
fread(&bmp_header, sizeof(bmp_header), 1, f);
7874
fread(&bmp_info_header, sizeof(bmp_info_header), 1, f);
7975

80-
/* checking file format: FD_IN_XRESxFD_IN_YRES 24bit BMP */
76+
/* checking file format: width X height 24bit BMP */
8177
if (bmp_header.type != 0x4D42 ||
82-
bmp_info_header.width != FD_IN_XRES ||
83-
bmp_info_header.height != FD_IN_YRES ||
84-
bmp_info_header.bits != 24 ) {
85-
printf("Invaid input file format: 24bit color BMP is supported\n");
78+
bmp_info_header.width != width ||
79+
bmp_info_header.height != height ||
80+
bmp_info_header.bits != 8 * pixel_size_bytes) {
81+
printf("Invaid input file format: %dx%d 24bit color BMP is expected\n", height, width);
8682
fclose(f);
8783
return NULL;
8884
}
8985

9086
/* Allocating the buffer and reading input file */
91-
grayscale_image = malloc(FD_IN_XRES * FD_IN_YRES);
92-
if (grayscale_image == NULL) {
93-
printf("Failed to allocate %d bytes to read the input file\n", FD_IN_XRES * FD_IN_YRES);
87+
size_t img_size = (size_t)width * height * pixel_size_bytes;
88+
image = malloc(img_size);
89+
if (image == NULL) {
90+
printf("Failed to allocate %d bytes to read the input file\n", width * height);
9491
fclose(f);
9592
return NULL;
9693
}
9794

98-
image = grayscale_image;
99-
pixel_size_bytes = bmp_info_header.bits >> 3;
95+
/* Read the data pixel-by-pixel and convert BGR->RGB.
96+
Note: if "height" is positive, BMP stores the pixel rows "bottom-up" */
97+
for (int y = (height - 1); y >= 0; y--) {
98+
uint8_t *image_row = &image[width * y * pixel_size_bytes];
10099

101-
/* Read the data pixel-by-pixel and convert to 8-bit grayscale.
102-
Note: if "height" is positive, BMP stores the pixel rows "bottom-up" */
103-
for (y = (FD_IN_YRES - 1); y >= 0; y--) {
104-
grayscale_image = (uint8_t*)((uint32_t)image + (FD_IN_XRES * y));
105-
106-
for (x = 0; x < FD_IN_XRES; x++) {
100+
for (int x = 0; x < width; x++) {
101+
const int idx = x * pixel_size_bytes;
107102
fread(&rgb_pixel, pixel_size_bytes, 1, f);
108-
grayscale_image[x] =
109-
(uint8_t)(rgb_pixel.r * LUMA_COEFF_R + rgb_pixel.g * LUMA_COEFF_G + rgb_pixel.b * LUMA_COEFF_B);
103+
image_row[idx + 0] = rgb_pixel.r;
104+
image_row[idx + 1] = rgb_pixel.g;
105+
image_row[idx + 2] = rgb_pixel.b;
110106
}
111107
}
112108
fclose(f);
113109
return image;
114110
}
115111

112+
116113
/*
117-
* Writing input image to BMP file with gray->RGB transformation and mask applying.
118-
*/
119-
void bmp_write_gray(const char* filename, const uint8_t *image, const uint8_t *mask) {
114+
* Writing input image to BMP file with required transformations.
115+
*/
116+
void bmp_rgb_write(const char* filename, const uint8_t* image, int32_t width, int32_t height) {
117+
const int32_t pixel_size_bytes = 3;
120118
const bmp_header_t bmp_header = {
121-
.type = 0x4D42,
122-
.size = FD_IN_XRES * FD_IN_YRES * 3 + (sizeof(bmp_header_t) + sizeof(bmp_info_header_t)),
123-
.offset = sizeof(bmp_header_t) + sizeof(bmp_info_header_t)
119+
.type = 0x4D42,
120+
.size = width * height * pixel_size_bytes + (sizeof(bmp_header_t) + sizeof(bmp_info_header_t)),
121+
.offset = sizeof(bmp_header_t) + sizeof(bmp_info_header_t)
124122
};
125-
123+
126124
const bmp_info_header_t bmp_info_header = {
127-
.size = sizeof(bmp_info_header_t),
128-
.width = FD_IN_XRES,
129-
.height = FD_IN_YRES,
130-
.planes = 1,
131-
.bits = 24,
132-
.compression = 0,
133-
.size_bytes = FD_IN_XRES * FD_IN_YRES * 3
125+
.size = sizeof(bmp_info_header_t),
126+
.width = width,
127+
.height = height,
128+
.planes = 1,
129+
.bits = 8 * pixel_size_bytes,
130+
.compression = 0,
131+
.size_bytes = width * height * pixel_size_bytes
134132
};
135-
133+
136134
/* open the input file */
137135
FILE* f = fopen(filename, "wb");
138-
139136
if (f == NULL) {
140137
printf("Failed to open the output file '%s': %s\n", filename, strerror(errno));
141138
return;
@@ -145,23 +142,20 @@ void bmp_write_gray(const char* filename, const uint8_t *image, const uint8_t *m
145142
fwrite((void*)&bmp_header, sizeof(bmp_header), 1, f);
146143
fwrite((void*)&bmp_info_header, sizeof(bmp_info_header), 1, f);
147144

148-
/* Write BMP pixel-by-pixel with gray -> rgb transformation */
149-
const int pixel_size = bmp_info_header.bits >> 3;
145+
/* Write BMP pixel-by-pixel with rgb->bgr transformation in bottom-up rows order*/
150146
rgb_pixel_t rgb_pixel = {0};
151147
for (int y_idx = bmp_info_header.height - 1; y_idx >= 0; y_idx--) {
152148
for (int x_idx = 0; x_idx < bmp_info_header.width; x_idx++) {
153-
const int idx = y_idx * bmp_info_header.width + x_idx;
154-
const int8_t val = image[idx];
155-
156-
/* Put non-zero pixels from mask to input as red pixels*/
157-
if(mask[idx]) {
158-
rgb_pixel.r = 255;
159-
rgb_pixel.g = rgb_pixel.b = 0;
160-
} else {
161-
rgb_pixel.r = rgb_pixel.g = rgb_pixel.b = val;
162-
}
163-
fwrite((void*)&rgb_pixel, pixel_size, 1, f);
149+
const int idx = (y_idx * bmp_info_header.width + x_idx) * pixel_size_bytes;
150+
rgb_pixel.r = image[idx];
151+
rgb_pixel.g = image[idx + 1];
152+
rgb_pixel.b = image[idx + 2];
153+
154+
fwrite((void*)&rgb_pixel, pixel_size_bytes, 1, f);
164155
}
165156
}
166157
fclose(f);
167-
}
158+
}
159+
#ifdef __cplusplus
160+
}
161+
#endif

0 commit comments

Comments
 (0)