사용자 정의함수 먼저 선언해야 한다.
cpp.cpp
#include <iostream> #include <stdio.h> #include <cstdlib> #include <iostream> #include <stdexcept> #include <string> std::string exec(const char* cmd) { char buffer[128]; std::string result = ""; FILE* pipe = popen(cmd, "r"); if (!pipe) throw std::runtime_error("popen() failed!"); try { while (fgets(buffer, sizeof buffer, pipe) != NULL) { result += buffer; } } catch (...) { pclose(pipe); throw; } pclose(pipe); return result; } int main() { std::string res; res = exec("python3 ./client.py example.jpeg"); std::cout << ">>output\n"; std::cout << res; }
컴파일
$ g++ -o cpp cpp.cpp
client.py
# -*- coding: utf-8 -*- import sys import re import base64 import json import os import io import requests #from dcfgrpc.api import dcf import time from PIL import Image imgPath = sys.argv[1] if __name__ == "__main__": with open(imgPath, "rb") as f: encoded_string = base64.encodestring(f.read()).decode("utf-8") #encoded_string = base64.encodebytes(f.read()).decode("utf-8") data = {"image": encoded_string} json_data = json.dumps(data) #print(json_data) # HTTP url = "http://127.0.0.1:8000/ocr/" result = requests.post(url, data=json_data) # DCF #result = dcf(url="keti.asuscomm.com:32222", service="keti-ocr", arg=json_data.encode()) #print(result) json_data = json.loads(result.text) result = json_data print(result['result'])
참고
std::cout 의 이중 콜론의 의미는 ?
[c++] 앞에 붙은 이중 콜론“::”의 의미는 무엇입니까? - 리뷰나라
수정해야 할 클래스에서이 코드 줄을 찾았습니다. ::Configuration * tmpCo = m_configurationDB;//pointer to current db 그리고 이중 콜론이 클래스 이름 앞에 붙는 것이 정확히 무엇을 의미하는지 모르겠습니다.
daplus.net
How to execute a command and get return code stdout and stderr of command in C++
Given the following answer (first c++11 answer): How do I execute a command and get the output of the command within C++ using POSIX? Here is the implementation for your convenience: #include <c...
stackoverflow.com
'프로그래밍 > C++' 카테고리의 다른 글
[NH] centos 7 python3 easyOCR 사용하기 위한 기본 모듈 설치 (0) | 2021.12.10 |
---|---|
c++ 문자열을 배열로 변환(explode) 방법 (0) | 2021.12.08 |
c++ system 함수를 이용한 외부 파일 실행 (0) | 2021.12.07 |
centos7 c++ 에서 http_client.h install 하는 방법 (rest sdk build) (0) | 2021.11.02 |
centos7 c++ Hello World 만들기 (0) | 2021.11.02 |