[英]Call a webcam from C++ and show images in Python
I want to call a function from C++ in Python.我想从 Python 中的 C++ 调用 function。
In my case, I already have a lot of code in Python but I want to change the camera that I use.就我而言,我已经在 Python 中有很多代码,但我想更换我使用的相机。 Some libraries for cameras only support C++.一些相机库仅支持 C++。
I have successfully call C++.dll and displaying images in Python with my code as follow.我已成功调用 C++.dll 并在 Python 中显示图像,代码如下。 However, when Python show images very slow.但是,当 Python 显示图像时非常缓慢。
I think it becomes slow because I need to call VideoCapture in a loop.我认为它变得很慢,因为我需要循环调用 VideoCapture。 If I only enter VideoCapture 1 time (before main function), the image only send 1 time also in Python.如果我只进入 VideoCapture 1 次(在 main 函数之前),图像也只在 Python 中发送 1 次。
This is my code for C++这是我的 C++ 代码
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
using namespace std;
using namespace cv;
Mat img;
Mat frameRGB;
vector<Mat>ch;
Mat1f datas, datas2, datas3, datas4;
float tmp_buf[640 * 480 * 3];
int tmp_buf_sz = 0;
VideoCapture cap;
#define LIBDLL extern "C" __declspec(dllexport)
LIBDLL int returnQ(float *data_img, int x)
{
if(x==1)
{
cap.open(0);
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
cap.set(CAP_PROP_FPS, 30);
}
cap >> frameRGB;
img = frameRGB.clone();
split(img, ch);
ch[0].convertTo(datas, CV_32F);
datas = datas.reshape(1, 1).t();
ch[1].convertTo(datas2, CV_32F);
datas2 = datas2.reshape(1, 1).t();
ch[2].convertTo(datas3, CV_32F);
datas3 = datas3.reshape(1, 1).t();
vconcat(datas, datas2, datas4);
vconcat(datas4, datas3, datas4);
tmp_buf_sz = 640 * 480 * 3;
memcpy(data_img, datas4.data, sizeof(float)*tmp_buf_sz);
waitKey(1);
return *data_img;
}
And my code for Python:我的 Python 代码:
import cv2
import ctypes
from ctypes import *
import numpy as np
dll = cdll.LoadLibrary(r"./UVCStreamerTest.dll")
dll.returnQ.restype = c_int
data_img = (c_float * 921600)()
x=1
while(True):
ss = dll.returnQ(data_img, x)
if ss!=0:
x=0
im = np.array(data_img)
im = np.uint8(im)
# Convert to BGR
B = im[0:640*480]
G = im[(640*480):(640*480*2)]
R = im[(640*480*2):]
B = B.reshape(480, 640)
G = G.reshape(480, 640)
R = R.reshape(480, 640)
img = cv2.merge((B,G,R))
cv2.imshow("Image",img)
cv2.waitKey(1)
Is there a way for better communication between C++ and Python?有没有办法在 C++ 和 Python 之间进行更好的通信?
When C++ has the task of activating the camera and sending images to Python.当 C++ 有激活相机并将图像发送到 Python 的任务时。
This webcam call from C++ is just an example for the communication between C++ and Python.这个来自 C++ 的网络摄像头调用只是 C++ 和 Python 之间通信的一个示例。 If it successful, I will change my camera which use specific library for UVC camera in C++.如果成功,我将更改我的相机,它使用 C++ 中的 UVC 相机特定库。 The flowchart will be the same, C++ capture image from camera and send to Python, then Python show the images.流程相同,C++ 从摄像头捕获图像并发送到 Python,然后 Python 显示图像。
Thank you in advance.先感谢您。
Edit: I found the solution.编辑:我找到了解决方案。 Only need to add: cap.open(0)只需要添加:cap.open(0)
I edit my code above in case someone has the same problem.我在上面编辑我的代码,以防有人遇到同样的问题。
Check out Boost.Python查看Boost.Python
I think it is more convient than the "cdll.LoadLibrary()"我认为它比“cdll.LoadLibrary()”更方便
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.