0

使用opencv的'videocapture',
我想获取cam的数据并将其插入Tensorflow-lite。
但是有一个错误。怎么了?

#define INFO  0
#define  WARN  1
#define   ERROR  2
#define  FATAL  3

using namespace std;
using namespace tflite;
using namespace tflite::logging;
int ww = 300;
int hh = 300;
typedef cv::Point3_<float> Pixel;
void normalize(Pixel &pixel){
    pixel.x = ((pixel.x / 255.0));//-0.5)*2.0;
    pixel.y = ((pixel.y / 255.0));//-0.5)*2.0;
    pixel.z = ((pixel.z / 255.0));//-0.5)*2.0;
}

int main(){
    std::unique_ptr<tflite::FlatBufferModel> model =
        tflite::FlatBufferModel::BuildFromFile("mnist.tflite");

    if (!model) {
        exit(-1);
    }

    tflite::ops::builtin::BuiltinOpResolver resolver;
    std::unique_ptr<tflite::Interpreter> interpreter;

    tflite::InterpreterBuilder(*model.get(), resolver)(&interpreter);

    if (!interpreter) {
        LOG(FATAL) << "Failed to construct interpreter\n";
        exit(-1);
    }

    const std::vector<int> inputs = interpreter->inputs();
    const std::vector<int> outputs = interpreter->outputs();

    printf("number of inputs: %d\n", inputs.size());
    printf("number of outputs: %d\n", outputs.size());

    cv::Mat image;

    cv::VideoCapture cap(0);
    cap.set( cv::CAP_PROP_FRAME_WIDTH, 640 );
    cap.set( cv::CAP_PROP_FRAME_HEIGHT, 480 );
    cap.set( cv::CAP_PROP_FPS, 30);

    if (interpreter->AllocateTensors() != kTfLiteOk) {
        return 0;
    }


    while (cap.isOpened())
    {
        if (!cap.read(image)) {
            continue; 
        }

        Pixel* pixel = image.ptr<Pixel>(0,0);
        const Pixel* endPixel = pixel + image.cols * image.rows;
        //for (; pixel != endPixel; pixel++)
            //normalize(*pixel);

        cv::resize(image, image, cv::Size(ww, hh));

        int input = interpreter->inputs()[0];

        auto inputLayer = interpreter->typed_input_tensor<float>(input);
        
        //memcpy(inputLayer, image.ptr<float>(0), ww * hh * 3 * sizeof(float));
        memcpy(inputLayer, image.data, image.total() * image.elemSize());

        // interpreter->SetAllowFp16PrecisionForFp32(true);
        // interpreter->SetNumThreads(6);

         if(interpreter->Invoke() != kTfLiteOk) {
           return 0;
         }
        
        cv::imshow("", image);

        if (cv::waitKey(30) == 27)
            break;
    }
    return 0;
}

错误以多种方式发生。(3~4件事)
内存方面有很多错误。
这是一个典型的错误。
什么原因?

objc [16745]:方法缓存损坏。这可能是给无效对象的消息,或者是其他地方的内存错误。objc[16745]: receiver 0x7fae88f20fc0, SEL 0x7fff7c54b6a8, isa 0x7fae89c08b30, cache 0x7fae89c08b40, buckets 0x7fae89043000, mask 0x3ff, occupied 0xc objc[16745]: receiver 400 bytes, buckets 16384 bytes objc[16745]: selector 'contentView' objc[16745] : isa 'NSKVONotifying_CVWindow' objc [16745]: 方法缓存损坏。这可能是给无效对象的消息,或者是其他地方的内存错误。zsh: 中止
./cmd

objc [16841]:自动释放池页面 0x7fc16f039000 损坏的魔法
0x1d201d1e 0x1a1c1f1d 0x0303181a 0x00010102 应该是 0xa1a1a1a1 0x4f545541 0x454c4552 0x2011455370x 应该是 0x0x

cmd(52824,0x10ab33e00) malloc:释放对象 0x7fdc9004f800 的校验和不正确:可能在释放后被修改。损坏值:0x4198000041b80000 cmd(52824,0x10ab33e00) malloc: *** 在 malloc_error_break 中设置断点进行调试

为什么memcpy之后会报错?

4

0 回答 0