我目前正在研究一個涉及將IDS UEye XC相機附加到Raspberry Pi 3以記錄視頻的項目。我用Python中的C++,OpenCV和IDS API以及另一個腳本(腳本2)編寫了一個腳本(腳本1),以使用Raspberry Pi上的GPIO按鈕啓動和停止腳本1。附上腳本1和2在Python中使用UEye API
腳本1:
#include "uEye.h"
#include "stdio.h"
#include "stdlib.h"
#include "iostream"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/core.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
HIDS hCam = 1;
SENSORINFO sensor_info;
CAMINFO camera_info;
int nRet;
int Mode;
char strCamFileName[256];
int img_width=640;
int img_height=480;
int main()
{
//initialize camera
nRet = is_InitCamera(&hCam, NULL);
cout << "Status Init: " << nRet << endl;
//get sensor info
//nRet = is_GetSensorInfo(hCam, &sensor_info);
//cout << "Sensor Color Mode: " << sensor_info.nColorMode << endl;
//cout << "Camera Model: " << sensor_info.strSensorName << endl;
//get camera info
//nRet = is_GetCameraInfo(hCam, &camera_info);
//cout << "Camera ID: " << camera_info.ID << endl;
//cout << "Camera SerNum: " << camera_info.SerNo << endl;
//cout << "Camera Version: " << camera_info.Version << endl;
//cout << "Camera Type: " << camera_info.Type << endl;
//color mode
Mode = IS_CM_RGB8_PACKED;
nRet = is_SetColorMode(hCam, Mode);
cout << "Color Mode: " << nRet << endl;
UINT formatID = 13;
nRet = is_ImageFormat(hCam, IMGFRMT_CMD_SET_FORMAT, &formatID, 4);
cout << "Status Image Format: " << nRet << endl;
char* pMem = NULL;
int memID = 0;
nRet = is_AllocImageMem(hCam, img_width, img_height, 24, &pMem, &memID);
nRet = is_SetImageMem(hCam, pMem, memID);
//set display mode
Mode = IS_SET_DM_DIB;
nRet = is_SetDisplayMode(hCam, Mode);
//zoom
double dZoomValue = 0;
nRet = is_Zoom(hCam, ZOOM_CMD_DIGITAL_SET_VALUE, (void*)&dZoomValue, sizeof(dZoomValue));
cout << "Zoom: " << dZoomValue << endl;
VideoWriter video("out.avi", CV_FOURCC('X','V','I','D'), 10, Size(img_width, img_height),true);
for(int ii=0; ii<600; ii++)
{
if(is_FreezeVideo(hCam, IS_WAIT) == IS_SUCCESS){
void *pMemVoid; //pointer to where the image is stored
is_GetImageMem (hCam, &pMemVoid);
Mat img=Mat(Size(img_width,img_height), CV_8UC3, pMemVoid);
video.write(img);
namedWindow("Live Video", WINDOW_NORMAL);
resizeWindow("Live Video", 320,240);
imshow("Live Video", img);
waitKey(1);
}
}
//exit camera
is_ExitCamera(hCam);
return 0;
}
腳本2:
import RPi.GPIO as GPIO
import signal
import time
import os
import subprocess
from subprocess import call
import sys
from sys import exit
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
set = 0
while True:
if GPIO.input(12) == False and set == 0:
print('Starting Video Recording')
p=subprocess.Popen("./video", shell=False, preexec_fn=os.setsid)
time.sleep(1)
set = 1
if GPIO.input(16) == False and set == 1:
print('Stopping Video Recording')
os.killpg(os.getpgid(p.pid), signal.SIGTERM)
time.sleep(2)
set = 0
if GPIO.input(18) == False and set == 0:
print('Exit Video Recording')
time.sleep(1)
break
GPIO.cleanup()
我以爲一切都完美地工作,直到我試圖把視頻文件從樹莓轉移到Windows 7筆記本電腦。我發現,如果我讓腳本1在整個運行過程中不間斷地運行,它會在筆記本電腦上播放,但如果我使用腳本2中斷了腳本1,然後嘗試在筆記本電腦上播放它,它將無法呈現。
這讓我相信,當我打斷它時,相機並沒有「清理乾淨」。我的意思是它沒有得到is_ExitCamera()
,它乾淨地釋放並關閉相機。我目前正在使用os.killpg()
殺死程序。有沒有辦法用is_ExitCamera()
替代它,即使一個是C++,另一個是Python?
在此先感謝。