opencv dnn module load tensorflow .pb file error - c++

I trained a mnist_fashion model with tensorflow2.4, and then used opencv to call the generated .pb file and the following error occurred.
Net net = readNetFromTensorflow(weightFile);
String field 'tensorflow.FunctionDef.Node.ret' contains invalid UTF-8 data when parsing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.
Failed to parse GraphDef file: models/saved_model.pb) in cv::dnn::ReadTFNetParamsFromBinaryFileOrDie

I found a solution, just convert saved_model.pb to frozen_graph.pb.
[https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py][1]

Related

How to convert ogg file to mp3 using python?

I am trying to convert the Ogg file to mp3/wav formats. I used:
FFmpeg
pyaudio
dlls
But nothing worked out.
Also, I am trying to first read the ogg data from an HTTP URL then want to convert it to mp3/wav, and then using speech_recognition converting to text.
If I don't use any method I get the following error.
Error: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if the file is corrupted or in another format.
Please suggest any libraries.
Code snippet:
audio_data = Data.get("audio")
if '.wav'or'.mp3' not in audio_data:
file = ("newspeech.mp3")
new_audio = urllib.request.urlretrieve(audio_data,file)

C++: Decode a HTTP response which is Base64 encoded and UTF-8 decoded

I have a C++ program which is receiving encoded binary data as a HTTP response. The response needs to be decoded and stored as a binary file. The HTTP server that is sending the binary data is written in Python and following is an example code that performs the encoding.
#!/usr/bin/env python3
import base64
# content of the file is string "abc" for testing, the file could be an image file
with open('/tmp/abc', 'rb') as _doc:
data = _doc.read()
# Get Base-64 encoded bytes
data_bytes = base64.b64encode(data)
# Convert the bytes to a string
data_str = data_bytes.decode('utf-8')
print(data_str)
Now, I want to decode the received data_str using a C++ program. I could make the python equivalent as below to work properly.
_data = data_str.encode('utf-8')
bin_data = base64.b64decode(_data)
But, with C++, I tried to use the Boost library's from_utf8 method, but no avail. Could anyone please guide the best way of decoding and getting the binary data in C++ (preferably using boost, since it is portable)?

_io.BytesIO' object has no attribute 'name' for small size file

I'm uploading file with python/Django and getting two different object. When file is small in size, getting InMemoryUploadedFile object, while file is quite large, i got in temporaryFileWrapper. I m checking file mime type with magic library.
when File is large, getting correctmime type with this code
file_name = self.cleaned_data.get('file')
file_mime= magic.from_file(file_name.file.name, mime=True)
supported_format= ['video/x-flv','video/mp4','video/3gpp','video/x-ms-wmv']
if file_mime in supported_format:
...........
But when file is small in size i m getting error
_io.BytesIO' object has no attribute 'name
For large file
For small file
As per Django's this doc, Django have two upload file handler MemoryFileUploadHandler and TemporaryFileUploadHandler.
MemoryFileUploadHandler stream to memory , and TemporaryFileUploadHandler stream to disk.
I have set deafult TemporaryFileUploadHandler im my setting.py
FILE_UPLOAD_HANDLERS= ["django.core.files.uploadhandler.TemporaryFileUploadHandler"]
We can also write own custom FileUploadHandler in django

Reading Geospatial Raster files with GDAL and OpenCV3.1.0

I'm trying to implement and run OpenCV sample code Reading Geospatial Raster files with GDAL
For DEM model, i download N37W123.hgt.zip from the SRTM file located at the USGS , (that is in the Results section of that page).
but, DEM model not loaded to cv::Mat dem by cv::Mat dem = cv::imread(argv[2], cv::IMREAD_LOAD_GDAL | cv::IMREAD_ANYDEPTH ); and i get run time error throw std::runtime_error("DEM image type must be CV_16SC1");
1) Why is this happening?
2) All DEM data type are 16 signed integer. is it ok?
3) How can read DEM model block with GDALDataset::RasterIO or GDALRasterBand::ReadBlock directly?
Most Likely you have to enable/set WITH_GDAL flag to true in CMake while building opencv.
for reference:
https://docs.opencv.org/4.4.0/d4/da8/group__imgcodecs.html#imread
gdal is most likely expecting an HGT file, not a ZIP file: Link.
In Python you would extract the archive first using the zipfile module, then you can access the file directly into a numpy array:
from osgeo import gdal
ds = gdal.Open(filename)
data = ds.ReadAsArray()
EDIT: You've pointed out in a comment that you are using C++, so see the tutorial for reading the image directly: Link

ITK: Could not create IO object

I am trying to calculate the gradient of an image. I tried this code on the sample image given (Gourds6.png).
I used cmake . to create the CMakeFiles and then make. Everything works fine and the executable file is created. Now when I run the code using command ./computeGradient Gourds6.png out.png 1.5, it complains that:
Error:
itk::ImageFileWriterException (0x1446b40)
Location: "void itk::ImageFileWriter<TInputImage>::Write() [with TInputImage = itk::Image<float, 2u>]"
File: /usr/local/include/ITK-4.3/itkImageFileWriter.hxx
Line: 152
Description: Could not create IO object for file out.png
Tried to create one of the following:
You probably failed to set a file suffix, or
set the suffix to an unsupported type.
I haven't done any change to this code. It should work. I don't know what is wrong with it :( Do you have any idea?
Also, why don't we need to update the reader to read the image? Why do we only update the writer?
I appreciate for any help!
The pixel type of the output file in this example of ITK is float. And writing an image of float as a PNG image is not possible.
A list of supported files formats and corresponding data types is given on the wiki of ITK.
To save this image of float, here are formats that are expected to work :
Analyze (.img)
DICOM (.dic : failed on my PC)
GIPL (.gipl)
MetaImage (mhd) (out.mhd+out.raw)
Nrrd (.nhdr, .nrrd)
Stimulate (.spr)
VTK (.vtk)
The VTK file format works well and may be opened by the paraview software.
To use a PNG format, the image should be casted to unsigned char type. It may be performed by the CastImageFilter(). See this example. Another solution is to use the RescaleIntensityImageFilter(). See this example.
This question and its answer (which happens to be mine) explains how to convert a float image type to a ùnsigned char` image type and save it as PNG.
typedef itk::RescaleIntensityImageFilter< FloatImageType, UCharImageType > RescaleFilterType;
RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
rescaleFilter ->SetInput(importFilter->GetOutput());
rescaleFilter->SetOutputMinimum(0);
rescaleFilter->SetOutputMaximum(255);
typedef itk::ImageFileWriter< UCharImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( "output.png" );
writer->SetInput(rescaleFilter->GetOutput() );
writer->Update();
Finally, your last question : why do we only update the writer ? As the writer is updated, it will first check if its entries are up to date. If it is not the case, it will call filter->Update(), and so on.