--- /dev/null
+/**
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <mv_barcode.h>
+#include <mv_private.h>
+
+#include <opencv2/core.hpp>
+#include <opencv2/highgui.hpp>
+#include <opencv2/imgproc.hpp>
+#include <opencv2/imgproc/imgproc_c.h>
+#include <opencv2/imgcodecs/legacy/constants_c.h>
+
+#include <error.h>
+#include <argp.h>
+
+#define MAX_ARGS 3
+#define INDEX_MAX 4
+#define BARCODE_STRING_MAX 255
+#define RESULT_STRING_MAX 20
+
+static const char doc[] = "[VQA-barcode]\vmediavision barcode performance test\n\
+ e.g) mv_barcode_test_performance barcode.png result.txt";
+static const char args_doc[] = "SOURCE ANNOTATION";
+
+struct arguments
+{
+ char *source;
+ char *annotation;
+};
+
+#define NANO_PER_SEC ((__clock_t) 1000000000)
+#define NANO_PER_MILLI ((__clock_t) 1000000)
+#define MILLI_PER_SEC ((__clock_t) 1000)
+
+struct timespec diff(struct timespec start, struct timespec end)
+{
+ struct timespec temp;
+ if ((end.tv_nsec - start.tv_nsec) < 0) {
+ temp.tv_sec = end.tv_sec - start.tv_sec - 1;
+ temp.tv_nsec = NANO_PER_SEC + end.tv_nsec - start.tv_nsec;
+ } else {
+ temp.tv_sec = end.tv_sec - start.tv_sec;
+ temp.tv_nsec = end.tv_nsec - start.tv_nsec;
+ }
+ return temp;
+}
+unsigned long gettotalmillisec(const struct timespec time)
+{
+ return time.tv_sec * MILLI_PER_SEC + time.tv_nsec / NANO_PER_MILLI;
+}
+
+int load_image_to_buffer(char *source,
+ unsigned char **buffer,
+ unsigned long *size,
+ unsigned int *width,
+ unsigned int *height)
+{
+ cv::Mat image;
+ image = cv::imread(source);
+
+ if (!image.data)
+ return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+
+ try {
+ cv::cvtColor(image, image, CV_BGR2GRAY);
+ } catch (cv::Exception &e) {
+ return MEDIA_VISION_ERROR_INVALID_OPERATION;
+ }
+
+ *size = image.total() * image.elemSize();
+ *buffer = new unsigned char[*size];
+ std::memcpy(*buffer, image.data, *size);
+
+ *width = image.cols;
+ *height = image.rows;
+
+ return MEDIA_VISION_ERROR_NONE;
+}
+
+void barcode_detected_cb(
+ mv_source_h source,
+ mv_engine_config_h engine_cfg,
+ const mv_quadrangle_s *barcodes_locations,
+ const char *messages[],
+ const mv_barcode_type_e *types,
+ int number_of_barcodes,
+ void *user_data)
+{
+ MEDIA_VISION_FUNCTION_ENTER();
+
+ struct arguments *arguments = (struct arguments *) user_data;
+ FILE *fp;
+ char buf[BARCODE_STRING_MAX + 1] = {0};
+ char result[INDEX_MAX][RESULT_STRING_MAX] = {"no_detect", "match", "no_match", "failed"};
+ unsigned int idx = INDEX_MAX -1;
+
+ if (number_of_barcodes == 0)
+ idx = 0;
+ else {
+ size_t message_size = sizeof(messages[0]);
+ fp = fopen(arguments->annotation, "rt");
+ if (fp != NULL) {
+ if (fgets(buf, BARCODE_STRING_MAX, fp) != NULL){
+ if (strncmp(messages[0], buf, message_size) == 0)
+ idx = 1;
+ else
+ idx = 2;
+ }
+ fclose(fp);
+ } else {
+ LOGE("Errors were occurred during opening the file!!! file : %s", arguments->annotation);
+ idx = 3;
+ }
+
+ }
+ printf("%s|%s|%s", arguments->source, arguments->annotation, result[idx]);
+
+ MEDIA_VISION_FUNCTION_LEAVE();
+}
+
+void perform_detect(struct arguments *arguments)
+{
+ MEDIA_VISION_FUNCTION_ENTER();
+
+ mv_engine_config_h mv_engine_config = NULL;
+ mv_source_h source = NULL;
+ unsigned char *data_buffer = NULL;
+ unsigned long buffer_size = 0;
+ unsigned int image_width = 0;
+ unsigned int image_height = 0;
+ int int_value = MV_BARCODE_DETECT_ATTR_TARGET_ALL;
+ mv_rectangle_s roi = { {0, 0}, 0, 0 };
+
+ int err = load_image_to_buffer(arguments->source,
+ &data_buffer, &buffer_size, &image_width, &image_height);
+
+ if (MEDIA_VISION_ERROR_NONE != err) {
+ LOGE("Errors were occurred during opening the file!!! code : %i", err);
+ goto out;
+ }
+
+ err = mv_create_engine_config(&mv_engine_config);
+ if (MEDIA_VISION_ERROR_NONE != err)
+ LOGE("Errors were occurred during creating the media engine config : %i", err);
+
+ if (MEDIA_VISION_ERROR_KEY_NOT_AVAILABLE ==
+ mv_engine_config_get_int_attribute(
+ mv_engine_config, MV_BARCODE_DETECT_ATTR_TARGET, &int_value)) {
+ LOGE("Errors were occurred during target attribute"
+ "configuration : %i", err);
+ goto out;
+ }
+
+ err = mv_create_source(&source);
+ if (MEDIA_VISION_ERROR_NONE != err) {
+ LOGE("Errors were occurred during creating the source!!! code : %i", err);
+ goto out;
+ }
+
+ err = mv_source_fill_by_buffer(source, data_buffer, buffer_size,
+ image_width, image_height, MEDIA_VISION_COLORSPACE_Y800);
+ if (MEDIA_VISION_ERROR_NONE != err) {
+ LOGE("Errors were occurred during filling the source!!! code : %i", err);
+ goto out;
+ }
+
+ roi.width = image_width;
+ roi.height = image_height;
+ err = mv_barcode_detect(source, mv_engine_config, roi, barcode_detected_cb, arguments);
+ if (MEDIA_VISION_ERROR_NONE != err) {
+ LOGE("Errors were occurred during barcode detection!!! code : %i", err);
+ }
+out:
+ if (MEDIA_VISION_ERROR_NONE != err)
+ printf("%s|%s|failed", arguments->source, arguments->annotation);
+
+ if (data_buffer != NULL) {
+ delete [] data_buffer;
+ data_buffer = NULL;
+ }
+
+ if (source != NULL) {
+ err = mv_destroy_source(source);
+ if (MEDIA_VISION_ERROR_NONE != err) {
+ LOGE("Errors were occurred during destroying the source!!! code : %i", err);
+ }
+ }
+
+ if (mv_engine_config != NULL) {
+ err = mv_destroy_engine_config(mv_engine_config);
+ if (MEDIA_VISION_ERROR_NONE != err) {
+ LOGE("Error were occurred during destroying the source!!! code : %i", err);
+ }
+ }
+
+ MEDIA_VISION_FUNCTION_LEAVE();
+}
+
+static error_t parse_opt (int key, char *arg, struct argp_state *state)
+{
+ struct arguments *arguments = (struct arguments *)state->input;
+
+ switch (key) {
+ case ARGP_KEY_NO_ARGS:
+ argp_usage(state);
+ break;
+ case ARGP_KEY_ARG:
+ if(state->argc > MAX_ARGS)
+ argp_failure(state, 1, 0, "too many arguments");
+ else if(state->argc < MAX_ARGS)
+ argp_failure(state, 1, 0, "too few arguments");
+ arguments->source = arg;
+ arguments->annotation = state->argv[state->next];
+ state->next = state->argc;
+ break;
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+
+ return 0;
+}
+
+static struct argp argp = { NULL, parse_opt, args_doc, doc };
+
+int main(int argc, char *argv[])
+{
+ struct timespec s_tspec;
+ struct timespec e_tspec;
+
+ LOGI("Media Vision Test-Accuracy is launched.");
+
+ struct arguments arguments;
+ argp_parse (&argp, argc, argv, 0, 0, &arguments);
+
+ clock_gettime(CLOCK_MONOTONIC, &s_tspec);
+ perform_detect(&arguments);
+ clock_gettime(CLOCK_MONOTONIC, &e_tspec);
+
+ struct timespec diffspec = diff(s_tspec, e_tspec);
+ unsigned long timeDiff = gettotalmillisec(diffspec);
+ printf("|%lums\n", timeDiff);
+
+ LOGI("Media Vision Test-Accuracy is closed.");
+
+ return 0;
+}