I'm NOT a C expert.
That been said building PostgreSQL extension using or not C is very powerful.
99% of the time I work also using PostGIS, and extensions too, but here is the problem:
Cannot properly include what is needed, I've played using
#include "../postgis_config.h"
#include "liblwgeom.h"
#include "lwgeom_pg.h"
but cannot have a proper result without recompiling whole PostGIS... and so create a derived version.
PostGIS should be dependency for the lib I've in mind, so I need something that link to it, if present or catch error, while compiling my lib...
Currently I'm resolving using the PostgreSQL's #include <utils/geo_decls.h>
so creating PostgreSQL functions interfaces that:
translates a PostGIS feature to a PostgreSQL base geo type
pass the results to my C libs
works with those very base types (loosing any support from powerful PostGIS lib )
send result to the function that translate back to PostGIS.
Just go give a simple idea of what I'm doing now:
mylib.c
#include "postgres.h"
#include "fmgr.h"
#include <utils/geo_decls.h>
#include "utils/builtins.h"
Datum pg_xytile_to_box(PG_FUNCTION_ARGS)
{
int x, y, zoom;
struct tile_box box;
BOX *pg_box = (BOX *) palloc(sizeof(BOX));
x = PG_GETARG_INT64(0);
y = PG_GETARG_INT64(1);
zoom = PG_GETARG_INT32(2);
if ( xytile_to_box(&box, x, y, zoom) != 0 )
{
pfree(pg_box);
PG_RETURN_NULL();
}
pg_box->low.x = box.w;
pg_box->low.y = box.s;
pg_box->high.x = box.e;
pg_box->high.y = box.n;
PG_RETURN_BOX_P(pg_box);
}
PG_FUNCTION_INFO_V1(pg_xytile_to_box);
extension_code.sql
CREATE OR REPLACE FUNCTION _xytile_to_box(x int, y int, zoom int default 11) RETURNS box
AS 'MODULE_PATHNAME', 'pg_xytile_to_box'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION xtile_to_geom(xtile int, ytile int, zoom int default 11) returns geometry(polygon, 4326)
language plpgsql security definer as $FUNC$
BEGIN
return (
select st_setsrid((ST_MakeBox2D(b[0]::geometry(point), b[1]::geometry(point)))::geometry(polygon), 4326) as t
from _xytile_to_box(xtile, ytile, zoom) as b
);
END; $FUNC$;
There is a way to instruct a Makefile to find and add PostGIS to the imports?
There is something better?
PostGIS doesn't seem to have a public C API, so you won't be able to link to the PostGIS libraries directly.
My recommendation is to call the PostGIS SQL functions from your C code by using the Server Programming Interface (SPI). An alternative is calling PostGIS functions directly with the function-call interface in include/fmgr.h, which would be faster, but requires that you first look up the functions in the catalog.
Related
My mission is to take a picture using the camera, send the pixels to C function, and provide a message to the user if the function returns an indication of a problematic image (e.g.: poor focus, picture too dark, etc).
I want to call a C function and send a pointer to the function which includes the pixels (and other parameters like image width/height). The idea is to use the C code and check the image quality.
The C code is ready. I found samples how to bind it into Flutter. But I do not know how to get the Bitmap data and pixels and send them to the C function.
You can do that binding to native code using dart:ffi.
Imagine that you have a C function that returns the sum of two numbers with this code:
#include <stdint.h>
extern "C" __attribute__((visibility("default"))) __attribute__((used))
int32_t native_add(int32_t x, int32_t y) {
return x + y;
}
And this C Makefile
cmake_minimum_required(VERSION 3.4.1) # for example
add_library( native_add
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
../native_add.cpp )
Now you need to encapsulate this CMake file into the externalNativeBuild inside build.gradle, this could be an example:
android {
// ...
externalNativeBuild {
// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path "CMakeLists.txt"
}
}
// ...
}
Now that you have the library and you have encapsulated the code you can load the code using the FFI library like this:
import 'dart:ffi'; // For FFI
import 'dart:io'; // For Platform.isX
final DynamicLibrary nativeAddLib = Platform.isAndroid
? DynamicLibrary.open("libnative_add.so")
: DynamicLibrary.process();
And with a handle to an enclosing library you can resolve the native_add symbol as we do here:
final int Function(int x, int y) nativeAdd =
nativeAddLib
.lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_add")
.asFunction();
Now you could use it on your application calling the nativeAdd function, here you have an example:
body: Center(
child: Text('1 + 2 == ${nativeAdd(1, 2)}'),
),
You can learn how flutter native code binding works in the following url: flutter docs c-interop
It is possible to make a fast model with keras with tensor flow as backend and use it to predict in C or C++?
I need to do the prediction inside of a C++ program, but I feel much more comfortable doing the model and the training in keras.
In case you don't need to utilize a GPU in the environment you are deploying to, you could also use my library, called frugally-deep. It is available on GitHub and published under the MIT License: https://github.com/Dobiasd/frugally-deep
frugally-deep allows running forward passes on already-trained Keras models directly in C++ without the need to link against TensorFlow or any other backend.
It not only supports prediction with sequential models but also with more complex models build with the functional API.
Additionally to supporting many common layer types it can keep up with (and sometimes even beat) the performance of TensorFlow on a single CPU. You can find up-to-date benchmark results for some common model in the repo.
By automatic testing frugally-deep guarantees that the output of a model used with it in C++ is exactly the same as if run with Keras in Python.
Yes, it is possible. TensorFlow provides a stable C API as well as a C++ one.
For more details, you probably want to ask a more specific question.
You can use cv::dnn module in opencv3.2. See example in the opencv samples.
In simple steps you can do this -
Save Keras model into text files in python
In C++/C, read those text files, and run the model in a multithreading environment.
Above Steps in detail
For first step, call save_keras_model_as_text from keras2cpp.py on Github-
'''
save_keras_model_as_text(model, open('/content/modeltext.txt', 'w') )
save_keras_model_as_text(model, open('/content/modellayersonlytext.txt', 'w') , noweight=True)
'''
In c++/c projects, include keras.h /keras.cpp files.
convert input image into Keras format or read input image into Keras format from a file.
'''
//assume dst is pointer to an image object. This
//can be of CxImage / CImg /OpenCV or of any other
//library.
int h = dst->GetHeight();
int w= dst->GetWidth();;
int *img = new int[h*w*3];
int *img_r = img, *img_g =&img[h*w], *img_b=&img[2*h*w];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w;x++) {
RGBQUAD rgb= dst->GetPixelColor(x,y);
*img_r= rgb.rgbRed; img_r++;
*img_g= rgb.rgbGreen; img_g++;
*img_b = rgb.rgbBlue; img_b++;
}
}
'''
Call ExecuteKerasSegmentation.
'''
//text files in which keras model is saved.
char *modelfile ="modellayersonlytext.txt";
char *weightfile="modeltext.txt";
int *result = ExecuteKerasSegmentation(img, h, w, 3, modelfile, weightfile);
'''
result contains the segmentation result. You may save it into pgm file or convert it into your image library object and use it further.
'''
save_image_pgm("segmentation_map.pgm",result,h,w,127); //127 is scaling factor for binary images.
'''
Our customer provided source code has portions of code that will be executed based on tool type. A sample code portion is given below. The function has common portions and tool specific(hardware platform) portions. The code is written in C and runs in VxWorks. Addition or deletion of new tool type has code modification. The customer wants addition or deletion new tool type with minimal code change and testing effort
int vsp_recv(char *const recv_text)
{
int rc = 0;
const int type = get_tool_type();
// Common Code
if (MODEL_CR == type)
{
rc = beamoff(recv_text);
}
else
{
rc = vsp_set(recv_text);
}
return(rc);
}
Is it the right technique to separate the code to two methods as given below, keep them in separate source files and define separate make files to generate tool specific binary? Is there any better ways to do this?
Tool type MODEL_CR code
int vsp_recv_tool_speccific(char *const recv_text)
{
return beamoff(recv_text);
}
Tool type MODEL_CV code
int vsp_recv_tool_speccific(char *const recv_text)
{
return vsp_set(recv_text);
}
Refactored method
int vsp_recv(char *const recv_text)
{
int rc = 0;
const int type = get_tool_type();
// Common Code
rc = vsp_recv_tool_speccific(recv_text);
}
Define a shared library for each tool and a configuration file that defines what functions get called for each tool. Load the shared libraries at startup and provide a signal catcher to reload if the configuration file changes.
the OPs question (and posted code) says that 3 places will need to be modified.
the function: get_tool_type()
the header file with the definitions of MODEL_CV, MODEL_CR, etc
the if-then-else list.
were it me, I would implement a table of function pointers, have get_tool_type() return an index into that table. Then all the if/then/else code would become a single statement that invokes a function from the table.
Then any updates would be additions to the table, modifications to 'get_too_type(), and the additional functions likebeam_off()`
The loss of a tool type would not require any code change.
the addition of a tool type would require appending an entry to the table, mod to get_tool_type() to recognize the new tool, and the new function to process the new tool type.
Of course, this could result in code that is never executed.
Does Octave have a good way to let the user select an input file? I've seen code like this for Matlab, but doesn't work in Octave.
A gui based method would be preferred, but some sort of command-line choice would work also. It would be great if there were some way to do this that would work in both Matlab and Octave.
I found this for Matlab but it does not work in Octave, even when you install Octave Forge Java package for the listdlg function. In Octave, dir() gives you:
647x1 struct array containing the fields:
name
date
bytes
isdir
datenum
statinfo
but I don't know how to convert this to an array of strings listdlg expects.
You have already the Octave Forge java package installed, so you can create instances of any java class and call any java method.
For example to create a JFileChooser and call the JFileChooser.showOpenDialog(Component parent) method:
frame = javaObject("javax.swing.JFrame");
frame.setBounds(0,0,100,100);
frame.setVisible(true);
fc = javaObject ("javax.swing.JFileChooser")
returnVal = fc.showOpenDialog(frame);
file = fc.getSelectedFile();
file.getName()
Btw. I had some troubles installing the package.
Here is a fix for Ubuntu. that worked also for my Debian Testing.
EDIT
#NoBugs In reply to your comment:
If you need to use listdlg you can do the following:
d = dir;
str = {d.name};
[sel,ok] = listdlg('PromptString','Select a file:',...
'SelectionMode','single',...
'ListString',str);
if ok == 1
disp(str{sel(1)});
end
This should be compatible with matlab, by I cannot test it right now.
If you want to select multiple files use this:
d = dir;
str = {d.name};
[sel,ok] = listdlg('PromptString','Select a file:',...
'SelectionMode','multiple',...
'ListString',str);
if ok == 1
imax = length(sel);
for i=1:1:imax
disp(str{sel(i)});
end
end
I never came across an open-file-dialog in octave.
If you are looking for a gui based method maybe guioctave can help you. I never used it, because it appears only be available for windows machines.
A possible solution would be to write a little script in octave, that would allow the user to parse through the directories and select a file like that.
Thought I'd provide an updated answer to this old question, since it is appearing in the 'related questions' field for other questions.
Octave provides the uigetdir and uigetfile functions, which do what you expect.
I would like to take the following unit (DrivesData) and display the drive column in a TListView. I've never worked with the (Synopse) SQLite3 code before so I'm hoping someone could give me a little push in the right direction.
Just add the DrivesData unit to the uses clause then run and it will create the "drives.sqlite" database file with a list of drives 'A' to 'Z'.
unit DrivesData;
interface
uses
SynCommons, SQLite3Commons;
type
TDrives = class(TSQLRecord)
private
{ Private declarations }
FDrive: RawUTF8;
protected
{ Protected declarations }
FDrivesModel: TSQLModel;
FDrivesDatabase: TSQLRest;
public
{ Public declarations }
constructor Create(); override;
destructor Destroy(); override;
published
{ Published declarations }
property Drive: RawUTF8 read FDrive write FDrive;
end;
var
DriveRecord: TDrives;
implementation
uses
SQLite3;
function CreateDrivesModel(): TSQLModel;
begin
Result := TSQLModel.Create([TDrives]);
end;
{ TDrives }
constructor TDrives.Create();
var
X: Char;
begin
inherited Create();
FDrivesModel := CreateDrivesModel();
FDrivesDatabase := TSQLRestServerDB.Create(FDrivesModel, 'drives.sqlite');
TSQLRestServerDB(FDrivesDatabase).DB.Execute(
'CREATE TABLE IF NOT EXISTS drives ' +
'(id INTEGER PRIMARY KEY, drive TEXT NOT NULL UNIQUE COLLATE NOCASE);');
for X := 'A' to 'Z' do
begin
TSQLRestServerDB(FDrivesDatabase).DB.Execute(
'INSERT OR IGNORE INTO drives (drive) VALUES ("' + X + ':")');
end;
end;
destructor TDrives.Destroy();
begin
if Assigned(FDrivesDatabase) then
FDrivesDatabase.Free();
if Assigned(FDrivesModel) then
FDrivesModel.Free();
inherited Destroy();
end;
initialization
DriveRecord := TDrives.Create();
finalization
if Assigned(DriveRecord) then
DriveRecord.Free();
end.
Nice try!
But I'm afraid you are missing some points of the framework:
for instance you're mixing record level and MVC application level: a TSQLRecord maps a DB table and you should not declare MVC TSQLModel and TSQLRest inside this class;
and you're missing the ORM approach, you don't need to write all those SQL code (the CREATE TABLE and the INSERT): the framework will write it for you, with no error, and the exact expected column type (with collations)!
Instead of using a TSQLRestServerDB directly by itself, you should better use a TSQLRestClientDB (which will instantiate its privately owned TSQLRestServerDB), even if you are still working locally. So you'll get a lot more features with no performance penalty.
You are using a Char type in your code. Our framework is UTF-8 oriented, so you should use AnsiChar instead, or use StringToUtf8() function to ensure correctness (at least with Unicode version of Delphi).
I'll recommend that you take a look at the sample code source code and the provided documentation (especially the SAD document, in the general presentation in the first pages, including the SynFile main demo).
To retrieve some data, then display it in the VCL (e.g. in a TListBox), take a look at the TSQLTableJSON class. There are some code sample in the SAD document (take a look at the keyword index, at the beginning of the document, if you're a bit lost).
Perhaps StackOverflow is not the best place to ask such specific questions. You have our forum available at http://synopse.info to post any questions regarding this framework. You can post your code here.
Thanks for your interest!