将以下程序转换成python程序 #include <opencv2/opencv.hpp> #include <iostream> #include<ctime> int main(int argc,char** argv) { clock_t start,end; int cam_num = 1; // 1,2 the number of cameras used // nano_id dev_id port_id 位置 // 13 0 9202 下巴 // 13 1 9201 前方 // 14 0 9203 左方 // 14 1 9204 右方 // 15 0 9205 腹部(默认) std::string IpLastSegment = "15"; int cam_id = 0; // the id of the camera used if cam_num is 1 if (argc>=2) cam_id = std::atoi(argv[1]); int udpPORT1 = 9201; // port_id of the camera which was used int udpPORT2 = 9202; // port_id of the camera which was used std::string udpstrPrevData = "udpsrc address=192.168.123."+ IpLastSegment + " port="; std::string udpstrBehindData = " ! application/x-rtp,media=video,encoding-name=H264 ! rtph264depay ! h264parse ! omxh264dec ! videoconvert ! appsink"; std::string udpSendIntegratedPipe1 = udpstrPrevData + std::to_string(udpPORT1) + udpstrBehindData; std::string udpSendIntegratedPipe2 = udpstrPrevData + std::to_string(udpPORT2) + udpstrBehindData; std::cout<<"udpSendIntegratedPipe1:"<<udpSendIntegratedPipe1<<std::endl; std::cout<<"udpSendIntegratedPipe2:"<<udpSendIntegratedPipe2<<std::endl; cv::VideoCapture cap1(udpSendIntegratedPipe1); cv::VideoCapture cap2(udpSendIntegratedPipe2); if(!cap1.isOpened()) return 0 ; if(!cap2.isOpened()) return 0 ; cv::Mat frame1, frame2; while(1) { start=clock(); //程序开始计时 cap1 >> frame1; cap2 >> frame2; if(frame1.empty()) break; if(frame2.empty()) break; imshow("video1", frame1); imshow("video2", frame2); end=clock(); double endtime=(double)(end-start)/CLOCKS_PER_SEC; std::cout << "FPS:"<<1/endtime<<"/s"<<std::endl; //ms为单位 char key = cv::waitKey(1); if(key == 27) // press ESC key break; } cap1.release();//释放资源 cap2.release();//释放资源 } return 0; }
时间: 2024-02-14 10:29:53 浏览: 161
opencv导入头文件时报错#include的解决方法
以下是将该程序转换为Python程序的代码:
import cv2
def main(cam_id):
cam_num = 1 # 1,2 the number of cameras used
IpLastSegment = "15"
udpPORT1 = 9201 # port_id of the camera which was used
udpPORT2 = 9202 # port_id of the camera which was used
udpstrPrevData = "udpsrc address=192.168.123." + IpLastSegment + " port="
udpstrBehindData = " ! application/x-rtp,media=video,encoding-name=H264 ! rtph264depay ! h264parse ! omxh264dec ! videoconvert ! appsink"
udpSendIntegratedPipe1 = udpstrPrevData + str(udpPORT1) + udpstrBehindData
udpSendIntegratedPipe2 = udpstrPrevData + str(udpPORT2) + udpstrBehindData
print("udpSendIntegratedPipe1:", udpSendIntegratedPipe1)
print("udpSendIntegratedPipe2:", udpSendIntegratedPipe2)
cap1 = cv2.VideoCapture(udpSendIntegratedPipe1)
cap2 = cv2.VideoCapture(udpSendIntegratedPipe2)
if not cap1.isOpened():
return 0
if not cap2.isOpened():
return 0
while True:
start = cv2.getTickCount() # 程序开始计时
ret1, frame1 = cap1.read()
ret2, frame2 = cap2.read()
if not ret1:
break
if not ret2:
break
cv2.imshow("video1", frame1)
cv2.imshow("video2", frame2)
end = cv2.getTickCount()
elapsed_time = (end - start) / cv2.getTickFrequency()
fps = 1 / elapsed_time
print("FPS:", fps, "/s") # ms为单位
key = cv2.waitKey(1)
if key == 27: # press ESC key
break
cap1.release() # 释放资源
cap2.release() # 释放资源
if __name__ == "__main__":
cam_id = 0 # the id of the camera used if cam_num is 1
main(cam_id)
阅读全文