# Frame Saved With Deepstream Corrupted On Jetson Orin But Not On dGPU

**URL:** <https://forums.developer.nvidia.com/t/frame-saved-with-deepstream-corrupted-on-jetson-orin-but-not-on-dgpu/303598>\
**Category:** DeepStream SDK\
**Tags:** gstreamer, ubuntu, camera, jetson, deepstream, cuda\
**Created:** [August 16, 2024, 11:01am UTC](https://forums.developer.nvidia.com/t/frame-saved-with-deepstream-corrupted-on-jetson-orin-but-not-on-dgpu/303598 "2024-08-16T11:01:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![noe.breton](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@noe.breton](https://forums.developer.nvidia.com/u/noe.breton)\
**Post date:** [August 16, 2024, 11:01am UTC](https://forums.developer.nvidia.com/t/frame-saved-with-deepstream-corrupted-on-jetson-orin-but-not-on-dgpu/303598/1 "2024-08-16T11:01:21Z")

</div>

**I saw the sample app deepstream\_image\_meta\_test, but I didn’t succeed to extract image with it, I don’t want to save processed image or cropped image, only raw frame**

**Frame Saved Corrupted On Jetson Orin But Not On dGPU**

**• Jetson Orin Nano**  
**• Deepstream 7.0**  
**• JetPack 6.0**  
**• 8.6.2.3-1+cuda12.2**  
**• bugs**  
**• Adding this code snippet in a callback probe on a usb camera sample**  
**• c++ 17; openCv**

I’m building a Deepstream app on c++ where I want to save a frame (without any cropping) in JPG format every 10 second , My app is available on x86 dGPU and jetson Orin.

When I use my application on dGPU, the image is correct:

 ![snap-20240816124612476](https://global.discourse-cdn.com/nvidia/original/4X/e/9/1/e9137b4c5d7237a8d76f11073639436c574924ea.jpeg)

But when I use it on my Orin I get discontigous image :

 ![snap-20240816124714010](https://global.discourse-cdn.com/nvidia/original/4X/0/6/4/06485ce5d97b5c36c318bae1a3edd5cf06c65603.jpeg)

 ![snap-20240816124703990](https://global.discourse-cdn.com/nvidia/original/4X/4/5/f/45f1d7c0e5140851a2583f28d233b9c1f3b15a2b.jpeg)

Here the code I use inside the loop of my Callback Probe Function :

```auto
#include <string>
#include <sstream>
#include <unordered_map>
#include <utility> // For std::pair
#include <cstdlib> // For exit
#include <ctime> // For localtime
#include <iomanip> // For std::setw and std::setfill
#include <chrono> // For time calculations
#include <cstring>
#include <iostream>
#include <filesystem>
#include <system_error>

// System headers
#include <sys/file.h> // For flock
#include <sys/stat.h> // For stat
#include <fcntl.h> // For open
#include <unistd.h> // For close

// CUDA headers
#include <cuda_runtime_api.h>

// GStreamer and DeepStream headers
#include <gst/gst.h>
#include <gst/video/video.h>
#include "gstnvdsmeta.h"
#include "nvdsmeta_schema.h"
#include "nvds_roi_meta.h"
#include <nvds_obj_encode.h>

// NVIDIA headers
#include <nvbufsurface.h>
#include <nvbufsurftransform.h>

// OpenCV headers
#include <opencv2/opencv.hpp>

/**
 * @brief Converts NvBufSurface to cv::Mat using CUDA.
 *
 * @param surface Pointer to the NvBufSurface.
 * @param batch_id Batch ID.
 * @return A cv::Mat object containing the image data.
 */

cv::Mat NvBufSurfaceToCvMat(NvBufSurface *surface, int batch_id) {
    //surface->memType = NVBUF_MEM_DEFAULT; // Ensure the memory type is set to surface array no
    //surface memtype is array
    // Map the surface for reading
    if (NvBufSurfaceMap(surface, -1, -1, NVBUF_MAP_READ_WRITE) != 0) {
        std::cerr << "Failed to map NvBufSurface" << std::endl;
        return cv::Mat(); // Return an empty cv::Mat on failure
    }

    // Sync the surface memory for CPU access
    NvBufSurfaceSyncForDevice(surface, batch_id, 0);
    NvBufSurfaceParams params = surface->surfaceList[0]; // Get the surface parameters for the specified batch ID
    cv::Mat img(params.height, params.width, CV_8UC4, params.dataPtr, params.pitch);

    

    // Unmap the surface after reading
    NvBufSurfaceUnMap(surface, 0, 0);

    return img; // Return the created cv::Mat
}

/**
 * @brief Saves a frame image to a file.
 *
 * @param buf Pointer to the GstBuffer.
 * @param logDir Directory where the image file will be saved.
 * @param filename The filename to save the image.
 */
void saveFrameImage(GstBuffer* buf, const std::string& logDir, const std::string& filename) {
    NvBufSurface* surface = nullptr;
    GstMapInfo map;

    if (gst_buffer_map(buf, &map, GST_MAP_READ)) {
        surface = (NvBufSurface*)map.data;
       // gst_buffer_unmap(buf, &map);
    } else {
        g_printerr("Unable to map GstBuffer to NvBufSurface\n");
        return;
    }

    int batch_id = 0;
    cv::Mat frame = NvBufSurfaceToCvMat(surface, batch_id);
    //display the image
    struct stat sb;
    if(stat(logDir.c_str(), &sb) != 0) {
        system(("mkdir -p " + logDir).c_str());
    }

    if (!cv::imwrite(logDir + "/" + filename, frame)) {
        g_printerr("Failed to save frame to file\n");
    } else {
        g_print("Frame saved to %s\n", filename.c_str());
    }
}

```

Here the beginig of pipeline:

```auto
pipeline:
  - v4l2src:
      device: /dev/video0
  - capsfilter:
      caps: "image/jpeg, width=1920, height=1080, framerate=30/1"
  - jpegdec: {}
  - videoconvert: {}
  - nvvideoconvert: {}
  - capsfilter:
      caps: "video/x-raw(memory:NVMM), format=RGBA, width=1920, height=1080"
  - mux.sink_0:
       nvstreammux:
          name: mux
          batch-size: 1
          width: 1920
          height: 1080
          batched-push-timeout: 4000000
          live-source: 1
          num-surfaces-per-frame: 1
          sync-inputs: 0
          max-latency: 0

```

Thanks for any help !

---

<div class="post-metadata">

**Author:** ![yuweiw](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/yuweiw/32/101255_2.png) [@yuweiw](https://forums.developer.nvidia.com/u/yuweiw)\
**Post date:** [August 19, 2024, 1:43am UTC](https://forums.developer.nvidia.com/t/frame-saved-with-deepstream-corrupted-on-jetson-orin-but-not-on-dgpu/303598/3 "2024-08-19T01:43:45Z")

</div>

Could you just display the camera source directly before the nvstreammux plugin and see if there’s still the same problem?  
Like

```auto
pipeline:
  - v4l2src:
      device: /dev/video0
  - capsfilter:
      caps: "image/jpeg, width=1920, height=1080, framerate=30/1"
  - jpegdec: {}
  - videoconvert: {}
  - nvvideoconvert: {}
  - nvds3dsink

```

---

<div class="post-metadata">

**Author:** ![yingliu](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/yingliu/32/136703_2.png) [@yingliu](https://forums.developer.nvidia.com/u/yingliu)\
**Post date:** [September 10, 2024, 2:27am UTC](https://forums.developer.nvidia.com/t/frame-saved-with-deepstream-corrupted-on-jetson-orin-but-not-on-dgpu/303598/4 "2024-09-10T02:27:46Z")

</div>

**There is no update from you for a period, assuming this is not an issue anymore. Hence we are closing this topic. If need further support, please open a new one. Thanks**

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [September 24, 2024, 2:28am UTC](https://forums.developer.nvidia.com/t/frame-saved-with-deepstream-corrupted-on-jetson-orin-but-not-on-dgpu/303598/5 "2024-09-24T02:28:24Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
