Skip to content

Commit

Permalink
fix ai demo bug (#430)
Browse files Browse the repository at this point in the history
* fix ai demo bug
* fix uart demo bug
  • Loading branch information
wycwyhwyq authored Dec 30, 2022
1 parent 6c74596 commit 0cc008e
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 52 deletions.
4 changes: 2 additions & 2 deletions package/ai/code/face_detect/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void ai_worker(ai_worker_args ai_args)

for (auto p : points_to_clear[index])
{
cv::circle(img_argb, p, 8, cv::Scalar(0, 0, 0, 0), -1);
cv::circle(img_argb, p, 4, cv::Scalar(0, 0, 0, 0), -1);
}
}

Expand Down Expand Up @@ -242,7 +242,7 @@ void ai_worker(ai_worker_args ai_args)
{
int32_t x0 = l.points[2 * ll + 0] * screen_width * net_len / valid_width;
int32_t y0 = l.points[2 * ll + 1] * net_len / valid_height * screen_height;
cv::circle(img_argb, cv::Point(x0, y0), 8, cv::Scalar(0, 0, 255, 255), -1);
cv::circle(img_argb, cv::Point(x0, y0), 4, cv::Scalar(0, 0, 255, 255), -1);
points_to_clear[index].push_back(cv::Point(x0, y0));
}
}
Expand Down
4 changes: 2 additions & 2 deletions package/ai/code/face_expression/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void ai_worker(ai_worker_args ai_args)

for (uint32_t cc = 0; cc < points_to_clear[index].size(); cc++)
{
cv::putText(img_argb, strs_to_clear[index][cc], points_to_clear[index][cc], cv::FONT_HERSHEY_COMPLEX, 2, cv::Scalar(0, 0, 0, 0), 2, 8, 0);
cv::putText(img_argb, strs_to_clear[index][cc], points_to_clear[index][cc], cv::FONT_HERSHEY_COMPLEX, 1, cv::Scalar(0, 0, 0, 0), 1, 8, 0);
}
}

Expand Down Expand Up @@ -283,7 +283,7 @@ void ai_worker(ai_worker_args ai_args)
std::string text = fexpre.labels[score_index] + ":" + std::to_string(round(score_max * 100) / 100.0).substr(0,4);
int x0 = std::max(0, std::min(int((b.x - b.w / 2) * fd_net_len / valid_width * screen_width), (int)screen_width));
int y0 = std::max(0, std::min(int((b.y - b.h / 2) * fd_net_len / valid_height * screen_height) + 60, (int)screen_height));
cv::putText(img_argb, text, cv::Point(x0, y0), cv::FONT_HERSHEY_COMPLEX, 2, cv::Scalar(255, 0, 0, 255), 2, 8, 0);
cv::putText(img_argb, text, cv::Point(x0, y0), cv::FONT_HERSHEY_COMPLEX, 1, cv::Scalar(255, 0, 0, 255), 1, 8, 0);
points_to_clear[index].push_back(cv::Point(x0, y0));
strs_to_clear[index].push_back(text);
}
Expand Down
28 changes: 18 additions & 10 deletions package/ai/code/hand_image_classify/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ void ai_worker(ai_worker_args ai_args)

// define cv::Mat for ai input
// padding offset is (hd_net_len - valid_width) / 2
cv::Mat rgb24_img_for_ai(hd_net_len, hd_net_len, CV_8UC3, hd.virtual_addr_input[0] + (hd_net_len - valid_width) / 2);
cv::Mat rgb24_img_for_ai(hd_net_len, hd_net_len, CV_8UC3, hd.virtual_addr_input[0] + (hd_net_len - valid_width) / 2 + (hd_net_len - valid_height) / 2 * hd_net_len);
// define cv::Mat for post process
cv::Mat ori_img_R = cv::Mat(hd_net_len, hd_net_len, CV_8UC1, hd.virtual_addr_input[0] + (hd_net_len - valid_width) / 2);
cv::Mat ori_img_G = cv::Mat(hd_net_len, hd_net_len, CV_8UC1, hd.virtual_addr_input[0] + offset_channel + (hd_net_len - valid_width) / 2);
cv::Mat ori_img_B = cv::Mat(hd_net_len, hd_net_len, CV_8UC1, hd.virtual_addr_input[0] + offset_channel * 2 + (hd_net_len - valid_width) / 2);
cv::Mat ori_img_R = cv::Mat(hd_net_len, hd_net_len, CV_8UC1, rgb24_img_for_ai.data);
cv::Mat ori_img_G = cv::Mat(hd_net_len, hd_net_len, CV_8UC1, rgb24_img_for_ai.data + offset_channel);
cv::Mat ori_img_B = cv::Mat(hd_net_len, hd_net_len, CV_8UC1, rgb24_img_for_ai.data + offset_channel * 2);


