Python Forum
I have problem on Communication between C++ and Python using subprocess Module
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I have problem on Communication between C++ and Python using subprocess Module
#1
I have problem on Communication between C++ and Python using subprocess Module

it is my Python Code for getting data.(it gets data from C++ OpenCV code.) (the data is coordinate x,y)


#this is imported for Inter-Language Communication!
from subprocess import Popen, PIPE


#Open Connection for CPP!
MyPopen = Popen(['~/ImageProcessingToGimbal/build/ZED_with_OpenCV'], shell=True, stdout=PIPE, stdin=PIPE)


while(1) :

  result = MyPopen.stdout.readline().strip()
  print(result)
and this is my OpenCV code in C++

line for communication between languages is colored by red.

[color=#ff3333]#include <stdio.h>
#include <string.h>
#include <iostream>
//this is imported for Communication between inter-language.[/color]


//this is imported for video processing.
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/opencv.hpp"


using namespace cv;
using namespace std;

 int main( int argc, char** argv )
 {
    VideoCapture cap(0); //capture the video from web cam

    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the web cam" << endl;
         return -1;
    }

    namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"

 int iLowH = 0;
 int iHighH = 179;

 int iLowS = 0; 
 int iHighS = 255;

 int iLowV = 0;
 int iHighV = 255;

 //Create trackbars in "Control" window
 cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
 cvCreateTrackbar("HighH", "Control", &iHighH, 179);

 cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
 cvCreateTrackbar("HighS", "Control", &iHighS, 255);

 cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
 cvCreateTrackbar("HighV", "Control", &iHighV, 255);

    while (true)
    {
        Mat imgOriginal;

        bool bSuccess = cap.read(imgOriginal); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }

  Mat imgHSV;

  cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
 
  Mat imgThresholded;
  Mat revimgThresholded;  //하얀색 검은색 뒤집기!

  inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); 
  //Threshold the image
  //inRange 함수는 뒤집기(하얀색이 검은색으로, 검은색이 하얀색으로)가 안되므로 Threshold 함수를 이용한다.

  threshold(imgThresholded, revimgThresholded, 2, 255, 1);  
   /*
   http://docs.opencv.org/2.4/doc/tutorials/imgproc/threshold/threshold.html

     0: Binary
     1: Binary Inverted
     2: Threshold Truncated
     3: Threshold to Zero
     4: Threshold to Zero Inverted

   원래
   THRESH_BINARY_INV 
   */

  //threshold 함수는 흑백에서만 되고 컬러에서는 안된다
  //threshold(imgHSV, imgThresholded, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), CV_THRESH_BINARY_INV);

  Mat imgBlobbing;
  //위 : 블로핑한 결과에 빨간 동그라미를 치기 위한 Mat
  
  
  //morphological opening (remove small objects from the foreground)
 erode(revimgThresholded, revimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
 dilate(revimgThresholded, revimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 

  //morphological closing (fill small holes in the foreground)
 dilate(revimgThresholded, revimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 
 erode(revimgThresholded, revimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
  

  //블로빙 시작


  //패러미터 설정을 위해 객체를 선언한다.
  SimpleBlobDetector::Params params;

  //블롭 패러미터 설정!
  //params.filterByColor = 1;
  //params.blobColor = 0;

// Change thresholds
 // params.minThreshold = 0;
 // params.maxThreshold = 50;
 // params.filterByArea = 1;
 // params.minArea = 50;
 // params.maxArea = 10000;  
 params.filterByArea = true;
 params.minArea = 100;
 params.maxArea = 100000000;
 params.filterByInertia = false;
 params.filterByConvexity = true;
 params.minConvexity = 0.50;
 //params.filterByArea = false;

  //디텍터 객체를 디폴트 패러미터로서 선언한다.
  SimpleBlobDetector Detector(params);

  //블롭 탐색 시작
  std::vector<KeyPoint> mykeypoints;
  Detector.detect(revimgThresholded, mykeypoints);

  //탐색된 블롭들에 빨간 동그라미들을 칩니다!
  drawKeypoints(revimgThresholded, mykeypoints, imgBlobbing, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
 
  //이 코드 넣으면 Keypoint가 그려지는 것 뿐만 아니라 Threshold 이미지로 변해서 뺐음.
  //drawKeypoints(revimgThresholded, keypoints, imgOriginal, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);


//가장 큰 사이즈 추출단계
//사이즈, 좌표 둘 다 자료형 float이라고 가정하겠다
float maxsize = 0;
float maxx,maxy = 0;

for(std::vector<cv::KeyPoint>::iterator blobIterator = mykeypoints.begin(); blobIterator != mykeypoints.end(); blobIterator++){
   //std::cout << "size of blob is: " << blobIterator->size << std::endl;
   if(maxsize < blobIterator->size)
    {
     maxsize = blobIterator->size;
     maxx = blobIterator->pt.x;
     maxy = blobIterator->pt.y;
    }
   

    
    //std::cout << "max size : " << maxsize << endl;
    //std::cout << "position is at : " <<  "x : "  << maxx << "y : " << maxy << endl;
     
[color=#ff3333]    std::cout << maxx << std::endl;
    std::cout.flush();  

    std::cout << maxy << std::endl;
    std::cout.flush();   [/color]
} 




  //이제 imshow로 출력하기만 하면 됩니다. 
  imshow("Thresholded Image Inverted", revimgThresholded); //show the thresholded image
  imshow("Original", imgOriginal); //show the original image
  imshow("imgBlobbing", imgBlobbing); //라벨링 이미지를 보여줌.

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break; 
       }
    }

  
   return 0;

}
when I run python script , I met empty screen.


[output]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with memory_graph module akbarza 3 394 Mar-04-2024, 10:15 AM
Last Post: snippsat
  problem using coloeama module akbarza 3 589 Jan-08-2024, 07:31 AM
Last Post: akbarza
  problem in using subprocess module akbarza 5 1,051 Sep-24-2023, 02:02 PM
Last Post: snippsat
  problem in import module from other folder akbarza 5 1,449 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  problem in using pyqrcode module to create QRcode akbarza 9 2,020 Aug-23-2023, 04:17 PM
Last Post: snippsat
  Problem with Pyinstaller. No module named '_tkinter' tonynapoli2309 0 1,027 May-15-2023, 02:38 PM
Last Post: tonynapoli2309
  Problem with module time and leap seconds Pedroski55 3 1,261 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Question on subprocess module. knoxvilles_joker 3 2,711 Apr-11-2021, 12:51 AM
Last Post: knoxvilles_joker
  keyboard module; must be root problem philipbergwerf 2 19,141 Apr-04-2021, 11:40 AM
Last Post: philipbergwerf
  Good Example for bi-directional Communication between Python and Windows (C#) raybowman 0 1,616 Nov-14-2020, 06:44 PM
Last Post: raybowman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020