I am writing a c++ code for letter recognition using OpenCv and tesseract, then I want to send a command to serial port ( Arduino) but the computer fezzes and I have to restart it.
I did some image processing in the code : convert to gray then blur the image then threshold and crop
finally I pass the image to Tesseract API to get the letter and save the letter in a text file.
I open the saved file and print the content of the file. then I open serial port connected with arduino and send a letter to the serial port.
when I compile the code the image processing part is done, the file is opened and the content of the file is printed, and it sends to serial port but then the code crashes and computer fezzes.
here is the code :
int fd;
char *buff;
int open_port(void)
{
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/kittens ");
}
else
fcntl(fd, F_SETFL, 0);
return (fd);
}
int main( int argc, char** argv )
{
open_port();
int const max_BINARY_value = 255;
char path[255];
//capture image
//gray
//Gaussian blur image
//threshold
//crop
system("tesseract crop.jpg txt -psm 10");//pass it to tesseract API and get the result in file
ifstream inFile;
inFile.open ("txt.txt", ofstream::in );//open file contains the letter
char singleCharacter;
inFile.get(singleCharacter);
while (!inFile.eof())
{
cout << singleCharacter;
inFile.get(singleCharacter);
}
int wr,rd;
char msg[]="W";
do
{
wr = write(fd, msg, 1);
printf("debugging - Wrote to port\n");
usleep(10000);
if (wr < 0) {
fputs("write() to port /dev/ttyACM0 failed!\n", stderr);
break;
}
}
while (buff != msg);
if (buff==msg)
printf(buff, "String Found! Now do the work.");
close(fd);
waitKey(0);
return 0;
}