cv::Mat hld_img_R = cv::Mat(hld_net_len, hld_net_len, CV_8UC1, hkd.virtual_addr_input[0]);
Expand Down Expand Up @@ -185,8 +185,9 @@ void ai_worker(ai_worker_args ai_args)
if (gnne_valid_width < gnne_input_width) {
uint32_t padding_r = (gnne_input_width - gnne_valid_width);
uint32_t padding_l = padding_r / 2;
uint32_t row_offset = (gnne_input_height - gnne_valid_height) / 2;
padding_r -= padding_l;
for (int row = 0; row < gnne_valid_height; row++) {
for (int row = row_offset; row < row_offset + gnne_valid_height; row++) {
uint32_t offset_l = row * gnne_input_width;
uint32_t offset_r = offset_l + gnne_valid_width + padding_l;
memset(r_addr + offset_l, PADDING_R, padding_l);
Expand All @@ -198,11 +199,18 @@ void ai_worker(ai_worker_args ai_args)
}
}
if (gnne_valid_height < gnne_input_height) {
uint32_t padding = (gnne_input_height - gnne_valid_height) * gnne_input_width;
uint32_t offset = gnne_valid_height * gnne_input_width;
memset(r_addr + offset, PADDING_R, padding);
memset(g_addr + offset, PADDING_G, padding);
memset(b_addr + offset, PADDING_B, padding);
uint32_t padding_t = gnne_input_height - gnne_valid_height;
uint32_t padding_b = padding_t / 2;
padding_t -= padding_b;
padding_t *= gnne_input_width;
padding_b *= gnne_input_width;
uint32_t offset = padding_t + gnne_valid_height * gnne_input_width;
memset(r_addr, PADDING_R, padding_t);
memset(g_addr, PADDING_G, padding_t);
memset(b_addr, PADDING_B, padding_t);
memset(r_addr + offset, PADDING_R, padding_b);
memset(g_addr + offset, PADDING_G, padding_b);
memset(b_addr + offset, PADDING_B, padding_b);
}
if(enable_dump_image)
{
Expand Down
22 changes: 15 additions & 7 deletions package/ai/code/object_detect/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void ai_worker(ai_worker_args ai_args)

// define cv::Mat for ai input
// padding offset is (net_len - valid_width) / 2
cv::Mat rgb24_img_for_ai(net_len, net_len, CV_8UC3, od.virtual_addr_input[0] + (net_len - valid_width) / 2);
cv::Mat rgb24_img_for_ai(net_len, net_len, CV_8UC3, od.virtual_addr_input[0] + (net_len - valid_width) / 2 + (net_len - valid_height) / 2 * net_len);
while(quit.load())
{
bool ret = false;
Expand All @@ -148,8 +148,9 @@ void ai_worker(ai_worker_args ai_args)
if (gnne_valid_width < gnne_input_width) {
uint32_t padding_r = (gnne_input_width - gnne_valid_width);
uint32_t padding_l = padding_r / 2;
uint32_t row_offset = (gnne_input_height - gnne_valid_height) / 2;
padding_r -= padding_l;
for (int row = 0; row < gnne_valid_height; row++) {
for (int row = row_offset; row < row_offset + gnne_valid_height; row++) {
uint32_t offset_l = row * gnne_input_width;
uint32_t offset_r = offset_l + gnne_valid_width + padding_l;
memset(r_addr + offset_l, PADDING_R, padding_l);
Expand All @@ -161,11 +162,18 @@ void ai_worker(ai_worker_args ai_args)
}
}
if (gnne_valid_height < gnne_input_height) {
uint32_t padding = (gnne_input_height - gnne_valid_height) * gnne_input_width;
uint32_t offset = gnne_valid_height * gnne_input_width;
memset(r_addr + offset, PADDING_R, padding);
memset(g_addr + offset, PADDING_G, padding);
memset(b_addr + offset, PADDING_B, padding);
uint32_t padding_t = gnne_input_height - gnne_valid_height;
uint32_t padding_b = padding_t / 2;
padding_t -= padding_b;
padding_t *= gnne_input_width;
padding_b *= gnne_input_width;
uint32_t offset = padding_t + gnne_valid_height * gnne_input_width;
memset(r_addr, PADDING_R, padding_t);
memset(g_addr, PADDING_G, padding_t);
memset(b_addr, PADDING_B, padding_t);
memset(r_addr + offset, PADDING_R, padding_b);
memset(g_addr + offset, PADDING_G, padding_b);
memset(b_addr + offset, PADDING_B, padding_b);
}
if(enable_dump_image)
{
Expand Down
31 changes: 19 additions & 12 deletions package/ai/code/object_detect_demo/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ void ai_worker()
float obj_thresh = 0.2;
float nms_thresh = 0.45;
int offset_channel = gnne_input_width * gnne_input_height;
uint32_t padding_r, padding_l;
objectDetect od(obj_thresh, nms_thresh);
objectDetect od(obj_thresh, nms_thresh, YOLOV5_FIX_SIZE, {gnne_valid_width, gnne_valid_height});

od.load_model(kmodel_name);

Expand All @@ -137,10 +136,7 @@ void ai_worker()
capture.set(cv::CAP_PROP_FRAME_HEIGHT, gnne_input_height);
capture.set(cv::CAP_PROP_FOURCC, dev_info[0].video_out_format[3] ? V4L2_PIX_FMT_ARGB32 : V4L2_PIX_FMT_RGB24);
mtx.unlock();
padding_r = (gnne_input_width - gnne_valid_width);
padding_l = padding_r / 2;
padding_r -= padding_l;
cv::Mat rgb24_img_for_ai(YOLOV5_FIX_SIZE, YOLOV5_FIX_SIZE, CV_8UC3, od.virtual_addr_input[0] + padding_l);
cv::Mat rgb24_img_for_ai(YOLOV5_FIX_SIZE, YOLOV5_FIX_SIZE, CV_8UC3, od.virtual_addr_input[0] + (gnne_input_height - gnne_valid_height) / 2 * gnne_input_width + (gnne_input_width - gnne_valid_width) / 2);

while(quit.load())
{
Expand All @@ -165,7 +161,11 @@ void ai_worker()
g_addr = (uint8_t *)od.virtual_addr_input[0] + offset_channel;
b_addr = (uint8_t *)od.virtual_addr_input[0] + offset_channel * 2;
if (gnne_valid_width < gnne_input_width) {
for (int row = 0; row < gnne_valid_height; row++) {
uint32_t padding_r = gnne_input_width - gnne_valid_width;
uint32_t padding_l = padding_r / 2;
uint32_t row_offset = (gnne_input_height - gnne_valid_height) / 2;
padding_r -= padding_l;
for (int row = row_offset; row < row_offset + gnne_valid_height; row++) {
uint32_t offset_l = row * gnne_input_width;
uint32_t offset_r = offset_l + gnne_valid_width + padding_l;
memset(r_addr + offset_l, 114, padding_l);
Expand All @@ -177,11 +177,18 @@ void ai_worker()
}
}
if (gnne_valid_height < gnne_input_height) {
uint32_t padding = (gnne_input_height - gnne_valid_height) * gnne_input_width;
uint32_t offset = gnne_valid_height * gnne_input_width;
memset(r_addr + offset, 114, padding);
memset(g_addr + offset, 114, padding);
memset(b_addr + offset, 114, padding);
uint32_t padding_t = gnne_input_height - gnne_valid_height;
uint32_t padding_b = padding_t / 2;
padding_t -= padding_b;
padding_t *= gnne_input_width;
padding_b *= gnne_input_width;
uint32_t offset = padding_t + gnne_valid_height * gnne_input_width;
memset(r_addr, 114, padding_t);
memset(g_addr, 114, padding_t);
memset(b_addr, 114, padding_t);
memset(r_addr + offset, 114, padding_b);
memset(g_addr + offset, 114, padding_b);
memset(b_addr + offset, 114, padding_b);
}
if(display_ds2)
{
Expand Down
4 changes: 2 additions & 2 deletions package/ai/code/object_detect_demo/object_detect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ void objectDetect::post_process(std::vector<BoxInfo> &result)
nms(result, nms_thresh);
}

objectDetect::objectDetect(float obj_thresh, float nms_thresh)
:obj_thresh(obj_thresh), nms_thresh(nms_thresh)
objectDetect::objectDetect(float obj_thresh, float nms_thresh, int net_len, Framesize frame_size)
:obj_thresh(obj_thresh), nms_thresh(nms_thresh), net_len(net_len), frame_size(frame_size)
{
int first_len = net_len / 8;
first_size = first_len * first_len;
Expand Down
8 changes: 4 additions & 4 deletions package/ai/code/object_detect_demo/object_detect.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ using namespace std;
#define GNNE_INPUT_WIDTH 320
#define GNNE_INPUT_HEIGHT 240
#endif
#define INPUT_SIZE (YOLOV5_FIX_SIZE * YOLOV5_FIX_SIZE * 3)
#define INPUT_SIZE (YOLOV5_FIX_SIZE * YOLOV5_FIX_SIZE * 4)

#define OUTPUT_SIZE0 (255*40*40*4)
#define OUTPUT_SIZE1 (255*20*20*4)
Expand All @@ -86,7 +86,7 @@ struct share_memory_alloc_align_args {
class objectDetect
{
public:
objectDetect(float obj_thresh, float nms_thresh);
objectDetect(float obj_thresh, float nms_thresh, int net_len, Framesize frame_size);
void prepare_memory();
void set_input(uint32_t index);
void set_output();
Expand Down Expand Up @@ -123,7 +123,7 @@ class objectDetect
private:
float obj_thresh;
float nms_thresh;
Framesize frame_size = {GNNE_INPUT_WIDTH, GNNE_INPUT_HEIGHT};
Framesize frame_size;
uint32_t output_size[3] = {
OUTPUT_SIZE0,
OUTPUT_SIZE1,
Expand All @@ -133,7 +133,7 @@ class objectDetect
interpreter interp_od;
vector<unsigned char> od_model;

int net_len = YOLOV5_FIX_SIZE;
int net_len;
int anchors_num = 3;
int classes_num = 80;
int channels = anchors_num * (5 + classes_num);
Expand Down
28 changes: 18 additions & 10 deletions package/ai/code/simple_pose/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ void ai_worker(ai_worker_args ai_args)

// define cv::Mat for ai input
// padding offset is (valid_width - valid_height) / 2 * valid_width
cv::Mat rgb24_img_for_ai(body_net_len, body_net_len, CV_8UC3, od.virtual_addr_input[0] + (body_net_len - valid_width) / 2);
cv::Mat rgb24_img_for_ai(body_net_len, body_net_len, CV_8UC3, od.virtual_addr_input[0] + (body_net_len - valid_width) / 2 + (body_net_len - valid_height) / 2 * body_net_len);
// define cv::Mat for post process
cv::Mat ori_img_R = cv::Mat(body_net_len, body_net_len, CV_8UC1, od.virtual_addr_input[0] + (body_net_len - valid_width) / 2);
cv::Mat ori_img_G = cv::Mat(body_net_len, body_net_len, CV_8UC1, od.virtual_addr_input[0] + offset_channel + (body_net_len - valid_width) / 2);
cv::Mat ori_img_B = cv::Mat(body_net_len, body_net_len, CV_8UC1, od.virtual_addr_input[0] + offset_channel * 2 + (body_net_len - valid_width) / 2);
cv::Mat ori_img_R = cv::Mat(body_net_len, body_net_len, CV_8UC1, rgb24_img_for_ai.data);
cv::Mat ori_img_G = cv::Mat(body_net_len, body_net_len, CV_8UC1, rgb24_img_for_ai.data + offset_channel);
cv::Mat ori_img_B = cv::Mat(body_net_len, body_net_len, CV_8UC1, rgb24_img_for_ai.data + offset_channel * 2);

cv::Mat spo_img_R = cv::Mat(pose_height, pose_width, CV_8UC1, spo.virtual_addr_input[0]);
cv::Mat spo_img_G = cv::Mat(pose_height, pose_width, CV_8UC1, spo.virtual_addr_input[0] + pose_height * pose_width);
Expand Down Expand Up @@ -165,8 +165,9 @@ void ai_worker(ai_worker_args ai_args)
if (gnne_valid_width < gnne_input_width) {
uint32_t padding_r = (gnne_input_width - gnne_valid_width);
uint32_t padding_l = padding_r / 2;
uint32_t row_offset = (gnne_input_height - gnne_valid_height) / 2;
padding_r -= padding_l;
for (int row = 0; row < gnne_valid_height; row++) {
for (int row = row_offset; row < row_offset + gnne_valid_height; row++) {
uint32_t offset_l = row * gnne_input_width;
uint32_t offset_r = offset_l + gnne_valid_width + padding_l;
memset(r_addr + offset_l, PADDING_R, padding_l);
Expand All @@ -178,11 +179,18 @@ void ai_worker(ai_worker_args ai_args)
}
}
if (gnne_valid_height < gnne_input_height) {
uint32_t padding = (gnne_input_height - gnne_valid_height) * gnne_input_width;
uint32_t offset = gnne_valid_height * gnne_input_width;
memset(r_addr + offset, PADDING_R, padding);
memset(g_addr + offset, PADDING_G, padding);
memset(b_addr + offset, PADDING_B, padding);
uint32_t padding_t = gnne_input_height - gnne_valid_height;
uint32_t padding_b = padding_t / 2;
padding_t -= padding_b;
padding_t *= gnne_input_width;
padding_b *= gnne_input_width;
uint32_t offset = padding_t + gnne_valid_height * gnne_input_width;
memset(r_addr, PADDING_R, padding_t);
memset(g_addr, PADDING_G, padding_t);
memset(b_addr, PADDING_B, padding_t);
memset(r_addr + offset, PADDING_R, padding_b);
memset(g_addr + offset, PADDING_G, padding_b);
memset(b_addr + offset, PADDING_B, padding_b);
}
if(enable_dump_image)
{
Expand Down
12 changes: 9 additions & 3 deletions package/uart/src/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int OpenUart(char *UART_DEV)
{
return -1;
}
SetOpt(fd);
// SetOpt(fd);
return fd;
}

Expand Down Expand Up @@ -113,9 +113,14 @@ void main(int argc, char **argv)
printf(" COM Open Fail !");
return;
}
SetOpt(fd);
// SetOpt(fd);
int TxLen = UartSend(fd, send_str, strlen(send_str));
printf("\nTxLen = %d\n", TxLen);
if (TxLen < 0)
{
printf("send error\n");
goto exit;
}
while (1)
{
while (((RxLen = UartRead(fd, RxBuff, TxLen)) > 0))
Expand All @@ -127,6 +132,7 @@ void main(int argc, char **argv)
if (flag == 1)
break;
}

exit:
tcflush(fd, TCIFLUSH);
UartClose(fd);
}

0 comments on commit 0cc008e

Please sign in to comment.