PYTHON
ubuntu16.04 默认安装的Python版本2.7.12,当用pip install opencv-python 安装了opencv for python 3.3.0.10后,运行命令
python -c "import cv2;cap=cv2.VideoCapture(0);print(cv2.isOpened())"
输出为false
经过各种百度,安装其他包文件也没有解决问题。
索性回头运行命令:pip uninstall opencv-python,卸载opencv for python 3.3.0.10
这时候再运行
python -c "import cv2;cap=cv2.VideoCapture(0);print(cv2.isOpened())"
输出为true
这时opencv for python 的版本是2.4.9.1
可运行命令 python -c "import cv2;print(cv2.__version__)"查看opencv的版本
因此得出结论,python2.7.12 与opencv for python 3.3.0.10 搭配不能正常工作。建议各位不要装新版的opencv for python。
#coding:utf-8 #http://blog.csdn.net/lance313/article/details/53885409 import os import numpy from PIL import Image,ImageDraw import cv2 cap = cv2.VideoCapture(0) fps = cap.get(cv2.cv.CV_CAP_PROP_FPS) size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.cv.CV_FOURCC('I','4','2','0') #video = cv2.VideoWriter("aaa.avi", fourcc, 5, size) print cap.isOpened() classifier=cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") index = 0; count=0 while count > -1: ret,img = cap.read() faceRects = classifier.detectMultiScale(img, 1.2, 2, cv2.CASCADE_SCALE_IMAGE,(20,20)) if len(faceRects)>0: for faceRect in faceRects: x, y, w, h = faceRect cv2.rectangle(img, (int(x), int(y)), (int(x)+int(w), int(y)+int(h)), (0,255,0),2,0) print "save faceimg" face_win = img[int(y):int(y) + int(h), int(x):int(x) + int(w)] cv2.imwrite('faceimg/index' + str(index) + '.bmp', face_win) index +=1 #facenet #video.write(img) cv2.imshow('video',img) key=cv2.waitKey(1) if key==ord('q'): break #video.release() cap.release() cv2.destroyAllWindows()
#coding:utf-8 #http://blog.csdn.net/lance313/article/details/53885409 import os import numpy from PIL import Image,ImageDraw import cv2 cap = cv2.VideoCapture(0) fps = cap.get(cv2.cv.CV_CAP_PROP_FPS) size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.cv.CV_FOURCC('I','4','2','0') #video = cv2.VideoWriter("aaa.avi", fourcc, 5, size) print cap.isOpened() classifier=cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") count=0 while count > -1: ret,img = cap.read() faceRects = classifier.detectMultiScale(img, 1.2, 2, cv2.CASCADE_SCALE_IMAGE,(20,20)) if len(faceRects)>0: for faceRect in faceRects: x, y, w, h = faceRect cv2.rectangle(img, (int(x), int(y)), (int(x)+int(w), int(y)+int(h)), (0,255,0),2,0) #video.write(img) cv2.imshow('video',img) key=cv2.waitKey(1) if key==ord('q'): break #video.release() cap.release() cv2.destroyAllWindows() # import cv2 # # capture=cv2.VideoCapture(0) # #将capture保存为motion-jpeg,cv_fourcc为保存格式 # size = (int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)), # int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))) # #cv_fourcc值要设置对,不然无法写入,而且不报错,坑 # #video=cv2.VideoWriter("VideoTest.avi", cv2.cv.CV_FOURCC('I','4','2','0'), 30, size) # #isopened可以查看摄像头是否开启 # print capture.isOpened() # num=0 # #要不断读取image需要设置一个循环 # while True: # ret,img=capture.read() # #视频中的图片一张张写入 # #video.write(img) # cv2.imshow('Video',img) # key=cv2.waitKey(1)#里面数字为delay时间,如果大于0为刷新时间, # #超过指定时间则返回-1,等于0没有返回值,但也可以读取键盘数值, # #cv2.imwrite('%s.jpg'%(str(num)),img) # num=num+1 # if key==ord('q'):#ord为键盘输入对应的整数, # break # video.release() # #如果不用release方法的话无法储存,要等结束程序再等摄像头关了才能显示保持成功 # capture.release()#把摄像头也顺便关了 # # cv2.destroyAllWindows() # OpenCV视频抓取好简单,主要用videowriter就可以了,主要要注意的是OpenCV中的抓取是放在内存中的,所以需要一个释放命令,不然就只能等到程序关闭后进行垃圾回收时才能释放了。视频抓取就不上图了。 # # 然后是脸部识别,OpenCV自带了很多特征库有脸部,眼睛的还有很多,原理都一样,只是眼睛的库识别率视乎并不高,直接上代码: # import cv2 # import cv2.cv as cv # # img = cv2.imread("face1.jpg") # # def detect(img, cascade): # '''detectMultiScale函数中smallImg表示的是要检测的输入图像为smallImg, # faces表示检测到的人脸目标序列,1.3表示每次图像尺寸减小的比例为1.3, # 4表示每一个目标至少要被检测到3次才算是真的目标(因为周围的像素和不同的窗口大小都可以检测到人脸), # CV_HAAR_SCALE_IMAGE表示不是缩放分类器来检测,而是缩放图像,Size(20, 20)为目标的最小最大尺寸''' # rects = cascade.detectMultiScale(img, scaleFactor=1.3, # minNeighbors=5, minSize=(30, 30), flags = cv.CV_HAAR_SCALE_IMAGE) # if len(rects) == 0: # return [] # rects[:,2:] += rects[:,:2] # print rects # return rects # # #在img上绘制矩形 # def draw_rects(img, rects, color): # for x1, y1, x2, y2 in rects: # cv2.rectangle(img, (x1, y1), (x2, y2), color, 2) # # # #转换为灰度图 # gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # #直方图均衡处理 # gray = cv2.equalizeHist(gray) # # #脸部特征分类地址,里面还有其他 # cascade_fn = 'haarcascade_frontalface_alt.xml' # # #读取分类器,CascadeClassifier下面有一个detectMultiScale方法来得到矩形 # cascade = cv2.CascadeClassifier(cascade_fn) # # #通过分类器得到rects # rects = detect(gray, cascade) # # #vis为img副本 # vis = img.copy() # # #画矩形 # draw_rects(vis, rects, (0, 255, 0)) # # cv2.imshow('facedetect', vis) # # cv2.waitKey(0) # cv2.destroyAllWindows()
C++
//---------------------------------【头文件、命名空间包含部分】---------------------------- // 描述:包含程序所使用的头文件和命名空间 //http://blog.csdn.net/gdut2015go/article/details/48825063 //------------------------------------------------------------------------------------------------- #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> using namespace std; using namespace cv; void detectAndDisplay( Mat frame ); //--------------------------------【全局变量声明】---------------------------------------------- // 描述:声明全局变量 //------------------------------------------------------------------------------------------------- //注意,需要把"haarcascade_frontalface_alt.xml"和"haarcascade_eye_tree_eyeglasses.xml"这两个文件复制到工程路径下 String face_cascade_name = "haarcascade_frontalface_alt.xml"; String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml"; CascadeClassifier face_cascade; CascadeClassifier eyes_cascade; string window_name = "Capture - Face detection"; RNG rng(12345); //--------------------------------【help( )函数】---------------------------------------------- // 描述:输出帮助信息 //------------------------------------------------------------------------------------------------- static void ShowHelpText() { //输出欢迎信息和OpenCV版本 cout <<" 非常感谢购买《OpenCV3编程入门》一书! " <<" 此为本书OpenCV2版的第11个配套示例程序 " << " 当前使用的OpenCV版本为:" << CV_VERSION <<" ----------------------------------------------------------------------------" ; } //-----------------------------------【main( )函数】-------------------------------------------- // 描述:控制台应用程序的入口函数,我们的程序从这里开始 //------------------------------------------------------------------------------------------------- int main( void ) { VideoCapture capture; Mat frame; //-- 1. 加载级联(cascades) if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading "); return -1; }; if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading "); return -1; }; //-- 2. 读取视频 capture.open(0); ShowHelpText(); if( capture.isOpened() ) { for(;;) { capture >> frame; //-- 3. 对当前帧使用分类器(Apply the classifier to the frame) if( !frame.empty() ) { detectAndDisplay( frame ); } else { printf(" --(!) No captured frame -- Break!"); break; } int c = waitKey(10); if( (char)c == 'c' ) { break; } } } return 0; } void detectAndDisplay( Mat frame ) { std::vector<Rect> faces; Mat frame_gray; cvtColor( frame, frame_gray, COLOR_BGR2GRAY ); equalizeHist( frame_gray, frame_gray ); //-- 人脸检测 face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) ); for( size_t i = 0; i < faces.size(); i++ ) { Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 ); ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2), 0, 0, 360, Scalar( 255, 0, 255 ), 2, 8, 0 ); // Mat faceROI = frame_gray( faces[i] ); // std::vector<Rect> eyes; // //-- 在脸中检测眼睛 // eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) ); // for( size_t j = 0; j < eyes.size(); j++ ) // { // Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 ); // int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 ); // circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 3, 8, 0 ); // } } //-- 显示最终效果图 imshow( window_name, frame ); }
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(DisplayImage)
find_package(OpenCV REQUIRED)
add_executable(DisplayImage DisplayImage.cpp)
target_link_libraries(DisplayImage ${OpenCV_LIBS})