Ubuntu 16.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-8import 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-8import 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++